// tightWindow.mjs: a clip tightened to its sentence (sentenceEdges.mjs) and // each edge into its pause (report-to-video/pauses.mjs) -- which sentence (the // playhead's, the cut's, the whole ones inside the window, the one it is // inside), the cache as a bound, and an edge to the nearest pause. // // Run with: node --test lib/report/tightWindow.test.mjs import assert from "node:assert/strict"; import test from "node:test"; import { HOW_SAID, nearestPause, pausesNear, sentenceAround, sentencesInside, snapSaid, tightWindow } from "./tightWindow.mjs"; // The e2e fixture's vid1 cues (e2e/fixtures/make-fixture.mjs): sentences 0-3, 3-9, 9-12, 12-15, 15-21. const VID1 = [ [0, 3, "This is a complete sentence."], [3, 6, "And this one runs on and because"], [6, 9, "of that it finishes here."], [9, 12, "Another whole sentence entirely."], [12, 15, "A fourth one, done."], [15, 18, "trailing off and then"], [18, 21, "it lands at last."], ].map(([start, end, text]) => ({ start, end, text })); // Timed words over it (the tight fixture's words.json), with the silences of the fixture's tone window. const WORDS = [ ["This", 0.2, 0.5], ["is", 0.55, 0.7], ["a", 0.75, 0.8], ["complete", 0.9, 1.6], ["sentence.", 1.7, 2.8], ["And", 3.2, 3.5], ["this", 3.55, 3.8], ["one", 3.85, 4.1], ["runs", 4.15, 4.5], ["on", 4.55, 4.7], ["and", 4.75, 5], ["because", 5.1, 5.85], ["of", 6.2, 6.4], ["that", 6.45, 6.8], ["it", 6.85, 7], ["finishes", 7.1, 7.9], ["here.", 8, 8.8], ["Another", 9.2, 9.8], ["whole", 9.9, 10.3], ["sentence", 10.4, 11], ["entirely.", 11.1, 11.8], ].map(([text, start, end]) => ({ text, start, end })); const SILENCES = [{ from: 2.9, to: 3.11 }, { from: 5.92, to: 6.11 }, { from: 8.85, to: 9.15 }]; test("sentenceAround: the last start at or before, the first end at or after", () => { assert.deepEqual(sentenceAround(VID1, 4.5, 4.5), { start: 3, end: 9 }); assert.deepEqual(sentenceAround(VID1, 1, 10), { start: 0, end: 12 }); // On a boundary: the sentence that starts there. assert.deepEqual(sentenceAround(VID1, 3, 3), { start: 3, end: 9 }); assert.equal(sentenceAround([{ start: 0, end: 3, text: "no end" }], 1, 1), null); }); test("sentencesInside: partial sentences at either edge are dropped", () => { assert.deepEqual(sentencesInside(VID1, 1.2, 12.4), { start: 3, end: 12 }); assert.equal(sentencesInside(VID1, 3.5, 8), null, "inside one sentence: nothing whole"); }); test("at the playhead: that sentence, each edge into its pause", () => { const r = tightWindow({ clip: { start: 1.2, end: 10.5 }, cues: VID1, words: WORDS, silences: SILENCES, at: 4.4 }); assert.equal(r.ok, true); if (!r.ok) return; assert.equal(r.how, "at"); assert.deepEqual(r.sentence, { start: 3, end: 9 }); // The start goes back into the 2.9-3.2 pause (the hard cut's 0.1 s of quiet before "And"); // the end is already in the pause after "here." (8.8-9.2), and stays. assert.ok(r.start >= 2.9 && r.start <= 3.2, `start ${r.start}`); assert.ok(r.end >= 8.8 && r.end <= 9.2, `end ${r.end}`); assert.equal(r.changed, true); }); test("the cut anchors it when the playhead is elsewhere: every sentence the quote touches", () => { const r = tightWindow({ clip: { start: 0.5, end: 11.9, cutStart: 4, cutEnd: 7.5 }, cues: VID1, at: null, snap: false }); assert.equal(r.ok && r.how, "cut"); assert.equal(r.ok && r.start, 3); assert.equal(r.ok && r.end, 9); }); test("no playhead, no cut: the whole sentences inside the window, else the one it is inside", () => { const a = tightWindow({ clip: { start: 1.2, end: 12.4 }, cues: VID1, snap: false }); assert.equal(a.ok && a.how, "window"); assert.deepEqual(a.ok && [a.start, a.end], [3, 12]); const b = tightWindow({ clip: { start: 3.5, end: 8 }, cues: VID1, snap: false }); assert.equal(b.ok && b.how, "middle"); assert.deepEqual(b.ok && [b.start, b.end], [3, 9]); assert.match(HOW_SAID.middle, /inside/); }); test("the cached window bounds it, and says so", () => { const r = tightWindow({ clip: { start: 1.2, end: 7.4 }, cues: VID1, at: 4, lo: 0, hi: 7.5, snap: false }); assert.equal(r.ok, true); if (!r.ok) return; assert.equal(r.end, 7.5); assert.match(r.said.join(" "), /past the cached window/); }); test("already tight: unchanged", () => { const r = tightWindow({ clip: { start: 3, end: 9 }, cues: VID1, snap: false }); assert.equal(r.ok && r.changed, false); }); test("refusals: no cues, no boundary, a window with no edges", () => { assert.equal(tightWindow({ clip: { start: 1, end: 2 }, cues: [] }).ok, false); assert.equal(tightWindow({ clip: { start: 1, end: 2 }, cues: [{ start: 0, end: 3, text: "no end" }] }).ok, false); assert.equal(tightWindow({ clip: /** @type {any} */ ({}), cues: VID1 }).ok, false); }); test("nearestPause: the middle of the closest pause, within a second; bounded; said", () => { const n = nearestPause(5.7, { words: WORDS, silences: SILENCES }); assert.equal(n.moved, true); assert.ok(n.at > 5.85 && n.at < 6.2, `at ${n.at}`); assert.match(String(snapSaid("end", n)), /^end 5\.70 → 6\.\d\d, into the pause/); const far = nearestPause(20, { words: WORDS, silences: SILENCES }); assert.equal(far.moved, false); assert.match(String(snapSaid("start", far)), /stays/); assert.ok(pausesNear({ words: WORDS, silences: SILENCES }, 4, 5).length >= 1); }); test("a playhead parked on an edge is not the place: the window decides", () => { const r = tightWindow({ clip: { start: 1.2, end: 7.4 }, cues: VID1, at: 1.2, snap: false }); assert.equal(r.ok && r.how, "middle"); assert.deepEqual(r.ok && [r.start, r.end], [3, 9]); });