// Room at a clip's edges (room.mjs, release 36 lane E): the silence rule on // synthetic words and silences, the plan, the sound's filters, the // validators, the strip's shading and the lint's window warning. // // Run with: pnpm test:scripts import assert from "node:assert/strict"; import test from "node:test"; import { ROOM, clipRoom, roomAudioFilter, roomOn, roomPlan, roomShade, roomWindowWarnings, speechStartFromSilences, speechStartFromWords, tailFromSilences, tailFromWords, validateRoom, } from "./room.mjs"; /** Words at the given [start, end] pairs. */ const W = (...pairs) => pairs.map(([start, end], i) => ({ w: `w${i}`, start, end })); test("tailFromWords: a cut through a word runs to the end of its run, stopping at the first gap of minGap", () => { // 10.0 cuts "w1" (9.8-10.2); w2 follows 0.1 s later; then a 0.5 s gap. const words = W([9.0, 9.5], [9.8, 10.2], [10.3, 10.9], [11.4, 11.8]); assert.deepEqual(tailFromWords(words, 10.0), { at: 10.9, extended: true, capped: false }); }); test("tailFromWords: a word starting within minGap after the cut belongs to the run; one starting later does not", () => { assert.equal(tailFromWords(W([9, 9.9], [10.2, 10.6]), 10.0).at, 10.6); assert.deepEqual(tailFromWords(W([9, 9.9], [10.5, 10.9]), 10.0), { at: 10.0, extended: false, capped: false }); // A narrower minGap: the same word is out of reach. assert.equal(tailFromWords(W([9, 9.9], [10.2, 10.6]), 10.0, { minGap: 0.15 }).at, 10.0); }); test("tailFromWords: the cap -- never more than maxS past the cut", () => { const words = []; for (let t = 9.8; t < 16; t += 0.3) words.push({ start: t, end: t + 0.25 }); assert.deepEqual(tailFromWords(words, 10.0), { at: 12.5, extended: true, capped: true }); assert.equal(tailFromWords(words, 10.0, { maxS: 1 }).at, 11.0); }); test("tailFromWords: no words at all leaves the sound at the cut", () => { assert.deepEqual(tailFromWords([], 10), { at: 10, extended: false, capped: false }); assert.deepEqual(tailFromWords(null, 10), { at: 10, extended: false, capped: false }); }); test("silences (no words): in a silence the sound stops at the cut; else at the next silence within maxS; none found, at the cut", () => { const sil = [{ s: 4, e: 4.6 }, { s: 11.2, e: 11.9 }]; assert.deepEqual(tailFromSilences(sil, 4.2), { at: 4.2, extended: false, capped: false, found: true }); assert.deepEqual(tailFromSilences(sil, 10.0), { at: 11.2, extended: true, capped: false, found: true }); assert.deepEqual(tailFromSilences(sil, 6.0), { at: 6.0, extended: false, capped: false, found: false }); assert.equal(speechStartFromSilences(sil, 4.1), 4.6); assert.equal(speechStartFromSilences(sil, 5), 5); }); test("speechStartFromWords: the first word not over before the cut's start", () => { const words = W([1, 1.4], [2.1, 2.5], [2.6, 3]); assert.equal(speechStartFromWords(words, 2.0), 2.1); assert.equal(speechStartFromWords(words, 2.2), 2.2); assert.equal(speechStartFromWords(words, 5), Infinity); }); test("roomPlan: the picture runs D + pad past the silence point; a head starts D early; both clamped into the file", () => { const p = roomPlan({ cutA: 2, cutB: 6, D: 0.4, soundEnd: 6.8, speechStart: 2.1, duration: 20 }); assert.deepEqual(p, { picA: 1.6, picB: 7.3, soundA: 2, soundB: 6.8, head: 0.4, tail: 0.5, clamped: [] }); // Speech starts later than D after the cut: no head. assert.equal(roomPlan({ cutA: 2, cutB: 6, D: 0.4, soundEnd: null, speechStart: 2.5, duration: 20 }).picA, 2); // A window that ends early clamps the tail; one that starts late clamps the head. const c = roomPlan({ cutA: 0.2, cutB: 6, D: 0.4, soundEnd: 6.8, speechStart: 0.2, duration: 7 }); assert.equal(c.picA, 0); assert.equal(c.picB, 7); assert.deepEqual(c.clamped, ["head", "tail"]); // Tail off: the cut's own end. assert.equal(roomPlan({ cutA: 2, cutB: 6, D: 0.4, soundEnd: null, speechStart: null }).picB, 6); }); test("roomAudioFilter: a fade in ending at the cut, a fade out from the silence point, in the segment's clock", () => { const p = roomPlan({ cutA: 2, cutB: 6, D: 0.4, soundEnd: 6.8, speechStart: 2.1, duration: 20 }); assert.equal(roomAudioFilter(p), `afade=t=in:st=0.340:d=${ROOM.fade},afade=t=out:st=5.200:d=${ROOM.fade}`); assert.equal(roomAudioFilter(roomPlan({ cutA: 2, cutB: 6, D: 0.4 })), ""); }); test("clipRoom / roomOn: an edge is on when its key is true; a clip's room beats the cut's", () => { const render = { room: { tail: true, head: true } }; assert.equal(roomOn({}), false); assert.equal(roomOn({ room: {} }), false); assert.equal(roomOn(render), true); assert.deepEqual(clipRoom({}, render), { tail: true, head: true, minGap: ROOM.minGap, maxS: ROOM.maxS }); assert.equal(clipRoom({ room: false }, render), null); assert.deepEqual(clipRoom({ room: { tail: false } }, render), { tail: false, head: true, minGap: 0.35, maxS: 2.5 }); assert.equal(clipRoom({ room: { tail: false, head: false } }, render), null); assert.deepEqual(clipRoom({ room: { tail: true } }, {}), { tail: true, head: false, minGap: 0.35, maxS: 2.5 }); assert.equal(clipRoom({}, { room: { tail: true, minGap: 0.5, maxS: 1 } }).maxS, 1); assert.equal(clipRoom({}, {}), null); }); test("validateRoom: absent or null says nothing; each problem in a sentence naming the key", () => { const m = (room, timeline = [], extra = {}) => ({ render: { room, ...extra }, timeline }); assert.deepEqual(validateRoom(m(undefined)), []); assert.deepEqual(validateRoom(m(null)), []); assert.deepEqual(validateRoom(m({ tail: true, head: true, minGap: 0.35, maxS: 2.5 })), []); assert.match(validateRoom(m(true))[0], /render\.room must be an object/); assert.match(validateRoom(m({ tail: "yes" }))[0], /render\.room\.tail must be true or false/); assert.match(validateRoom(m({ tail: true, pad: 1 }))[0], /render\.room\.pad is not a setting here/); assert.match(validateRoom(m({ tail: true, minGap: 5 }))[0], /render\.room\.minGap must be seconds from 0\.1 to 2/); assert.match(validateRoom(m({ tail: true, maxS: -1 }))[0], /render\.room\.maxS must be seconds from 0 to 10/); assert.match(validateRoom(m({ tail: true }, [], { montage: {} }))[0], /does not combine with render\.montage/); const clip = (room) => ({ type: "clip", id: "c1", room }); assert.deepEqual(validateRoom(m(undefined, [clip(false), clip({ tail: false })])), []); assert.match(validateRoom(m(undefined, [clip("off")]))[0], /timeline\[0\] \(c1\)\.room must be false or/); assert.match(validateRoom(m(undefined, [clip({ middle: true })]))[0], /\.room\.middle is not a setting here/); assert.match(validateRoom(m(undefined, [{ type: "card", id: "k", room: false }]))[0], /only a clip has room/); }); test("roomShade: the strip's muted head and tail, from the words; nothing with room off or no join", () => { const room = clipRoom({}, { room: { tail: true, head: true } }); const words = W([2.05, 2.4], [5.6, 6.1], [6.2, 6.7], [7.5, 8]); assert.deepEqual(roomShade({ cutStart: 2, cutEnd: 6, room, D: 0.4, words }), { head: [1.6, 2], tail: [6.7, 7.2], soundEnd: 6.7 }); assert.deepEqual(roomShade({ cutStart: 2, cutEnd: 6, room: null, D: 0.4, words }), { head: null, tail: null, soundEnd: null }); assert.deepEqual(roomShade({ cutStart: 2, cutEnd: 6, room, D: 0, words }), { head: null, tail: null, soundEnd: null }); // No words: the nominal room. assert.deepEqual(roomShade({ cutStart: 2, cutEnd: 6, room, D: 0.4 }), { head: [1.6, 2], tail: [6, 6.5], soundEnd: 6 }); }); test("roomWindowWarnings: room that can reach past a fetched window; no transition", () => { const clip = { type: "clip", id: "c1", video: "v", start: 10, end: 20, cutStart: 10, cutEnd: 20 }; assert.deepEqual(roomWindowWarnings({ render: { transition: 0.4 }, timeline: [clip] }), []); assert.deepEqual(roomWindowWarnings({ render: { transition: 0.4, room: { tail: true, head: true } }, timeline: [clip] }), []); const w = roomWindowWarnings({ render: { transition: 0.4, fetchPad: 1, room: { tail: true } }, timeline: [clip] }); assert.equal(w.length, 1); assert.match(w[0], /c1: its room can run 3 s past the cut but a fetched window ends 1 s after it/); assert.match(roomWindowWarnings({ render: { transition: 0, room: { tail: true } }, timeline: [clip] })[0], /room does nothing/); });