droboticflow/tests/frontend/annotation-project-loading.test.mjs

351 lines
12 KiB
JavaScript

import assert from "node:assert/strict";
import test from "node:test";
import { loadOwnedAnnotationProject, loadAnnotationSidebar } from "../../src/lib/annotation-project-loading.js";
function deferred() {
let resolve, reject;
const promise = new Promise((yes, no) => {
resolve = yes;
reject = no;
});
return { promise, resolve, reject };
}
function fixture(count, isCurrent = () => true) {
const counts = [];
return {
counts,
options: {
projectId: "project-a",
isCurrent,
onCount: (value) => counts.push(value),
projects: { listMembers: async () => [], listWorkflows: async () => [] },
human: { listItems: async () => [{ id: "mine", legacyTaskId: "task-a" }] },
annotation: {
listTemplates: async () => [],
claimCount: () => count.promise,
getTaskSummary: async (id) => ({ task: { id } }),
},
},
};
}
test("owned tasks are ready while claim-count is still pending", async () => {
const count = deferred(),
f = fixture(count);
let result;
const ready = loadOwnedAnnotationProject(f.options).then((value) => {
result = value;
});
await new Promise((resolve) => setImmediate(resolve));
try {
assert.deepEqual(result?.tasks, [{ id: "task-a" }]);
assert.deepEqual(f.counts, []);
} finally {
count.resolve({ count: 12504 });
await ready;
}
});
test("a late count cannot update a newer account/project/load", async () => {
const count = deferred();
let current = true;
const f = fixture(count, () => current);
const ready = loadOwnedAnnotationProject(f.options);
current = false;
count.resolve({ count: 12504 });
await ready;
assert.deepEqual(f.counts, []);
});
test("count failure leaves owned tasks usable and reports an unknown count", async () => {
const count = deferred(),
f = fixture(count);
const ready = loadOwnedAnnotationProject(f.options);
count.reject(new Error("count timeout"));
const result = await ready;
await new Promise((resolve) => setImmediate(resolve));
assert.deepEqual(result.tasks, [{ id: "task-a" }]);
assert.deepEqual(f.counts, [null]);
});
test("annotator task view is paged before the batched task join", async () => {
const calls = [];
const result = await loadOwnedAnnotationProject({
projectId: "p",
annotationView: "active",
isCurrent: () => true,
onCount: () => {},
onPage: (page) => calls.push(page.nextCursor),
projects: { listMembers: async () => [], listWorkflows: async () => [] },
human: {
listItemsPage: async (filter) => {
assert.equal(filter.annotationView, "active");
return { items: [{ legacyTaskId: "older-pending" }], nextCursor: "next" };
},
},
annotation: {
claimCount: async () => ({ count: 0 }),
listTemplates: async () => [],
listTasksPage: async (filter) => {
assert.deepEqual(filter.ids, ["older-pending"]);
return { items: [{ id: "older-pending" }] };
},
},
});
assert.deepEqual(result.tasks, [{ id: "older-pending" }]);
assert.deepEqual(calls, ["next"]);
});
test("the first sidebar page uses the same workflow/run scope as load-more", async () => {
// Review 2026-10-18: page 1 omitted workflowId/runId while load-more added
// them to the same cursor, mixing scopes and hiding other-workflow tasks.
const filters = [];
await loadOwnedAnnotationProject({
projectId: "p",
workflowId: "workflow-a",
runId: "run-a",
annotationView: "active",
isCurrent: () => true,
onCount: () => {},
projects: { listMembers: async () => [], listWorkflows: async () => [] },
human: {},
annotation: {
claimCount: async () => ({ count: 0 }),
listTemplates: async () => [],
getSidebar: async (filter) => {
filters.push(filter);
return { items: [], tasks: [] };
},
},
});
assert.deepEqual(filters, [{ projectId: "p", workflowId: "workflow-a", runId: "run-a", annotationView: "active" }]);
});
test("large history joins have short request URLs and bounded concurrency", async () => {
const { loadAnnotationTaskJoin } = await import("../../src/lib/annotation-project-loading.js");
let running = 0,
max = 0,
calls = 0;
const ids = Array.from({ length: 620 }, (_, i) => `annotation-task-${String(i).padStart(32, "0")}`);
const tasks = await loadAnnotationTaskJoin(
{
listTasksPage: async (filter) => {
running++;
max = Math.max(max, running);
calls++;
assert.ok(filter.ids.length <= 50);
await Promise.resolve();
running--;
return { items: filter.ids.map((id) => ({ id })) };
},
},
{ projectId: "p" },
ids,
);
assert.equal(tasks.length, 620);
assert.equal(calls, 13);
assert.ok(max <= 4);
});
for (const annotationView of ["active", "submitted"]) {
test(`${annotationView} sidebar is ready while auxiliary project data is still pending`, async () => {
const metadata = deferred();
const f = fixture(deferred());
const mine = [{ id: "mine", legacyTaskId: "task-a" }];
const tasks = [{ id: "task-a", sliceCount: 3 }];
const readyPages = [];
let finished = false;
const ready = loadOwnedAnnotationProject({
...f.options,
annotationView,
projects: { listMembers: () => metadata.promise, listWorkflows: () => metadata.promise },
human: { listItemsPage: async () => ({ items: mine }) },
annotation: {
...f.options.annotation,
listTemplates: () => metadata.promise,
listTasksPage: async () => ({ items: tasks }),
},
onTasks: (page) => readyPages.push(page),
}).then((value) => {
finished = true;
return value;
});
await new Promise((resolve) => setImmediate(resolve));
try {
assert.deepEqual(readyPages, [{ mine, tasks }], "sidebar must not wait for members, templates or workflows");
assert.equal(finished, false, "the full result must still wait for required project metadata");
} finally {
metadata.resolve([]);
const result = await ready;
assert.deepEqual(result, { members: [], templates: [], workflows: [], mine, tasks });
}
});
}
test("a late task page cannot replace a newer project or view", async () => {
const page = deferred();
let current = true;
const seen = [];
const f = fixture(deferred(), () => current);
const ready = loadOwnedAnnotationProject({
...f.options,
annotationView: "active",
human: { listItemsPage: async () => ({ items: [{ legacyTaskId: "task-a" }] }) },
annotation: { ...f.options.annotation, listTasksPage: () => page.promise },
onTasks: (value) => seen.push(value),
});
await new Promise((resolve) => setImmediate(resolve));
current = false;
page.resolve({ items: [{ id: "task-a" }] });
await ready;
assert.deepEqual(seen, []);
});
test("project failure prevents a later task response from publishing a ready sidebar", async () => {
const metadata = deferred(),
page = deferred();
const f = fixture(deferred());
const seen = [];
const ready = loadOwnedAnnotationProject({
...f.options,
annotationView: "active",
projects: { listMembers: () => metadata.promise, listWorkflows: async () => [] },
human: { listItemsPage: async () => ({ items: [{ legacyTaskId: "task-a" }] }) },
annotation: { ...f.options.annotation, listTasksPage: () => page.promise },
onTasks: (value) => seen.push(value),
});
const rejected = assert.rejects(ready, /metadata failed/);
await new Promise((resolve) => setImmediate(resolve));
metadata.reject(new Error("metadata failed"));
await rejected;
page.resolve({ items: [{ id: "task-a" }] });
await new Promise((resolve) => setImmediate(resolve));
assert.deepEqual(seen, []);
});
test("owned sidebar uses one read and publishes its cursor before auxiliary metadata", async () => {
const metadata = deferred();
const ready = [];
const pages = [];
const mine = [{ id: "mine", legacyTaskId: "task-a" }];
const tasks = [{ id: "task-a", sliceCount: 2 }];
const options = fixture(deferred()).options;
const loading = loadOwnedAnnotationProject({
...options,
annotationView: "active",
projects: { listMembers: () => metadata.promise, listWorkflows: () => metadata.promise },
human: {
listItemsPage: () => {
throw new Error("legacy pool must not run");
},
},
annotation: {
...options.annotation,
getSidebar: async (filter) => {
assert.deepEqual(filter, { projectId: "project-a", annotationView: "active" });
return { items: mine, tasks, nextCursor: "next", hasMore: true };
},
listTemplates: () => metadata.promise,
listTasksPage: () => {
throw new Error("legacy join must not run");
},
},
onTasks: (value) => ready.push(value),
onPage: (value) => pages.push(value),
});
await new Promise((resolve) => setImmediate(resolve));
try {
assert.deepEqual(ready, [{ mine, tasks }]);
assert.equal(pages[0]?.nextCursor, "next");
} finally {
metadata.resolve([]);
await loading;
}
});
test("sidebar denial never falls back to legacy access paths", async () => {
const options = fixture(deferred()).options;
let legacy = 0;
await assert.rejects(
loadOwnedAnnotationProject({
...options,
annotationView: "active",
human: {
listItemsPage: async () => {
legacy++;
return { items: [] };
},
},
annotation: {
...options.annotation,
getSidebar: async () => {
throw Object.assign(new Error("revoked"), { status: 403 });
},
},
}),
/revoked/,
);
assert.equal(legacy, 0);
});
for (const status of [404, 405, 500]) {
test(`sidebar status ${status} ${status === 500 ? "propagates" : "uses bounded legacy fallback"}`, async () => {
const calls = [];
const loading = loadAnnotationSidebar(
{
getSidebar: async () => {
throw Object.assign(new Error("unavailable"), { status });
},
listTasksPage: async (filter) => {
calls.push(filter.ids);
return { items: [{ id: "t" }] };
},
},
{
listItemsPage: async (filter) => {
calls.push(filter);
return { items: [{ legacyTaskId: "t" }], nextCursor: "cursor-2", hasMore: true };
},
},
{ projectId: "p", annotationView: "submitted", cursor: "cursor-1" },
);
if (status === 500) {
await assert.rejects(loading, /unavailable/);
assert.deepEqual(calls, []);
} else {
const result = await loading;
assert.equal(result.nextCursor, "cursor-2");
assert.deepEqual(result.tasks, [{ id: "t" }]);
assert.equal(calls[0].cursor, "cursor-1");
assert.equal(calls[0].annotationView, "submitted");
assert.deepEqual(calls[1], ["t"]);
}
});
}
test("empty legacy sidebar never issues an unscoped task query", async () => {
const result = await loadAnnotationSidebar(
{
listTasksPage: () => {
throw new Error("unscoped query");
},
},
{ listItemsPage: async () => ({ items: [], hasMore: false }) },
{ annotationView: "active" },
);
assert.deepEqual(result, { items: [], tasks: [], hasMore: false });
});
test("my-tasks count marks itself partial while another sidebar page exists", async () => {
// Review 2026-10-18: the annotator count was the number of loaded items, so
// 500 of 800 submitted items read as exactly 500.
const { readFileSync } = await import("node:fs");
const page = readFileSync(new URL("../../src/pages/AnnotationOperations.tsx", import.meta.url), "utf8");
const tabs = page.slice(
page.indexOf('className="annotation-task-pool-tabs'),
page.indexOf('data-testid="project-random-claim"'),
);
assert.match(tabs, /blindAnnotator && value === "mine" && minePoolCursorRef\.current/);
assert.match(tabs, /\? "\+"\s*:\s*""/);
});