forked from longtao.wu/droboticflow
217 lines
8.2 KiB
JavaScript
217 lines
8.2 KiB
JavaScript
import test from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import fs from "node:fs";
|
|
import ts from "typescript";
|
|
import { refillAnnotationWorkBuffer, reconcileAnnotationBufferSummaries } from "../../src/lib/annotation-work-mode.js";
|
|
const source = fs.readFileSync(new URL("../../src/pages/AnnotationOperations.tsx", import.meta.url), "utf8");
|
|
function callback(name, env) {
|
|
const ast = ts.createSourceFile("page.tsx", source, ts.ScriptTarget.Latest, true, ts.ScriptKind.TSX);
|
|
let text;
|
|
function visit(node) {
|
|
if (ts.isVariableDeclaration(node) && node.name.getText(ast) === name) text = "const " + node.getText(ast) + ";";
|
|
ts.forEachChild(node, visit);
|
|
}
|
|
visit(ast);
|
|
assert.ok(text, name);
|
|
return new Function(
|
|
"env",
|
|
`with(env){${ts.transpileModule(text, { compilerOptions: { target: ts.ScriptTarget.ES2022 } }).outputText};return ${name};}`,
|
|
)(
|
|
new Proxy(env, {
|
|
has: () => true,
|
|
get: (o, k) => (k === Symbol.unscopables ? undefined : k in o ? o[k] : globalThis[k]),
|
|
}),
|
|
);
|
|
}
|
|
const item = (id, extra = {}) => ({
|
|
id,
|
|
legacyTaskId: "task-" + id,
|
|
projectId: "p",
|
|
assignedUserId: "u",
|
|
processingProfile: "raw_human",
|
|
currentStageId: "annotation_combined",
|
|
status: "in_progress",
|
|
...extra,
|
|
});
|
|
function fixture({ mine = [item("gone")], items = [], read } = {}) {
|
|
let pools = { mine },
|
|
missing = null,
|
|
buffer = null;
|
|
const origin = { userId: "u", projectId: "p" };
|
|
const env = {
|
|
projectId: "p",
|
|
currentUser: { id: "u" },
|
|
bufferFlight: { current: false },
|
|
claimContextRef: { current: origin },
|
|
bufferReadEpochRef: { current: 0 },
|
|
bufferManagedIdsRef: { current: new Set() },
|
|
annotationMutationScopeRef: { current: { busy: () => false } },
|
|
detailRef: { current: null },
|
|
humanPoolsSnapshotRef: { current: pools },
|
|
humanLoopApi: { getItem: async (id) => ({ item: item(id, { subject: { payload: { fromDetail: true } } }) }) },
|
|
refillAnnotationWorkBuffer,
|
|
annotationOperationsApi: {
|
|
workBufferSummary:
|
|
read || (async () => ({ enabled: true, capacity: 8, occupied: items.length, summaries: items })),
|
|
},
|
|
workSession: { hasContinuousIntent: () => false, pauseContinuous: () => {} },
|
|
viewportBlocksDispatch: () => false,
|
|
setWorkBuffer: (x) => (buffer = x),
|
|
setHumanPools: (f) => (pools = f(pools)),
|
|
setTasks: () => {},
|
|
setExternalBufferTaskId: (x) => (missing = x),
|
|
reconcileAnnotationBufferSummaries,
|
|
};
|
|
return {
|
|
env,
|
|
run: () => callback("reconcileWorkBuffer", env)(() => true, false),
|
|
state: () => ({ pools, missing, buffer }),
|
|
};
|
|
}
|
|
test("authoritative empty buffer removes submitted work on the first poll while preserving legacy and other scope", async () => {
|
|
const f = fixture({
|
|
mine: [
|
|
item("gone"),
|
|
item("legacy", { processingProfile: "legacy" }),
|
|
item("other-project", { projectId: "other" }),
|
|
item("other-user", { assignedUserId: "other" }),
|
|
item("legacy-split", { currentStageId: "split_annotation" }),
|
|
item("legacy-semantic", { currentStageId: "semantic_annotation" }),
|
|
],
|
|
});
|
|
await f.run();
|
|
assert.deepEqual(
|
|
f.state().pools.mine.map((x) => x.id),
|
|
["legacy", "other-project", "other-user", "legacy-split", "legacy-semantic"],
|
|
);
|
|
});
|
|
test("a response read before a local submission cannot reinsert its old task", async () => {
|
|
let resolve;
|
|
const pending = new Promise((r) => (resolve = r));
|
|
const f = fixture({ mine: [], read: () => pending });
|
|
const run = f.run();
|
|
await Promise.resolve();
|
|
f.env.bufferReadEpochRef.current++;
|
|
resolve({ enabled: true, capacity: 8, occupied: 1, summaries: [item("gone")] });
|
|
await run;
|
|
assert.deepEqual(f.state().pools.mine, []);
|
|
assert.equal(f.state().buffer, null);
|
|
});
|
|
test("A to B to A scope changes cannot apply the original response", async () => {
|
|
let resolve;
|
|
const f = fixture({ mine: [], read: () => new Promise((r) => (resolve = r)) });
|
|
const run = f.run();
|
|
await Promise.resolve();
|
|
f.env.claimContextRef.current = { userId: "u", projectId: "p" };
|
|
resolve({ enabled: true, capacity: 8, occupied: 1, summaries: [item("gone")] });
|
|
await run;
|
|
assert.equal(f.state().buffer, null);
|
|
});
|
|
|
|
test("disappearing current task pauses work without replacing its draft or navigating", async () => {
|
|
const f = fixture();
|
|
const detail = {
|
|
task: { id: "task-gone", continuousActions: true },
|
|
humanItem: item("gone"),
|
|
draft: { text: "unsaved" },
|
|
};
|
|
f.env.detailRef.current = detail;
|
|
f.env.isOwnedRecoverableAnnotationItem = (x, p, u) => x.projectId === p && x.assignedUserId === u;
|
|
f.env.externalBufferTaskId = null;
|
|
let paused = 0;
|
|
f.env.workSession.pauseContinuous = () => paused++;
|
|
await f.run();
|
|
assert.equal(f.state().missing, "task-gone");
|
|
assert.equal(paused, 1);
|
|
assert.equal(f.env.detailRef.current, detail);
|
|
assert.equal(detail.draft.text, "unsaved");
|
|
});
|
|
test("disabled buffer does not clear legacy tasks", async () => {
|
|
const f = fixture({ read: async () => ({ enabled: false, capacity: 2, occupied: 0, summaries: [] }) });
|
|
await f.run();
|
|
assert.equal(f.state().pools.mine.length, 1);
|
|
});
|
|
test("failed reads preserve the previous task list and unlock the next poll", async () => {
|
|
const f = fixture({
|
|
read: async () => {
|
|
throw Error("offline");
|
|
},
|
|
});
|
|
await assert.rejects(f.run(), /offline/);
|
|
assert.equal(f.state().pools.mine.length, 1);
|
|
assert.equal(f.env.bufferFlight.current, false);
|
|
});
|
|
|
|
test("legacy records once observed in the server buffer are retired without guessing their profile", async () => {
|
|
const f = fixture({ mine: [item("known", { processingProfile: "" })] });
|
|
f.env.bufferManagedIdsRef.current.add("known");
|
|
await f.run();
|
|
assert.deepEqual(f.state().pools.mine, []);
|
|
});
|
|
test("snapshot refresh during a mutation never overwrites the task list", async () => {
|
|
const f = fixture({ items: [item("new")] });
|
|
f.env.annotationMutationScopeRef.current.busy = () => true;
|
|
await f.run();
|
|
assert.equal(f.state().pools.mine[0].id, "gone");
|
|
assert.equal(f.state().buffer, null);
|
|
});
|
|
|
|
test("late buffer snapshots never resume allocation after a local operation begins", async () => {
|
|
let release;
|
|
const f = fixture({ mine: [], read: () => new Promise((r) => (release = r)) });
|
|
f.env.workSession.hasContinuousIntent = () => true;
|
|
let claims = 0;
|
|
f.env.pendingClaimRequest = () => "";
|
|
f.env.newAnnotationClaimRequestID = () => "request";
|
|
f.env.localStorage = { setItem: () => {} };
|
|
f.env.randomClaimKeys = { current: {} };
|
|
f.env.forgetClaimRequest = () => {};
|
|
f.env.annotationOperationsApi.randomClaim = async () => {
|
|
claims++;
|
|
return { item: item("new"), task: { id: "task-new" } };
|
|
};
|
|
const run = callback("reconcileWorkBuffer", f.env)(() => true, true);
|
|
await Promise.resolve();
|
|
f.env.bufferReadEpochRef.current++;
|
|
release({ enabled: true, capacity: 8, occupied: 0, summaries: [] });
|
|
await run;
|
|
assert.equal(claims, 0);
|
|
assert.deepEqual(f.state().pools.mine, []);
|
|
});
|
|
|
|
test("summary refresh preserves payload and draft and never creates an incomplete item", async () => {
|
|
const subject = { payload: { draft: "unsaved", large: [1, 2, 3] } };
|
|
const f = fixture({ mine: [item("kept", { subject })], items: [item("kept", { version: 9 }), item("unopened")] });
|
|
await f.run();
|
|
assert.deepEqual(
|
|
f.state().pools.mine.map((x) => x.id),
|
|
["kept", "unopened"],
|
|
);
|
|
assert.equal(f.state().pools.mine[0].subject, subject);
|
|
assert.equal(subject.payload.draft, "unsaved");
|
|
assert.equal(f.state().buffer.summaries.length, 2);
|
|
});
|
|
|
|
test("stable summary polling does not request full task records", async () => {
|
|
const f = fixture({ mine: [item("known")], items: [item("known")] });
|
|
let reads = 0;
|
|
f.env.humanLoopApi.getItem = async () => {
|
|
reads++;
|
|
throw Error("unexpected detail read");
|
|
};
|
|
await f.run();
|
|
await f.run();
|
|
assert.equal(reads, 0);
|
|
});
|
|
|
|
test("scope changes during first detail hydration discard the entire late snapshot", async () => {
|
|
const f = fixture({ mine: [], items: [item("new")] });
|
|
f.env.humanLoopApi.getItem = async () => {
|
|
f.env.claimContextRef.current = { userId: "other", projectId: "p" };
|
|
return { item: item("new") };
|
|
};
|
|
await f.run();
|
|
assert.equal(f.state().buffer, null);
|
|
assert.deepEqual(f.state().pools.mine, []);
|
|
});
|