import { describe, test, beforeEach, afterEach } from "node:test"; import assert from "node:assert/strict"; import { execFileSync } from "node:child_process"; import { mkdtempSync, rmSync, existsSync, writeFileSync } from "node:fs"; import { join, dirname } from "node:path"; import { fileURLToPath } from "node:url"; import { tmpdir } from "node:os"; const HERE = dirname(fileURLToPath(import.meta.url)); const CLI = join(HERE, "..", "src", "cli.ts"); /** Run the brain CLI in a child process with LINKEDIN_STUDIO_DATA pointed at `root`. */ function runCli(root: string, args: string[]): { stdout: string; code: number } { try { const stdout = execFileSync("node", ["--import", "tsx", CLI, ...args], { env: { ...process.env, LINKEDIN_STUDIO_DATA: root }, encoding: "utf8", }); return { stdout, code: 0 }; } catch (e: any) { return { stdout: (e.stdout ?? "") + (e.stderr ?? ""), code: e.status ?? 1 }; } } describe("brain CLI dispatch (SB-S1)", () => { let root: string; beforeEach(() => { root = mkdtempSync(join(tmpdir(), "brain-cli-")); }); afterEach(() => { rmSync(root, { recursive: true, force: true }); }); test("`init` still works after the CLI refactor (regression)", () => { const { stdout, code } = runCli(root, ["init"]); assert.equal(code, 0); assert.ok(existsSync(join(root, "brain", "profile.md")), "init scaffolded brain/"); assert.match(stdout, /Created|Brain root/); }); test("`ingest --file` writes a published record", () => { const f = join(root, "post.md"); writeFileSync(f, "A published post body.", "utf8"); const { stdout, code } = runCli(root, ["ingest", "--file", f]); assert.equal(code, 0); assert.match(stdout, /Wrote ingest\/published\/[0-9a-f]{12}\.md/); }); test("`ingest --file` twice on the same body reports a duplicate", () => { const f = join(root, "post.md"); writeFileSync(f, "dup body", "utf8"); runCli(root, ["ingest", "--file", f]); const { stdout } = runCli(root, ["ingest", "--file", f]); assert.match(stdout, /Duplicate/); }); test("`ingest --scan-inbox` processes the inbox", () => { runCli(root, ["init"]); writeFileSync(join(root, "ingest", "inbox", "a.md"), "inbox post", "utf8"); const { stdout, code } = runCli(root, ["ingest", "--scan-inbox"]); assert.equal(code, 0); assert.match(stdout, /1 new/); }); test("`published list` lists ingested records with provenance", () => { const f = join(root, "post.md"); writeFileSync(f, "listed post", "utf8"); runCli(root, ["ingest", "--file", f]); const { stdout, code } = runCli(root, ["published", "list"]); assert.equal(code, 0); assert.match(stdout, /published/); }); test("unknown command exits 2 (usage error)", () => { const { code } = runCli(root, ["bogus"]); assert.equal(code, 2); }); });