Files
xiaobailing-turn-detection/labels/eot-studio/tests/edit-history.test.ts
T

45 lines
1.9 KiB
TypeScript
Raw Normal View History

import assert from "node:assert/strict";
import test from "node:test";
// @ts-expect-error Node's native TypeScript runner requires the explicit extension.
import { createEditHistory, commitEdit, undoEdit, redoEdit } from "../lib/edit-history.ts";
test("ASR and EOT snapshots restore together through multiple undo and redo steps", () => {
const initial = { transcripts: ["原转写"], points: [1000] };
const asr = { ...initial, transcripts: ["修正转写"] };
const eot = { ...asr, points: [1200, 1800] };
let history = commitEdit(commitEdit(createEditHistory(initial), asr), eot);
history = undoEdit(history);
assert.deepEqual(history.present, asr);
history = undoEdit(history);
assert.deepEqual(history.present, initial);
history = redoEdit(history);
assert.deepEqual(history.present, asr);
history = redoEdit(history);
assert.deepEqual(history.present, eot);
assert.equal(history.future.length, 0);
assert.deepEqual(initial, { transcripts: ["原转写"], points: [1000] });
});
test("a new edit after undo abandons the old redo branch", () => {
let history = commitEdit(commitEdit(createEditHistory(0), 1), 2);
history = commitEdit(undoEdit(history), 3);
assert.equal(history.future.length, 0);
assert.equal(redoEdit(history).present, 3);
assert.equal(undoEdit(history).present, 1);
});
test("empty and unchanged operations preserve history, and forty steps can be restored", () => {
const initial = createEditHistory(0);
assert.equal(undoEdit(initial), initial);
assert.equal(redoEdit(initial), initial);
assert.equal(commitEdit(initial, 0), initial);
let history = initial;
for (let i = 1; i <= 60; i++) history = commitEdit(history, i);
assert.equal(history.past.length, 40);
for (let i = 0; i < 40; i++) history = undoEdit(history);
assert.equal(history.present, 20);
assert.equal(undoEdit(history), history);
for (let i = 0; i < 40; i++) history = redoEdit(history);
assert.equal(history.present, 60);
});