ai-chatbot/tests/routes/runtime-active-work.test.ts

31 lines
1.6 KiB
TypeScript

import { expect, test } from "@playwright/test";
import { serverLoader } from "../helpers/canvas-download-server-loader";
test("release draining includes background work until its original promise settles", async () => {
const counts: number[] = [];
const work = serverLoader({
"node:fs": { writeFileSync: (_path: string, text: string) => counts.push(JSON.parse(text).active), unlinkSync() {} },
})("lib/observability/active-work.ts");
let finish!: () => void;
const original = new Promise<void>(resolve => { finish = resolve; });
const pending = work.trackRuntimeWork(() => original);
await work.trackRuntimeWork(async () => "another request");
expect(counts).toEqual([1, 2, 1]);
finish();
await pending;
expect(counts).toEqual([1, 2, 1, 0]);
await expect(work.trackRuntimeWork(async () => { throw new Error("expected failure"); })).rejects.toThrow("expected failure");
expect(counts.at(-1)).toBe(0);
});
test("durable archive handoff is separate from unfinished original generation", async () => {
const records: Array<{ active: number; durable: number }> = [];
const work = serverLoader({ "node:fs": { writeFileSync: (_path: string, value: string) => records.push(JSON.parse(value)), unlinkSync() {} } })("lib/observability/active-work.ts");
let complete!: () => void;
const original = work.trackRuntimeWork(() => new Promise<void>(resolve => { complete = resolve; }));
await work.trackRuntimeWork(async () => "archive", { durable: true });
expect(records.map(x => [x.active, x.durable])).toEqual([[1, 0], [2, 1], [1, 0]]);
complete(); await original;
expect(records.at(-1)).toMatchObject({ active: 0, durable: 0 });
});