droboticflow/tests/frontend/annotation-generation-retry.test.mjs
d-robotics 63512da71f
Some checks failed
web-ci / checks (push) Waiting to run
web-ci / checks (pull_request) Has been cancelled
🐛 Preserve confirmed annotation frames and enforce v4 time evidence
2026-10-22 12:54:08 +08:00

176 lines
6.0 KiB
JavaScript

import test from "node:test";
import assert from "node:assert/strict";
import {
annotationGenerationConfirmationMode,
retryAnnotationGeneration,
SLICE_RETRY_CONFLICT_MESSAGE,
} from "../../src/lib/annotation-generation-retry.js";
import { createAnnotationPersistence } from "../../src/lib/annotation-persistence.js";
import {
createAuthoritativeMutationContext,
createAuthoritativeMutationScope,
} from "../../src/lib/annotation-authoritative-mutation.js";
function fixture() {
const scope = createAuthoritativeMutationScope();
const context = createAuthoritativeMutationContext(scope, { taskId: "task", userId: "operator" });
const state = {
version: 4,
dirty: false,
saving: false,
locked: false,
owned: true,
history: ["edit"],
slices: [{ id: "saved", start: 0, end: 0.366728 }],
wait: async () => true,
request: async () => ({ item: { version: 6 } }),
};
const calls = [],
events = [];
const persistence = createAnnotationPersistence({
scope,
context: context.current,
adapter: {
waitForPending: () => state.wait(),
version: () => state.version,
},
view: {
canWrite: () => state.owned,
begin() {},
idle() {},
blocked() {},
},
});
const api = Object.fromEntries(
["retryFailedSlices", "reconfirmSavedSlices", "recoverExistingSlices"].map((method) => [
method,
async (taskId, version) => {
calls.push({ method, taskId, version });
return state.request();
},
]),
);
const run = (mode = "retryFailed") =>
retryAnnotationGeneration({
taskId: "task",
mode,
canStart: () => !state.dirty && !state.saving && !state.locked && state.owned,
persistence,
owns: (token) => scope.owns(token, context.current()),
api,
accepted: () => {
state.history = [];
events.push("accepted");
},
conflict: (message) => events.push(message),
reconcile: () => events.push("reconcile"),
});
return { state, calls, events, run, scope, context };
}
test("confirmation uses server eligibility, including legacy recovery precedence", () => {
assert.equal(annotationGenerationConfirmationMode(undefined), "retryFailed");
assert.equal(annotationGenerationConfirmationMode({ canReconfirmSavedGeneration: false }), "retryFailed");
assert.equal(annotationGenerationConfirmationMode({ canReconfirmSavedGeneration: true }), "reconfirmSaved");
assert.equal(
annotationGenerationConfirmationMode({ canRecoverGeneration: true, canReconfirmSavedGeneration: true }),
"recoverExisting",
);
});
test("each explicit generation action uses the post-autosave version and clears history only on acceptance", async () => {
for (const [mode, method] of [
["retryFailed", "retryFailedSlices"],
["reconfirmSaved", "reconfirmSavedSlices"],
["recoverExisting", "recoverExistingSlices"],
]) {
const f = fixture();
f.state.wait = async () => {
f.state.version = 5;
return true;
};
f.state.request = async () => {
assert.deepEqual(f.state.history, ["edit"]);
return { item: { version: 6 } };
};
await f.run(mode);
assert.deepEqual(f.calls, [{ method, taskId: "task", version: 5 }]);
assert.deepEqual(f.events, ["accepted"]);
assert.deepEqual(f.state.history, []);
assert.deepEqual(f.state.slices, [{ id: "saved", start: 0, end: 0.366728 }]);
assert.equal(f.scope.busy(), false);
}
});
test("retry snapshot conflict never switches to saved content or resets editor/history", async () => {
const f = fixture();
f.state.request = async () => {
throw { status: 409, code: "slice_retry_snapshot_conflict", message: "internal details" };
};
const before = structuredClone(f.state.slices);
await f.run();
assert.deepEqual(
f.calls.map((c) => c.method),
["retryFailedSlices"],
);
assert.deepEqual(f.events, [SLICE_RETRY_CONFLICT_MESSAGE]);
assert.deepEqual(f.state.history, ["edit"]);
assert.deepEqual(f.state.slices, before);
assert.equal(f.scope.busy(), false);
// A later explicit button click is the only way to choose current saved data.
f.state.request = async () => ({ item: { version: 6 } });
await f.run("reconfirmSaved");
assert.deepEqual(
f.calls.map((c) => c.method),
["retryFailedSlices", "reconfirmSavedSlices"],
);
});
test("other rejection or response loss reconciles without retrying another snapshot", async () => {
for (const mode of ["retryFailed", "reconfirmSaved", "recoverExisting"]) {
const f = fixture();
f.state.request = async () => {
throw new Error("response lost");
};
await f.run(mode);
assert.equal(f.calls.length, 1);
assert.deepEqual(f.events, ["reconcile"]);
assert.deepEqual(f.state.history, ["edit"]);
assert.equal(f.scope.busy(), false);
}
});
test("dirty, saving, locked, unowned and unsettled autosave block all generation commands", async () => {
for (const mode of ["retryFailed", "reconfirmSaved", "recoverExisting"]) {
for (const field of ["dirty", "saving", "locked", "owned", "wait"]) {
const f = fixture();
f.state[field] = field === "wait" ? async () => false : field !== "owned";
await f.run(mode);
assert.deepEqual(f.calls, []);
assert.deepEqual(f.state.history, ["edit"]);
assert.equal(f.scope.busy(), false);
}
}
});
test("task/account switches during autosave or a request fence stale completions", async () => {
for (const userId of ["operator", "other"]) {
for (const stage of ["wait", "request"]) {
const f = fixture();
let finish;
f.state[stage] = () =>
new Promise((resolve) => {
finish = resolve;
});
const pending = f.run("reconfirmSaved");
while (!finish) await Promise.resolve();
f.context.switchTo({ taskId: userId === "operator" ? "other-task" : "task", userId });
finish(stage === "wait" ? true : { item: { version: 6 } });
await pending;
assert.equal(f.calls.length, stage === "wait" ? 0 : 1);
assert.deepEqual(f.events, []);
assert.deepEqual(f.state.history, ["edit"]);
}
}
});