|
| 1 | +/** |
| 2 | + * /sync-gbrain Step 3.5 offered a `--dream` call-graph build for any source |
| 3 | + * with pages, including one that holds no code pages (synced with gbrain's |
| 4 | + * default markdown strategy), where a build cannot help. On gbrain >= 0.60, |
| 5 | + * `code-def` for an impossible symbol answers `.status` (`ready` / |
| 6 | + * `out_of_scope`); probed live against gbrain 0.60.96. Runs the template's own |
| 7 | + * block against a stub gbrain; older gbrain or a probe error keeps the old |
| 8 | + * behavior (`unknown`). |
| 9 | + */ |
| 10 | +import { describe, it, expect, beforeEach, afterEach } from "bun:test"; |
| 11 | +import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "fs"; |
| 12 | +import { tmpdir } from "os"; |
| 13 | +import { join } from "path"; |
| 14 | +import { spawnSync } from "child_process"; |
| 15 | + |
| 16 | +const TEMPLATE = join(import.meta.dir, "..", "sync-gbrain", "SKILL.md.tmpl"); |
| 17 | +const SRC = "gstack-code-app-1a2b3c4d"; |
| 18 | +const block = () => { |
| 19 | + const blocks = [...readFileSync(TEMPLATE, "utf-8").matchAll(/```bash\n([\s\S]*?)```/g)].map((m) => m[1]!); |
| 20 | + return blocks.find((b) => b.includes('echo "code scope for $SOURCE_ID'))!; |
| 21 | +}; |
| 22 | + |
| 23 | +describe("sync-gbrain Step 3.5 code-scope probe", () => { |
| 24 | + let home = ""; |
| 25 | + beforeEach(() => { |
| 26 | + home = mkdtempSync(join(tmpdir(), "gstack-code-scope-")); |
| 27 | + const capDir = join(home, ".claude", "skills", "gstack", "bin"); |
| 28 | + mkdirSync(capDir, { recursive: true }); |
| 29 | + writeFileSync(join(capDir, "gstack-gbrain-read-capability.ts"), `console.log(JSON.stringify({ status: "source", source_id: ${JSON.stringify(SRC)} }));\n`); |
| 30 | + mkdirSync(join(home, "bin"), { recursive: true }); |
| 31 | + }); |
| 32 | + afterEach(() => rmSync(home, { recursive: true, force: true })); |
| 33 | + |
| 34 | + function scope(codeDef: string, exit = 0): string { |
| 35 | + writeFileSync(join(home, "code-def.out"), codeDef); |
| 36 | + writeFileSync(join(home, "bin", "gbrain"), `#!/bin/sh |
| 37 | +case "$1" in |
| 38 | + doctor) echo '{"checks":[]}' ;; |
| 39 | + code-def) [ "$4" = "${SRC}" ] || exit 9; cat "${join(home, "code-def.out")}"; exit ${exit} ;; |
| 40 | + *) exit 1 ;; |
| 41 | +esac |
| 42 | +`, { mode: 0o755 }); |
| 43 | + const bunDir = process.execPath.replace(/\/[^/]+$/, ""); |
| 44 | + const r = spawnSync("bash", ["-c", block()], { env: { HOME: home, PATH: `${join(home, "bin")}:${bunDir}:/usr/bin:/bin` }, encoding: "utf-8", timeout: 30_000 }); |
| 45 | + return (r.stdout || "").trim().split("\n").find((l) => l.startsWith("code scope for"))!; |
| 46 | + } |
| 47 | + |
| 48 | + it("a source holding code reports ready (log lines before the JSON are skipped)", () => { |
| 49 | + expect(scope(`[code-def] resolving\n{\n "status": "ready",\n "count": 0\n}\n`)).toBe(`code scope for ${SRC}: ready`); |
| 50 | + }); |
| 51 | + it("a markdown-only source reports out_of_scope", () => { |
| 52 | + expect(scope(`{"status":"out_of_scope","count":0}\n`)).toBe(`code scope for ${SRC}: out_of_scope`); |
| 53 | + }); |
| 54 | + it("gbrain below 0.60 (no status field) stays unknown, so the offer is unchanged", () => { |
| 55 | + expect(scope(`{"count":0,"results":[]}\n`)).toBe(`code scope for ${SRC}: unknown`); |
| 56 | + }); |
| 57 | + it("a probe error stays unknown", () => { |
| 58 | + expect(scope("error: no such source\n", 1)).toBe(`code scope for ${SRC}: unknown`); |
| 59 | + }); |
| 60 | + it("the offer is gated on the probe: out_of_scope reports the cause instead of asking", () => { |
| 61 | + const tmpl = readFileSync(TEMPLATE, "utf-8"); |
| 62 | + expect(tmpl).toContain("If `CODE_SCOPE == out_of_scope`, do not offer a build"); |
| 63 | + expect(tmpl).toContain("If `CYCLE == never` AND `CODE_SCOPE` is `ready` or `unknown`"); |
| 64 | + }); |
| 65 | +}); |
0 commit comments