droboticflow/tests/frontend/optional-annotations.test.mjs

63 lines
2.6 KiB
JavaScript

import test from "node:test";
import assert from "node:assert/strict";
import fs from "node:fs";
import {
validateAnnotationWorkspace,
annotationDraftAtPlayhead,
annotationCoverageGaps,
} from "../../src/lib/annotation-workbench.js";
import { analyzeSliceCoverage } from "../../src/lib/annotation-operations.js";
const slices = [
{ id: "a", start: 0, end: 10 },
{ id: "b", start: 10, end: 20 },
];
test("blank and partial semantic annotations are valid without changing media coverage", () => {
for (const events of [
[],
[{ id: "a", start: 2, end: 3, description: "拿起物品" }],
[{ id: "i", start: 12, end: 13, description: "无效片段", isInvalid: true }],
]) {
const before = JSON.stringify({ slices, events });
assert.deepEqual(validateAnnotationWorkspace(slices, events), []);
assert.equal(analyzeSliceCoverage(slices, 0, 20).complete, true);
assert.equal(JSON.stringify({ slices, events }), before);
assert.ok(annotationCoverageGaps(slices[0], events).length);
}
});
test("blank permission never permits overlap, invalid ranges or annotations in discarded slices", () => {
for (const events of [
[
{ id: "a", start: 2, end: 4 },
{ id: "b", start: 3, end: 5 },
],
[{ id: "a", start: 9, end: 11 }],
[{ id: "a", start: 3, end: 3 }],
[{ id: "a", start: NaN, end: 3 }],
])
assert.ok(validateAnnotationWorkspace(slices, events).length);
assert.ok(
validateAnnotationWorkspace([{ ...slices[0], deliveryDisposition: "discard" }], [{ id: "a", start: 2, end: 3 }])
.length,
);
});
test("new annotation leaves earlier blanks untouched", () => {
const events = [
{ id: "a", start: 1, end: 2 },
{ id: "b", start: 8, end: 9 },
];
assert.deepEqual(annotationDraftAtPlayhead(slices[0], events, 5), { start: 5, end: 5, limit: 8 });
});
test("submit uses validity checks, not semantic coverage, and UI explains blank delivery", () => {
const page = fs.readFileSync(new URL("../../src/pages/AnnotationOperations.tsx", import.meta.url), "utf8");
const ui = fs.readFileSync(new URL("../../src/components/CaptureAnnotationWorkbench.tsx", import.meta.url), "utf8");
assert.doesNotMatch(page, /continuousAnnotationIssues/);
assert.match(page, /validateAnnotationWorkspace\(sliceSegments, segments\)/);
assert.match(page, /!sliceCoverage.complete/);
assert.match(ui, /允许部分或全部留空/);
assert.match(ui, /留空不代表无效或舍弃/);
assert.match(ui, /删除此动作(保留空白)/);
assert.doesNotMatch(ui, /并入相邻动作/);
assert.match(ui, /Math.abs\(segment.end - annotationSegments\[index \+ 1\].start\) <= 0.000001/);
});