feat: add extensive test suite
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
type DebugRuntime = {
|
||||
bun: string;
|
||||
node: string;
|
||||
platform: string;
|
||||
arch: string;
|
||||
uptime: number;
|
||||
};
|
||||
|
||||
type DebugHandlerDependencies = {
|
||||
adminCheck: () => Promise<unknown>;
|
||||
runtime: () => DebugRuntime;
|
||||
memoryUsage: () => NodeJS.MemoryUsage;
|
||||
probes: ReadonlyArray<{
|
||||
name: string;
|
||||
run: () => Promise<unknown>;
|
||||
}>;
|
||||
now?: () => number;
|
||||
};
|
||||
|
||||
export function createDebugHandler({
|
||||
adminCheck,
|
||||
runtime,
|
||||
memoryUsage,
|
||||
probes,
|
||||
now = () => performance.now(),
|
||||
}: DebugHandlerDependencies) {
|
||||
return async function getDebugStatus() {
|
||||
try {
|
||||
if (!(await adminCheck())) return unauthorized();
|
||||
} catch {
|
||||
return unauthorized();
|
||||
}
|
||||
|
||||
const results = await Promise.all(
|
||||
probes.map(async ({ name, run }) => {
|
||||
const started = now();
|
||||
|
||||
try {
|
||||
await run();
|
||||
return { name, ok: true, latencyMs: now() - started };
|
||||
} catch {
|
||||
return { name, ok: false, latencyMs: now() - started };
|
||||
}
|
||||
}),
|
||||
);
|
||||
|
||||
return Response.json({
|
||||
timestamp: new Date().toISOString(),
|
||||
runtime: runtime(),
|
||||
memory: memoryUsage(),
|
||||
probes: results,
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
function unauthorized() {
|
||||
return new Response("Unauthorized", { status: 401 });
|
||||
}
|
||||
Reference in New Issue
Block a user