type IO = { exists(path: string): Promise; read(path: string): Promise; write(path: string, response: Response): Promise; test: Test; }; type Assert = { equal(actual: T, expected: T, message?: string): void; }; type Test = (name: string, func: TestFunc) => Promise; type TestFunc = (assert: Assert, subtest: Subtest) => Promise | void; type Subtest = (name: string, func: SubtestFunc) => Promise; type SubtestFunc = (assert: Assert) => Promise | void; let io: IO | null = null; export async function getIO(): Promise { if (io === null) { io = await _getIO(); } return io; } async function _getIO(): Promise { if (globalThis.process?.release?.name === "node") { // Assume node compatibility const { access, readFile } = await import("node:fs/promises"); const { deepStrictEqual } = await import("node:assert"); const assert: Assert = { equal: deepStrictEqual, }; let test: Test; let writeFile: ( file: string, data: ReadableStream, ) => Promise; if (typeof globalThis.Deno !== "undefined") { // Except for Deno, which does its own thing writeFile = Deno.writeFile; test = (name, func) => { Deno.test(name, (t) => { return func(assert, async (name, func): Promise => { await t.step(name, () => { return func(assert); }); }); }); return Promise.resolve(); }; } else { writeFile = (await import("node:fs/promises"))["writeFile"]; const { suite, test: subtest } = await import("node:test"); test = (name, func) => { suite(name, () => { return func(assert, async (name, func): Promise => { await subtest(name, async () => { await func(assert); }); }); }); return Promise.resolve(); }; } return { async exists(path: string): Promise { try { await access(path); return true; } catch { return false; } }, read(path: string): Promise { return readFile(path, { encoding: "utf-8" }); }, write(path: string, response: Response): Promise { return writeFile(path, response.body!); }, test, }; } throw new Error( `unsupported runtime for testing${ navigator.userAgent ? `: ${navigator.userAgent}` : "" }`, ); }