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.
109 lines
4.4 KiB
JavaScript
109 lines
4.4 KiB
JavaScript
import test from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import {
|
|
annotationSegmentsForSlice,
|
|
summarizeSliceProgress,
|
|
annotationDraftFromHumanItem,
|
|
} from "../../src/lib/annotation-workbench.js";
|
|
import { normalizeAnnotationWorkspace, normalizeAnnotationTaskDetail } from "../../src/lib/annotation-workspace.js";
|
|
|
|
const slices = [
|
|
{ id: "empty", start: 0, end: 3 },
|
|
{ id: "other", start: 3, end: 10 },
|
|
];
|
|
test("API boundary normalizes nullable lists but never erases malformed annotation data", () => {
|
|
for (const segments of [null, undefined, []]) {
|
|
const input = { sliceSegments: slices, segments, operations: null, statuses: null };
|
|
const output = normalizeAnnotationWorkspace(input);
|
|
assert.deepEqual(output.segments, []);
|
|
assert.deepEqual(output.operations, []);
|
|
assert.deepEqual(output.statuses, {});
|
|
assert.equal(output.sliceSegments, slices);
|
|
assert.equal(input.segments, segments);
|
|
}
|
|
for (const segments of ["bad", {}, 0])
|
|
assert.throws(() => normalizeAnnotationWorkspace({ sliceSegments: slices, segments }), /动作描述数据格式错误/);
|
|
assert.throws(() => normalizeAnnotationWorkspace({ sliceSegments: null, segments: [] }), /切片工作区/);
|
|
});
|
|
test("blank revision payloads remain blank and cannot crash model-prelabel selection", () => {
|
|
const detail = {
|
|
task: { id: "test" },
|
|
revisions: [
|
|
{ revision: 0, segments: null },
|
|
{ revision: 1, segments: [] },
|
|
],
|
|
sliceRevisions: null,
|
|
reviews: null,
|
|
};
|
|
const value = normalizeAnnotationTaskDetail(detail);
|
|
assert.deepEqual(value.sliceRevisions, []);
|
|
assert.deepEqual(value.reviews, []);
|
|
assert.deepEqual(
|
|
value.revisions.map((r) => r.segments),
|
|
[[], []],
|
|
);
|
|
assert.equal(detail.revisions[0].segments, null);
|
|
const events = [{ id: "a", start: 1, end: 2, description: "原描述" }];
|
|
assert.equal(normalizeAnnotationWorkspace({ sliceSegments: slices, segments: events }).segments, events);
|
|
});
|
|
test("restoring a blank slice accepts the historical null segments response", () => {
|
|
assert.deepEqual(annotationSegmentsForSlice(slices[0], null), []);
|
|
const progress = summarizeSliceProgress(slices, null, {});
|
|
assert.equal(progress.pending, 2);
|
|
assert.equal(progress.labeled, 0);
|
|
assert.equal(progress.discarded, 0);
|
|
});
|
|
test("refresh preserves historical blank workspace slice dispositions and boundaries", () => {
|
|
const raw = {
|
|
sliceSegments: [{ ...slices[0], deliveryDisposition: "discard", discardReason: "遮挡" }, slices[1]],
|
|
segments: null,
|
|
operations: null,
|
|
statuses: null,
|
|
userId: "owner",
|
|
};
|
|
const snapshot = JSON.stringify(raw),
|
|
restored = annotationDraftFromHumanItem({ subject: { payload: { annotationWorkspace: raw } } });
|
|
assert.ok(restored);
|
|
assert.deepEqual(restored.segments, []);
|
|
assert.deepEqual(restored.operations, []);
|
|
assert.deepEqual(restored.statuses, {});
|
|
assert.deepEqual(restored.sliceSegments, raw.sliceSegments);
|
|
assert.equal(JSON.stringify(raw), snapshot);
|
|
});
|
|
|
|
test("task recovery never falls back to an empty editor for malformed saved containers", async () => {
|
|
const { annotationRecoverableWorkspaceFromHumanItem } = await import("../../src/lib/annotation-workbench.js");
|
|
for (const key of ["annotationWorkspace", "annotationDraft"]) {
|
|
for (const raw of [
|
|
"unreadable saved content",
|
|
false,
|
|
{ sliceSegments: slices, segments: { description: "保留内容" } },
|
|
{ sliceSegments: slices, segments: [], operations: {} },
|
|
{ sliceSegments: slices, segments: [], statuses: [] },
|
|
{ sliceSegments: null, segments: [{ id: "a", start: 0, end: 1, description: "保留内容" }] },
|
|
]) {
|
|
const item = {
|
|
assignedUserId: "u",
|
|
status: "claimed",
|
|
currentStageId: "annotation_combined",
|
|
subject: { payload: { [key]: raw } },
|
|
};
|
|
assert.throws(
|
|
() => annotationRecoverableWorkspaceFromHumanItem(item),
|
|
/格式错误/,
|
|
`${key} must not silently become an empty initial partition`,
|
|
);
|
|
}
|
|
}
|
|
});
|
|
|
|
test("task recovery keeps absent and empty historic placeholders compatible", async () => {
|
|
const { annotationRecoverableWorkspaceFromHumanItem } = await import("../../src/lib/annotation-workbench.js");
|
|
for (const raw of [null, undefined, {}, { userId: "u", segments: null, operations: [], statuses: {} }]) {
|
|
assert.equal(
|
|
annotationRecoverableWorkspaceFromHumanItem({ subject: { payload: { annotationWorkspace: raw } } }),
|
|
null,
|
|
);
|
|
}
|
|
});
|