Skip to content

Open-source AI podcast clipper. Generate vertical clips with face tracking and burned-in captions. CLI, MCP server, web app.

tests: add vitest suites for core TypeScript services

Nika Siradze5f7de58b5ff7
diff --git a/src/services/asset-manager.test.ts b/src/services/asset-manager.test.ts
new file mode 100644
index 0000000..a55aa56
--- /dev/null
+++ b/src/services/asset-manager.test.ts
@@ -0,0 +1,72 @@
+import { describe, it, expect, beforeEach } from "vitest";
+import { mkdtempSync, writeFileSync, rmSync, mkdirSync } from "fs";
+import { tmpdir } from "os";
+import { join } from "path";
+
+const tmp = mkdtempSync(join(tmpdir(), "podcli-assets-test-"));
+process.env.PODCLI_HOME = tmp;
+process.env.PODCLI_DATA = tmp;
+
+const { AssetManager } = await import("./asset-manager.js");
+
+function makeFile(name: string): string {
+ const p = join(tmp, name);
+ writeFileSync(p, "stub");
+ return p;
+}
+
+describe("AssetManager", () => {
+ let manager: InstanceType<typeof AssetManager>;
+
+ beforeEach(() => {
+ rmSync(join(tmp, "assets"), { recursive: true, force: true });
+ mkdirSync(join(tmp, "assets"), { recursive: true });
+ manager = new AssetManager();
+ });
+
+ it("registers an asset and lists it", async () => {
+ const file = makeFile("logo.png");
+ await manager.register("main-logo", file, "logo");
+ const list = await manager.list();
+ expect(list).toHaveLength(1);
+ expect(list[0].name).toBe("main-logo");
+ expect(list[0].type).toBe("logo");
+ });
+
+ it("rejects registration of missing files", async () => {
+ await expect(
+ manager.register("ghost", "/nonexistent/file.png", "logo"),
+ ).rejects.toThrow(/File not found/);
+ });
+
+ it("upserts on repeat registration with same name", async () => {
+ const a = makeFile("a.png");
+ const b = makeFile("b.png");
+ await manager.register("same", a, "logo");
+ await manager.register("same", b, "logo");
+ const list = await manager.list();
+ expect(list).toHaveLength(1);
+ expect(list[0].path).toBe(b);
+ });
+
+ it("filters list by type", async () => {
+ await manager.register("logo1", makeFile("l1.png"), "logo");
+ await manager.register("vid1", makeFile("v1.mp4"), "video");
+ expect(await manager.list("logo")).toHaveLength(1);
+ expect(await manager.list("video")).toHaveLength(1);
+ });
+
+ it("resolve returns path for registered name, direct path, or null", async () => {
+ const file = makeFile("resolvable.png");
+ await manager.register("friendly", file, "logo");
+ expect(await manager.resolve("friendly")).toBe(file);
+ expect(await manager.resolve(file)).toBe(file);
+ expect(await manager.resolve("nope")).toBeNull();
+ });
+
+ it("unregister removes the asset", async () => {
+ await manager.register("gone", makeFile("g.png"), "logo");
+ await manager.unregister("gone");
+ expect(await manager.list()).toHaveLength(0);
+ });
+});
diff --git a/src/services/clips-history.test.ts b/src/services/clips-history.test.ts
new file mode 100644
index 0000000..252e7a9
--- /dev/null
+++ b/src/services/clips-history.test.ts
@@ -0,0 +1,97 @@
+import { describe, it, expect, beforeEach } from "vitest";
+import { mkdtempSync, writeFileSync, rmSync, mkdirSync } from "fs";
+import { tmpdir } from "os";
+import { join } from "path";
+
+// Isolate PODCLI_HOME before the module (and its paths import) are evaluated.
+const tmp = mkdtempSync(join(tmpdir(), "podcli-test-"));
+process.env.PODCLI_HOME = tmp;
+process.env.PODCLI_DATA = tmp;
+
+const { ClipsHistory } = await import("./clips-history.js");
+
+function makeFakeOutput(name: string): string {
+ const p = join(tmp, name);
+ writeFileSync(p, "stub");
+ return p;
+}
+
+describe("ClipsHistory", () => {
+ let history: InstanceType<typeof ClipsHistory>;
+
+ beforeEach(() => {
+ // Reset history dir for each test
+ rmSync(join(tmp, "history"), { recursive: true, force: true });
+ mkdirSync(join(tmp, "history"), { recursive: true });
+ history = new ClipsHistory();
+ });
+
+ it("records and lists clips", async () => {
+ await history.record({
+ source_video: "/videos/show.mp4",
+ output_path: makeFakeOutput("a.mp4"),
+ start_second: 10,
+ end_second: 40,
+ caption_style: "karaoke",
+ crop_strategy: "smart",
+ title: "hook A",
+ } as any);
+
+ const list = await history.list();
+ expect(list).toHaveLength(1);
+ expect(list[0].title).toBe("hook A");
+ });
+
+ it("findDuplicate matches within ±2s tolerance", async () => {
+ const output = makeFakeOutput("b.mp4");
+ await history.record({
+ source_video: "/videos/show.mp4",
+ output_path: output,
+ start_second: 10,
+ end_second: 40,
+ caption_style: "karaoke",
+ crop_strategy: "smart",
+ title: "original",
+ } as any);
+
+ const dup = await history.findDuplicate("/videos/show.mp4", 11, 41, "karaoke", "smart");
+ expect(dup).not.toBeNull();
+ expect(dup?.title).toBe("original");
+
+ const miss = await history.findDuplicate("/videos/show.mp4", 15, 45, "karaoke", "smart");
+ expect(miss).toBeNull();
+ });
+
+ it("findDuplicate ignores entries whose output file is missing", async () => {
+ const output = makeFakeOutput("c.mp4");
+ await history.record({
+ source_video: "/videos/show.mp4",
+ output_path: output,
+ start_second: 100,
+ end_second: 130,
+ caption_style: "karaoke",
+ crop_strategy: "smart",
+ title: "ghost",
+ } as any);
+
+ rmSync(output);
+ const dup = await history.findDuplicate("/videos/show.mp4", 100, 130, "karaoke", "smart");
+ expect(dup).toBeNull();
+ });
+
+ it("findDuplicate matches by basename, not full path", async () => {
+ const output = makeFakeOutput("d.mp4");
+ await history.record({
+ source_video: "/absolute/path/one/show.mp4",
+ output_path: output,
+ start_second: 0,
+ end_second: 30,
+ caption_style: "karaoke",
+ crop_strategy: "smart",
+ title: "basename test",
+ } as any);
+
+ const dup = await history.findDuplicate("/different/path/show.mp4", 0, 30, "karaoke", "smart");
+ expect(dup).not.toBeNull();
+ });
+});
diff --git a/src/services/knowledge-base.test.ts b/src/services/knowledge-base.test.ts
new file mode 100644
index 0000000..2350243
--- /dev/null
+++ b/src/services/knowledge-base.test.ts
@@ -0,0 +1,89 @@
+import { describe, it, expect, beforeEach } from "vitest";
+import { mkdtempSync, rmSync, mkdirSync, existsSync, readFileSync } from "fs";
+import { tmpdir } from "os";
+import { join } from "path";
+
+const tmp = mkdtempSync(join(tmpdir(), "podcli-kb-test-"));
+process.env.PODCLI_HOME = tmp;
+process.env.PODCLI_DATA = tmp;
+
+const { KnowledgeBase } = await import("./knowledge-base.js");
+
+describe("KnowledgeBase", () => {
+ let kb: InstanceType<typeof KnowledgeBase>;
+
+ beforeEach(() => {
+ rmSync(join(tmp, "knowledge"), { recursive: true, force: true });
+ mkdirSync(join(tmp, "knowledge"), { recursive: true });
+ kb = new KnowledgeBase();
+ });
+
+ it("ensureDir creates the directory and seeds a README", async () => {
+ rmSync(join(tmp, "knowledge"), { recursive: true, force: true });
+ await kb.ensureDir();
+ expect(existsSync(join(tmp, "knowledge", "README.md"))).toBe(true);
+ expect(readFileSync(join(tmp, "knowledge", "README.md"), "utf-8")).toMatch(/Knowledge Base/);
+ });
+
+ it("writeFile auto-appends .md extension if missing", async () => {
+ await kb.writeFile("style", "# Style\nbrand voice here");
+ expect(existsSync(join(tmp, "knowledge", "style.md"))).toBe(true);
+ });
+
+ it("writeFile preserves explicit .md extension", async () => {
+ await kb.writeFile("hosts.md", "# Hosts");
+ expect(existsSync(join(tmp, "knowledge", "hosts.md"))).toBe(true);
+ });
+
+ it("readFile round-trips content", async () => {
+ await kb.writeFile("voice.md", "# Voice\ncasual & punchy");
+ const content = await kb.readFile("voice.md");
+ expect(content).toContain("casual & punchy");
+ });
+
+ it("readFile throws for missing files", async () => {
+ await expect(kb.readFile("ghost.md")).rejects.toThrow(/File not found/);
+ });
+
+ it("listFiles returns all .md files sorted by filename, excluding nothing", async () => {
+ await kb.writeFile("zeta.md", "z");
+ await kb.writeFile("alpha.md", "a");
+ await kb.writeFile("mid.md", "m");
+ const files = await kb.listFiles();
+ const names = files.map((f) => f.filename);
+ expect(names).toContain("alpha.md");
+ expect(names).toContain("mid.md");
+ expect(names).toContain("zeta.md");
+ // Ensure sorted
+ const customSorted = [...names].sort();
+ expect(names).toEqual(customSorted);
+ });
+
+ it("readAll concatenates all files except README", async () => {
+ // ensureDir seeds README
+ await kb.ensureDir();
+ await kb.writeFile("one.md", "first");
+ await kb.writeFile("two.md", "second");
+ const all = await kb.readAll();
+ expect(all).toContain("--- one.md ---");
+ expect(all).toContain("first");
+ expect(all).toContain("--- two.md ---");
+ expect(all).toContain("second");
+ expect(all).not.toContain("README");
+ });
+
+ it("readAll returns empty string when only README exists", async () => {
+ await kb.ensureDir();
+ expect(await kb.readAll()).toBe("");
+ });
+
+ it("deleteFile removes the file", async () => {
+ await kb.writeFile("temp.md", "temporary");
+ await kb.deleteFile("temp.md");
+ expect(existsSync(join(tmp, "knowledge", "temp.md"))).toBe(false);
+ });
+
+ it("deleteFile is idempotent on missing files", async () => {
+ await expect(kb.deleteFile("never.md")).resolves.toBeUndefined();
+ });
+});
diff --git a/src/services/transcript-cache.test.ts b/src/services/transcript-cache.test.ts
new file mode 100644
index 0000000..0c16820
--- /dev/null
+++ b/src/services/transcript-cache.test.ts
@@ -0,0 +1,78 @@
+import { describe, it, expect, beforeEach } from "vitest";
+import { mkdtempSync, writeFileSync, rmSync, mkdirSync } from "fs";
+import { tmpdir } from "os";
+import { join } from "path";
+import type { TranscriptResult } from "../models/index.js";
+
+const tmp = mkdtempSync(join(tmpdir(), "podcli-cache-test-"));
+process.env.PODCLI_HOME = tmp;
+process.env.PODCLI_DATA = tmp;
+
+const { TranscriptCache } = await import("./transcript-cache.js");
+
+function makeFakeVideo(name: string, content: string): string {
+ const p = join(tmp, name);
+ writeFileSync(p, content);
+ return p;
+}
+
+const fakeTranscript: TranscriptResult = {
+ transcript: "hello world",
+ segments: [],
+ words: [
+ { word: "hello", start: 0, end: 0.5, confidence: 0.99 },
+ { word: "world", start: 0.5, end: 1.0, confidence: 0.99 },
+ ],
+ duration: 1.0,
+ language: "en",
+ speakers: { num_speakers: 0, speakers: {} },
+ speaker_segments: [],
+};
+
+describe("TranscriptCache", () => {
+ let cache: InstanceType<typeof TranscriptCache>;
+
+ beforeEach(() => {
+ rmSync(join(tmp, "cache"), { recursive: true, force: true });
+ mkdirSync(join(tmp, "cache", "transcripts"), { recursive: true });
+ cache = new TranscriptCache();
+ });
+
+ it("returns null for an uncached file", async () => {
+ const file = makeFakeVideo("uncached.mp4", "fresh content");
+ expect(await cache.get(file)).toBeNull();
+ });
+
+ it("set then get round-trips the transcript", async () => {
+ const file = makeFakeVideo("cached.mp4", "stable content here");
+ await cache.set(file, fakeTranscript);
+ const loaded = await cache.get(file);
+ expect(loaded).not.toBeNull();
+ expect(loaded?.transcript).toBe("hello world");
+ expect(loaded?.words).toHaveLength(2);
+ });
+
+ it("hashes the file content — different files get different cache keys", async () => {
+ const a = makeFakeVideo("a.mp4", "content A");
+ const b = makeFakeVideo("b.mp4", "content B");
+ await cache.set(a, { ...fakeTranscript, transcript: "A" });
+ await cache.set(b, { ...fakeTranscript, transcript: "B" });
+ expect((await cache.get(a))?.transcript).toBe("A");
+ expect((await cache.get(b))?.transcript).toBe("B");
+ });
+
+ it("getFileHash is stable for identical content", async () => {
+ const a = makeFakeVideo("first.mp4", "identical bytes");
+ const b = makeFakeVideo("second.mp4", "identical bytes");
+ const hashA = await cache.getFileHash(a);
+ const hashB = await cache.getFileHash(b);
+ expect(hashA).toBe(hashB);
+ });
+
+ it("get returns null when the cache file is corrupt", async () => {
+ const file = makeFakeVideo("corrupt-source.mp4", "any content");
+ const hash = await cache.getFileHash(file);
+ writeFileSync(join(tmp, "cache", "transcripts", `${hash}.json`), "this is not json");
+ expect(await cache.get(file)).toBeNull();
+ });
+});