forked from longtao.wu/droboticflow
Preserve legacy system suggestions and unreadable saved content across workspace writers. Compare explicit dictionary snapshots and normalize empty optional evidence during save readback; keep transient autosave failures retryable. Cover recovery entry points, source shapes, failed saves and stale-snapshot rejection with isolated HTTP and browser regressions.
349 lines
12 KiB
JavaScript
349 lines
12 KiB
JavaScript
import assert from "node:assert/strict";
|
|
import test from "node:test";
|
|
|
|
import {
|
|
annotationWorkspaceMatches,
|
|
createAuthoritativeMutationContext,
|
|
createAuthoritativeMutationScope,
|
|
sliceResetWasApplied,
|
|
settleAuthoritativeMutation,
|
|
} from "../../src/lib/annotation-authoritative-mutation.js";
|
|
|
|
function deferred() {
|
|
let resolve;
|
|
let reject;
|
|
const promise = new Promise((nextResolve, nextReject) => {
|
|
resolve = nextResolve;
|
|
reject = nextReject;
|
|
});
|
|
return { promise, resolve, reject };
|
|
}
|
|
|
|
test("a backend response confirms a write only while its task and account scope is current", async () => {
|
|
const scope = createAuthoritativeMutationScope();
|
|
const context = { taskId: "task-a", userId: "annotator-a" };
|
|
const token = scope.begin(context);
|
|
|
|
const result = await settleAuthoritativeMutation({
|
|
scope,
|
|
token,
|
|
currentContext: () => context,
|
|
write: async () => ({ item: { version: 8 }, workspace: { segments: [{ id: "saved" }] } }),
|
|
read: async () => assert.fail("confirmed writes must not reread"),
|
|
isApplied: () => false,
|
|
});
|
|
|
|
assert.deepEqual(result, {
|
|
status: "confirmed",
|
|
response: { item: { version: 8 }, workspace: { segments: [{ id: "saved" }] } },
|
|
reconciled: false,
|
|
});
|
|
});
|
|
|
|
test("a failed write rereads authority and recovers when the intended state was persisted", async () => {
|
|
const scope = createAuthoritativeMutationScope();
|
|
const context = { taskId: "task-a", userId: "annotator-a" };
|
|
const token = scope.begin(context);
|
|
const authoritative = { humanItem: { version: 8 }, persistedDescription: "拿起商品" };
|
|
|
|
const result = await settleAuthoritativeMutation({
|
|
scope,
|
|
token,
|
|
currentContext: () => context,
|
|
write: async () => {
|
|
throw new TypeError("Failed to fetch");
|
|
},
|
|
read: async () => authoritative,
|
|
isApplied: (detail) => detail.persistedDescription === "拿起商品",
|
|
});
|
|
|
|
assert.deepEqual(result, {
|
|
status: "confirmed",
|
|
authoritative,
|
|
reconciled: true,
|
|
});
|
|
});
|
|
|
|
test("a conflict keeps the attempted edit rejected after authority shows different state", async () => {
|
|
const scope = createAuthoritativeMutationScope();
|
|
const context = { taskId: "task-a", userId: "annotator-a" };
|
|
const token = scope.begin(context);
|
|
const conflict = Object.assign(new Error("Human item version conflict"), {
|
|
code: "version_conflict",
|
|
status: 409,
|
|
});
|
|
const authoritative = { humanItem: { version: 9 }, persistedDescription: "其他页面的新内容" };
|
|
|
|
const result = await settleAuthoritativeMutation({
|
|
scope,
|
|
token,
|
|
currentContext: () => context,
|
|
write: async () => {
|
|
throw conflict;
|
|
},
|
|
read: async () => authoritative,
|
|
isApplied: (detail) => detail.persistedDescription === "本页旧内容",
|
|
});
|
|
|
|
assert.equal(result.status, "rejected");
|
|
assert.equal(result.error, conflict);
|
|
assert.equal(result.authoritative, authoritative);
|
|
});
|
|
|
|
test("an unreadable authoritative state leaves the mutation unknown", async () => {
|
|
const scope = createAuthoritativeMutationScope();
|
|
const context = { taskId: "task-a", userId: "annotator-a" };
|
|
const token = scope.begin(context);
|
|
const writeError = new TypeError("Failed to fetch");
|
|
const readError = new TypeError("Failed to fetch");
|
|
|
|
const result = await settleAuthoritativeMutation({
|
|
scope,
|
|
token,
|
|
currentContext: () => context,
|
|
write: async () => {
|
|
throw writeError;
|
|
},
|
|
read: async () => {
|
|
throw readError;
|
|
},
|
|
isApplied: () => false,
|
|
});
|
|
|
|
assert.deepEqual(result, { status: "unknown", error: writeError, readError });
|
|
});
|
|
|
|
test("a late response cannot affect a newer mutation after returning to the original task", async () => {
|
|
const scope = createAuthoritativeMutationScope();
|
|
const context = createAuthoritativeMutationContext(scope, { taskId: "task-a", userId: "annotator-a" });
|
|
const token = scope.begin(context.current());
|
|
const response = deferred();
|
|
const pending = settleAuthoritativeMutation({
|
|
scope,
|
|
token,
|
|
currentContext: context.current,
|
|
write: () => response.promise,
|
|
read: async () => assert.fail("a late successful write must not reread"),
|
|
isApplied: () => false,
|
|
});
|
|
|
|
context.switchTo({ taskId: "task-b", userId: "annotator-a" });
|
|
context.switchTo({ taskId: "task-a", userId: "annotator-a" });
|
|
const newerToken = scope.begin(context.current());
|
|
response.resolve({ item: { version: 8 } });
|
|
|
|
assert.deepEqual(await pending, { status: "stale" });
|
|
assert.equal(scope.end(token), false);
|
|
assert.equal(scope.owns(newerToken, context.current()), true);
|
|
assert.equal(scope.busy(), true);
|
|
});
|
|
|
|
test("an account change invalidates authority before passive effects run", async () => {
|
|
const scope = createAuthoritativeMutationScope();
|
|
const context = createAuthoritativeMutationContext(scope, { taskId: "task-a", userId: "annotator-a" });
|
|
const token = scope.begin(context.current());
|
|
const response = deferred();
|
|
const pending = settleAuthoritativeMutation({
|
|
scope,
|
|
token,
|
|
currentContext: context.current,
|
|
write: () => response.promise,
|
|
read: async () => assert.fail("a late successful write must not reread"),
|
|
isApplied: () => false,
|
|
});
|
|
|
|
context.switchTo({ taskId: "task-a", userId: "annotator-b" });
|
|
response.resolve({ item: { version: 8 } });
|
|
|
|
assert.deepEqual(await pending, { status: "stale" });
|
|
});
|
|
|
|
test("a late finally cannot unlock a newer mutation with the same task and account", () => {
|
|
const scope = createAuthoritativeMutationScope();
|
|
const context = { taskId: "task-a", userId: "annotator-a" };
|
|
const first = scope.begin(context);
|
|
scope.invalidate();
|
|
const second = scope.begin(context);
|
|
|
|
assert.notEqual(first.id, second.id);
|
|
assert.equal(scope.end(first), false);
|
|
assert.equal(scope.owns(second, context), true);
|
|
assert.equal(scope.end(second), true);
|
|
assert.equal(scope.busy(), false);
|
|
});
|
|
|
|
test("authoritative description matching ignores server timing metadata but detects changed content", () => {
|
|
const expected = {
|
|
sliceSegments: [{ id: "slice-a", name: "取货", start: 0, end: 2, annotationStatus: "labeled" }],
|
|
operations: [],
|
|
segments: [
|
|
{
|
|
id: "description-a",
|
|
labelId: "template-a",
|
|
label: "",
|
|
handedness: "",
|
|
description: "拿起商品",
|
|
start: 0.5,
|
|
end: 1.5,
|
|
source: "manual",
|
|
},
|
|
],
|
|
statuses: { "slice-a": "labeled" },
|
|
};
|
|
const authoritative = {
|
|
...expected,
|
|
sliceSegments: [
|
|
{
|
|
...expected.sliceSegments[0],
|
|
annotationStatus: "pending",
|
|
startTimestampUs: 10_000,
|
|
endTimestampUs: 2_010_000,
|
|
},
|
|
],
|
|
segments: [
|
|
{
|
|
...expected.segments[0],
|
|
startTimestampUs: 510_000,
|
|
endTimestampUs: 1_510_000,
|
|
},
|
|
],
|
|
};
|
|
|
|
assert.equal(annotationWorkspaceMatches(authoritative, expected), true);
|
|
authoritative.segments[0].description = "放下商品";
|
|
assert.equal(annotationWorkspaceMatches(authoritative, expected), false);
|
|
});
|
|
|
|
test("slice reset reconciliation requires a newer authoritative reset workspace", () => {
|
|
const detail = {
|
|
humanItem: {
|
|
version: 12,
|
|
subject: {
|
|
payload: {
|
|
sliceWorkflow: { status: "editing", phase: "slice", resetAt: "2026-09-17T12:00:00Z" },
|
|
annotationWorkspace: {
|
|
sliceSegments: [{ id: "new-slice", name: "切片 001", start: 0, end: 10 }],
|
|
operations: [],
|
|
segments: [],
|
|
statuses: { "new-slice": "pending" },
|
|
},
|
|
},
|
|
},
|
|
},
|
|
};
|
|
|
|
assert.equal(sliceResetWasApplied(detail, 11), true);
|
|
assert.equal(sliceResetWasApplied(detail, 12), false);
|
|
// A timed-out write must be reconciled against its original version and mode,
|
|
// not a different reset that happened to leave an empty workspace.
|
|
assert.equal(sliceResetWasApplied(detail, 11, true), false);
|
|
Object.assign(detail.humanItem.subject.payload.sliceWorkflow, {
|
|
resetFromVersion: 11,
|
|
resetToUnifiedRange: false,
|
|
});
|
|
assert.equal(sliceResetWasApplied(detail, 11, false), true);
|
|
assert.equal(sliceResetWasApplied(detail, 11, true), false);
|
|
assert.equal(sliceResetWasApplied(detail, 10, false), false);
|
|
detail.humanItem.subject.payload.annotationWorkspace.segments.push({ id: "still-present" });
|
|
assert.equal(sliceResetWasApplied(detail, 11), false);
|
|
});
|
|
|
|
test("lost save responses cannot be confirmed from another dictionary or classification snapshot", async () => {
|
|
const selection = (kind) => ({ id: kind + "-id", kind, version: 2, code: kind, name: "冻结词条", tags: ["原分类"] });
|
|
const workspace = {
|
|
sliceSegments: [
|
|
{
|
|
id: "s",
|
|
name: "切片",
|
|
start: 0,
|
|
end: 4,
|
|
dictionary: selection("slice"),
|
|
discardReasonDictionary: selection("discard_reason"),
|
|
discardedAnnotations: [
|
|
{ id: "archived", start: 0, end: 1, description: "保留描述", dictionary: selection("action") },
|
|
],
|
|
},
|
|
],
|
|
segments: [{ id: "a", start: 0, end: 4, status: "pending", dictionary: selection("action") }],
|
|
statuses: { s: "pending" },
|
|
};
|
|
for (const target of [
|
|
(w) => w.sliceSegments[0].dictionary,
|
|
(w) => w.segments[0].dictionary,
|
|
(w) => w.sliceSegments[0].discardReasonDictionary,
|
|
(w) => w.sliceSegments[0].discardedAnnotations[0].dictionary,
|
|
]) {
|
|
for (const field of ["id", "version", "tags"]) {
|
|
const actual = structuredClone(workspace);
|
|
target(actual)[field] = field === "version" ? 3 : field === "tags" ? ["另一分类"] : "another-id";
|
|
const scope = createAuthoritativeMutationScope();
|
|
const context = { taskId: "a", userId: "operator" };
|
|
const token = scope.begin(context);
|
|
const result = await settleAuthoritativeMutation({
|
|
scope,
|
|
token,
|
|
currentContext: () => context,
|
|
write: async () => {
|
|
throw new Error("response lost");
|
|
},
|
|
read: async () => actual,
|
|
isApplied: (value) => annotationWorkspaceMatches(value, workspace),
|
|
});
|
|
assert.equal(result.status, "rejected", `${field} changes must not masquerade as the requested save`);
|
|
}
|
|
}
|
|
});
|
|
|
|
test("readback keeps legacy dictionary enrichment and semantic tag-set equivalence", () => {
|
|
const expected = {
|
|
sliceSegments: [{ id: "s", name: "旧切片", start: 0, end: 4 }],
|
|
segments: [{ id: "a", start: 0, end: 4, status: "pending" }],
|
|
};
|
|
const actual = structuredClone(expected);
|
|
actual.sliceSegments[0].dictionary = {
|
|
id: "slice",
|
|
kind: "slice",
|
|
version: 1,
|
|
code: "slice",
|
|
name: "旧切片",
|
|
tags: [],
|
|
};
|
|
assert.equal(annotationWorkspaceMatches(actual, expected), true);
|
|
expected.sliceSegments[0].dictionary = { ...actual.sliceSegments[0].dictionary, tags: ["甲", "乙"] };
|
|
actual.sliceSegments[0].dictionary.tags = ["乙", "甲"];
|
|
assert.equal(annotationWorkspaceMatches(actual, expected), true);
|
|
});
|
|
|
|
test("readback accepts omitted optional empty containers emitted by Go JSON", () => {
|
|
const expected = {
|
|
sliceSegments: [{ id: "s", name: "切片", start: 0, end: 4, sourceIds: [], discardedAnnotations: [] }],
|
|
segments: [{ id: "a", start: 0, end: 4, status: "pending", phrases: [] }],
|
|
};
|
|
const actual = {
|
|
sliceSegments: [{ id: "s", name: "切片", start: 0, end: 4 }],
|
|
segments: [{ id: "a", start: 0, end: 4, status: "pending" }],
|
|
};
|
|
assert.equal(annotationWorkspaceMatches(actual, expected), true);
|
|
actual.segments[0].phrases = [{ text: "保留的短语" }];
|
|
assert.equal(annotationWorkspaceMatches(actual, expected), false);
|
|
});
|
|
|
|
test("readback enriches historic compact dictionary identities without ignoring explicit tags", () => {
|
|
const expected = {
|
|
sliceSegments: [{ id: "s", name: "切片", start: 0, end: 4, dictionary: { id: "slice", version: 1 } }],
|
|
segments: [],
|
|
};
|
|
const actual = structuredClone(expected);
|
|
actual.sliceSegments[0].dictionary = {
|
|
id: "slice",
|
|
kind: "slice",
|
|
version: 1,
|
|
code: "slice",
|
|
name: "切片",
|
|
tags: ["既有分类"],
|
|
};
|
|
assert.equal(annotationWorkspaceMatches(actual, expected), true);
|
|
expected.sliceSegments[0].dictionary.tags = [];
|
|
assert.equal(annotationWorkspaceMatches(actual, expected), false);
|
|
});
|