forked from longtao.wu/droboticflow
269 lines
12 KiB
JavaScript
269 lines
12 KiB
JavaScript
import test from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import {
|
|
acceptanceActions,
|
|
acceptanceActionStatus,
|
|
acceptanceTimestampOpinions,
|
|
acceptanceSliceName,
|
|
acceptanceChecks,
|
|
acceptanceFrameTimes,
|
|
acceptanceFullVideoSeekTarget,
|
|
acceptanceFullVideoWindow,
|
|
acceptanceRequiredFiles,
|
|
acceptanceSliceGroups,
|
|
} from "../../src/lib/acceptance-workspace.js";
|
|
const entry = {
|
|
taskId: "task",
|
|
materialization: {
|
|
artifactId: "artifact",
|
|
sliceCount: 2,
|
|
coverage: { contiguous: true, gapMicroseconds: 0, overlapMicroseconds: 0 },
|
|
},
|
|
finalSlices: [
|
|
{ name: "A", start: 0.2, end: 1 },
|
|
{ name: "B", start: 1, end: 2 },
|
|
],
|
|
};
|
|
const members = [2, 1].flatMap((index) =>
|
|
acceptanceRequiredFiles.map((name) => ({
|
|
id: `${index}-${name}`,
|
|
group: `slices/slice-${String(index).padStart(6, "0")}`,
|
|
name,
|
|
sizeBytes: 10,
|
|
contentAvailable: true,
|
|
})),
|
|
);
|
|
test("acceptance uses exact sorted materialized groups and preserves slice names", () => {
|
|
const groups = acceptanceSliceGroups([entry], { artifact: { items: members } });
|
|
assert.deepEqual(
|
|
groups.map((g) => g.name),
|
|
["A", "B"],
|
|
);
|
|
assert.equal(groups[0].start, 0.2);
|
|
assert.deepEqual(acceptanceChecks([entry], groups, false, ""), { filesReady: true, coverageReady: true });
|
|
});
|
|
test("missing duplicate or unavailable files cannot produce a green file check", () => {
|
|
for (const items of [
|
|
members.slice(1),
|
|
[...members, members[0]],
|
|
members.map((m, i) => (i ? m : { ...m, contentAvailable: false })),
|
|
]) {
|
|
const groups = acceptanceSliceGroups([entry], { artifact: { items } });
|
|
assert.equal(acceptanceChecks([entry], groups, false, "").filesReady, false);
|
|
}
|
|
assert.equal(acceptanceChecks([entry], [], true, "").filesReady, false);
|
|
assert.equal(acceptanceChecks([{ materialization: {} }], [], false, "").coverageReady, false);
|
|
});
|
|
|
|
test("source-index acceptance requires its source proof and JSON, without invented sliced CSVs", () => {
|
|
const names = ["left.mp4", "right.mp4", "annotation.json", "synchronized-slice.json", "source-index.json"];
|
|
const items = names.map((name) => ({
|
|
name,
|
|
id: name,
|
|
group: "slices/slice-000001",
|
|
sizeBytes: 100,
|
|
contentAvailable: true,
|
|
}));
|
|
const groups = acceptanceSliceGroups([entry], { artifact: { items } });
|
|
assert.equal(groups[0].sourceIndex, true);
|
|
assert.deepEqual(groups[0].missing, []);
|
|
for (const name of names) {
|
|
const incomplete = acceptanceSliceGroups([entry], { artifact: { items: items.filter((m) => m.name !== name) } });
|
|
assert.ok(incomplete[0].missing.length > 0, name);
|
|
}
|
|
});
|
|
test("description times stay in the exported slice-local coordinate system", () => {
|
|
const result = acceptanceActions({
|
|
descriptions: [
|
|
{ id: "b", clipStartMs: 500, clipEndMs: 1000, description: "B" },
|
|
{ id: "a", clipStartMs: 5.543, clipEndMs: 500, description: "A" },
|
|
],
|
|
});
|
|
assert.equal(result[0].start, 0.005543);
|
|
assert.equal(result[1].end, 1);
|
|
assert.deepEqual(acceptanceActions({ descriptions: [] }), []);
|
|
assert.throws(() => acceptanceActions({ actions: [{ clipStartMs: 1000, clipEndMs: 500 }] }));
|
|
});
|
|
test("PTS preview reads actual clip times and rejects invalid sequences", () => {
|
|
const times = acceptanceFrameTimes(
|
|
"frame_index,timestamp_ms,clip_time_ms\n0,1000,0\n1,1037.819,37.819\n2,1104.491,104.491",
|
|
);
|
|
assert.equal(times.length, 3);
|
|
times.forEach((value, index) => assert.ok(Math.abs(value - [0, 0.037819, 0.104491][index]) < 1e-12));
|
|
assert.throws(() => acceptanceFrameTimes("frame_index,timestamp_ms\n0,1000"));
|
|
assert.throws(() => acceptanceFrameTimes("clip_time_ms\n0\n0"));
|
|
});
|
|
test("full-video window maps the active slice onto the whole video (defect 1)", () => {
|
|
// One entry, contiguous slices: cumulative spans reproduce capture offsets.
|
|
const groups = acceptanceSliceGroups([entry], { artifact: { items: members } });
|
|
assert.deepEqual(acceptanceFullVideoWindow(groups, "0:1"), { offset: 0, total: 1.8 });
|
|
assert.deepEqual(acceptanceFullVideoWindow(groups, "0:2"), { offset: 0.8, total: 1.8 });
|
|
// Multiple entries (each with its own capture clock) still produce one
|
|
// monotonic window across the concatenated viewing order.
|
|
const secondEntry = {
|
|
taskId: "task2",
|
|
materialization: { artifactId: "artifact2", sliceCount: 1, coverage: entry.materialization.coverage },
|
|
finalSlices: [{ name: "C", start: 5, end: 8 }],
|
|
};
|
|
const secondMembers = [1].flatMap((index) =>
|
|
acceptanceRequiredFiles.map((name) => ({
|
|
id: `e2-${index}-${name}`,
|
|
group: `slices/slice-${String(index).padStart(6, "0")}`,
|
|
name,
|
|
sizeBytes: 10,
|
|
contentAvailable: true,
|
|
})),
|
|
);
|
|
const combined = acceptanceSliceGroups([entry, secondEntry], {
|
|
artifact: { items: members },
|
|
artifact2: { items: secondMembers },
|
|
});
|
|
assert.deepEqual(acceptanceFullVideoWindow(combined, "1:1"), { offset: 1.8, total: 4.8 });
|
|
// Defensive against legacy records with missing/invalid slice ranges.
|
|
assert.equal(acceptanceFullVideoWindow([], "0:1"), null);
|
|
assert.equal(acceptanceFullVideoWindow([{ id: "0:1", start: NaN, end: NaN }], "0:1"), null);
|
|
assert.equal(acceptanceFullVideoWindow(groups, "missing"), null);
|
|
// A zero-span group inside otherwise valid data contributes no width but is
|
|
// still addressable (offset of the following slice is unaffected).
|
|
const withZero = [
|
|
{ id: "0:1", start: 2, end: 2 },
|
|
{ id: "0:2", start: 2, end: 3 },
|
|
];
|
|
assert.deepEqual(acceptanceFullVideoWindow(withZero, "0:2"), { offset: 0, total: 1 });
|
|
});
|
|
test("full-video seek maps a concatenated timestamp onto the owning slice (defect 1)", () => {
|
|
// Two entries: entry 1 slices [0.2,1)+[1,2) (own clock), entry 2 [5,8) (clock
|
|
// restarts). Concatenated: 0:1 → [0,0.8), 0:2 → [0.8,1.8), 1:1 → [1.8,4.8).
|
|
const single = acceptanceSliceGroups([entry], { artifact: { items: members } });
|
|
assert.deepEqual(acceptanceFullVideoSeekTarget(single, 0), { groupId: "0:1", inSlice: 0 });
|
|
assert.ok(
|
|
Math.abs(acceptanceFullVideoSeekTarget(single, 0.9).inSlice - 0.1) < 1e-9,
|
|
"cross-slice seek keeps the in-slice offset (float-safe)",
|
|
);
|
|
assert.equal(acceptanceFullVideoSeekTarget(single, 0.9).groupId, "0:2");
|
|
assert.equal(acceptanceFullVideoSeekTarget(single, 1.799).groupId, "0:2");
|
|
assert.ok(Math.abs(acceptanceFullVideoSeekTarget(single, 1.799).inSlice - 0.999) < 1e-9);
|
|
const combined = acceptanceSliceGroups([entry, secondEntryFixture()], combinedResponses());
|
|
// Cross-entry seek lands in entry 2 with its LOCAL (clock-reset) time.
|
|
const cross = acceptanceFullVideoSeekTarget(combined, 2.0);
|
|
assert.equal(cross.groupId, "1:1");
|
|
assert.ok(Math.abs(cross.inSlice - 0.2) < 1e-9);
|
|
const late = acceptanceFullVideoSeekTarget(combined, 4.7);
|
|
assert.equal(late.groupId, "1:1");
|
|
assert.ok(Math.abs(late.inSlice - 2.9) < 1e-9);
|
|
// Clamping: before the first slice → its start; beyond the end → last end.
|
|
assert.deepEqual(acceptanceFullVideoSeekTarget(combined, -3), { groupId: "0:1", inSlice: 0 });
|
|
assert.deepEqual(acceptanceFullVideoSeekTarget(combined, 99), { groupId: "1:1", inSlice: 3 });
|
|
// Degenerate shapes (legacy records with invalid ranges) map to null.
|
|
assert.equal(acceptanceFullVideoSeekTarget([], 1), null);
|
|
assert.equal(acceptanceFullVideoSeekTarget([{ id: "0:1", start: NaN, end: NaN }], 1), null);
|
|
// A zero-span slice contributes no seekable width: the timestamp falls
|
|
// through to the next slice's own clock (no crash, no NaN, no selection of
|
|
// the zero-width group).
|
|
const zeroSkip = acceptanceFullVideoSeekTarget(withZeroSlices(), 0.5);
|
|
assert.equal(zeroSkip.groupId, "0:2");
|
|
assert.ok(Math.abs(zeroSkip.inSlice - 0.5) < 1e-9);
|
|
});
|
|
function secondEntryFixture() {
|
|
return {
|
|
taskId: "task2",
|
|
materialization: { artifactId: "artifact2", sliceCount: 1, coverage: entry.materialization.coverage },
|
|
finalSlices: [{ name: "C", start: 5, end: 8 }],
|
|
};
|
|
}
|
|
function combinedResponses() {
|
|
const secondMembers = [1].flatMap((index) =>
|
|
acceptanceRequiredFiles.map((name) => ({
|
|
id: `e2-${index}-${name}`,
|
|
group: `slices/slice-${String(index).padStart(6, "0")}`,
|
|
name,
|
|
sizeBytes: 10,
|
|
contentAvailable: true,
|
|
})),
|
|
);
|
|
return { artifact: { items: members }, artifact2: { items: secondMembers } };
|
|
}
|
|
function withZeroSlices() {
|
|
return [
|
|
{ id: "0:1", start: 2, end: 2 },
|
|
{ id: "0:2", start: 2, end: 3 },
|
|
];
|
|
}
|
|
|
|
test("discarded acceptance slices use the frozen reason, preserving legacy fallback", () => {
|
|
assert.equal(
|
|
acceptanceSliceName({ deliveryDisposition: "discard", discardReason: "玩手机", name: "旧名字" }, 2),
|
|
"已舍弃:玩手机",
|
|
);
|
|
assert.equal(acceptanceSliceName({ deliveryDisposition: "discard", discardReasonName: "其他" }, 2), "已舍弃:其他");
|
|
assert.equal(acceptanceSliceName({ deliveryDisposition: "discard", name: "历史片名" }, 2), "历史片名");
|
|
assert.equal(acceptanceSliceName({ deliveryDisposition: "discard" }, 2), "切片 002");
|
|
assert.equal(
|
|
acceptanceSliceName({ deliveryDisposition: "keep", name: "原片名", discardReason: "旧原因" }, 2),
|
|
"原片名",
|
|
);
|
|
});
|
|
|
|
test("acceptance markers retain source identity and map only the active slice clock", () => {
|
|
const entryA = { sourceHumanItemId: "source-A", finalSlices: [{ id: "same-id", start: 8, end: 16 }] };
|
|
const entryB = { sourceHumanItemId: "source-B", finalSlices: [{ id: "same-id", start: 20, end: 28 }] };
|
|
const groups = [
|
|
{ id: "A", entry: entryA, index: 1, start: 8, end: 16 },
|
|
{ id: "B", entry: entryB, index: 1, start: 20, end: 28 },
|
|
];
|
|
const opinions = [
|
|
{
|
|
id: "local",
|
|
sourceItemId: "source-A",
|
|
sliceId: "same-id",
|
|
timeBasis: "slice-seconds",
|
|
atSeconds: 2,
|
|
text: "local",
|
|
},
|
|
{
|
|
id: "capture",
|
|
sourceItemId: "source-A",
|
|
sliceId: "same-id",
|
|
timeBasis: "capture-seconds",
|
|
atSeconds: 11,
|
|
text: "capture",
|
|
},
|
|
{ id: "other", sourceItemId: "source-B", sliceId: "same-id", timeBasis: "slice-seconds", atSeconds: 2 },
|
|
{ id: "ambiguous", sliceId: "same-id", timeBasis: "slice-seconds", atSeconds: 2 },
|
|
{ id: "bad-clock", sourceItemId: "source-A", sliceId: "same-id", atSeconds: 2 },
|
|
{ id: "outside", sourceItemId: "source-A", sliceId: "same-id", timeBasis: "slice-seconds", atSeconds: 9 },
|
|
];
|
|
assert.deepEqual(
|
|
acceptanceTimestampOpinions(groups, "A", opinions).map((p) => [p.id, p.atSeconds]),
|
|
[
|
|
["local", 2],
|
|
["capture", 3],
|
|
],
|
|
);
|
|
assert.deepEqual(
|
|
acceptanceTimestampOpinions(groups, "B", opinions).map((p) => p.id),
|
|
["other"],
|
|
);
|
|
assert.deepEqual(acceptanceTimestampOpinions(groups, "missing", opinions), []);
|
|
assert.equal(acceptanceTimestampOpinions([groups[0]], "A", [opinions[3]])[0].id, "ambiguous");
|
|
});
|
|
|
|
test("short non-invalid actions remain distinct without judging blank actions as valid", () => {
|
|
const actions = acceptanceActions({
|
|
descriptions: [
|
|
{ id: "invalid-before", clipStartMs: 0, clipEndMs: 2000, isInvalid: true },
|
|
{ id: "brief", clipStartMs: 2000, clipEndMs: 2500, description: "短暂有效动作" },
|
|
{ id: "invalid-after", clipStartMs: 2500, clipEndMs: 5000, status: "invalid" },
|
|
{ id: "blank", clipStartMs: 5000, clipEndMs: 5500 },
|
|
{ id: "exact-three", clipStartMs: 5500, clipEndMs: 8500, description: "三秒动作" },
|
|
],
|
|
});
|
|
assert.deepEqual(actions.map(acceptanceActionStatus), [
|
|
{ duration: 2, label: "无效", short: false },
|
|
{ duration: 0.5, label: "已描述", short: true },
|
|
{ duration: 2.5, label: "无效", short: false },
|
|
{ duration: 0.5, label: "待标注", short: true },
|
|
{ duration: 3, label: "已描述", short: false },
|
|
]);
|
|
});
|