// The sentence boundaries the phone walk's edge strip steps and snaps to // (sentenceEdges.mjs): the pre-cut's rule -- . ? ! (closing quotes allowed), // or a pause of SENTENCE_GAP -- the starts and ends kept apart across a pause, // stepping strictly past an edge, snapping within the radius, and the words // shown under the strip. // // Run with: node --test lib/report/sentenceEdges.test.mjs import assert from "node:assert/strict"; import test from "node:test"; import { SENTENCE_GAP, SNAP_RADIUS, endsSentence, endsWithTerminal, sentenceBoundaries, snapToBoundary, startsSentence, stepBoundary, wordsAtEdge, } from "./sentenceEdges.mjs"; // The e2e fixture's vid1 (e2e/fixtures/make-fixture.mjs), which the phone // walk spec steps through: 0-3, 9, 12, 15 close sentences; 3-6 and 15-18 run on. 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 })); test("terminal punctuation, with closing quotes and brackets after it", () => { for (const t of ["done.", "really?", "stop!", 'he said "no."', "(as he put it.)", "it’s over.’", "end. "]) assert.equal(endsWithTerminal(t), true, t); for (const t of ["and because", "e.g. the", "3.5 seconds", "", "comma,"]) assert.equal(endsWithTerminal(t), false, t); }); test("a pause of SENTENCE_GAP or more is a boundary with no punctuation at all", () => { const a = { start: 0, end: 2, text: "no full stop here" }; const near = { start: 2 + SENTENCE_GAP - 0.01, end: 5, text: "x" }; const far = { start: 2 + SENTENCE_GAP, end: 5, text: "x" }; assert.equal(endsSentence(a, near), false); assert.equal(endsSentence(a, far), true); assert.equal(startsSentence(far, a), true); assert.equal(startsSentence(near, a), false); // The first cue starts one; the last ends one only by its punctuation. assert.equal(startsSentence(a, null), true); assert.equal(endsSentence(a, null), false); }); test("vid1's boundaries: starts and ends coincide on a contiguous transcript", () => { const b = sentenceBoundaries(VID1); assert.deepEqual(b.starts, [0, 3, 9, 12, 15]); assert.deepEqual(b.ends, [3, 9, 12, 15, 21]); assert.deepEqual(b.all, [0, 3, 9, 12, 15, 21]); }); test("across a pause the end is the cue's end and the start the next cue's start", () => { const cues = [ { start: 0, end: 2, text: "one thought" }, { start: 4, end: 6, text: "another one" }, ]; const b = sentenceBoundaries(cues); assert.deepEqual(b.ends, [2]); assert.deepEqual(b.starts, [0, 4]); assert.deepEqual(b.all, [0, 2, 4]); }); test("unsorted cues and junk rows are tolerated", () => { const b = sentenceBoundaries([VID1[3], null, VID1[2], { start: "x", end: 1, text: "y" }, VID1[1]]); assert.deepEqual(b.ends, [9, 12]); }); test("stepping moves strictly past an edge already on a boundary", () => { const { starts, ends } = sentenceBoundaries(VID1); // The phone walk spec's clip: 3.00-6.00. assert.equal(stepBoundary(ends, 6, 1), 9); assert.equal(stepBoundary(ends, 6, -1), 3); assert.equal(stepBoundary(starts, 3, -1), 0); assert.equal(stepBoundary(starts, 3, 1), 9); assert.equal(stepBoundary(ends, 9, 1), 12); assert.equal(stepBoundary(ends, 21, 1), null); assert.equal(stepBoundary(starts, 0, -1), null); assert.equal(stepBoundary(starts, Number.NaN, 1), null); }); test("a drag snaps to the nearest boundary within the radius, else stands", () => { const { ends } = sentenceBoundaries(VID1); assert.deepEqual(snapToBoundary(ends, 8.6), { t: 9, boundary: 9 }); assert.deepEqual(snapToBoundary(ends, 9 + SNAP_RADIUS), { t: 9, boundary: 9 }); assert.deepEqual(snapToBoundary(ends, 7.5), { t: 7.5, boundary: null }); // Nearest wins when two are in reach. assert.deepEqual(snapToBoundary([1, 1.8], 1.5, 0.6), { t: 1.8, boundary: 1.8 }); }); test("the words either side of an edge", () => { assert.equal(wordsAtEdge(VID1, 9, "end"), "of that it finishes here.|"); assert.equal(wordsAtEdge(VID1, 9, "end", 3), "…it finishes here.|"); assert.equal(wordsAtEdge(VID1, 3, "start"), "|And this one runs on and because"); assert.equal(wordsAtEdge(VID1, 3, "start", 3), "|And this one…"); assert.equal(wordsAtEdge(VID1, 0, "end"), ""); assert.equal(wordsAtEdge([], 3, "start"), ""); });