import assert from "node:assert/strict"; import test from "node:test"; import { loadAnnotationTaskJoin, loadOwnedAnnotationProject } from "../../src/lib/annotation-project-loading.js"; test("obsolete review pool reads are cancelled before task joins start", async () => { const controller = new AbortController(); const pending = Promise.withResolvers(); let joins = 0; const signals = []; const read = async (_filters, options) => { signals.push(options?.signal); await pending.promise; return [{ legacyTaskId: "task" }]; }; const result = loadAnnotationPoolPage({ scope: {}, view: { kind: "workspace", reviewOnly: true }, signal: controller.signal, human: { listItems: read, listItemsPage: async (...args) => ({ items: await read(...args) }) }, annotation: { listTasksPage: async () => { joins++; return { items: [] }; }, }, }); controller.abort(); pending.resolve(); await assert.rejects(result, { name: "AbortError" }); assert.equal(joins, 0); assert.equal(signals.length, 3); assert.ok(signals.every((s) => s === controller.signal)); }); test("cancelled task joins stop before the next bounded batch", async () => { const controller = new AbortController(); let calls = 0; const annotation = { listTasksPage: async (_filters, options) => { assert.equal(options?.signal, controller.signal); if (++calls === 4) controller.abort(); return { items: [] }; }, }; await assert.rejects( loadAnnotationTaskJoin( annotation, {}, Array.from({ length: 250 }, (_, i) => String(i)), controller.signal, ), { name: "AbortError" }, ); assert.equal(calls, 4); }); test("an obsolete owned-project count cannot publish after cancellation", async () => { const controller = new AbortController(); const pending = Promise.withResolvers(); const updates = []; const result = loadOwnedAnnotationProject({ projectId: "p", annotationView: "active", signal: controller.signal, isCurrent: () => true, onCount: () => updates.push("count"), onTasks: () => updates.push("tasks"), onPage: () => updates.push("page"), projects: { listMembers: async () => [], listWorkflows: async () => [] }, human: {}, annotation: { claimCount: async () => pending.promise, getSidebar: async () => { await pending.promise; return { items: [], tasks: [] }; }, listTemplates: async () => [], }, }); await Promise.resolve(); controller.abort(); pending.resolve({ count: 1 }); await assert.rejects(result, { name: "AbortError" }); assert.deepEqual(updates, []); }); import { loadAnnotationPoolPage } from "../../src/lib/annotation-project-loading.js"; test("reviewed search never waits for or requests unrelated slow pools", async () => { const calls = []; const result = await loadAnnotationPoolPage({ scope: {}, view: { kind: "review", pool: "reviewed", search: "FYLX_B07/part0002", category: "all", stageIds: ["quality_review"], }, human: { listItemsPage: async (filters) => { calls.push(filters); assert.equal(filters.pool, "reviewed", "unrelated pools may time out"); return { items: [{ id: "reviewed", legacyTaskId: "task", canClaim: false }], filtersApplied: true, hasMore: false, }; }, }, annotation: { listTasksPage: async () => ({ items: [{ id: "task" }] }) }, }); assert.equal(calls.length, 1); assert.equal(calls[0].stageIds, undefined, "returned-stage review history remains visible"); assert.equal(result.pools.reviewed[0].id, "reviewed"); assert.deepEqual(result.poolCursors, { reviewed: "" }); }); test("selected review pool failure propagates instead of presenting an empty search", async () => { await assert.rejects( loadAnnotationPoolPage({ scope: {}, view: { kind: "review", pool: "mine", search: "capture", category: "all" }, human: { listItemsPage: async () => { throw new Error("selected pool failed"); }, }, annotation: {}, }), /selected pool failed/, ); }); const scope = { projectId: "p", workflowId: "w", runId: "r", nodeId: "n", artifactId: "a" }; function fixture(view = {}) { const calls = []; return { calls, options: { scope, view: { kind: "workspace", isManager: false, reviewOnly: true, stageIds: ["quality_review"], ...view }, annotation: { listTasksPage: async (filters) => { calls.push(["tasks", filters]); return { items: (filters.ids || ["manager-task"]).map((id) => ({ id })), nextCursor: "task-next" }; }, }, human: { listItems: async (filters) => { calls.push(["pool", filters]); return [{ id: filters.pool, legacyTaskId: "old-returned-task", canClaim: filters.pool === "available" }]; }, listItemsPage: async (filters) => { calls.push(["mine", filters]); return { items: [{ id: "mine", legacyTaskId: "mine-task" }], nextCursor: "mine-next" }; }, }, }, }; } test("review pools drive a deduplicated task join and preserve server eligibility", async () => { const f = fixture(); const result = await loadAnnotationPoolPage(f.options); assert.deepEqual( result.tasks.map((t) => t.id), ["old-returned-task", "mine-task"], ); assert.equal(result.pools.available[0].canClaim, true); assert.equal(result.pools.reviewed[0].canClaim, false); assert.equal(result.mineCursor, "mine-next"); assert.equal(result.taskCursor, ""); for (const [kind, filters] of f.calls) { if (kind === "tasks") assert.deepEqual(filters, { ...scope, ids: ["old-returned-task", "mine-task"] }); else { assert.equal(filters.projectId, "p"); assert.equal(filters.workflowId, "w"); assert.equal(filters.runId, "r"); assert.deepEqual(filters.stageIds, filters.pool === "reviewed" ? undefined : ["quality_review"]); assert.equal(filters.nodeId, undefined); assert.equal(filters.artifactId, undefined); } } assert.ok(!f.calls.some(([, filter]) => filter.pool === "all")); }); test("review joins use bounded existing batching for a large initial pool", async () => { const f = fixture(); f.options.human.listItems = async ({ pool }) => pool === "available" ? Array.from({ length: 1220 }, (_, i) => ({ legacyTaskId: `t-${i}` })) : []; const result = await loadAnnotationPoolPage(f.options); assert.equal(result.tasks.length, 1221); assert.equal(f.calls.filter(([kind]) => kind === "tasks").length, 25); assert.ok(f.calls.filter(([kind]) => kind === "tasks").every(([, query]) => query.ids.length <= 50)); }); test("an empty review pool does not load an unrelated task window", async () => { const f = fixture(); f.options.human.listItems = async () => []; f.options.human.listItemsPage = async () => ({ items: [] }); const result = await loadAnnotationPoolPage(f.options); assert.deepEqual(result.tasks, []); assert.deepEqual(f.calls, []); }); test("manager task windows retain server pagination and the all pool", async () => { const f = fixture({ isManager: true, reviewOnly: false, limit: 200 }); const result = await loadAnnotationPoolPage(f.options); assert.equal(result.taskCursor, "task-next"); assert.deepEqual(result.tasks, [{ id: "manager-task" }]); assert.ok(f.calls.some(([, filter]) => filter.pool === "all")); assert.deepEqual(f.calls.find(([kind]) => kind === "tasks")[1], { ...scope, limit: 200 }); }); for (const isManager of [false, true]) { test(`acceptance retains its dedicated merged-stage protocol (manager=${isManager})`, async () => { const f = fixture({ acceptance: true, isManager, userId: "reviewer" }); const pools = { available: [{ legacyTaskId: "prepackage-task" }], mine: [], reviewed: [], all: [] }; f.options.human.listAcceptancePools = async (...args) => { assert.deepEqual(args, [ { pluginId: "annotation-qa", projectId: "p", workflowId: "w", runId: "r" }, "reviewer", isManager, { signal: undefined }, ]); return pools; }; const result = await loadAnnotationPoolPage(f.options); assert.equal(result.pools, pools); assert.ok(!f.calls.some(([kind]) => kind === "pool" || kind === "mine")); if (!isManager) assert.deepEqual(result.tasks, [{ id: "prepackage-task" }]); }); } for (const pool of ["available", "mine", "all"]) { test(`admin ${pool} rows, tasks, count and cursor come from one response`, async () => { const f = fixture({ kind: "admin", filters: { pool, search: "CAPTURE", category: "pending_quality", projectId: "p" }, }); const controller = new AbortController(); const items = [{ id: "h", canClaim: false }], tasks = [{ id: "t" }], counts = { available: 606, mine: 1, all: 611 }; f.options.annotation.adminPool = async (filters, options) => { assert.deepEqual(filters, { ...f.options.view.filters, cursor: "page-1" }); assert.equal(options.signal, controller.signal); return { items, tasks, counts, nextCursor: "page-2" }; }; const result = await loadAnnotationPoolPage({ ...f.options, cursor: "page-1", signal: controller.signal }); assert.equal(result.tasks, tasks); assert.equal(result.pools[pool], items); assert.equal(result.counts, counts); assert.equal(result.taskCursor, "page-2"); assert.deepEqual(f.calls, []); }); } for (const annotationView of ["active", "submitted"]) { test(`sidebar ${annotationView} continuation preserves scope and joined cursor`, async () => { const f = fixture({ kind: "sidebar", annotationView }); f.options.annotation.getSidebar = async (filters) => { assert.deepEqual(filters, { projectId: "p", workflowId: "w", runId: "r", annotationView, cursor: "mine-1" }); return { items: [{ id: "owned" }], tasks: [{ id: "owned-task" }], nextCursor: "mine-2" }; }; const result = await loadAnnotationPoolPage({ ...f.options, cursor: "mine-1" }); assert.equal(result.mineCursor, "mine-2"); assert.deepEqual(result.tasks, [{ id: "owned-task" }]); assert.deepEqual(f.calls, []); }); } test("manager continuation reads only its task page", async () => { const f = fixture({ limit: 200 }); const result = await loadAnnotationPoolPage({ ...f.options, cursor: "tasks-1" }); assert.equal(result.pools, null); assert.deepEqual(f.calls, [["tasks", { ...scope, limit: 200, cursor: "tasks-1" }]]); }); test("failed task joins return no partially advanced pool page", async () => { const f = fixture(); f.options.annotation.listTasksPage = async () => { throw new Error("join unavailable"); }; await assert.rejects(loadAnnotationPoolPage(f.options), /join unavailable/); }); test("admin denial propagates without fallback to a broader legacy pool", async () => { const f = fixture({ kind: "admin", filters: { pool: "all" } }); f.options.annotation.adminPool = async () => { throw Object.assign(new Error("denied"), { status: 403 }); }; await assert.rejects(loadAnnotationPoolPage(f.options), { status: 403 }); assert.deepEqual(f.calls, []); }); test("review tasks retain global creation/ID descending order across join batches", async () => { const f = fixture(); f.options.human.listItems = async ({ pool }) => pool === "available" ? Array.from({ length: 51 }, (_, i) => ({ legacyTaskId: `t-${String(i).padStart(2, "0")}` })) : []; f.options.human.listItemsPage = async () => ({ items: [] }); f.options.annotation.listTasksPage = async ({ ids }) => ({ items: ids .map((id) => ({ id, createdAt: id === "t-50" ? "2026-10-21" : "2026-10-20" })) .sort((a, b) => b.id.localeCompare(a.id)), }); const result = await loadAnnotationPoolPage(f.options); assert.deepEqual( result.tasks.map((t) => t.id), Array.from({ length: 51 }, (_, i) => `t-${String(50 - i).padStart(2, "0")}`), ); });