← Public packages
@kody/skills
Store, retrieve, edit, version, and revert reusable agent skill documents via skill_list then skill_get.
Files
import assert from 'node:assert/strict'
import { describe, it } from 'node:test'
import narrationAlign, { chunkPlan, whisperArgs } from '../src/narration-align.ts'
import voicePauses from '../src/voice-pauses.ts'
import voiceTiming from '../src/voice-timing.ts'
import finalQa from '../src/final-qa.ts'
import { matchingBlocks, tokenize, wordRecall } from '../src/promo-text.ts'
import { parseEbur128, parseSilencedetect } from '../src/media-logs.ts'
const LINES = [
{ id: 'n01', text: 'One package, two accounts.' },
{ id: 'n02', text: 'Kody maps the right one.' },
]
// Speech: n01 0.0-1.9 (phrase pause 0.8-1.0), line pause 1.9-2.4, n02 2.4-4.0.
const SILENCES = [
{ start: 0.8, end: 1.0 },
{ start: 1.9, end: 2.4 },
{ start: 4.0, end: 4.5 },
]
const ASR = [
{ start: 0.0, end: 0.3, text: 'One' },
{ start: 0.3, end: 0.8, text: 'package,' },
{ start: 1.0, end: 1.3, text: 'two' },
{ start: 1.3, end: 1.9, text: 'accounts.' },
{ start: 2.4, end: 2.8, text: 'Cody' },
{ start: 2.8, end: 3.1, text: 'maps' },
{ start: 3.1, end: 3.3, text: 'the' },
{ start: 3.3, end: 3.6, text: 'right' },
{ start: 3.6, end: 4.0, text: 'one.' },
]
const ALIASES = { 'cody|kodi': 'kody' }
function align() {
return narrationAlign({ lines: LINES, asrWords: ASR, silences: SILENCES, durationSeconds: 4.5, aliases: ALIASES })
}
describe('promo text helpers', () => {
it('normalizes camelCase, hyphens, numbers and aliases', () => {
assert.deepEqual(tokenize('WorkMorning pr-summary 2 Git Hub', ALIASES), ['work', 'morning', 'pr', 'summary', 'two', 'github'])
assert.deepEqual(tokenize('Kodi', ALIASES), ['kody'])
})
it('matches like difflib get_matching_blocks', () => {
assert.deepEqual(matchingBlocks(['a', 'b', 'x', 'c'], ['a', 'b', 'c']), [
{ a: 0, b: 0, size: 2 },
{ a: 3, b: 2, size: 1 },
])
assert.equal(wordRecall(['one', 'map'], ['one', 'nap']).recall, 0.5)
})
it('parses silencedetect and ebur128 logs', () => {
const log = 'silence_start: 0.5\nsilence_end: 0.9 | silence_duration: 0.4\nsilence_start: 3.2\n'
assert.deepEqual(parseSilencedetect(log, 4), [
{ start: 0.5, end: 0.9 },
{ start: 3.2, end: 4 },
])
const eb = 'Summary:\n Integrated loudness:\n I: -15.2 LUFS\n Loudness range:\n LRA: 1.7 LU\n True peak:\n Peak: -2.1 dBFS\n'
assert.deepEqual(parseEbur128(eb), { integratedLufs: -15.2, lraLu: 1.7, truePeakDbtp: -2.1 })
})
})
describe('narration-align', () => {
it('aligns lines to measured pauses and passes', () => {
const r = align()
assert.equal(r.pass, true, r.issues.join('; '))
assert.equal(r.matchRatio, 1)
assert.deepEqual(r.lines.map((l) => [l.id, l.start, l.end]), [
['n01', 0, 1.9],
['n02', 2.4, 4],
])
// The comma phrase pairs with the measured 0.8-1.0 pause.
assert.equal(r.words.find((w) => w.token === 'two')!.start, 1)
})
const collapsed = ASR.map((w, i) => (i >= 4 ? { ...w, start: 2.4, end: 2.4 } : w))
it('repairs collapsed ASR times from measured speech segments', () => {
const r = narrationAlign({ lines: LINES, asrWords: collapsed, silences: SILENCES, durationSeconds: 4.5, aliases: ALIASES })
assert.equal(r.pass, true, r.issues.join('; '))
assert.deepEqual(r.clusters, [])
const n02 = r.words.filter((w) => w.line === 'n02').map((w) => w.start)
assert.deepEqual(n02, [...n02].sort((a, b) => a - b))
assert.equal(new Set(n02).size, n02.length)
})
it('fails instead of relocating a line onto other speech', () => {
// The silence spans all of n02, so there is no measured speech to refine against.
const r = narrationAlign({ lines: LINES, asrWords: collapsed, silences: [SILENCES[0], { start: 1.9, end: 4.5 }], durationSeconds: 4.5, aliases: ALIASES })
assert.equal(r.pass, false)
assert.ok(r.issues.some((i) => i.startsWith('n02: refined edges moved')), r.issues.join('; '))
})
it('is deterministic', () => {
assert.deepEqual(align(), align())
})
it('plans chunks at natural pauses and uses a short prompt only when given', () => {
assert.deepEqual(chunkPlan({ durationSeconds: 20, silences: [{ start: 5, end: 5.4 }, { start: 6, end: 6.4 }, { start: 12, end: 12.4 }], maxChunkSeconds: 8 }), [0, 5.2, 12.2, 20])
assert.ok(!whisperArgs({ model: 'm', file: 'f', outBase: 'o' }).includes('--prompt'))
})
it('rejects unknown keys', () => {
assert.throws(() => narrationAlign({ lines: LINES, asrWords: ASR, silences: [], durationSeconds: 4.5, extra: 1 } as never), /unknown input key/)
})
})
describe('voice-pauses', () => {
it('separates pre/post roll and fails long interior pauses', () => {
const r = voicePauses({ durationSeconds: 10, silences: [{ start: 0, end: 0.3 }, { start: 2, end: 2.5 }, { start: 5, end: 5.9 }, { start: 9.6, end: 10 }] })
assert.equal(r.preRollSeconds, 0.3)
assert.equal(r.postRollSeconds, 0.4)
assert.equal(r.interiorGapCount, 2)
assert.equal(r.maxInteriorGapSeconds, 0.9)
assert.equal(r.pass, false)
assert.equal(voicePauses({ durationSeconds: 10, silences: [{ start: 2, end: 2.5 }] }).pass, true)
})
})
describe('voice-timing', () => {
const base = () => ({
align: align(),
pauses: SILENCES.slice(0, 2),
leadSeconds: 3,
scenes: [
{ id: 'intro', lines: [] },
{ id: 'problem', lines: ['n01'] },
{ id: 'payoff', lines: ['n02'] },
],
anchors: [{ id: 'logo', line: 'n02', words: 'kody maps', scene: 'payoff' }],
})
it('never moves speech and cuts inside the measured pause', () => {
const r = voiceTiming(base())
assert.equal(r.pass, true, r.issues.join('; '))
assert.equal(r.voice.dataStart, 3)
assert.deepEqual(r.lines.map((l) => [l.id, l.start, l.dur]), [
['n01', 3, 1.9],
['n02', 5.4, 1.6],
])
const cut = r.scenes.find((s) => s.id === 'payoff')!.start
assert.ok(cut > 4.9 && cut < 5.4, `cut ${cut} inside pause 4.9-5.4`)
assert.equal(r.anchors.logo, 5.4)
assert.equal(r.durationSeconds, 8.5)
})
it('snaps a cut to a beat inside the pause and ends the intro on a downbeat', () => {
const r = voiceTiming({ ...base(), music: { beats: [5.0, 5.15, 6], downbeats: [0.3, 2.8, 6], downbeatAtSeconds: 0.3 } })
assert.equal(r.scenes.find((s) => s.id === 'payoff')!.start, 5.15)
assert.equal(r.cuts[0].onBeat, true)
assert.equal(r.music!.introEndSeconds, 2.8)
assert.equal(r.music!.fadeOutStartSeconds, 7)
})
it('reports a missing anchor or a reveal outside its scene', () => {
const r = voiceTiming({ ...base(), anchors: [{ id: 'x', line: 'n01', words: 'nope' }, { id: 'y', line: 'n01', words: 'two', scene: 'payoff' }] })
assert.equal(r.pass, false)
assert.equal(r.issues.length, 2)
})
})
describe('final-qa', () => {
const probe = {
streams: [
{ codec_type: 'video', codec_name: 'h264', width: 1920, height: 1080, avg_frame_rate: '30/1' },
{ codec_type: 'audio', codec_name: 'aac', sample_rate: '48000', channels: 2 },
],
format: { duration: '10.000', size: '1000' },
}
const eb = (i: number, p: number) => `Summary:\n I: ${i} LUFS\n LRA: 2.0 LU\n True peak:\n Peak: ${p} dBFS\n`
const input = () => ({
probe,
decodedFrames: 300,
blackdetectLog: '',
ebur128Log: eb(-15.2, -2.1),
lines: [{ id: 'n01', script: 'One package. Two accounts.', heard: 'One package, two accounts.' }],
fadeRms: [
{ t: 8.5, rmsDb: -30, sourceRmsDb: -10 },
{ t: 8.75, rmsDb: -28, sourceRmsDb: -6 },
{ t: 9.0, rmsDb: -40, sourceRmsDb: -9 },
],
fadeStartSeconds: 8.5,
lastWordEndSeconds: 8.5,
expect: { fps: 30, width: 1920, height: 1080, durationSeconds: 10 },
})
it('passes good measurements but never approves', () => {
const r = finalQa(input())
assert.equal(r.machinePass, true, JSON.stringify(r.checks.filter((c) => !c.ok)))
assert.equal(r.approved, false)
assert.equal(r.listeningVerified, false)
assert.equal(r.measurements.fade.method, 'mix_minus_source')
})
it('fails hot masters, black frames and missing speech', () => {
const r = finalQa({ ...input(), ebur128Log: eb(-10, -0.2), blackdetectLog: 'black_start:0 black_end:1', lines: [{ id: 'n01', script: 'One package.', heard: '' }] })
const failed = r.checks.filter((c) => !c.ok).map((c) => c.name)
assert.deepEqual(failed, ['black', 'loudness', 'truePeak', 'speechOverall', 'speechLines'])
})
it('flags a raw-mix fade that rises', () => {
const r = finalQa({ ...input(), fadeRms: input().fadeRms.map(({ t, rmsDb }) => ({ t, rmsDb })) })
assert.equal(r.checks.find((c) => c.name === 'fade')!.ok, false)
})
})