import test from "node:test"; import assert from "node:assert/strict"; import { readFileSync } from "node:fs"; import ts from "typescript"; const source = readFileSync(new URL("../../src/pages/AnnotationOperations.tsx", import.meta.url), "utf8"); const marker = source.indexOf( "const status = await annotationOperationsApi.getTaskStatus(selectedTaskId, controller.signal)", ); const start = source.lastIndexOf(" useEffect(() => {", marker) + " useEffect(() => {".length; const end = source.indexOf("\n }, [", marker); const body = ts.transpile(source.slice(start, end), { target: ts.ScriptTarget.ES2022 }); function fixture({ failed = true, dirty = false, phase = "slice", status = { sliceStatus: "completed" }, failRead = false, } = {}) { const timers = [], delays = [], errors = []; const dirtyRef = { current: dirty }; let reads = 0, refresh = 0; const cleanup = new Function( "combinedEditorPhase", "selectedTaskId", "sliceGenerationFailed", "annotationOperationsApi", "setTimeout", "clearTimeout", "setProcessingNotice", "setTaskDetailRefreshVersion", "workSession", "setError", "leaveDirtyRef", "annotationMutationScopeRef", body, )( phase, "task", failed, { getTaskStatus: async () => { reads++; if (failRead) throw new Error("offline"); return status; }, }, (f, delay) => { delays.push(delay); timers.push(f); return timers.length; }, () => {}, () => {}, () => refresh++, { pauseContinuous() {} }, (e) => errors.push(e), dirtyRef, { current: { busy: () => false } }, ); return { timers, delays, dirtyRef, cleanup, errors, get reads() { return reads; }, get refresh() { return refresh; }, }; } test("failed generation notices external completion and refreshes authority", async () => { const f = fixture(); assert.equal(f.timers.length, 1); await f.timers.shift()(); assert.equal(f.reads, 1); assert.equal(f.refresh, 1); }); test("external recovery never reloads unsaved editing", async () => { const f = fixture({ dirty: true }); await f.timers.shift()?.(); assert.equal(f.refresh, 0); assert.equal(f.timers.length, 1); }); test("unchanged failure uses status polling without full detail reload", async () => { const f = fixture({ status: { sliceStatus: "failed", needsRefresh: true } }); await f.timers.shift()?.(); assert.equal(f.reads, 1); assert.equal(f.refresh, 0); assert.equal(f.timers.length, 1); }); test("failed status read retries without issuing a generation write", async () => { const f = fixture({ failRead: true }); await f.timers.shift()?.(); assert.equal(f.reads, 1); assert.equal(f.timers.length, 1); }); test("leaving task ignores a late status response", async () => { const f = fixture(); const pending = f.timers.shift()?.(); f.cleanup?.(); await pending; assert.equal(f.refresh, 0); }); test("normal annotation does not gain another polling loop", () => { const f = fixture({ failed: false, phase: "annotate" }); assert.equal(f.timers.length, 0); }); test("saving dirty edits allows the next status check to reconcile", async () => { const f = fixture({ dirty: true }); await f.timers.shift()(); assert.equal(f.refresh, 0); f.dirtyRef.current = false; await f.timers.shift()(); assert.equal(f.refresh, 1); assert.deepEqual(f.delays, [10000, 10000]); }); test("editing while a status request is in flight suppresses detail refresh", async () => { const f = fixture(); const pending = f.timers.shift()(); f.dirtyRef.current = true; await pending; assert.equal(f.refresh, 0); }); test("active generation keeps the existing two-second interval", () => { assert.deepEqual(fixture({ failed: false, phase: "materializing" }).delays, [2000]); });