tests/video-script.test.ts
import { describe, it, expect, vi } from "vitest";
// @ts-ignore -- plain-JS agent module without type declarations
import agentModule, { handleMcpMessage, makeContext } from "../src/index.js";
const agent: any = agentModule;
const now = () => new Date("2026-03-20T09:00:00Z");
const A = "https://energy.example.org/heat-pumps-2025";
const B = "https://www.news.example.com/eu-heat-pumps";
const brief = {
topic: "heat pump adoption in Europe",
generated_at: "2026-03-20T08:00:00.000Z",
key_points: [
{ claim: "Heat pump sales in Europe grew 30% in 2025, according to the industry association.", sources: [A, B] },
{ claim: "Government subsidies were the main driver of demand in Germany.", sources: [B] },
{ claim: "An unsourced statement that must never appear in the script.", sources: [] },
{ claim: "Installers report waiting lists of several months in rural areas.", sources: [A] },
],
open_questions: ["Sources disagree, figures differ (30% vs 45%)."],
};
const words = (s: string) => s.trim().split(/\s+/).length;
describe("video-script: build_script", () => {
it("builds hook/body/CTA segments timed from word count (happy path)", async () => {
const out = await agent.call("build_script", { brief }, { now });
const { script } = out;
expect(script.title).toBe("heat pump adoption in Europe");
expect(script.segments.map((s: any) => s.kind)).toEqual(["hook", "body", "body", "body", "cta"]);
expect(script.segments[0].text).toBe("What do the sources say about heat pump adoption in Europe? Here are 3 points.");
expect(script.segments[1].text).toBe("According to energy.example.org and news.example.com: Heat pump sales in Europe grew 30% in 2025, according to the industry association.");
let cursor = 0;
for (const s of script.segments) {
expect(s.start).toBeCloseTo(cursor, 3);
expect(s.end - s.start).toBeCloseTo(words(s.text) / 2.5, 2);
cursor = s.end;
}
expect(script.duration_seconds).toBeCloseTo(cursor, 2);
expect(script.duration_seconds).toBeGreaterThanOrEqual(15);
expect(script.duration_seconds).toBeLessThanOrEqual(45);
expect(out.word_budget).toMatchObject({ target_seconds: 45, total: 112 });
});
it("keeps a claims list linked to sources and never invents or uses unsourced claims", async () => {
const out = await agent.call("build_script", { brief }, { now });
const briefClaims = brief.key_points.map((k) => k.claim);
expect(out.script.claims.map((c: any) => [c.text, c.source_url, c.sources])).toEqual([
[briefClaims[0], A, [A, B]],
[briefClaims[1], B, [B]],
[briefClaims[3], A, [A]],
]);
for (const c of out.script.claims) expect(out.script.segments[c.segment].text.endsWith(c.text)).toBe(true);
const allText = out.script.segments.map((s: any) => s.text).join(" ");
expect(allText).not.toContain("unsourced statement");
expect(out.warnings.join(" ")).toMatch(/Key point 3 was skipped because it has no http\(s\) source URL/);
expect(out.warnings.join(" ")).toMatch(/1 open question/);
});
it("respects a configurable speaking rate and the duration budget", async () => {
const slow = await agent.call("build_script", { brief, words_per_second: 2, attribute_sources: false }, { now });
const body = slow.script.segments[1];
expect(body.text).toBe(brief.key_points[0].claim);
expect(body.end - body.start).toBeCloseTo(words(body.text) / 2, 3);
const short = await agent.call("build_script", { brief, target_seconds: 15 }, { now });
expect(short.script.duration_seconds).toBeLessThanOrEqual(15);
expect(short.script.claims.map((c: any) => c.brief_point)).toEqual([2]);
expect(short.warnings.join(" ")).toMatch(/Key point 1 \(19 words\) was left out to stay within 15 s/);
expect(short.warnings.join(" ")).toMatch(/below the 15 s short-form minimum/);
});
it("exports valid SubRip captions aligned with the segments", async () => {
const out = await agent.call("build_script", { brief, max_chars_per_line: 32 }, { now });
const cues = out.captions_srt.trim().split(/\n\n/);
expect(cues.length).toBeGreaterThanOrEqual(out.script.segments.length);
cues.forEach((cue: string, i: number) => {
const lines = cue.split("\n");
expect(lines[0]).toBe(String(i + 1));
expect(lines[1]).toMatch(/^\d{2}:\d{2}:\d{2},\d{3} --> \d{2}:\d{2}:\d{2},\d{3}$/);
expect(lines.length).toBeGreaterThanOrEqual(3);
expect(lines.length).toBeLessThanOrEqual(4);
for (const l of lines.slice(2)) expect(l.length).toBeLessThanOrEqual(32);
});
expect(cues[0].split("\n")[1].startsWith("00:00:00,000 --> ")).toBe(true);
const lastEnd = cues[cues.length - 1].split("\n")[1].split(" --> ")[1];
const [h, m, rest] = lastEnd.split(":");
const [s, ms] = rest.split(",");
expect(+h * 3600 + +m * 60 + +s + +ms / 1000).toBeCloseTo(out.script.duration_seconds, 2);
});
it("splits long segments into balanced cues without one-word orphans", async () => {
const seg = { start: 0, end: 8.4, text: "What do the sources say about e-bike commuting in Canadian cities? Here are 5 points." };
const out = await agent.call("captions_from_script", { script: { segments: [seg] }, max_chars_per_line: 42 });
const cues = out.captions_srt.trim().split(/\n\n/).map((c: string) => c.split("\n").slice(2));
expect(cues.length).toBe(2);
for (const lines of cues) {
expect(lines.length).toBeLessThanOrEqual(2);
for (const l of lines) expect(l.length).toBeLessThanOrEqual(42);
expect(lines.join(" ").split(" ").length).toBeGreaterThanOrEqual(4);
}
// The break falls after the question mark, and no word is lost or reordered.
expect(cues[0].join(" ").endsWith("cities?")).toBe(true);
expect(cues.map((l: string[]) => l.join(" ")).join(" ")).toBe(seg.text);
});
it("warns about segments the brief marks as disputed or single-source", async () => {
const C = "https://blog.example.net/heat-pump-market";
const disputed = {
topic: "heat pump sales in Europe",
key_points: [
{ claim: "Heat pump sales in Europe grew 30% in 2025, according to the industry association.", sources: [A, B] },
{ claim: "Heat pump sales in Europe grew 45% in 2025, based on early retail estimates.", sources: [C] },
{ claim: "Installers report waiting lists of several months in rural areas.", sources: [A] },
],
open_questions: [
`Sources disagree, figures differ (30% vs 45%): "Heat pump sales in Europe grew 30% in 2025, according to the industry association." (${A}, ${B}) versus "Heat pump sales in Europe grew 45% in 2025, based on early retail estimates." (${C}). Which is correct?`,
`Only one source supports: "Heat pump sales in Europe grew 45% in 2025, based on early retail estimates." (${C}). Is there independent confirmation?`,
`Only one source supports: "Installers report waiting lists of several months in rural areas." (${A}). Is there independent confirmation?`,
],
};
const out = await agent.call("build_script", { brief: disputed, target_seconds: 60 }, { now });
const w = out.warnings.join("\n");
expect(w).toMatch(/Segments 1 and 2 state points that the sources disagree on \(figures differ \(30% vs 45%\)\)/);
expect(w).toMatch(/Segment 3 rests on a single source/);
expect(w).not.toMatch(/Segment 2 rests on a single source/);
// A brief whose open questions do not quote key points only gets the generic warning.
const plain = await agent.call("build_script", { brief }, { now });
expect(plain.warnings.join(" ")).not.toMatch(/disagree on|single source/);
});
it("flags user-supplied hooks and CTAs as unsourced text", async () => {
const out = await agent.call("build_script", { brief, hook: "Europe is switching heating systems.", cta: "Follow for part two." }, { now });
expect(out.script.segments[0].text).toBe("Europe is switching heating systems.");
expect(out.warnings.join(" ")).toMatch(/hook text was supplied by you and is not linked to a source/);
expect(out.warnings.join(" ")).toMatch(/call to action was supplied by you/);
});
it("refuses briefs without any sourced key point", async () => {
const bad = { topic: "x", key_points: [{ claim: "No source here.", sources: ["not a url"] }] };
await expect(agent.call("build_script", { brief: bad }, { now })).rejects.toMatchObject({ code: "insufficient_content" });
await expect(agent.call("build_script", { brief: { topic: "x", key_points: [] } }, { now })).rejects.toMatchObject({ code: "insufficient_content" });
});
it("rejects invalid input", async () => {
await expect(agent.call("build_script", { brief, target_seconds: 10 })).rejects.toMatchObject({ code: "invalid_input" });
await expect(agent.call("build_script", { brief, target_seconds: 120 })).rejects.toMatchObject({ code: "invalid_input" });
await expect(agent.call("build_script", { brief: { topic: "x" } })).rejects.toMatchObject({ code: "invalid_input" });
await expect(agent.call("build_script", { brief, words_per_second: 9 })).rejects.toMatchObject({ code: "invalid_input" });
});
});
describe("video-script: captions_from_script", () => {
it("exports captions from edited segments and neutralises SRT arrows", async () => {
const out = await agent.call("captions_from_script", {
script: { segments: [{ start: 4, end: 6.5, text: "Second --> part" }, { start: 0, end: 4, text: "First part of the video" }] },
});
expect(out.captions_srt).toBe("1\n00:00:00,000 --> 00:00:04,000\nFirst part of the video\n\n2\n00:00:04,000 --> 00:00:06,500\nSecond -> part\n");
await expect(agent.call("captions_from_script", { script: { segments: [{ start: 3, end: 1, text: "x" }] } })).rejects.toMatchObject({ code: "invalid_input" });
});
});
describe("video-script: permissions and MCP", () => {
it("has no network, filesystem or credential permissions", async () => {
expect(agent.manifest.permissions).toEqual({ filesystem: "none", network: [], credentials: [] });
const fetch = vi.fn();
await expect(makeContext(agent.manifest, { fetch }).fetch("https://api.example.com/tts")).rejects.toMatchObject({ code: "network_denied" });
expect(fetch).not.toHaveBeenCalled();
});
it("serves tools/list and tools/call over MCP", async () => {
const list = await handleMcpMessage(agent, { jsonrpc: "2.0", id: 1, method: "tools/list" });
expect(list.result.tools.map((t: any) => t.name)).toEqual(["build_script", "captions_from_script"]);
const call = await handleMcpMessage(agent, { jsonrpc: "2.0", id: 2, method: "tools/call", params: { name: "build_script", arguments: { brief } } });
expect(call.result.isError).toBe(false);
expect(call.result.structuredContent.script.claims).toHaveLength(3);
const bad = await handleMcpMessage(agent, { jsonrpc: "2.0", id: 3, method: "tools/call", params: { name: "build_script", arguments: { brief, target_seconds: 5 } } });
expect(bad.result.isError).toBe(true);
});
});