|
| 1 | +import { |
| 2 | + ansiColorFormatter, |
| 3 | + configure, |
| 4 | + type LogRecord, |
| 5 | +} from "@logtape/logtape"; |
| 6 | +import { AsyncLocalStorage } from "node:async_hooks"; |
| 7 | +// @ts-ignore: The following code is generated |
| 8 | +import { testDefinitions } from "./dist/testing/mod.js"; |
| 9 | +// @ts-ignore: The following code is generated |
| 10 | +import "./imports.ts"; |
| 11 | + |
| 12 | +interface TestDefinition { |
| 13 | + name: string; |
| 14 | + ignore?: boolean; |
| 15 | + fn: ( |
| 16 | + // deno-lint-ignore no-explicit-any |
| 17 | + ctx: { name: string; origin: string; step: any }, |
| 18 | + ) => void | Promise<void>; |
| 19 | +} |
| 20 | + |
| 21 | +// @ts-ignore: testDefinitions is untyped |
| 22 | +const tests: TestDefinition[] = testDefinitions; |
| 23 | +const logs: LogRecord[] = []; |
| 24 | + |
| 25 | +await configure({ |
| 26 | + sinks: { |
| 27 | + buffer: logs.push.bind(logs), |
| 28 | + }, |
| 29 | + loggers: [ |
| 30 | + { category: [], sinks: ["buffer"], lowestLevel: "debug" }, |
| 31 | + ], |
| 32 | + contextLocalStorage: new AsyncLocalStorage(), |
| 33 | +}); |
| 34 | + |
| 35 | +export default { |
| 36 | + async fetch(request: Request): Promise<Response> { |
| 37 | + if (request.method === "GET") { |
| 38 | + return new Response( |
| 39 | + JSON.stringify(tests.map(({ name }) => name)), |
| 40 | + { |
| 41 | + headers: { "Content-Type": "application/json" }, |
| 42 | + }, |
| 43 | + ); |
| 44 | + } |
| 45 | + const testName = await request.text(); |
| 46 | + for (const def of tests) { |
| 47 | + const { name, fn, ignore } = def; |
| 48 | + if (testName !== name) continue; |
| 49 | + if (ignore) { |
| 50 | + return new Response( |
| 51 | + "", |
| 52 | + { |
| 53 | + status: 404, |
| 54 | + headers: { "Content-Type": "text/plain" }, |
| 55 | + }, |
| 56 | + ); |
| 57 | + } |
| 58 | + let failed: unknown = undefined; |
| 59 | + let capturedLogs: LogRecord[] | undefined = undefined; |
| 60 | + // deno-lint-ignore no-inner-declarations |
| 61 | + async function step( |
| 62 | + arg: string | { |
| 63 | + name: string; |
| 64 | + ignore?: boolean; |
| 65 | + // deno-lint-ignore no-explicit-any |
| 66 | + fn: (def: any) => void | Promise<void>; |
| 67 | + // deno-lint-ignore no-explicit-any |
| 68 | + } | ((ctx: any) => void | Promise<void>), |
| 69 | + // deno-lint-ignore no-explicit-any |
| 70 | + fn?: (ctx: any) => void | Promise<void>, |
| 71 | + ) { |
| 72 | + let def: { |
| 73 | + name: string; |
| 74 | + ignore?: boolean; |
| 75 | + // deno-lint-ignore no-explicit-any |
| 76 | + fn: (def: any) => void | Promise<void>; |
| 77 | + }; |
| 78 | + if (typeof arg === "string") { |
| 79 | + def = { name: arg, fn: fn! }; |
| 80 | + } else if (typeof arg === "function") { |
| 81 | + def = { name: arg.name, fn: arg }; |
| 82 | + } else { |
| 83 | + def = arg; |
| 84 | + } |
| 85 | + if (def.ignore) return; |
| 86 | + try { |
| 87 | + await def.fn({ |
| 88 | + name: def.name, |
| 89 | + origin: "", |
| 90 | + step, |
| 91 | + }); |
| 92 | + } catch (e) { |
| 93 | + failed ??= e; |
| 94 | + capturedLogs ??= [...logs]; |
| 95 | + return false; |
| 96 | + } |
| 97 | + return true; |
| 98 | + } |
| 99 | + logs.splice(0, logs.length); // Clear logs |
| 100 | + try { |
| 101 | + await fn({ name, origin: "", step }); |
| 102 | + } catch (e) { |
| 103 | + failed ??= e; |
| 104 | + } |
| 105 | + capturedLogs ??= [...logs]; |
| 106 | + if (typeof failed === "undefined") { |
| 107 | + return new Response( |
| 108 | + "", |
| 109 | + { status: 200, headers: { "Content-Type": "text/plain" } }, |
| 110 | + ); |
| 111 | + } else { |
| 112 | + return new Response( |
| 113 | + `${ |
| 114 | + failed instanceof Error |
| 115 | + ? `${failed.message}\n${failed.stack ?? ""}` |
| 116 | + : String(failed) |
| 117 | + }\n${capturedLogs.map(ansiColorFormatter).join("")}`, |
| 118 | + { |
| 119 | + status: 500, |
| 120 | + headers: { "Content-Type": "text/plain" }, |
| 121 | + }, |
| 122 | + ); |
| 123 | + } |
| 124 | + } |
| 125 | + return new Response( |
| 126 | + "Test not found", |
| 127 | + { |
| 128 | + status: 404, |
| 129 | + headers: { "Content-Type": "text/plain" }, |
| 130 | + }, |
| 131 | + ); |
| 132 | + }, |
| 133 | +}; |
0 commit comments