55 lines
2.3 KiB
TypeScript
55 lines
2.3 KiB
TypeScript
import { afterAll, beforeAll, describe, expect, it } from "vitest";
|
|
import { createServer, type Server } from "node:http";
|
|
import { spawn } from "node:child_process";
|
|
import { mkdtemp, readFile, rm, writeFile, statfs } from "node:fs/promises";
|
|
import { tmpdir } from "node:os";
|
|
import { join } from "node:path";
|
|
import { createApp } from "../src/http.ts";
|
|
import { redact } from "@spike/x";
|
|
|
|
let server: Server;
|
|
let base = "";
|
|
beforeAll(async () => {
|
|
server = createServer((req, res) => {
|
|
res.setHeader("content-type", "application/json");
|
|
res.end(JSON.stringify({ path: req.url, method: req.method }));
|
|
});
|
|
await new Promise<void>((r) => server.listen(0, "127.0.0.1", () => r()));
|
|
const addr = server.address();
|
|
base = `http://127.0.0.1:${typeof addr === "object" && addr ? addr.port : 0}`;
|
|
});
|
|
afterAll(async () => { await new Promise((r) => server.close(r)); });
|
|
|
|
describe("node compat under test runner", () => {
|
|
it("node:http mock server + fetch", async () => {
|
|
const r = await fetch(`${base}/api/v2/torrents/info?x=1`);
|
|
expect(await r.json()).toEqual({ path: "/api/v2/torrents/info?x=1", method: "GET" });
|
|
});
|
|
it("node:child_process spawn + kill", async () => {
|
|
const child = spawn("sleep", ["30"]);
|
|
await new Promise((r) => setTimeout(r, 100));
|
|
const exited = new Promise<{ code: number | null; signal: string | null }>((r) => child.on("exit", (code, signal) => r({ code, signal })));
|
|
expect(child.kill("SIGKILL")).toBe(true);
|
|
expect(await exited).toEqual({ code: null, signal: "SIGKILL" });
|
|
});
|
|
it("fs temp dir + statfs", async () => {
|
|
const dir = await mkdtemp(join(tmpdir(), "umt-"));
|
|
await writeFile(join(dir, "a.txt"), "hi");
|
|
expect(await readFile(join(dir, "a.txt"), "utf8")).toBe("hi");
|
|
const s = await statfs(dir);
|
|
expect(s.bsize).toBeGreaterThan(0);
|
|
expect(typeof s.bavail).toBe("number");
|
|
await rm(dir, { recursive: true });
|
|
});
|
|
it("hono app.request SSE", async () => {
|
|
const res = await createApp().request("/api/v1/events?after=1");
|
|
expect(res.headers.get("content-type")).toContain("text/event-stream");
|
|
const text = await res.text();
|
|
expect(text).toContain("id: 2");
|
|
expect(text).toContain("event: tick");
|
|
});
|
|
it("workspace package import", () => {
|
|
expect(redact({ token: "x" })).toEqual({ token: "[REDACTED]" });
|
|
});
|
|
});
|