seoanything-cli 2.2.0 → 2.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,8 +1,8 @@
1
- import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
1
+ import { mkdirSync, mkdtempSync, readFileSync, readdirSync, rmSync, writeFileSync } from "node:fs";
2
2
  import os from "node:os";
3
3
  import path from "node:path";
4
4
  import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
5
- import { buildArgs, checkJob, connect, deltaOf, ownEnv, pair, parseOutput, runJob, startBackground } from "./connect.js";
5
+ import { JobError, buildArgs, checkJob, checkVideo, connect, deltaOf, detectVideo, filmSkillDirs, fitWords, listModels, ownEnv, pair, parseModels, parseOutput, runJob, runVideo, startBackground, videoFolder } from "./connect.js";
6
6
 
7
7
  const job = { kind: "claude", prompt: "say ok", system: "be brief", timeout: 60 };
8
8
  let root;
@@ -147,6 +147,8 @@ describe("linking with a setup code", () => {
147
147
  const [cmd, args, opts] = spawnImpl.mock.calls[0];
148
148
  expect([cmd, args]).toEqual([process.execPath, ["/bin/seo.js", "connect", "--server", server]]);
149
149
  expect(opts.detached).toBe(true); expect(child.unref).toHaveBeenCalled();
150
+ startBackground(server, { script: "/bin/seo.js", spawnImpl, video: true });
151
+ expect(spawnImpl.mock.calls[1][1]).toEqual(["/bin/seo.js", "connect", "--server", server, "--video"]);
150
152
  });
151
153
 
