// A post item's SLAM (post-slam.mjs): its validation, its timing (three hits
// spread over the word's spoken span, a phrase's too; --gap; --at), its motion
// (the teaser's slam, from its own scale), its place (above the line, below
// when it would cover the words read), its sound (the teaser's trailer hit, one
// per glyph) and the segment key (an item without a slam keeps its key).
import assert from "node:assert/strict";
import { test } from "node:test";
import {
SLAM_BAR, SLAM_GAP, SLAM_MOTION, slamBarAt, slamBarHtml, slamHits, slamLandings, slamLayout, slamPlan, slamShakeAt, slamShakeExpr, slamSpan, slamStateAt, slamTimes, slamUnitAt, slamUnits,
} from "./post-slam.mjs";
import { TEASER_MOTION } from "./deck.mjs";
import { TRAILER_HIT_TIERS, trailerHitsGraph } from "./trailer-hits.mjs";
import { captionPlan } from "./post-caption.mjs";
import { validatePostFx, wordZoomTimes } from "./word-zoom.mjs";
const TEXT = "I'm ignoring my broken bones so my training can continue. I have achieved Fudōshin. Its amazing.";
const BAJI = "For those wondering, the martial art I'm pursuing is called Baji Quan, need to start.";
const base = (o = {}) => ({ type: "post", id: "p1", post: "s/1", platform: "x", author: "S", handle: "s", date: "2026-10-20T19:46:00.000Z", text: TEXT, ...o });
const slam = (o = {}) => ({ kind: "slam", text: "不動心", match: "Fudōshin", ...o });
const take = (o = {}) => ({ file: "narration/posts/1/take-1.orpheus-zac.wav", engine: "orpheus", voice: "zac", seconds: 10, script: TEXT, ...o });
// The take's words: "achieved" 3.84–4.24, "Fudōshin." 4.24–5.36 (the proof post's own).
const WORDS = [["I'm", 0.2, 0.4], ["ignoring", 0.4, 0.8], ["my", 0.8, 0.9], ["broken", 0.9, 1.2], ["bones", 1.2, 1.5], ["so", 1.5, 1.6], ["my", 1.6, 1.7], ["training", 1.7, 2.1], ["can", 2.1, 2.3], ["continue.", 2.3, 2.9], ["I", 3.3, 3.4], ["have", 3.4, 3.84], ["achieved", 3.84, 4.24], ["Fudōshin.", 4.24, 5.36], ["Its", 5.7, 5.9], ["amazing.", 5.9, 6.4]].map(([w, start, end]) => ({ w, start, end }));
const narrated = (o = {}) => base({ narration: { script: TEXT, takes: [take({ words: WORDS })], lead: 0.5, rate: 1 }, ...o });
test("validation: a slam's keys, text, match, knobs -- in the build's words; a slam is not a word zoom", () => {
assert.deepEqual(validatePostFx(base({ fx: [slam()] })), []);
const errs = validatePostFx(base({ fx: [slam({ match: "Fudoshinn", text: "", place: "left", hold: 9, scale: 2, color: "gold", face: "zh", sfx: "boom", fromScale: 20 })] }));
for (const re of [/fx\[0\]\.scale is not a slam key/, /match "Fudoshinn" is not in the post's words/, /\.text must be the glyphs/, /\.place must be one of over-match, center/, /\.hold must be 0.3–4 seconds/, /\.color must be a colour/, /\.face must be one of jp, sc, tc, kr/, /\.sfx must be one of trailer-hit, off/, /\.fromScale must be 1.5–8/]) {
assert.ok(errs.some((e) => re.test(e)), `${re}\n${errs.join("\n")}`);
}
assert.ok(validatePostFx(base({ fx: [slam({ text: "不".repeat(13) })] })).some((e) => /longer than 12 characters/.test(e)));
// No match: an `at` and the card's middle.
assert.ok(validatePostFx(base({ fx: [slam({ match: undefined })] })).some((e) => /needs its word \(match\) or its second \(at\)/.test(e)));
assert.ok(validatePostFx(base({ fx: [slam({ match: undefined, at: 2 })] })).some((e) => /no word to land over/.test(e)));
assert.deepEqual(validatePostFx(base({ fx: [slam({ match: undefined, at: 2, place: "center" })] })), []);
assert.deepEqual(wordZoomTimes(base({ fx: [slam()] })), [{ i: 0, kind: "slam" }]);
});
test("units: one per glyph (whitespace dropped), or the whole text as one", () => {
assert.deepEqual(slamUnits(slam()), ["不", "動", "心"]);
assert.deepEqual(slamUnits(slam({ text: "八极拳" })), ["八", "极", "拳"]);
assert.deepEqual(slamUnits(slam({ text: " 不 動心 " })), ["不", "動", "心"]);
assert.deepEqual(slamUnits(slam({ whole: true })), ["不動心"]);
});
test("WHEN: three hits spread evenly over the word's spoken span -- the first on its start, the last on its end, the second midway", () => {
const e = narrated({ fx: [slam()] });
const [t] = slamTimes(e);
// On the item's clock: the read's lead (0.5) + the take's second.
assert.deepEqual(t, { i: 0, t: 4.74, end: 5.86, source: "words" });
assert.deepEqual(slamLandings(e.fx[0], t), [4.74, 5.3, 5.86]);
// The read's rate (its schedule's): a take second is 1/rate of the item's.
assert.deepEqual(slamTimes(e, undefined, { at: 0.5, rate: 2 })[0], { i: 0, t: 2.62, end: 3.18, source: "words" });
});
test("WHEN: a two-word phrase spreads over both -- the first hit on Baji's start, the last on Quan's end", () => {
const words = [["For", 0.1, 0.3], ["those", 0.3, 0.6], ["wondering,", 0.6, 1.1], ["the", 1.2, 1.3], ["martial", 1.3, 1.7], ["art", 1.7, 1.9], ["I'm", 1.9, 2.1], ["pursuing", 2.1, 2.5], ["is", 2.5, 2.6], ["called", 2.6, 2.88], ["Baji", 2.88, 3.36], ["Quan,", 3.44, 3.84], ["need", 4.0, 4.2]].map(([w, start, end]) => ({ w, start, end }));
const e = base({ text: BAJI, narration: { script: BAJI, takes: [take({ script: BAJI, words })], lead: 0, rate: 1 }, fx: [slam({ text: "八极拳", match: "Baji Quan", face: "sc" })] });
assert.deepEqual(validatePostFx(e), []);
const [t] = slamTimes(e);
assert.deepEqual(t, { i: 0, t: 2.88, end: 3.84, source: "words" });
assert.deepEqual(slamLandings(e.fx[0], t), [2.88, 3.36, 3.84]);
});
test("WHEN: --gap overrides the spread; --at moves the first hit (the span kept when the words have one); none: SLAM_GAP apart", () => {
const e = narrated({ fx: [slam({ gap: 0.3 })] });
assert.deepEqual(slamLandings(e.fx[0], slamTimes(e)[0]), [4.74, 5.04, 5.34]);
const at = narrated({ fx: [slam({ at: 2 })] });
assert.deepEqual(slamTimes(at)[0], { i: 0, t: 2, end: 3.12, source: "at" });
const plain = base({ fx: [slam({ match: undefined, at: 1, place: "center" })] });
assert.deepEqual(slamTimes(plain)[0], { i: 0, t: 1, end: null, source: "at" });
assert.deepEqual(slamLandings(plain.fx[0], slamTimes(plain)[0]), [1, 1 + SLAM_GAP, 1 + 2 * SLAM_GAP]);
// Un-narrated with a match: the reading estimate's start ("Fudōshin." is word 13: 1 + 13/3).
assert.deepEqual(slamTimes(base({ fx: [slam()] }))[0], { i: 0, t: 5.33, end: null, source: "estimate" });
// One glyph (whole): one hit, on the start.
assert.deepEqual(slamLandings(slam({ whole: true }), { t: 4, end: 5 }), [4]);
});
test("SPAN: in from `hit` before the first landing, held after the last, faded; shortened to the item's end, refused past it", () => {
const sp = slamSpan(slam(), [4, 4.5, 5]);
assert.deepEqual(sp, { t0: 3.8, t2: 6.6, t3: 6.95, landings: [4, 4.5, 5] });
assert.equal(slamSpan(slam(), [4, 4.5, 5], { end: 6 }).t3, 6);
assert.match(slamSpan(slam(), [4, 4.5, 5], { end: 5 }).error, /lands at 5 s, at or after the item's 5 s/);
});
test("MOTION: the teaser's slam (deck.mjs TEASER_MOTION's hit, under, settle, blur), from its own scale -- huge, shrinking onto its spot, landing, settling", () => {
for (const k of ["hit", "under", "settle", "blur"]) assert.equal(SLAM_MOTION[k], TEASER_MOTION[k], k);
assert.equal(slamUnitAt(4, 3.79), null);
const start = slamUnitAt(4, 3.8);
assert.equal(start.scale, 3.5);
assert.equal(start.blur, 18);
assert.equal(start.opacity, 0);
assert.ok(start.ghost > 0.99);
const mid = slamUnitAt(4, 3.9);
assert.ok(mid.scale < 3.5 && mid.scale > 2, String(mid.scale));
// Lands under its rest size, the flash and the ring and the dust fire.
const land = slamUnitAt(4, 4);
assert.equal(land.scale, SLAM_MOTION.under);
assert.equal(land.flash, 1);
assert.equal(land.ring, 0);
assert.equal(land.ghost, 0);
assert.equal(slamUnitAt(4, 5).scale, 1);
assert.equal(slamUnitAt(4, 5).blur, 0);
assert.equal(slamUnitAt(4, 5).ring, -1);
// The row: glyphs land left to right; before the second's start only the first is in.
const st = slamStateAt({ span: slamSpan(slam(), [4, 4.5, 5]), fromScale: 3.5 }, 4.2);
assert.deepEqual(st.units.map((u) => u.k), [0]);
assert.equal(slamStateAt({ span: slamSpan(slam(), [4, 4.5, 5]), fromScale: 3.5 }, 7), null);
});
test("SHAKE: a punch at each landing that dies in its span; the ffmpeg term is the same curve", () => {
assert.deepEqual(slamShakeAt([4], 3.9), { x: 0, y: 0 });
assert.equal(slamShakeAt([4], 4).y, 0.75);
assert.deepEqual(slamShakeAt([4], 4.4), { x: 0, y: 0 });
const ex = slamShakeExpr([4, 4.5], "x", 9);
assert.match(ex, /^9\*\(if\(between\(t,4,4\.3\),exp\(-\(t-4\)\/0\.075\)\*sin\(/);
assert.equal(slamShakeExpr([], "x", 9), "0");
});
test("SOUND: one title-tier trailer hit per landing (the teaser's generator), the swell after the last with swell; none with sfx off", () => {
const sp = slamSpan(slam(), [4, 4.5, 5]);
const hits = slamHits(slam(), sp);
assert.deepEqual(hits.map((h) => [h.kind, h.at, h.gain, h.f0]), [["hit", 4, 1, 92], ["hit", 4.5, 1, 92], ["hit", 5, 1, 92]]);
assert.deepEqual({ ...hits[0], kind: undefined, at: undefined, role: undefined }, { ...TRAILER_HIT_TIERS.title, kind: undefined, at: undefined, role: undefined });
assert.equal(slamHits(slam({ swell: true }), sp).at(-1).kind, "swell");
assert.deepEqual(slamHits(slam({ sfx: "off" }), sp), []);
const g = trailerHitsGraph(hits, { seconds: 8, rate: 48000, prefix: "sl" });
assert.match(g, /\[sla\]$/);
assert.match(g, /\[slb\]\[slp\]\[slr\]amix/);
assert.ok(!/\[ta\]/.test(g));
});
test("PLAN: a slam up with a word zoom is refused; a caption up with it waits for it to be gone (the slam first, then the chip)", () => {
const e = narrated({ fx: [{ kind: "caption", match: "Fudōshin", text: "不動心 · fudōshin" }, slam()] });
const [p] = slamPlan(e);
assert.deepEqual(p.span.landings, [4.74, 5.3, 5.86]);
assert.deepEqual(p.units, ["不", "動", "心"]);
// "Its" (word 14) starts 5.7 + 0.5 = 6.2 s, within 0.25 s... of no hit (5.86 is 0.34 s before): not protected; nothing else is near.
assert.deepEqual(p.avoid, []);
const caps = captionPlan(e, undefined, { zooms: [{ t0: p.span.t0, t3: p.span.t3, i: p.i }] });
assert.equal(caps[0].span.t0, p.span.t3);
assert.equal(caps[0].span.waited, 1);
assert.throws(() => slamPlan(e, undefined, { zooms: [{ t0: 5, t3: 6, i: 0 }] }), /is up while word zoom 1 is/);
assert.throws(() => slamPlan(narrated({ fx: [slam(), slam({ text: "心" })] })), /slams 1 and 2 are up at once/);
});
test("WHERE: a row centred on the word just above its line; below when above would leave the frame or cover a word read at a hit; centre on the card", () => {
const word = { x: 400, y: 300, width: 200, height: 40 };
const card = { x: 100, y: 100, width: 900, height: 800 };
const bounds = { x: 12, y: 12, width: 1896, height: 1056 };
const units = [{ width: 120, height: 120 }, { width: 120, height: 120 }, { width: 120, height: 120 }];
const up = slamLayout({ word, card, bounds, units, gap: 6, protect: [word], obstacles: [{ x: 300, y: 250, width: 400, height: 40 }] });
assert.equal(up.where, "above");
assert.deepEqual([up.x, up.y, up.width, up.height], [320, 174, 360, 120]);
assert.deepEqual(up.centers, [{ x: 380, y: 234 }, { x: 500, y: 234 }, { x: 620, y: 234 }]);
// It lies over the line above: the gag, counted.
assert.equal(up.covers, 1);
assert.equal(up.blocked, false);
// Near the frame's top: below its line.
assert.equal(slamLayout({ word: { ...word, y: 60 }, card, bounds, units, gap: 6, protect: [{ ...word, y: 60 }] }).where, "below");
// A word read at a hit above it: below.
const read = { x: 420, y: 250, width: 80, height: 40 };
assert.equal(slamLayout({ word, card, bounds, units, gap: 6, protect: [word, read] }).where, "below");
// Kept in the frame sideways.
assert.equal(slamLayout({ word: { ...word, x: 0, width: 50 }, card, bounds, units, gap: 6 }).x, 12);
const mid = slamLayout({ card, bounds, units, place: "center" });
assert.deepEqual([mid.where, mid.x, mid.y], ["center", 370, 440]);
});
test("THE SEGMENT KEY (post-render.mjs postFxKeyFields): nothing for an item without a slam; a slam adds its drawing version, its motion and its hit", async () => {
const { postFxKeyFields, POST_SLAM_DRAW_V } = await import("./post-render.mjs");
assert.deepEqual(postFxKeyFields(base(), {}), {});
assert.deepEqual(postFxKeyFields(base({ fx: [{ kind: "caption", match: "x", text: "y" }] }), {}), { captionV: 2 });
const k = postFxKeyFields(base({ fx: [slam()] }), {});
assert.deepEqual(Object.keys(k), ["slam"]);
assert.equal(k.slam.v, POST_SLAM_DRAW_V);
assert.deepEqual(k.slam.hit, TRAILER_HIT_TIERS.title);
assert.deepEqual(k.slam.motion, SLAM_MOTION);
});
test("THE ENCODE (postSegmentArgs): the slam's frames laid over everything while up, the frame's punch, the hits mixed into the audio", async () => {
const { postSegmentArgs } = await import("./post-render.mjs");
const args = (o) => postSegmentArgs({
entry: base(), box: { width: 1600, height: 900 }, plan: { total: 8, steps: [] }, layout: { mode: "card", slot: null }, cards: { read: "/c/read.png" }, files: [],
render: { fps: 30, audioRate: 48000, palette: { bg: "#101010" } }, outPath: "/o.mp4", ...o,
});
const plainArgs = args({});
const fc0 = plainArgs[plainArgs.indexOf("-filter_complex") + 1];
assert.ok(!/slm|sla|crop=/.test(fc0));
assert.match(fc0, /anull\[a\]$/);
const a = args({ slams: [{ list: "/c/slam-1/frames.ffconcat", t0: 3.8, t3: 6.95, landings: [4, 4.5, 5] }], slamHitList: slamHits(slam(), slamSpan(slam(), [4, 4.5, 5])) });
const fc = a[a.indexOf("-filter_complex") + 1];
assert.match(fc, /\[slm0\]overlay=x=0:y=0:eof_action=pass:enable='between\(t,3\.8,6\.95\)'/);
assert.match(fc, /pad=iw\+\d+:ih\+\d+:\d+:\d+:color=0x101010,crop=1600:900:'\d+\+7\.5\*\(if\(between\(t,4,/);
assert.match(fc, /anull\[apre\]/);
assert.match(fc, /\[apre\]\[sla\]amix=inputs=2:normalize=0:duration=first,alimiter=limit=0\.95/);
assert.ok(a.includes("/c/slam-1/frames.ffconcat"));
});
test("THE BAR (operator, 4 Nov: \"have the characters appear above a bar with the translation\"): validated; the row lands ON it, the two placed as one block above the words; it rises in at the first hit and fades with the glyphs", () => {
assert.deepEqual(validatePostFx(base({ fx: [slam({ bar: 'fudōshin — "immovable mind"', barStyle: "gold" })] })), []);
const errs = validatePostFx(base({ fx: [slam({ bar: "x".repeat(141), barStyle: "neon" })] }));
assert.ok(errs.some((e) => /\.bar is longer than 140 characters/.test(e)), errs.join("\n"));
assert.ok(errs.some((e) => /\.barStyle must be one of dark, gold, plain/.test(e)));
assert.ok(validatePostFx(base({ fx: [slam({ barStyle: "gold" })] })).some((e) => /the slam has no bar/.test(e)));
assert.equal(slamBarHtml('fudōshin — "immovable mind"'), 'fudōshin — "immovable mind"');
const word = { x: 400, y: 400, width: 200, height: 40 };
const card = { x: 100, y: 100, width: 900, height: 800 };
const bounds = { x: 12, y: 12, width: 1896, height: 1056 };
const units = [{ width: 120, height: 120, k: 0 }, { width: 120, height: 120, k: 1 }, { width: 120, height: 120, k: 2 }, { width: 500, height: 50, k: -1 }];
const lay = slamLayout({ word, card, bounds, units, gap: 6, protect: [word] });
assert.equal(lay.where, "above");
// The block: 500 wide (the bar), 120 + 50 tall, its foot `gap` above the word; the row centred on the bar, standing on it.
assert.deepEqual([lay.x, lay.y, lay.width, lay.height], [250, 224, 500, 170]);
assert.deepEqual(lay.bar, { x: 250, y: 344, width: 500, height: 50, cx: 500, cy: 369 });
assert.deepEqual(lay.centers.map((c) => c.x), [380, 500, 620]);
assert.equal(lay.centers[0].y + 60, lay.bar.y - Math.round(120 * SLAM_BAR.overlap));
// Too tall above (the word near the frame's top): the block goes below.
assert.equal(slamLayout({ word: { ...word, y: 150 }, card, bounds, units, gap: 6, protect: [{ ...word, y: 150 }] }).where, "below");
// Without a bar: no bar box.
assert.equal(slamLayout({ word, card, bounds, units: units.slice(0, 3), gap: 6 }).bar, null);
const sp = slamSpan(slam(), [4, 4.5, 5]);
assert.equal(slamBarAt(sp, 3.9), null);
const b0 = slamBarAt(sp, 4 - SLAM_BAR.lead);
assert.deepEqual(b0, { opacity: 0, rise: SLAM_BAR.rise });
assert.deepEqual(slamBarAt(sp, 5), { opacity: 1, rise: 0 });
assert.ok(slamBarAt(sp, 6.8).opacity < 0.5);
assert.equal(slamBarAt(sp, 7), null);
});
test("THE SEGMENT KEY with a bar: a slam with one draws at the bar's version and carries its look; one without keeps its key", async () => {
const { postFxKeyFields, POST_SLAM_DRAW_V, POST_SLAM_BAR_DRAW_V } = await import("./post-render.mjs");
const plain = postFxKeyFields(base({ fx: [slam()] }), {});
assert.equal(plain.slam.v, POST_SLAM_DRAW_V);
assert.equal("bar" in plain.slam, false);
const barred = postFxKeyFields(base({ fx: [slam({ bar: "fudōshin" })] }), {});
assert.equal(barred.slam.v, POST_SLAM_BAR_DRAW_V);
assert.deepEqual(barred.slam.bar, SLAM_BAR);
});