linkedin-studio/scripts/brain/tests/cli.test.ts
Kjell Tore Guttormsen b65eb25328 feat(linkedin-studio): SB-S1 brain CLI — ingest + published list (init preserved) [skip-docs]
Add flag-routing infra (parseFlags) + two subcommands to the brain CLI, keeping
the existing `init` branch intact:
- `ingest --file <path> [--source] [--date]` / `ingest --scan-inbox` → capture
  published posts into ingest/published/ (Wrote / Duplicate / Collision report).
- `published list [--json]` → inspect the gold corpus (id · provenance · date · first line).
6 subprocess tests incl. an explicit `init` regression guard. brain 57→63 tests, tsc clean.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RigJBiRFNtFZKCz21qNbQ4
2026-06-23 14:56:45 +02:00

78 lines
2.8 KiB
TypeScript

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);
});
});