// channelNames.mjs (lane channel-title): a cut's per-channel display name // (`render.channelNames`), its rules, its merge patch and its warnings. import { test } from "node:test"; import assert from "node:assert/strict"; import { CHANNEL_NAME_MAX, channelNameProblem, channelNamesWarnings, patchChannelNames, validateChannelNames, } from "./channelNames.mjs"; import { LOOK_KEYS, applyLookPatch, lookOf } from "./look.mjs"; test("a name: a string, at most 60 characters, no control character, no newline", () => { assert.equal(CHANNEL_NAME_MAX, 60); assert.equal(channelNameProblem("Synth Channel"), null); assert.equal(channelNameProblem("x".repeat(60)), null); assert.match(String(channelNameProblem("x".repeat(61))), /at most 60/); assert.match(String(channelNameProblem("Synth\nChannel")), /control character/); assert.match(String(channelNameProblem("Synth\tChannel")), /control character/); assert.match(String(channelNameProblem("Synth\u0007Channel")), /control character/); assert.match(String(channelNameProblem(" ")), /empty/); assert.match(String(channelNameProblem(42)), /words/); }); test("validateChannelNames: absent is fine; a slug per key, a name per value", () => { assert.deepEqual(validateChannelNames({ render: {} }), []); assert.deepEqual(validateChannelNames({ render: { channelNames: { "synth-chan": "Synth Channel" } } }), []); assert.match(validateChannelNames({ render: { channelNames: ["Synth Channel"] } }).join(), /an object/); assert.match(validateChannelNames({ render: { channelNames: { "a b": "Synth" } } }).join(), /not a channel slug/); assert.match(validateChannelNames({ render: { channelNames: { "synth-chan": "a\nb" } } }).join(), /render\.channelNames\.synth-chan.*control character/); }); test("patchChannelNames: merged over what it holds, a key null drops one, null drops all, trimmed", () => { assert.deepEqual(patchChannelNames(undefined, { a: " Synth A " }), { a: "Synth A" }); assert.deepEqual(patchChannelNames({ a: "Synth A" }, { b: "Synth B" }), { a: "Synth A", b: "Synth B" }); assert.equal(patchChannelNames({ a: "Synth A" }, { a: null }), null, "empty is no key"); assert.equal(patchChannelNames({ a: "Synth A" }, null), null); // A value that is not words is kept as given, for the validator to name. assert.deepEqual(patchChannelNames({}, { a: 7 }), { a: 7 }); }); test("the look writes it: a LOOK key, merged, checked", () => { assert.ok(LOOK_KEYS.includes("channelNames")); const m = { render: { fps: 30 }, timeline: [{ type: "clip", id: "c1", channel: "synth-chan", video: "v", start: 0, end: 1 }] }; const a = applyLookPatch(m, { channelNames: { "synth-chan": "Synth Channel" } }); assert.deepEqual(a.errors, []); assert.deepEqual(lookOf(a.render).channelNames, { "synth-chan": "Synth Channel" }); const b = applyLookPatch({ ...m, render: a.render }, { channelNames: { "synth-chan": null } }); assert.deepEqual(b.errors, []); assert.equal("channelNames" in b.render, false); const bad = applyLookPatch(m, { channelNames: { "synth-chan": "Synth\u0000" } }); assert.match(bad.errors.join(), /control character/); }); test("channelNamesWarnings: a slug no clip of the cut uses is named, not refused", () => { const m = { provenance: { channelSlug: "own-chan" }, render: { channelNames: { "synth-chan": "Synth Channel", "own-chan": "Own", "gone-chan": "Gone" } }, timeline: [ { type: "clip", id: "c1", channel: "synth-chan", video: "v", start: 0, end: 1 }, { type: "clip", id: "c2", video: "w", start: 0, end: 1 }, ], }; const w = channelNamesWarnings(m); assert.equal(w.length, 1); assert.match(w[0], /gone-chan/); assert.deepEqual(channelNamesWarnings({ render: {} }), []); });