160 lines
8.3 KiB
JavaScript
160 lines
8.3 KiB
JavaScript
import assert from "node:assert/strict";
|
||
import test from "node:test";
|
||
import {
|
||
coarsePendingSliceBoundary,
|
||
mergePendingSliceBoundary,
|
||
movePendingSliceBoundary,
|
||
setPendingSliceDisposition,
|
||
setPendingSliceName,
|
||
splitPendingSlice,
|
||
stepPendingSliceBoundary,
|
||
} from "../../src/lib/slice-edit-panel-ops.js";
|
||
|
||
// 帧边界 0..30(1 秒一帧);三片铺满 [0,30]。
|
||
const frames = Array.from({ length: 31 }, (_, index) => index);
|
||
const slice = (id, start, end, extra = {}) => ({ id, name: `片${id}`, start, end, ...extra });
|
||
const baseSlices = () => [slice("s1", 0, 10), slice("s2", 10, 20), slice("s3", 20, 30)];
|
||
let idSeed = 0;
|
||
const makeIds = () => [`split-${++idSeed}-a`, `split-${++idSeed}-b`];
|
||
|
||
test("moving a start boundary moves the shared boundary on both sides", () => {
|
||
const slices = baseSlices();
|
||
const result = movePendingSliceBoundary(slices, "s2", "start", 8, frames);
|
||
assert.ok(!result.issue, result.issue);
|
||
assert.equal(result.slices[0].end, 8, "左邻片的 end 联动");
|
||
assert.equal(result.slices[1].start, 8, "当前片的 start 联动");
|
||
assert.equal(result.slices[2].start, 20, "无关片不动");
|
||
assert.equal(slices[0].end, 10, "输入不可变");
|
||
});
|
||
|
||
test("moving an end boundary moves the shared boundary on both sides", () => {
|
||
const result = movePendingSliceBoundary(baseSlices(), "s2", "end", 22.4, frames);
|
||
assert.ok(!result.issue);
|
||
assert.equal(result.slices[1].end, 22, "精确帧模式下吸附到统一帧边界");
|
||
assert.equal(result.slices[2].start, 22);
|
||
});
|
||
|
||
test("capture edges are fixed and empty-side moves are refused", () => {
|
||
const first = movePendingSliceBoundary(baseSlices(), "s1", "start", 2, frames);
|
||
assert.ok(first.issue && /采集边缘|固定/.test(first.issue));
|
||
assert.deepEqual(
|
||
first.slices.map((item) => item.start),
|
||
[0, 10, 20],
|
||
"拒绝时结构原样返回",
|
||
);
|
||
const last = movePendingSliceBoundary(baseSlices(), "s3", "end", 28, frames);
|
||
assert.ok(last.issue && /采集边缘|固定/.test(last.issue));
|
||
const squeezed = movePendingSliceBoundary(baseSlices(), "s2", "start", 0, frames);
|
||
assert.ok(squeezed.issue && /至少包含一帧/.test(squeezed.issue), "挪到左邻起点会掏空左片");
|
||
assert.deepEqual(
|
||
squeezed.slices.map((item) => item.start),
|
||
[0, 10, 20],
|
||
);
|
||
const beyond = movePendingSliceBoundary(baseSlices(), "s2", "end", 30, frames);
|
||
assert.ok(beyond.issue && /至少包含一帧/.test(beyond.issue), "贴上右邻末端同样拒绝");
|
||
});
|
||
|
||
test("frame steppers step ±1/±2 frames and stop at the frame list edges", () => {
|
||
const forward = stepPendingSliceBoundary(baseSlices(), "s2", "end", 1, 1, frames);
|
||
assert.equal(forward.slices[1].end, 21);
|
||
const double = stepPendingSliceBoundary(baseSlices(), "s2", "end", 1, 2, frames);
|
||
assert.equal(double.slices[1].end, 22);
|
||
const backward = stepPendingSliceBoundary(forward.slices, "s2", "start", -1, 1, frames);
|
||
assert.equal(backward.slices[1].start, 9, "start 边界也能逐帧退");
|
||
// 步进越过帧表两端:no-op + 提示。
|
||
const edgeSlices = [slice("a", 0, 28), slice("b", 28, 30)];
|
||
const beyond = stepPendingSliceBoundary(edgeSlices, "b", "start", 1, 3, frames);
|
||
assert.ok(beyond.issue && /帧/.test(beyond.issue), "步进目标越出帧表");
|
||
assert.equal(beyond.slices, edgeSlices, "拒绝时原数组引用返回");
|
||
// 步进恰好贴上邻居远端(会掏空相邻片):同样拒绝。
|
||
const squeeze = stepPendingSliceBoundary(edgeSlices, "b", "start", 1, 2, frames);
|
||
assert.ok(squeeze.issue && /至少包含一帧/.test(squeeze.issue));
|
||
// 首片 start / 尾片 end 是采集边缘:固定不可挪。
|
||
const fixedStart = stepPendingSliceBoundary(baseSlices(), "s1", "start", -1, 1, frames);
|
||
assert.ok(fixedStart.issue && /采集边缘|固定/.test(fixedStart.issue));
|
||
const fixedEnd = stepPendingSliceBoundary(baseSlices(), "s3", "end", 1, 1, frames);
|
||
assert.ok(fixedEnd.issue && /采集边缘|固定/.test(fixedEnd.issue));
|
||
});
|
||
|
||
test("steppers fall back to 1/30s steps without a frame timeline", () => {
|
||
const result = stepPendingSliceBoundary(baseSlices(), "s2", "end", 1, 2, null);
|
||
assert.ok(Math.abs(result.slices[1].end - 20.067) < 1e-9, "无帧表时按 1/30 秒步进并保留 3 位小数");
|
||
const coarse = coarsePendingSliceBoundary(baseSlices(), "s2", "end", -1, null);
|
||
assert.ok(Math.abs(coarse.slices[1].end - 19.5) < 1e-9, "粗步进 ±0.5s(旧模式键义)");
|
||
});
|
||
|
||
test("coarse nudge snaps to the nearest frame boundary in exact mode", () => {
|
||
const result = coarsePendingSliceBoundary(baseSlices(), "s2", "end", 1, frames);
|
||
assert.equal(result.slices[1].end, 20, "+0.5s 吸附到统一帧边界(家族 nearestFrameBoundaryIndex:等距取较早帧)");
|
||
const other = coarsePendingSliceBoundary(baseSlices(), "s3", "end", -1, frames);
|
||
assert.ok(other.issue && /采集边缘|固定/.test(other.issue), "尾片 end 固定不挪(采集边缘)");
|
||
});
|
||
|
||
test("splitPendingSlice cuts at a snapped playhead and selects the leading child", () => {
|
||
const result = splitPendingSlice(baseSlices(), "s2", 15.2, makeIds, frames);
|
||
assert.ok(!result.issue, result.issue);
|
||
assert.equal(result.slices.length, 4);
|
||
const [leading, trailing] = result.slices.slice(1, 3);
|
||
assert.equal(leading.start, 10);
|
||
assert.equal(leading.end, 15, "吸附到统一帧边界");
|
||
assert.equal(trailing.start, 15);
|
||
assert.equal(trailing.end, 20);
|
||
assert.equal(result.selectedSliceId, leading.id, "拆分后焦点落在左段(家族先例)");
|
||
const edge = splitPendingSlice(baseSlices(), "s2", 10.0005, makeIds, frames);
|
||
assert.ok(edge.issue && /拆分|播放点/.test(edge.issue), "播放点贴边不允许拆分");
|
||
assert.equal(edge.slices.length, 3);
|
||
});
|
||
|
||
test("mergePendingSliceBoundary merges with the neighbor and keeps the panel target identity", () => {
|
||
const left = mergePendingSliceBoundary(baseSlices(), "s2", "start");
|
||
assert.ok(!left.issue, left.issue);
|
||
assert.equal(left.slices.length, 2);
|
||
assert.equal(left.slices[0].id, "s2", "删除首边界=并左片,幸存者是面板目标");
|
||
assert.equal(left.slices[0].start, 0);
|
||
assert.equal(left.slices[0].end, 20);
|
||
assert.equal(left.slices[0].name, "", "合并结果回到未定义状态(词汇表:取消切分)");
|
||
const right = mergePendingSliceBoundary(baseSlices(), "s2", "end");
|
||
assert.equal(right.slices.length, 2);
|
||
assert.equal(right.slices[1].id, "s2", "删除尾边界=并右片,幸存者同样是面板目标");
|
||
assert.equal(right.slices[1].start, 10);
|
||
assert.equal(right.slices[1].end, 30);
|
||
const firstStart = mergePendingSliceBoundary(baseSlices(), "s1", "start");
|
||
assert.ok(firstStart.issue && /左侧/.test(firstStart.issue), "首片没有左邻");
|
||
const lastEnd = mergePendingSliceBoundary(baseSlices(), "s3", "end");
|
||
assert.ok(lastEnd.issue && /右侧/.test(lastEnd.issue), "尾片没有右邻");
|
||
});
|
||
|
||
test("name and disposition edits only touch the target slice", () => {
|
||
const named = setPendingSliceName(baseSlices(), "s2", "新名字");
|
||
assert.equal(named.slices[1].name, "新名字");
|
||
assert.equal(named.slices[0].name, "片s1");
|
||
const discarded = setPendingSliceDisposition(baseSlices(), "s3", "discard", {
|
||
reason: "画面模糊",
|
||
reasonLabelId: "label-1",
|
||
reasonVersion: 3,
|
||
reasonNote: "备注",
|
||
});
|
||
assert.equal(discarded.slices[2].deliveryDisposition, "discard");
|
||
assert.equal(discarded.slices[2].discardReason, "画面模糊");
|
||
assert.equal(discarded.slices[2].discardReasonLabelId, "label-1");
|
||
assert.equal(discarded.slices[2].discardReasonVersion, 3);
|
||
assert.equal(discarded.slices[2].discardReasonNote, "备注");
|
||
const kept = setPendingSliceDisposition(discarded.slices, "s3", "keep", {});
|
||
assert.equal(kept.slices[2].deliveryDisposition, "keep");
|
||
assert.equal(kept.slices[2].discardReason, undefined, "恢复保留时舍弃原因清空");
|
||
assert.equal(kept.slices[0].name, "片s1", "其它片不动");
|
||
});
|
||
|
||
test("unknown slice ids are refused without mutating anything", () => {
|
||
const slices = baseSlices();
|
||
for (const result of [
|
||
movePendingSliceBoundary(slices, "ghost", "end", 15, frames),
|
||
stepPendingSliceBoundary(slices, "ghost", "end", 1, 1, frames),
|
||
splitPendingSlice(slices, "ghost", 15, makeIds, frames),
|
||
mergePendingSliceBoundary(slices, "ghost", "end"),
|
||
]) {
|
||
assert.ok(result.issue, "未知切片必须给 issue 而不是静默成功");
|
||
assert.equal(result.slices, slices, "拒绝时原数组引用返回");
|
||
}
|
||
});
|