forked from longtao.wu/droboticflow
68 lines
3.1 KiB
JavaScript
68 lines
3.1 KiB
JavaScript
import test from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import fs from "node:fs";
|
|
import ts from "typescript";
|
|
|
|
const source = fs.readFileSync(new URL("../../src/services/api/annotation-operations.ts", import.meta.url), "utf8");
|
|
const ast = ts.createSourceFile("api.ts", source, ts.ScriptTarget.Latest, true);
|
|
const declaration = ast.statements.find(
|
|
(node) => ts.isFunctionDeclaration(node) && node.name?.text === "summarizeWorkBuffer",
|
|
);
|
|
const compiled = ts.transpileModule(declaration.getText(ast).replace(/^export /, ""), {
|
|
compilerOptions: { target: ts.ScriptTarget.ES2022 },
|
|
}).outputText;
|
|
const summarize = new Function(`${compiled}; return summarizeWorkBuffer;`)();
|
|
|
|
test("legacy full buffer converts to independent summary without mutating payload", () => {
|
|
const subject = {
|
|
payload: { sliceWorkflow: { status: "completed", phase: "annotate", large: [1, 2, 3] }, draft: "unsaved" },
|
|
};
|
|
const full = {
|
|
enabled: true,
|
|
capacity: 4,
|
|
occupied: 1,
|
|
processing: { i: { status: "failed", kind: "slice" } },
|
|
items: [{ id: "i", legacyTaskId: "t", version: 8, reviewRound: 2, status: "rework", subject }],
|
|
};
|
|
const snapshot = summarize(full);
|
|
assert.equal(snapshot.occupied, 1);
|
|
assert.equal(snapshot.capacity, 4);
|
|
assert.equal(snapshot.summaries[0].sliceStatus, "completed");
|
|
assert.equal(snapshot.summaries[0].slicePhase, "annotate");
|
|
assert.equal(snapshot.summaries[0].reviewRound, 2);
|
|
assert.equal(snapshot.summaries[0].version, 8);
|
|
assert.equal(snapshot.processing.i.status, "failed");
|
|
assert.equal("subject" in snapshot.summaries[0], false);
|
|
assert.equal("items" in snapshot, false);
|
|
assert.equal(full.items[0].subject, subject);
|
|
assert.equal(subject.payload.draft, "unsaved");
|
|
});
|
|
|
|
test("legacy disabled empty buffer preserves disabled state and empty summaries", () => {
|
|
assert.deepEqual(summarize({ enabled: false, capacity: 2, occupied: 0, items: [] }), {
|
|
enabled: false,
|
|
capacity: 2,
|
|
occupied: 0,
|
|
summaries: [],
|
|
});
|
|
});
|
|
|
|
test("source failure survives legacy conversion and prevents automatic selection until recovery", () => {
|
|
const pageSource = fs.readFileSync(new URL("../../src/pages/AnnotationOperations.tsx", import.meta.url), "utf8");
|
|
const pageAST = ts.createSourceFile("page.tsx", pageSource, ts.ScriptTarget.Latest, true, ts.ScriptKind.TSX);
|
|
const fn = pageAST.statements.find((n) => ts.isFunctionDeclaration(n) && n.name?.text === "readyBufferedItems");
|
|
const code = ts.transpileModule(fn.getText(pageAST), {
|
|
compilerOptions: { target: ts.ScriptTarget.ES2022 },
|
|
}).outputText;
|
|
const ready = new Function("hasDirectedAnnotationRework", `${code}; return readyBufferedItems;`)(() => false);
|
|
for (const status of ["failed", "pending", "unverified"]) {
|
|
const snapshot = summarize({ items: [{ id: "bad", sourceValidationStatus: status, subject: {} }] });
|
|
assert.equal(snapshot.summaries[0].sourceValidationStatus, status);
|
|
assert.deepEqual(ready(snapshot.summaries), []);
|
|
assert.deepEqual(
|
|
ready([{ ...snapshot.summaries[0], sourceValidationStatus: undefined }]).map((i) => i.id),
|
|
["bad"],
|
|
);
|
|
}
|
|
});
|