forked from longtao.wu/droboticflow
112 lines
4.7 KiB
JavaScript
112 lines
4.7 KiB
JavaScript
import test from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import fs from "node:fs";
|
|
import {
|
|
alignSliceTimeline,
|
|
initializeFrameAlignedSlices,
|
|
sliceTimelineIssue,
|
|
} from "../../src/lib/annotation-timeline.js";
|
|
import { analyzeSliceCoverage } from "../../src/lib/annotation-operations.js";
|
|
|
|
const start = 0.06665679014814815,
|
|
end = 899.833338604926;
|
|
const frames = [start, 0.09998518522222222, 329.881, 329.914, end];
|
|
const timeline = { boundaryContract: "hardware-frame-v1", rangeStartSecond: start, rangeEndSecond: end };
|
|
const initial = [{ id: "initial", name: "切片 001", start: 0, end: 899.866667 }];
|
|
|
|
test("system default is aligned to the authoritative frame range before enabling editing", () => {
|
|
const next = initializeFrameAlignedSlices(initial, initial, timeline, true);
|
|
assert.deepEqual(next, [{ ...initial[0], start, end }]);
|
|
assert.equal(initial[0].start, 0);
|
|
assert.equal(analyzeSliceCoverage(next, start, end).complete, true);
|
|
});
|
|
|
|
test("late initialization never overwrites a human draft, revision, changed default, or multiple slices", () => {
|
|
const human = [
|
|
{ id: "a", name: "保留名称", start: 0, end: 329.894 },
|
|
{ id: "b", name: "第二片", start: 329.894, end: 899.866667 },
|
|
];
|
|
assert.equal(initializeFrameAlignedSlices(human, initial, timeline, true), human);
|
|
assert.equal(initializeFrameAlignedSlices(initial, initial, timeline, false), initial);
|
|
const changed = [{ ...initial[0], start: 1 }];
|
|
assert.equal(initializeFrameAlignedSlices(changed, initial, timeline, true), changed);
|
|
assert.equal(initializeFrameAlignedSlices(initial, initial, {}, true), initial);
|
|
});
|
|
|
|
test("reported 100 percent coverage distinguishes endpoint overflow from missing content", () => {
|
|
const slices = [
|
|
{ start: 0, end: 329.894 },
|
|
{ start: 329.894, end: 899.866667 },
|
|
];
|
|
const coverage = analyzeSliceCoverage(slices, start, end);
|
|
assert.equal(coverage.coverageRatio, 1);
|
|
assert.deepEqual(coverage.gaps, []);
|
|
assert.deepEqual(coverage.overlaps, []);
|
|
assert.equal(coverage.complete, false);
|
|
assert.equal(sliceTimelineIssue(slices, start, end, frames), "切片边界超出统一帧范围");
|
|
});
|
|
|
|
test("explicit alignment preserves IDs, names, count and shared edges without mutating input", () => {
|
|
const slices = [
|
|
{ id: "a", name: "发大发", start: 0, end: 329.894 },
|
|
{ id: "b", name: "1分钟多少分", start: 329.894, end: 899.866667 },
|
|
];
|
|
const before = structuredClone(slices);
|
|
const result = alignSliceTimeline(slices, frames);
|
|
assert.equal(result.error, "");
|
|
assert.equal(result.changed, true);
|
|
assert.deepEqual(result.segments, [
|
|
{ ...slices[0], start, end: 329.881 },
|
|
{ ...slices[1], start: 329.881, end },
|
|
]);
|
|
assert.equal(sliceTimelineIssue(result.segments, start, end, frames), "");
|
|
assert.deepEqual(slices, before);
|
|
assert.equal(alignSliceTimeline(result.segments, frames).changed, false);
|
|
});
|
|
|
|
test("alignment does not remove slices, hide real gaps/overlaps, or modify existing descriptions", () => {
|
|
for (const slices of [
|
|
[
|
|
{ start: 0, end: 0.01 },
|
|
{ start: 0.01, end: 899.866667 },
|
|
],
|
|
[
|
|
{ start: 0, end: 30 },
|
|
{ start: 31, end: 899.866667 },
|
|
],
|
|
[
|
|
{ start: 0, end: 32 },
|
|
{ start: 31, end: 899.866667 },
|
|
],
|
|
[{ start: NaN, end: 32 }],
|
|
]) {
|
|
const result = alignSliceTimeline(slices, frames);
|
|
assert.ok(result.error);
|
|
assert.equal(result.segments, slices);
|
|
}
|
|
assert.match(alignSliceTimeline(initial, frames, [{ description: "已有描述" }]).error, /已有动作描述/);
|
|
assert.ok(alignSliceTimeline(initial, [NaN, 1]).error);
|
|
});
|
|
|
|
test("loading is task-owned, all mutation entry points are gated, and confirmation never silently realigns", () => {
|
|
const page = fs.readFileSync(new URL("../../src/pages/AnnotationOperations.tsx", import.meta.url), "utf8");
|
|
const workbench = fs.readFileSync(
|
|
new URL("../../src/components/CaptureAnnotationWorkbench.tsx", import.meta.url),
|
|
"utf8",
|
|
);
|
|
assert.match(page, /requestId !== timelineRequestRef.current/);
|
|
assert.match(page, /selectedTaskDetailIDRef.current !== task.id/);
|
|
assert.match(page, /detail\?\.task.id === selectedTaskId/);
|
|
assert.match(page, /frameTimelineStatus === "ready" && frameBoundaries.length >= 2/);
|
|
assert.match(page, /if \(!exactFrameEditing\)/);
|
|
assert.match(workbench, /providedReadOnly \|\| !timelineReady/);
|
|
assert.match(workbench, /!selectedSlice \|\| !timelineReady/);
|
|
assert.match(workbench, /等待统一帧范围/);
|
|
const confirmation = page.slice(
|
|
page.indexOf("const confirmSlicesAndMaterialize ="),
|
|
page.indexOf("const resetToSliceEditing ="),
|
|
);
|
|
assert.match(confirmation, /!exactFrameEditing \|\| sliceAlignmentIssue/);
|
|
assert.doesNotMatch(confirmation, /nearestFrameBoundaryIndex|const snapped/);
|
|
});
|