forked from longtao.wu/droboticflow
181 lines
6.2 KiB
JavaScript
181 lines
6.2 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/pages/AnnotationOperations.tsx", import.meta.url), "utf8");
|
|
const ast = ts.createSourceFile("page.tsx", source, ts.ScriptTarget.Latest, true, ts.ScriptKind.TSX);
|
|
function actualCallback(name, env) {
|
|
let text;
|
|
function visit(node) {
|
|
if (ts.isVariableDeclaration(node) && node.name.getText(ast) === name) text = "const " + node.getText(ast) + ";";
|
|
if (ts.isFunctionDeclaration(node) && node.name?.getText(ast) === name) text = node.getText(ast);
|
|
ts.forEachChild(node, visit);
|
|
}
|
|
visit(ast);
|
|
assert.ok(text, name);
|
|
const js = ts.transpileModule(text, { compilerOptions: { target: ts.ScriptTarget.ES2022 } }).outputText;
|
|
return new Function("env", `with(env){${js}; return ${name};}`)(
|
|
new Proxy(env, {
|
|
has: () => true,
|
|
get: (o, k) => (k === Symbol.unscopables ? undefined : k in o ? o[k] : globalThis[k]),
|
|
}),
|
|
);
|
|
}
|
|
function entry({ items = [], pending = "", continuous = false } = {}) {
|
|
const ctx = {},
|
|
taskScope = {};
|
|
let epoch = 0,
|
|
allocations = 0,
|
|
opened = "",
|
|
refill,
|
|
notice = "",
|
|
cleared = false;
|
|
const session = { active: false };
|
|
const env = {
|
|
viewportBlocksDispatch: () => false,
|
|
blindAnnotator: true,
|
|
canAccessWorkspace: true,
|
|
projectId: "p",
|
|
currentUser: { id: "u" },
|
|
projects: [{ id: "p" }],
|
|
loading: false,
|
|
projectLoadError: "",
|
|
annotationDirty: false,
|
|
saving: false,
|
|
randomClaimFlight: { current: false },
|
|
workEntryFlight: { current: false },
|
|
claimContextRef: { current: ctx },
|
|
annotationMutationContextRef: { current: { current: () => taskScope } },
|
|
leaveDirtyRef: { current: false },
|
|
claimPermissionRef: { current: true },
|
|
viewportDeferredEntryRef: { current: null },
|
|
workPage: true,
|
|
detailRef: { current: null },
|
|
selectedTaskId: "task",
|
|
workSession: {
|
|
continuousEpoch: () => epoch,
|
|
owns: () => true,
|
|
acquire: async () => ({ status: "acquired", session }),
|
|
setContinuous: () => epoch++,
|
|
prefersContinuous: () => continuous,
|
|
release: () => {},
|
|
activate: () => true,
|
|
pauseContinuous: () => {},
|
|
},
|
|
pendingClaimRequest: () => pending,
|
|
randomClaimTask: async () => allocations++,
|
|
reconcileWorkBuffer: async (_, auto) => {
|
|
refill = auto;
|
|
if (auto) allocations++;
|
|
return { enabled: true, summaries: items, occupied: items.length, capacity: 2 };
|
|
},
|
|
humanLoopApi: { getItem: async (id) => ({ item: items.find((item) => item.id === id) }) },
|
|
annotationOperationsApi: { getTaskSummary: async (id) => ({ task: { id } }) },
|
|
isOwnedRecoverableAnnotationItem: () => true,
|
|
readyBufferedItems: (x) => x,
|
|
projectLoadRequestRef: { current: 0 },
|
|
setLoading: () => {},
|
|
setProjectLoadError: () => {},
|
|
setHumanPools: () => {},
|
|
setTasks: () => {},
|
|
clearEditHistory: () => {
|
|
cleared = true;
|
|
},
|
|
setDetail: () => {},
|
|
setTaskDetailRefreshVersion: () => {},
|
|
openWorkTask: (id, s) => {
|
|
opened = id;
|
|
s.active = true;
|
|
},
|
|
navigate: () => {},
|
|
setRandomClaimNotice: (x) => (notice = x),
|
|
setWorkEntryBusy: () => {},
|
|
getActionErrorMessage: (e) => {
|
|
throw e;
|
|
},
|
|
};
|
|
return { run: actualCallback("startWork", env), state: () => ({ allocations, opened, refill, notice, cleared }) };
|
|
}
|
|
test("actual focused entry keeps manual mode and opens the selected owned task without allocation", async () => {
|
|
const x = entry({ items: [{ id: "owned", legacyTaskId: "task" }], pending: "unresolved-request" });
|
|
await x.run();
|
|
assert.deepEqual(x.state(), { allocations: 0, opened: "task", refill: false, notice: "", cleared: true });
|
|
});
|
|
test("actual focused entry with manual empty buffer never allocates", async () => {
|
|
const x = entry();
|
|
await x.run();
|
|
assert.equal(x.state().allocations, 0);
|
|
assert.equal(x.state().opened, "");
|
|
});
|
|
test("actual focused entry only reconciles an unresolved claim automatically when mode is explicitly on", async () => {
|
|
const x = entry({ continuous: true, pending: "unresolved-request" });
|
|
await x.run();
|
|
assert.equal(x.state().allocations, 1);
|
|
});
|
|
test("actual click on an owned task resumes a manual focused session without a claim", async () => {
|
|
const session = { active: false };
|
|
let opened = "",
|
|
mode,
|
|
claims = 0;
|
|
const scope = {};
|
|
const env = {
|
|
viewportBlocksDispatch: () => false,
|
|
canAccessWorkspace: true,
|
|
workEntryFlight: { current: false },
|
|
claimContextRef: { current: scope },
|
|
claimPermissionRef: { current: true },
|
|
setWorkEntryBusy: () => {},
|
|
workSession: {
|
|
acquire: async () => ({ status: "acquired", session }),
|
|
prefersContinuous: () => false,
|
|
setContinuous: (_, value) => (mode = value),
|
|
release: () => {},
|
|
},
|
|
explicitBufferTask: { current: "" },
|
|
openWorkTask: (id, s) => {
|
|
opened = id;
|
|
s.active = true;
|
|
},
|
|
randomClaimTask: () => claims++,
|
|
};
|
|
await actualCallback("resumeOwnedTask", env)("owned");
|
|
assert.equal(opened, "owned");
|
|
assert.equal(mode, false);
|
|
assert.equal(claims, 0);
|
|
});
|
|
|
|
test("actual manual random-click acquires a session and makes exactly one explicit claim", async () => {
|
|
const session = { active: false };
|
|
const scope = {};
|
|
let epoch = 0,
|
|
claims = 0;
|
|
const env = {
|
|
workPage: true,
|
|
viewportBlocksDispatch: () => false,
|
|
canAccessWorkspace: true,
|
|
annotationDirty: false,
|
|
workEntryFlight: { current: false },
|
|
claimContextRef: { current: scope },
|
|
claimPermissionRef: { current: true },
|
|
setWorkEntryBusy: () => {},
|
|
workSession: {
|
|
active: false,
|
|
acquire: async () => ({ status: "acquired", session }),
|
|
prefersContinuous: () => false,
|
|
setContinuous: () => epoch++,
|
|
continuousEpoch: () => epoch,
|
|
owns: () => true,
|
|
release: () => {},
|
|
},
|
|
randomClaimTask: async (enter, current, s) => {
|
|
assert.equal(enter, true);
|
|
assert.equal(current(), true);
|
|
assert.equal(s, session);
|
|
claims++;
|
|
s.active = true;
|
|
},
|
|
};
|
|
await actualCallback("claimTaskManually", env)();
|
|
assert.equal(claims, 1);
|
|
});
|