/** * Unit tests for bin/gstack-brain-context-load.ts (Lane C). * * Tests CLI surface, template var substitution, manifest vs default-fallback * routing, datamark envelope wrapping, and graceful degradation when gbrain * CLI is missing. Full E2E (real gbrain MCP calls) lives in Lane F. */ import { describe, it, expect } from "bun:test"; import { chmodSync, mkdtempSync, readFileSync, writeFileSync, mkdirSync, rmSync } from "fs"; import { tmpdir } from "os"; import { delimiter, join } from "path"; import { spawnSync } from "child_process"; const SCRIPT = join(import.meta.dir, "..", "bin", "gstack-brain-context-load.ts"); function runScript(args: string[], env: Record = {}): { stdout: string; stderr: string; exitCode: number } { const result = spawnSync("bun", [SCRIPT, ...args], { encoding: "utf-8", timeout: 30000, env: { ...process.env, ...env }, }); return { stdout: result.stdout || "", stderr: result.stderr || "", exitCode: result.status ?? 1, }; } function writeFakeGbrain(binDir: string): void { if (process.platform === "win32") { writeFileSync( join(binDir, "gbrain.cmd"), "@echo off\r\nif \"%1\"==\"--version\" (\r\n echo gbrain 0.test\r\n) else (\r\n echo fake gbrain %*\r\n)\r\n", "utf-8", ); return; } const fakeBin = join(binDir, "gbrain"); writeFileSync( fakeBin, `#!/bin/sh if [ "$1" = "--version" ]; then echo "gbrain 0.test" else echo "fake gbrain $*" fi `, "utf-8", ); chmodSync(fakeBin, 0o755); } /** * Like writeFakeGbrain, but every invocation appends its argv to `logFile`. * Still answers `--version` successfully ON PURPOSE: a revert from the * memoized PATH stat scan back to the old `gbrain --version` spawn probe * would pass every non-logging test — only the argv log catches it. */ function writeLoggingGbrain(binDir: string, logFile: string): void { if (process.platform === "win32") { writeFileSync( join(binDir, "gbrain.cmd"), `@echo off\r\necho %* >> "${logFile}"\r\nif "%1"=="--version" (\r\n echo gbrain 0.test\r\n) else (\r\n echo fake gbrain %*\r\n)\r\n`, "utf-8", ); return; } const fakeBin = join(binDir, "gbrain"); writeFileSync( fakeBin, `#!/bin/sh printf '%s\\n' "$*" >> "${logFile}" if [ "$1" = "--version" ]; then echo "gbrain 0.test" else echo "fake gbrain $*" fi `, "utf-8", ); chmodSync(fakeBin, 0o755); } function prependPath(binDir: string): Record { const pathKey = Object.keys(process.env).find((key) => key.toLowerCase() === "path") || "PATH"; const currentPath = process.env[pathKey] || ""; return { [pathKey]: `${binDir}${delimiter}${currentPath}`, // Cold process spawns on a loaded machine can exceed the 500ms default // budget; the fake gbrain is instant once spawned, so give it headroom. GSTACK_BRAIN_TIMEOUT_MS: "10000", }; } describe("gstack-brain-context-load CLI", () => { it("--help exits 0 with usage", () => { const r = runScript(["--help"]); expect(r.exitCode).toBe(0); expect(r.stderr).toContain("Usage: gstack-brain-context-load"); expect(r.stderr).toContain("--skill"); expect(r.stderr).toContain("--repo"); }); it("rejects unknown flag", () => { const r = runScript(["--bogus"]); expect(r.exitCode).toBe(1); expect(r.stderr).toContain("Unknown argument: --bogus"); }); it("--limit must be positive integer", () => { const r = runScript(["--limit", "0"]); expect(r.exitCode).toBe(1); expect(r.stderr).toContain("--limit requires a positive integer"); }); }); describe("gstack-brain-context-load — manifest dispatch", () => { it("falls back to default manifest when --skill resolves to no file", () => { const r = runScript(["--skill", "nonexistent-skill-xyz", "--repo", "test-repo", "--explain", "--quiet"]); expect(r.exitCode).toBe(0); expect(r.stderr).toContain("mode=default"); // 3 queries in default expect(r.stderr).toContain("queries=3"); }); it("uses skill manifest when --skill-file points at a valid SKILL.md", () => { const dir = mkdtempSync(join(tmpdir(), "gstack-bcl-")); const skillFile = join(dir, "SKILL.md"); writeFileSync( skillFile, `--- name: test-skill gbrain: schema: 1 context_queries: - id: my-prior kind: filesystem glob: "${dir}/notes/*.md" sort: mtime_desc limit: 5 render_as: "## My prior notes" --- body `, "utf-8" ); // Create some matching files mkdirSync(join(dir, "notes")); writeFileSync(join(dir, "notes", "one.md"), "first\n"); writeFileSync(join(dir, "notes", "two.md"), "second\n"); const r = runScript(["--skill-file", skillFile, "--repo", "test-repo", "--explain"]); expect(r.exitCode).toBe(0); expect(r.stderr).toContain("mode=manifest"); expect(r.stderr).toContain("queries=1"); expect(r.stdout).toContain("## My prior notes"); expect(r.stdout).toContain("one.md"); expect(r.stdout).toContain("two.md"); rmSync(dir, { recursive: true, force: true }); }); it("wraps rendered body in USER_TRANSCRIPT_DATA envelope (datamark per D12)", () => { const dir = mkdtempSync(join(tmpdir(), "gstack-bcl-")); const skillFile = join(dir, "SKILL.md"); writeFileSync( skillFile, `--- name: x gbrain: schema: 1 context_queries: - id: fs kind: filesystem glob: "${dir}/*.md" render_as: "## FS results" --- `, "utf-8" ); writeFileSync(join(dir, "a.md"), "x\n"); const r = runScript(["--skill-file", skillFile]); expect(r.exitCode).toBe(0); expect(r.stdout).toContain(""); expect(r.stdout).toContain(""); rmSync(dir, { recursive: true, force: true }); }); it("substitutes {repo_slug} in render_as", () => { const dir = mkdtempSync(join(tmpdir(), "gstack-bcl-")); const skillFile = join(dir, "SKILL.md"); writeFileSync( skillFile, `--- name: x gbrain: schema: 1 context_queries: - id: fs kind: filesystem glob: "${dir}/*.md" render_as: "## My events for {repo_slug}" --- `, "utf-8" ); writeFileSync(join(dir, "a.md"), "x\n"); const r = runScript(["--skill-file", skillFile, "--repo", "my-test-repo"]); expect(r.exitCode).toBe(0); expect(r.stdout).toContain("## My events for my-test-repo"); rmSync(dir, { recursive: true, force: true }); }); it("skips queries with unresolved template vars (logged via --explain)", () => { const dir = mkdtempSync(join(tmpdir(), "gstack-bcl-")); const skillFile = join(dir, "SKILL.md"); writeFileSync( skillFile, `--- name: x gbrain: schema: 1 context_queries: - id: needs-user kind: filesystem glob: "${dir}/{user_slug}/file.md" render_as: "## Needs user_slug" --- `, "utf-8" ); // No --user passed; {user_slug} unresolved const r = runScript(["--skill-file", skillFile, "--repo", "x", "--explain"]); expect(r.exitCode).toBe(0); expect(r.stderr).toContain("template vars unresolved"); expect(r.stderr).toContain("user_slug"); rmSync(dir, { recursive: true, force: true }); }); it("--quiet suppresses rendered output", () => { const dir = mkdtempSync(join(tmpdir(), "gstack-bcl-")); const skillFile = join(dir, "SKILL.md"); writeFileSync( skillFile, `--- name: x gbrain: schema: 1 context_queries: - id: fs kind: filesystem glob: "${dir}/*.md" render_as: "## Stuff" --- `, "utf-8" ); writeFileSync(join(dir, "a.md"), "x\n"); const r = runScript(["--skill-file", skillFile, "--quiet"]); expect(r.exitCode).toBe(0); expect(r.stdout).toBe(""); rmSync(dir, { recursive: true, force: true }); }); }); describe("gstack-brain-context-load — graceful gbrain absence", () => { it("uses gbrain when a binary is available on PATH", () => { const dir = mkdtempSync(join(tmpdir(), "gstack-bcl-")); const binDir = join(dir, "bin"); mkdirSync(binDir); writeFakeGbrain(binDir); try { const r = runScript(["--repo", "test-repo", "--explain"], prependPath(binDir)); expect(r.exitCode).toBe(0); expect(r.stderr).toContain("OK"); expect(r.stderr).not.toContain("gbrain CLI missing"); expect(r.stdout).toContain("fake gbrain list_pages"); } finally { rmSync(dir, { recursive: true, force: true }); } }); it("manifest filter: blocks reach gbrain as --filter args with template vars resolved (#1687)", () => { const dir = mkdtempSync(join(tmpdir(), "gstack-bcl-")); const binDir = join(dir, "bin"); mkdirSync(binDir); writeFakeGbrain(binDir); const skillFile = join(dir, "SKILL.md"); writeFileSync( skillFile, `--- name: x gbrain: schema: 1 context_queries: - id: prior-sessions kind: list filter: type: ceo-plan tags_contains: "repo:{repo_slug}" sort: updated_at_desc limit: 5 render_as: "## Prior sessions" --- `, "utf-8" ); try { const r = runScript(["--skill-file", skillFile, "--repo", "my-test-repo"], prependPath(binDir)); expect(r.exitCode).toBe(0); expect(r.stdout).toContain("fake gbrain list_pages"); expect(r.stdout).toContain("--filter type=ceo-plan"); expect(r.stdout).toContain("--filter tags_contains=repo:my-test-repo"); expect(r.stdout).toContain("--sort updated_at_desc"); } finally { rmSync(dir, { recursive: true, force: true }); } }); it("gbrain detection never spawns gbrain — stat-based PATH scan, not a `--version` probe (revert trap)", () => { // The fix replaced a per-query `gbrain --version` spawn probe with a // memoized PATH stat scan. The plain writeFakeGbrain shim still ANSWERS // --version, so a revert to the spawn probe passes every other test in // this file. This fake logs its argv: detection must invoke gbrain zero // times, so the only invocations are the 3 default-manifest list_pages // queries — a revert adds `--version` lines (and re-probing adds one per // query) and fails exactly here. const dir = mkdtempSync(join(tmpdir(), "gstack-bcl-")); const binDir = join(dir, "bin"); mkdirSync(binDir); const logFile = join(dir, "gbrain-argv.log"); writeLoggingGbrain(binDir, logFile); try { const r = runScript(["--repo", "test-repo", "--explain", "--quiet"], prependPath(binDir)); expect(r.exitCode).toBe(0); expect(r.stderr).toContain("queries=3"); const invocations = readFileSync(logFile, "utf-8").split("\n").filter(Boolean); expect(invocations.some((argv) => argv.includes("--version"))).toBe(false); // Exactly the 3 real queries — no extra availability spawns of any shape. expect(invocations).toHaveLength(3); for (const argv of invocations) expect(argv.startsWith("list_pages")).toBe(true); } finally { rmSync(dir, { recursive: true, force: true }); } }); it("availability survives a 1ms query budget — detection is not subject to GSTACK_BRAIN_TIMEOUT_MS", () => { // The spawn probe ran under the same MCP_TIMEOUT_MS budget as the queries, // so a cold spawn slower than the budget misreported gbrain as MISSING. // With the stat scan, a 1ms budget kills the queries themselves (SKIP) // but detection still sees the CLI — "gbrain CLI missing" must not appear. const dir = mkdtempSync(join(tmpdir(), "gstack-bcl-")); const binDir = join(dir, "bin"); mkdirSync(binDir); // A SLOW fake, not the shared instant one: on a fast CI runner the // instant fake answered inside even a 1ms budget (observed dur=0ms on // ubicloud) and no SKIP ever printed. Sleeping makes the timeout // deterministic on every machine; --version stays instant so detection // has nothing to wait on. const fakeBin = join(binDir, "gbrain"); writeFileSync( fakeBin, `#!/bin/sh if [ "$1" = "--version" ]; then echo "gbrain 0.test" else sleep 0.3 echo "fake gbrain $*" fi `, "utf-8", ); chmodSync(fakeBin, 0o755); try { const env = { ...prependPath(binDir), GSTACK_BRAIN_TIMEOUT_MS: "1" }; const r = runScript(["--repo", "test-repo", "--explain", "--quiet"], env); expect(r.exitCode).toBe(0); expect(r.stderr).toContain("SKIP"); expect(r.stderr).not.toContain("gbrain CLI missing"); } finally { rmSync(dir, { recursive: true, force: true }); } }); it("vector + list queries still complete (with SKIP) when gbrain CLI is missing", () => { // We can't easily un-install gbrain; rely on the helper's own missing-binary // detection. The default manifest uses kind: list which calls gbrain. If // gbrain is missing, the helper should still exit 0 and explain shows SKIP. // We use --explain to verify the SKIP code path doesn't hard-fail. const r = runScript(["--repo", "test-repo", "--explain", "--quiet"]); expect(r.exitCode).toBe(0); // Either OK (gbrain available) or SKIP (gbrain missing or query timeout) — both fine expect(r.stderr).toMatch(/(OK|SKIP)/); }); });