152
154
  it("links with the computer token, says hello once per scan, and looks again when asked", async () => {
@@ -168,6 +170,45 @@ describe("linking with a setup code", () => {
168
170
  expect(await done).toMatch(/connector closed/);
169
171
  });
170
172
 
173
+ it("offers video only with --video and the tools found, runs one film at a time and cancels it", async () => {
174
+ FakeSocket.all = [];
175
+ vi.stubGlobal("fetch", vi.fn(async () => Response.json({ url: "wss://seo.test/x" })));
176
+ const ac = new AbortController();
177
+ const runVideoImpl = vi.fn((_m, { signal, onProgress }) => new Promise((_, reject) => {
178
+ onProgress("voice", 15);
179
+ signal.addEventListener("abort", () => reject(new JobError("cancelled", "the video was cancelled.")));
180
+ }));
181
+ const done = connect({ server, deviceToken: "u1.tok", log: () => {}, signal: ac.signal, WebSocketImpl: FakeSocket, video: true, detectVideoImpl: async () => ({ skill: "/s", python: "/py" }), runVideoImpl });
182
+ await vi.waitFor(() => expect(FakeSocket.all[0]?.sent.length).toBe(1), { timeout: 5000 });
183
+ const ws = FakeSocket.all[0];
184
+ expect(ws.sent[0].features).toEqual(["rescan", "video"]);
185
+ ws.emit("message", { data: JSON.stringify({ type: "video", id: "aa11", look: "night", size: "1920x1080", script: {} }) });
186
+ ws.emit("message", { data: JSON.stringify({ type: "video", id: "bb22" }) });
187
+ expect(ws.sent.slice(1)).toEqual([
188
+ { type: "video_progress", id: "aa11", stage: "voice", pct: 15 },
189
+ { type: "video_failed", id: "bb22", code: "busy", message: "this computer is already making a video." },
190
+ ]);
191
+ ws.emit("message", { data: JSON.stringify({ type: "video_cancel", id: "aa11" }) });
192
+ await vi.waitFor(() => expect(ws.sent.at(-1)).toMatchObject({ type: "video_failed", id: "aa11", code: "cancelled" }));
193
+ expect(runVideoImpl).toHaveBeenCalledOnce();
194
+ ac.abort();
195
+ await done;
196
+ });
197
+
198
+ it("leaves video off and says what is missing", async () => {
199
+ FakeSocket.all = [];
200
+ vi.stubGlobal("fetch", vi.fn(async () => Response.json({ url: "wss://seo.test/x" })));
201
+ const ac = new AbortController(), logs = [];
202
+ const done = connect({ server, deviceToken: "u1.tok", log: (s) => logs.push(s), signal: ac.signal, WebSocketImpl: FakeSocket, video: true, detectVideoImpl: async () => ({ missing: ["ffmpeg"] }) });
203
+ await vi.waitFor(() => expect(FakeSocket.all[0]?.sent.length).toBe(1), { timeout: 5000 });
204
+ expect(FakeSocket.all[0].sent[0].features).toEqual(["rescan"]);
205
+ expect(logs).toContain("! video is off: missing ffmpeg.");
206
+ FakeSocket.all[0].emit("message", { data: JSON.stringify({ type: "video", id: "aa11" }) });
207
+ expect(FakeSocket.all[0].sent.at(-1)).toMatchObject({ type: "video_failed", code: "missing_setup" });
208
+ ac.abort();
209
+ await done;
210
+ });
211
+
171
212
  it("forgets the computer token when it is disconnected from the settings page", async () => {
172
213
  FakeSocket.all = [];
173
214
  vi.stubGlobal("fetch", vi.fn(async () => Response.json({ url: "wss://seo.test/x" })));
@@ -185,3 +226,153 @@ describe("linking with a setup code", () => {
185
226
  expect(onUnlinked).toHaveBeenCalledOnce();
186
227
  });
187
228
  });
229
+
230
+ describe("model lists", () => {
231
+ it("reads the codex catalog: hidden models skipped, in its own order, names kept", () => {
232
+ const out = JSON.stringify({ models: [{ slug: "gpt-b", display_name: "GPT B", visibility: "list", priority: 2 }, { slug: "hidden-x", display_name: "X", visibility: "hide", priority: 0 }, { slug: "gpt-a", display_name: "GPT A", visibility: "list", priority: 1 }, { slug: "bad id!", display_name: "Bad", visibility: "list", priority: 3 }] });
233
+ expect(parseModels("codex", out)).toEqual([{ id: "gpt-a", label: "GPT A" }, { id: "gpt-b", label: "GPT B" }]);
234
+ expect(parseModels("codex", "not json")).toEqual([]);
235
+ });
236
+ it("reads agy's id and name lines, ignoring its status line", () => {
237
+ expect(parseModels("agy", "Fetching available models...\ngemini-3.8-flash-high\tGemini 3.8 Flash (High)\nclaude-sonnet-4-6\tClaude Sonnet 4.6 (Thinking)\n")).toEqual([{ id: "gemini-3.8-flash-high", label: "Gemini 3.8 Flash (High)" }, { id: "claude-sonnet-4-6", label: "Claude Sonnet 4.6 (Thinking)" }]);
238
+ });
239
+ it("gives Claude Code its aliases without running it, and nothing when a list can't be read", async () => {
240
+ const run = vi.fn();
241
+ expect((await listModels("claude", "/bin/claude", {}, run)).map((m) => m.id)).toEqual(["fable", "opus", "sonnet", "haiku"]);
242
+ expect(run).not.toHaveBeenCalled();
243
+ expect(await listModels("codex", "/bin/codex", {}, async () => { throw new Error("boom"); })).toEqual([]);
244
+ });
245
+ });
246
+
247
+ describe("video", () => {
248
+ const line = (text, extra = {}) => ({ text, spoken: text, hero: "h", typed: "", ...extra });
249
+ const msg = (lines = [line("One."), line("Two.")], extra = {}) => ({ type: "video", id: "abcd1234", title: "NPL final", slug: "npl-final", look: "night", size: "1920x1080", script: { title: "x", takes: [{ take: "../evil", lines }] }, ...extra });
250
+ let tmp;
251
+ beforeEach(() => { tmp = mkdtempSync(path.join(os.tmpdir(), "vid-")); });
252
+ afterEach(() => rmSync(tmp, { recursive: true, force: true }));
253
+
254
+ it("looks for the skill in the env folder first, then the agent skill folders", () => {
255
+ expect(filmSkillDirs({ SEOANYTHING_FILM_SKILL: "/x" }, "/h")).toEqual(["/x", "/h/.claude/skills/cinematic-film", "/h/.agents/skills/cinematic-film", "/h/.codex/skills/cinematic-film", "/h/.cinematic-skill/skills/cinematic-film"]);
256
+ expect(filmSkillDirs({}, "/h")[0]).toBe("/h/.claude/skills/cinematic-film");
257
+ });
258
+
259
+ it("finds the skill, python with skia, ffmpeg and a voice, or names what is missing", async () => {
260
+ const bin = path.join(tmp, "bin"), skill = path.join(tmp, "home", ".codex", "skills", "cinematic-film", "scripts");
261
+ mkdirSync(bin); mkdirSync(skill, { recursive: true });
262
+ for (const b of ["python3", "ffmpeg", "ffprobe", "say"]) writeFileSync(path.join(bin, b), "#!/bin/sh\n", { mode: 0o755 });
263
+ for (const f of ["render.py", "engine.py"]) writeFileSync(path.join(skill, f), "");
264
+ const env = { PATH: bin }, home = path.join(tmp, "home");
265
+ const ok = vi.fn(async () => ({ code: 0, stdout: "", stderr: "" }));
266
+ expect(await detectVideo({ env, source: {}, home, run: ok })).toEqual({ skill: path.dirname(skill), python: path.join(bin, "python3"), ffmpeg: path.join(bin, "ffmpeg") });
267
+ expect(ok.mock.calls[0][0]).toEqual([path.join(bin, "python3"), "-c", "import skia, numpy, PIL"]);
268
+ const r = await detectVideo({ env, source: {}, home, run: async () => ({ code: 1, stdout: "", stderr: "No module named skia" }) });
269
+ expect(r.missing).toEqual([expect.stringMatching(/skia-python/)]);
270
+ rmSync(path.join(bin, "ffprobe")); rmSync(path.join(skill, "engine.py"));
271
+ expect((await detectVideo({ env, source: {}, home, run: ok })).missing).toEqual([expect.stringMatching(/cinematic-film skill/), "ffmpeg"]);
272
+ });
273
+
274
+ it("checks the message and rebuilds the script: fresh ids and take names, no dashes or markup, fields capped", () => {
275
+ const j = checkVideo(msg([line("-v Alex hi [[volm 0]]", { typed: "x".repeat(200), gap_after: 9 }), line("B", { spoken: "", gap_after: 1 })]));
276
+ expect(j.script.takes).toEqual([{ take: "T01", lines: [
277
+ { id: 1, text: "v Alex hi volm 0", spoken: "v Alex hi volm 0", hero: "h", typed: "x".repeat(80) },
278
+ { id: 2, text: "B", spoken: "B", hero: "h", typed: "", gap_after: 1 },
279
+ ] }]);
280
+ expect(j).toMatchObject({ id: "abcd1234", look: "night", size: "1920x1080", slug: "npl-final" });
281
+ expect(() => checkVideo(msg(undefined, { id: "../x" }))).toThrow(/job id/);
282
+ expect(() => checkVideo(msg(undefined, { look: "neon" }))).toThrow(/look/);
283
+ expect(() => checkVideo(msg(undefined, { size: "4k" }))).toThrow(/size/);
284
+ expect(() => checkVideo(msg(Array.from({ length: 41 }, () => line("a"))))).toThrow(/40 lines/);
285
+ expect(() => checkVideo(msg([line("a", { typed: 5 })]))).toThrow(/must be text/);
286
+ expect(() => checkVideo(msg([]))).toThrow(/no lines/);
287
+ expect(() => checkVideo({ ...msg(), script: "x" })).toThrow(/no takes/);
288
+ });
289
+
290
+ it("names a new folder from a clean slug and the time, never reusing one", () => {
291
+ const now = new Date(2026, 10, 17, 9, 5, 7);
292
+ expect(videoFolder("../NPL Final: Tickets!!", now, tmp)).toBe(path.join(tmp, "npl-final-tickets-20261117-090507"));
293
+ expect(path.basename(videoFolder("a".repeat(80), now, tmp))).toBe(`${"a".repeat(60)}-20261117-090507`);
294
+ expect(path.basename(videoFolder("", now, tmp))).toBe("video-20261117-090507");
295
+ mkdirSync(path.join(tmp, "x-20261117-090507"));
296
+ expect(path.basename(videoFolder("x", now, tmp))).toBe("x-20261117-090507-2");
297
+ });
298
+
299
+ // a fake skill run: records each script and writes what the real one would leave behind
300
+ const fakeRun = (calls, failOn) => async (argv, opts) => {
301
+ const script = argv[0] === "/ff" ? "ffmpeg" : path.basename(argv[1]), dir = argv[2] === "--project" ? argv[3] : argv[2];
302
+ calls.push({ script, args: argv.slice(script === "ffmpeg" ? 1 : 2), cwd: opts.cwd, env: opts.env });
303
+ if (script === failOn) return { code: 1, stdout: "", stderr: "Traceback...\nboom" };
304
+ if (script === "ffmpeg") { writeFileSync(argv.at(-1), "small"); return { code: 0, stdout: "", stderr: "" }; }
305
+ if (script === "build_narration.py") { mkdirSync(path.join(dir, "audio"), { recursive: true }); writeFileSync(path.join(dir, "audio", "timing.json"), '{"total_duration": 12.34}'); }
306
+ const out = opts && argv.includes("--out") ? argv[argv.indexOf("--out") + 1] : null;
307
+ if (out) { mkdirSync(path.join(dir, "out"), { recursive: true }); writeFileSync(path.join(dir, out), "mp4!"); }
308
+ return { code: 0, stdout: "", stderr: "" };
309
+ };
310
+
311
+ it("runs the skill's steps in order in a new folder and reports the phone-sized copy of the captioned film", async () => {
312
+ const calls = [], progress = [];
313
+ const r = await runVideo(msg(), { setup: { skill: "/skill", python: "/py", ffmpeg: "/ff" }, env: { PATH: "/bin" }, run: fakeRun(calls), onProgress: (s, p) => progress.push([s, p]), root: tmp, now: new Date(2026, 10, 17, 9, 5, 7) });
314
+ const dir = path.join(tmp, "npl-final-20261117-090507");
315
+ expect(calls.map((c) => c.script)).toEqual(["new_project.py", "tts_say.py", "split_takes.py", "build_narration.py", "prep_assets.py", "render.py", "build_sfx.py", "mix_soundtrack.py", "render.py", "burn_captions.py", "ffmpeg"]);
316
+ expect(calls[0].args).toEqual([dir]);
317
+ expect(calls[5].args).toEqual(["--project", dir, "--cues"]);
318
+ expect(calls[8].args).toEqual(["--project", dir, "--size", "1920x1080", "--out", "out/film.mp4", "--audio", "audio/mix.m4a"]);
319
+ expect(calls.every((c) => c.cwd === "/skill/scripts" && c.env.PATH === "/bin")).toBe(true);
320
+ const out = path.join(dir, "out");
321
+ expect(calls.at(-1).args).toEqual(["-hide_banner", "-nostdin", "-loglevel", "error", "-y", "-i", path.join(out, "film_captions.mp4"), "-c:v", "libx264", "-profile:v", "high", "-preset", "medium", "-crf", "23",
322
+ "-maxrate", "3M", "-bufsize", "6M", "-pix_fmt", "yuv420p", "-c:a", "aac", "-b:a", "128k", "-movflags", "+faststart", path.join(out, "film_final.mp4")]);
323
+ expect(progress.map((p) => p[0])).toEqual(["setup", "voice", "timing", "timing", "assets", "sound", "sound", "sound", "render", "captions", "captions"]);
324
+ expect(progress.slice(-2)).toEqual([["captions", 97], ["captions", 100]]);
325
+ expect(r).toEqual({ path: path.join(out, "film_final.mp4"), seconds: 12.3, bytes: 5 });
326
+ expect(readdirSync(out)).toEqual(["film_final.mp4"]); // the big masters are gone
327
+ expect(JSON.parse(readFileSync(path.join(dir, "film.json"), "utf8"))).toEqual({ look: "night", size: "1920x1080" });
328
+ expect(JSON.parse(readFileSync(path.join(dir, "script.json"), "utf8")).takes[0].take).toBe("T01");
329
+ expect(readFileSync(path.join(dir, "scene.py"), "utf8")).toMatch(/def build_film\(doc\)/);
330
+ });
331
+
332
+ it("hands over the clean film when captions fail, and stops with stderr's tail when a step fails", async () => {
333
+ const r = await runVideo(msg(), { setup: { skill: "/skill", python: "/py" }, run: fakeRun([], "burn_captions.py"), root: tmp });
334
+ expect(path.basename(r.path)).toBe("film.mp4"); // no ffmpeg in the setup: no small copy
335
+ const calls2 = [];
336
+ const r2 = await runVideo(msg(), { setup: { skill: "/skill", python: "/py", ffmpeg: "/ff" }, run: fakeRun(calls2, "burn_captions.py"), root: tmp });
337
+ expect(calls2.at(-1).args).toContain(path.join(path.dirname(r2.path), "film.mp4"));
338
+ expect(path.basename(r2.path)).toBe("film_final.mp4");
339
+ const calls = [];
340
+ await expect(runVideo(msg(), { setup: { skill: "/skill", python: "/py" }, run: fakeRun(calls, "split_takes.py"), root: tmp })).rejects.toMatchObject({ code: "failed", message: "split_takes.py failed: Traceback...\nboom" });
341
+ expect(calls.map((c) => c.script)).toEqual(["new_project.py", "tts_say.py", "split_takes.py"]);
342
+ });
343
+
344
+ it("keeps the full-size films when the small copy fails", async () => {
345
+ const logs = [], progress = [];
346
+ const r = await runVideo(msg(), { setup: { skill: "/skill", python: "/py", ffmpeg: "/ff" }, run: fakeRun([], "ffmpeg"), root: tmp, log: (s) => logs.push(s), onProgress: (s, p) => progress.push([s, p]) });
347
+ expect(path.basename(r.path)).toBe("film_captions.mp4");
348
+ expect(r.bytes).toBe(4);
349
+ expect(readdirSync(path.dirname(r.path)).sort()).toEqual(["film.mp4", "film_captions.mp4"]);
350
+ expect(logs[0]).toMatch(/phone-sized copy failed.*ffmpeg failed: Traceback/);
351
+ expect(progress.at(-1)).toEqual(["captions", 100]);
352
+ });
353
+
354
+ it("keeps a script near 90 s: drops whole lines from the middle takes, never the first or last take", () => {
355
+ const L = (n, tag = "w") => ({ spoken: Array(n).fill(tag).join(" ") });
356
+ const takes = [[L(60, "a"), L(60, "a")], [L(30), L(30), L(30)], [L(30), L(30)], [L(60, "z")]];
357
+ expect(fitWords(takes)).toBe(4); // 330 words, 30 a cut, the fuller middle take first (the later one on a tie): 210
358
+ expect(takes.map((t) => t.length)).toEqual([2, 1, 1]); // the emptied take is gone
359
+ const short = [[L(5)], [L(5)]];
360
+ expect(fitWords(short)).toBe(0);
361
+ const ends = [[L(200)], [L(10)], [L(200)]];
362
+ expect(fitWords(ends)).toBe(1); // only the middle may go; the ends stay whole even when still over
363
+ expect(ends.length).toBe(2);
364
+ const many = (n) => ({ text: "x", spoken: Array(n).fill("w").join(" "), hero: "", typed: "" });
365
+ const j = checkVideo({ ...msg(), script: { takes: [{ lines: [many(100)] }, { lines: [many(10), many(10)] }, { lines: [many(10)] }, { lines: [many(100)] }] } });
366
+ expect(j.dropped).toBe(2);
367
+ expect(j.script.takes.map((t) => [t.take, t.lines.map((l) => l.id)])).toEqual([["T01", [1]], ["T02", [2]], ["T03", [3]]]);
368
+ });
369
+
370
+ it("stops at once when cancelled or out of time", async () => {
371
+ const ac = new AbortController();
372
+ const run = (argv, { signal }) => new Promise((_, reject) => signal.addEventListener("abort", () => reject(new JobError("cancelled", "the call was cancelled."))));
373
+ const p = runVideo(msg(), { setup: { skill: "/skill", python: "/py" }, run, signal: ac.signal, root: tmp });
374
+ ac.abort();
375
+ await expect(p).rejects.toMatchObject({ code: "cancelled" });
376
+ await expect(runVideo(msg(), { setup: { skill: "/s", python: "/py" }, run: async () => { throw new JobError("timeout", "x"); }, root: tmp })).rejects.toMatchObject({ code: "timeout", message: "the video took longer than 40 minutes." });
377
+ });
378
+ });