droboticflow/tests/frontend/annotation-playhead-draft.test.mjs

75 lines
3.6 KiB
JavaScript

import test from "node:test";
import assert from "node:assert/strict";
import {
annotationDraftAtPlayhead,
annotationDraftCheckAtPlayhead,
annotationConfirmationInterval,
annotationEventOverlaps,
stepFrameBoundary,
} from "../../src/lib/annotation-workbench.js";
test("actual slice-start sub-microsecond discrepancy allows an empty draft without changing data", () => {
const slice = { start: 0.3000564311705609, end: 1.2642688846358063 };
const segments = [{ id: "existing", start: 1.10007, end: 1.233401 }];
const before = JSON.stringify({ slice, segments });
assert.deepEqual(annotationDraftAtPlayhead(slice, segments, 0.300056), {
start: slice.start,
end: slice.start,
limit: 1.10007,
});
assert.equal(JSON.stringify({ slice, segments }), before);
});
test("one microsecond start tolerance never expands to a frame or permits real overlap", () => {
const slice = { start: 0.3, end: 1.2 };
assert.equal(annotationDraftAtPlayhead(slice, [], slice.start - 0.000001).start, slice.start);
assert.equal(annotationDraftAtPlayhead(slice, [], slice.start + 0.000001).start, slice.start);
for (const point of [slice.start - 0.0000011, slice.start - 0.03])
assert.equal(annotationDraftCheckAtPlayhead(slice, [], point).reason, "outside");
assert.equal(annotationDraftCheckAtPlayhead(slice, [], slice.end).reason, "end");
assert.equal(annotationDraftCheckAtPlayhead(slice, [{ id: "a", start: 0.3, end: 0.5 }], 0.3).reason, "occupied");
assert.equal(
annotationDraftCheckAtPlayhead(slice, [{ id: "a", start: 0.3, end: 0.5000004 }], 0.5).reason,
"occupied",
);
assert.equal(annotationDraftAtPlayhead(slice, [{ id: "a", start: 0.3, end: 0.5 }], 0.5).start, 0.5);
const longSlice = { start: 2515.486218, end: 2520 };
assert.equal(annotationDraftAtPlayhead(longSlice, [], longSlice.start - 0.000001).start, longSlice.start);
assert.equal(annotationDraftCheckAtPlayhead(longSlice, [], longSlice.start - 0.0000011).reason, "outside");
});
test("new action starts at the playhead and preserves an intentional leading blank", () => {
assert.deepEqual(annotationDraftAtPlayhead({ start: 0, end: 1.038 }, [], 0.205), {
start: 0.205,
end: 0.205,
limit: 1.038,
});
});
test("new action cannot start in an existing action or outside the slice", () => {
const slice = { start: 0, end: 1 };
const segments = [{ id: "existing", start: 0.2, end: 0.5 }];
for (const point of [-1, 0.3, 1, NaN]) assert.equal(annotationDraftAtPlayhead(slice, segments, point), null);
assert.deepEqual(annotationDraftAtPlayhead(slice, segments, 0.1), { start: 0.1, end: 0.1, limit: 0.2 });
assert.deepEqual(annotationDraftAtPlayhead(slice, segments, 0.5), { start: 0.5, end: 0.5, limit: 1 });
});
test("editing an existing end excludes itself but is capped by the next action", () => {
const segments = [
{ id: "a", start: 0.1, end: 0.4 },
{ id: "b", start: 0.6, end: 1 },
];
const draft = annotationDraftAtPlayhead({ start: 0, end: 1 }, segments, 0.1, "a");
assert.equal(draft.limit, 0.6);
assert.equal(annotationEventOverlaps(segments, { ...draft, id: "a", end: 0.6 }, "a"), false);
});
test("real PTS frame steps preserve the fixed start and confirmation preserves the end", () => {
const frames = [0.2, 0.237819, 0.271155, 0.304491];
const draft = annotationDraftAtPlayhead({ start: 0.2, end: 0.304491 }, [], 0.237819);
let end = stepFrameBoundary(frames, draft.end, 1);
assert.equal(end, 0.271155);
end = stepFrameBoundary(frames, end, -1);
assert.equal(end, draft.start);
assert.deepEqual(annotationConfirmationInterval({ ...draft, end: 0.304491 }, { start: 0.2, end: 0.304491 }), {
start: 0.237819,
end: 0.304491,
});
});