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.
487 lines
19 KiB
JavaScript
487 lines
19 KiB
JavaScript
import assert from "node:assert/strict";
|
||
import fs from "node:fs";
|
||
import test from "node:test";
|
||
|
||
import * as annotationWorkbench from "../../src/lib/annotation-workbench.js";
|
||
import {
|
||
annotationDraftFromHumanItem,
|
||
annotationEventOverlaps,
|
||
annotationOrphanSegments,
|
||
applySliceStatuses,
|
||
deriveSliceStatuses,
|
||
filterAnnotationTasks,
|
||
findSliceIndexForEvent,
|
||
mergePagedManifestMembers,
|
||
nearestFrameBoundaryIndex,
|
||
normalizeFrameBoundaries,
|
||
parseSegmentCommentsFromText,
|
||
serializeSegmentCommentsToText,
|
||
stepFrameBoundary,
|
||
summarizeSliceProgress,
|
||
validateAnnotationWorkspace,
|
||
} from "../../src/lib/annotation-workbench.js";
|
||
|
||
test("splitting a selected slice returns two fresh children and keeps the completed child selected", () => {
|
||
const source = [
|
||
{ id: "before", name: "搬运", start: 0, end: 2 },
|
||
{ id: "current", name: "装配", start: 2, end: 6 },
|
||
{ id: "after", name: "检查", start: 6, end: 9 },
|
||
];
|
||
|
||
const result = annotationWorkbench.splitSliceAtBoundary?.(source, 1, 4, "current-left", "current-right");
|
||
|
||
assert.deepEqual(result, {
|
||
slices: [
|
||
source[0],
|
||
{ id: "current-left", name: "装配", start: 2, end: 4, sourceIds: ["current"] },
|
||
{ id: "current-right", name: "装配", start: 4, end: 6, sourceIds: ["current"] },
|
||
source[2],
|
||
],
|
||
selectedSliceId: "current-left",
|
||
});
|
||
assert.deepEqual(source[1], { id: "current", name: "装配", start: 2, end: 6 });
|
||
});
|
||
|
||
test("playback spans the Capture while slicing and the selected slice while annotating", () => {
|
||
const selected = { id: "middle", start: 2, end: 6 };
|
||
|
||
assert.deepEqual(annotationWorkbench.capturePlaybackBounds?.("slice", selected, 0, 9), {
|
||
start: 0,
|
||
end: 9,
|
||
});
|
||
assert.deepEqual(annotationWorkbench.capturePlaybackBounds?.("annotate", selected, 0, 9), {
|
||
start: 2,
|
||
end: 6,
|
||
});
|
||
});
|
||
|
||
test("explicit slice review stops at its boundary even during Capture slicing", () => {
|
||
assert.deepEqual(annotationWorkbench.capturePlaybackBounds("slice", { start: 2, end: 6 }, 0, 9, true), {
|
||
start: 2,
|
||
end: 6,
|
||
});
|
||
});
|
||
|
||
test("a later cut targets the slice containing the continuously advanced playhead", () => {
|
||
const partition = [
|
||
{ id: "selected-left", start: 0, end: 4 },
|
||
{ id: "right", start: 4, end: 8 },
|
||
{ id: "tail", start: 8, end: 12 },
|
||
];
|
||
|
||
assert.equal(annotationWorkbench.sliceIndexAtPlayhead?.(partition, 6.5), 1);
|
||
assert.equal(annotationWorkbench.sliceIndexAtPlayhead?.(partition, 10), 2);
|
||
assert.equal(annotationWorkbench.sliceIndexAtPlayhead?.(partition, 12.1), -1);
|
||
});
|
||
|
||
test("cutting at an existing boundary preserves the partition and explains why it was rejected", () => {
|
||
const slice = { id: "right", start: 4, end: 8 };
|
||
|
||
assert.match(annotationWorkbench.sliceCutIssue?.(slice, 4) || "", /切片边界/);
|
||
assert.match(annotationWorkbench.sliceCutIssue?.(slice, 8) || "", /切片边界/);
|
||
assert.equal(annotationWorkbench.sliceCutIssue?.(slice, 6), "");
|
||
});
|
||
|
||
const slices = [
|
||
{ id: "slice-001", start: 0, end: 5 },
|
||
{ id: "slice-002", start: 5, end: 10 },
|
||
{ id: "slice-003", start: 10, end: 15 },
|
||
];
|
||
const segments = [{ id: "event-001", labelId: "01", label: "鲜食处理", start: 1, end: 3 }];
|
||
|
||
test("slice progress distinguishes labeled confirmed-empty and untouched slices", () => {
|
||
const progress = summarizeSliceProgress(slices, segments, { "slice-002": "empty_confirmed" });
|
||
assert.deepEqual(
|
||
{
|
||
labeled: progress.labeled,
|
||
emptyConfirmed: progress.emptyConfirmed,
|
||
pending: progress.pending,
|
||
completed: progress.completed,
|
||
total: progress.total,
|
||
},
|
||
{ labeled: 1, emptyConfirmed: 1, pending: 1, completed: 2, total: 3 },
|
||
);
|
||
assert.equal(progress.ratio, 2 / 3);
|
||
assert.deepEqual(
|
||
applySliceStatuses(slices, progress.statuses).map((slice) => slice.annotationStatus),
|
||
["labeled", "empty_confirmed", "pending"],
|
||
);
|
||
});
|
||
|
||
test("events override stale empty confirmation while removing the last event returns to pending", () => {
|
||
assert.equal(deriveSliceStatuses(slices, segments, { "slice-001": "empty_confirmed" })["slice-001"], "labeled");
|
||
assert.equal(deriveSliceStatuses(slices, [], { "slice-001": "labeled" })["slice-001"], "pending");
|
||
});
|
||
|
||
test("a published task label completes a slice even when detailed events are optional", () => {
|
||
const labeled = [{ ...slices[0], taskLabelId: "01", taskLabel: "鲜食处理" }, ...slices.slice(1)];
|
||
assert.equal(deriveSliceStatuses(labeled, [], {})["slice-001"], "labeled");
|
||
});
|
||
|
||
test("event navigation resolves the owning slice and rejects cross-boundary events", () => {
|
||
assert.equal(findSliceIndexForEvent(slices, segments[0]), 0);
|
||
const crossBoundary = { id: "cross", labelId: "01", label: "bad", start: 4, end: 6 };
|
||
assert.equal(findSliceIndexForEvent(slices, crossBoundary), -1);
|
||
assert.deepEqual(
|
||
annotationOrphanSegments(slices, [crossBoundary]).map((event) => event.id),
|
||
["cross"],
|
||
);
|
||
});
|
||
|
||
test("workspace validation detects duplicate ids and orphan events", () => {
|
||
const issues = validateAnnotationWorkspace(
|
||
[...slices, { id: "slice-001", start: 15, end: 20 }],
|
||
[...segments, { ...segments[0] }, { ...segments[0], id: "outside", start: 20, end: 21 }],
|
||
);
|
||
assert.ok(issues.some((issue) => issue.code === "slice_id_duplicate"));
|
||
assert.ok(issues.some((issue) => issue.code === "event_id_duplicate"));
|
||
assert.ok(issues.some((issue) => issue.code === "event_outside_slice"));
|
||
});
|
||
|
||
test("annotation events fail closed on overlap while touching boundaries remain valid", () => {
|
||
assert.equal(
|
||
annotationEventOverlaps(segments, { id: "candidate", start: 2.5, end: 4, labelId: "02", label: "重叠" }),
|
||
true,
|
||
);
|
||
assert.equal(
|
||
annotationEventOverlaps(segments, { id: "candidate", start: 3, end: 5, labelId: "02", label: "相邻" }),
|
||
false,
|
||
);
|
||
const issues = validateAnnotationWorkspace(slices, [
|
||
...segments,
|
||
{ id: "overlap", labelId: "02", label: "重叠", start: 2.5, end: 4 },
|
||
]);
|
||
assert.ok(issues.some((issue) => issue.code === "event_overlap"));
|
||
});
|
||
|
||
test("manifest pagination merges more than 200 members without duplicates", () => {
|
||
const pageOne = {
|
||
items: Array.from({ length: 200 }, (_, index) => ({ id: `member-${index}` })),
|
||
total: 386,
|
||
offset: 0,
|
||
truncated: true,
|
||
};
|
||
const pageTwo = {
|
||
items: Array.from({ length: 187 }, (_, index) => ({ id: `member-${index + 199}` })),
|
||
total: 386,
|
||
offset: 200,
|
||
truncated: true,
|
||
};
|
||
const merged = mergePagedManifestMembers([pageOne, pageTwo]);
|
||
assert.equal(merged.items.length, 386);
|
||
assert.equal(new Set(merged.items.map((item) => item.id)).size, 386);
|
||
assert.equal(merged.truncated, false);
|
||
});
|
||
|
||
test("task search prioritizes Capture context and supports status filters", () => {
|
||
const tasks = [
|
||
{ id: "task-a", runId: "run-a", status: "pending_annotation" },
|
||
{ id: "task-b", runId: "run-b", status: "accepted" },
|
||
{ id: "task-c", runId: "run-c", status: "pending_annotation" },
|
||
{ id: "task-d", runId: "run-d", status: "pending_annotation" },
|
||
];
|
||
const context = new Map([
|
||
[
|
||
"task-a",
|
||
{
|
||
captureDirectories: ["capture-kitchen-001"],
|
||
projectName: "泛宇",
|
||
workflowName: "同步切分",
|
||
detailedStatus: "pending_slice",
|
||
},
|
||
],
|
||
[
|
||
"task-b",
|
||
{
|
||
captureDirectories: ["capture-store-002"],
|
||
projectName: "泛宇",
|
||
workflowName: "同步切分",
|
||
detailedStatus: "accepted",
|
||
},
|
||
],
|
||
[
|
||
"task-c",
|
||
{
|
||
captureDirectories: ["capture-office-003"],
|
||
projectName: "泛宇",
|
||
workflowName: "同步切分",
|
||
detailedStatus: "slicing",
|
||
},
|
||
],
|
||
[
|
||
"task-d",
|
||
{
|
||
captureDirectories: ["capture-lab-004"],
|
||
projectName: "泛宇",
|
||
workflowName: "同步切分",
|
||
detailedStatus: "pending_annotation",
|
||
},
|
||
],
|
||
]);
|
||
assert.deepEqual(
|
||
filterAnnotationTasks(tasks, context, "kitchen", "all").map((task) => task.id),
|
||
["task-a"],
|
||
);
|
||
assert.deepEqual(
|
||
filterAnnotationTasks(tasks, context, "", "accepted").map((task) => task.id),
|
||
["task-b"],
|
||
);
|
||
assert.deepEqual(
|
||
filterAnnotationTasks(tasks, context, "", "pending_slice").map((task) => task.id),
|
||
["task-a"],
|
||
);
|
||
assert.deepEqual(
|
||
filterAnnotationTasks(tasks, context, "", "slicing").map((task) => task.id),
|
||
["task-c"],
|
||
);
|
||
assert.deepEqual(
|
||
filterAnnotationTasks(tasks, context, "", "pending_annotation").map((task) => task.id),
|
||
["task-d"],
|
||
);
|
||
});
|
||
|
||
test("server draft restores slice decisions without confusing an empty slice with untouched", () => {
|
||
const draft = annotationDraftFromHumanItem({
|
||
subject: {
|
||
payload: {
|
||
annotationDraft: {
|
||
sliceSegments: slices,
|
||
segments,
|
||
operations: [],
|
||
statuses: { "slice-001": "labeled", "slice-002": "empty_confirmed", "slice-003": "pending" },
|
||
userId: "annotator-a",
|
||
savedAt: "2026-08-30T00:00:00Z",
|
||
},
|
||
},
|
||
},
|
||
});
|
||
assert.equal(draft.userId, "annotator-a");
|
||
assert.equal(draft.statuses["slice-002"], "empty_confirmed");
|
||
});
|
||
|
||
test("empty annotation collections from disposition responses remain a valid workspace", () => {
|
||
assert.equal(typeof annotationWorkbench.normalizeAnnotationWorkspace, "function");
|
||
const workspace = {
|
||
sliceSegments: [{ id: "slice-001", start: 0, end: 1, deliveryDisposition: "keep" }],
|
||
segments: null,
|
||
operations: null,
|
||
statuses: { "slice-001": "pending" },
|
||
userId: "annotator-a",
|
||
savedAt: "2026-09-17T00:00:00Z",
|
||
};
|
||
|
||
const normalized = annotationWorkbench.normalizeAnnotationWorkspace(workspace);
|
||
assert.deepEqual(normalized.segments, []);
|
||
assert.deepEqual(normalized.operations, []);
|
||
assert.equal(normalized.sliceSegments[0].deliveryDisposition, "keep");
|
||
assert.equal(normalized.statuses["slice-001"], "pending");
|
||
assert.throws(
|
||
() => annotationWorkbench.normalizeAnnotationWorkspace({ ...workspace, segments: { unexpected: true } }),
|
||
/格式错误/,
|
||
);
|
||
|
||
const restored = annotationDraftFromHumanItem({ subject: { payload: { annotationWorkspace: workspace } } });
|
||
assert.deepEqual(restored, normalized);
|
||
});
|
||
|
||
test("frame boundary helpers use the real irregular PTS sequence", () => {
|
||
const boundaries = normalizeFrameBoundaries([0, 0.033333, 0.066667, 0.100001], 0, 0.100001);
|
||
assert.deepEqual(boundaries, [0, 0.033333, 0.066667, 0.100001]);
|
||
assert.equal(nearestFrameBoundaryIndex(boundaries, 0.065), 2);
|
||
assert.equal(stepFrameBoundary(boundaries, 0.065, -1), 0.033333);
|
||
assert.equal(stepFrameBoundary(boundaries, 0.065, 1), 0.100001);
|
||
});
|
||
|
||
test("annotation workbench exposes compact visual slicing and JSON description controls", () => {
|
||
const component = fs.readFileSync(
|
||
new URL("../../src/components/CaptureAnnotationWorkbench.tsx", import.meta.url),
|
||
"utf8",
|
||
);
|
||
const page = fs.readFileSync(new URL("../../src/pages/AnnotationOperations.tsx", import.meta.url), "utf8");
|
||
assert.match(component, /data-testid="capture-annotation-workbench"/);
|
||
assert.match(component, /确认全部切片/);
|
||
assert.match(component, /逐片标注/);
|
||
assert.doesNotMatch(component, /高级模式/);
|
||
assert.doesNotMatch(component, /prelabels/);
|
||
assert.match(component, /onPointerDown=\{\(event\) => beginSliceBoundaryDrag\(event, index\)\}/);
|
||
assert.match(component, /从字典选择/);
|
||
assert.match(component, /设为无效片段/);
|
||
assert.match(component, /当前区间已设为无效片段/);
|
||
assert.match(component, /aria-label="动作描述"/);
|
||
assert.doesNotMatch(component, /使用手别/);
|
||
assert.doesNotMatch(component, /当前切片(任务)标签/);
|
||
assert.match(component, /checked=\{showLeft\}/);
|
||
assert.match(component, /checked=\{showRight\}/);
|
||
assert.match(component, /上一帧/);
|
||
assert.match(component, /下一帧/);
|
||
assert.match(component, /insertSlice\(splitPreview\.sliceIndex, splitPreview\.time\)/);
|
||
assert.match(component, /删除切片/);
|
||
assert.match(component, /annotationBoundaryDragRef/);
|
||
assert.match(component, /beginAnnotationBoundaryDrag/);
|
||
assert.doesNotMatch(component, /切片可用性/);
|
||
assert.match(component, /frameBoundaries/);
|
||
assert.doesNotMatch(page, /保存服务端草稿/);
|
||
assert.match(page, /annotationPlayhead/);
|
||
assert.match(page, /onConfirmSlices=\{\(\) => void confirmSlicesAndMaterialize\(\)\}/);
|
||
// 票 06(D1 修订版):首标「重新切片」入口恢复(仅首标任务渲染),E 模式保留。
|
||
assert.match(page, /重新切片会丢失全部标注/);
|
||
assert.match(component, /重新切片将删除全部已有标注/);
|
||
assert.match(component, /sliceEditPanelShortcut/);
|
||
assert.match(component, /切片重设面板/);
|
||
assert.match(page, /onConfirmAnnotations=\{confirmAnnotationDescription\}/);
|
||
assert.doesNotMatch(page, /切片与语义修订历史/);
|
||
assert.doesNotMatch(page, /审核历史/);
|
||
assert.doesNotMatch(page, /默认仅显示待处理任务/);
|
||
assert.match(page, /getFrameTimeline/);
|
||
assert.match(page, /搜索 Capture/);
|
||
});
|
||
|
||
test("segment QA comments serialize and parse correctly across reviewer and rework workflows", () => {
|
||
const segmentComments = [
|
||
{
|
||
sliceName: "切片 001",
|
||
segmentIndex: 1,
|
||
timeRange: "00:03.500-00:06.904",
|
||
tag: "时间边界偏差",
|
||
comment: "放杯子起始时间提前了约0.5s,需微调对齐",
|
||
},
|
||
{
|
||
sliceName: "切片 002",
|
||
segmentIndex: 0,
|
||
timeRange: "00:07.199-00:10.500",
|
||
tag: "动作描述不准",
|
||
comment: "应描述为“双手将杯子平稳移至桌面上”",
|
||
},
|
||
];
|
||
const generalComment = "请重点核对上述2处问题片段后重新提交。";
|
||
|
||
const serialized = serializeSegmentCommentsToText(segmentComments, generalComment);
|
||
assert.match(
|
||
serialized,
|
||
/【切片 001 · 动作 #2 \(00:03\.500-00:06\.904\)】\[时间边界偏差\] 放杯子起始时间提前了约0\.5s,需微调对齐/,
|
||
);
|
||
assert.match(
|
||
serialized,
|
||
/【切片 002 · 动作 #1 \(00:07\.199-00:10\.500\)】\[动作描述不准\] 应描述为“双手将杯子平稳移至桌面上”/,
|
||
);
|
||
assert.match(serialized, /请重点核对上述2处问题片段后重新提交。/);
|
||
|
||
const parsed = parseSegmentCommentsFromText(serialized);
|
||
assert.equal(parsed.segmentComments.length, 2);
|
||
assert.equal(parsed.segmentComments[0].sliceName, "切片 001");
|
||
assert.equal(parsed.segmentComments[0].segmentIndex, 1);
|
||
assert.equal(parsed.segmentComments[0].tag, "时间边界偏差");
|
||
assert.equal(parsed.segmentComments[0].comment, "放杯子起始时间提前了约0.5s,需微调对齐");
|
||
assert.equal(parsed.segmentComments[1].sliceName, "切片 002");
|
||
assert.equal(parsed.segmentComments[1].segmentIndex, 0);
|
||
assert.equal(parsed.segmentComments[1].tag, "动作描述不准");
|
||
assert.equal(parsed.generalComment, "请重点核对上述2处问题片段后重新提交。");
|
||
});
|
||
|
||
test("discarded cuts retain the decision and stable parent across repeated unsaved splits", () => {
|
||
const original = {
|
||
id: "discarded",
|
||
start: 0,
|
||
end: 8,
|
||
name: "draft",
|
||
deliveryDisposition: "discard",
|
||
discardReason: "遮挡",
|
||
discardReasonLabelId: "reason",
|
||
discardReasonVersion: 2,
|
||
discardedByUserId: "annotator",
|
||
discardedAt: "saved",
|
||
discardedAnnotations: [{ id: "e", start: 0, end: 8, description: "old" }],
|
||
};
|
||
const first = annotationWorkbench.splitSliceAtBoundary([original], 0, 4, "left", "right");
|
||
assert.equal(first.selectedSliceId, original.id);
|
||
const second = annotationWorkbench.splitSliceAtBoundary(first.slices, 1, 6, "right-left", "right-right");
|
||
assert.equal(second.slices.length, 3);
|
||
for (const slice of second.slices) {
|
||
assert.equal(slice.name, "");
|
||
assert.equal(slice.deliveryDisposition, "discard");
|
||
assert.equal(slice.discardReason, original.discardReason);
|
||
assert.equal(slice.discardReasonVersion, 2);
|
||
assert.equal(slice.discardedByUserId, "annotator");
|
||
assert.ok(slice.sourceIds.includes(original.id));
|
||
assert.deepEqual(slice.discardedAnnotations, original.discardedAnnotations);
|
||
}
|
||
assert.equal(original.end, 8);
|
||
});
|
||
|
||
test("canceling a slice partition resets mixed judgments and records both adjacent sources", () => {
|
||
const before = [
|
||
{ id: "a", name: "拿取", start: 0, end: 2, sourceIds: ["older"], taskLabelId: "named" },
|
||
{
|
||
id: "b",
|
||
name: "legacy",
|
||
start: 2,
|
||
end: 6,
|
||
deliveryDisposition: "discard",
|
||
discardReason: "遮挡",
|
||
discardedAnnotations: [{ id: "old" }],
|
||
},
|
||
{ id: "c", name: "包装", start: 6, end: 9 },
|
||
];
|
||
const result = annotationWorkbench.cancelSlicePartition?.(before, 0);
|
||
assert.deepEqual(result, {
|
||
selectedSliceId: "b",
|
||
slices: [
|
||
{
|
||
id: "b",
|
||
name: "",
|
||
start: 0,
|
||
end: 6,
|
||
sourceIds: ["a", "b", "older"],
|
||
deliveryDisposition: "keep",
|
||
annotationStatus: "pending",
|
||
},
|
||
before[2],
|
||
],
|
||
});
|
||
assert.equal(before[1].discardReason, "遮挡");
|
||
});
|
||
|
||
test("canceling the final slice clears its left neighbor judgment and cannot remove the sole slice", () => {
|
||
const before = [
|
||
{ id: "a", name: "A", start: 0, end: 4, deliveryDisposition: "discard", discardReason: "遮挡" },
|
||
{ id: "b", name: "B", start: 4, end: 8 },
|
||
];
|
||
const result = annotationWorkbench.cancelSlicePartition(before, 1);
|
||
assert.deepEqual(result, {
|
||
selectedSliceId: "a",
|
||
slices: [
|
||
{
|
||
id: "a",
|
||
name: "",
|
||
start: 0,
|
||
end: 8,
|
||
sourceIds: ["a", "b"],
|
||
deliveryDisposition: "keep",
|
||
annotationStatus: "pending",
|
||
},
|
||
],
|
||
});
|
||
assert.equal(annotationWorkbench.cancelSlicePartition(result.slices, 0), null);
|
||
assert.equal(annotationWorkbench.mergeSlicePartitions([before[0], { ...before[1], start: 5 }], 0), null);
|
||
});
|
||
|
||
test("archivedAnnotationsForSlice: discarded slice lists its archived annotations read-only style; kept slices none", () => {
|
||
const archived = [
|
||
{ id: "a3", description: "第三段", start: 16, end: 20 },
|
||
{ id: "a2", description: "第二段", start: 12, end: 16 },
|
||
];
|
||
const discarded = { id: "s2", start: 10, end: 20, deliveryDisposition: "discard", discardedAnnotations: archived };
|
||
// 弃用片:归档动作按起点稳定排序、返回副本(不暴露原数组)。
|
||
const listed = annotationWorkbench.archivedAnnotationsForSlice(discarded);
|
||
assert.deepEqual(
|
||
listed.map((a) => a.id),
|
||
["a2", "a3"],
|
||
);
|
||
assert.notEqual(listed, archived);
|
||
// 保留片 / 无归档 / 空入参:不提供任何动作(动作列表仍走活动标注)。
|
||
const kept = { id: "s1", start: 0, end: 10 };
|
||
assert.deepEqual(annotationWorkbench.archivedAnnotationsForSlice(kept), []);
|
||
assert.deepEqual(annotationWorkbench.archivedAnnotationsForSlice({ ...kept, deliveryDisposition: "discard" }), []);
|
||
assert.deepEqual(annotationWorkbench.archivedAnnotationsForSlice(null), []);
|
||
});
|