|
| 1 | +/** |
| 2 | + * Phase 3 in-repo test bench: spawn CLI against fixtures/minimal after one index. |
| 3 | + * Complements cmd-cli-parity-e2e (resources, batch, trace) with show/snippet/impact/validate/SARIF. |
| 4 | + */ |
| 5 | +import { beforeAll, describe, expect, it } from "bun:test"; |
| 6 | +import { existsSync } from "node:fs"; |
| 7 | +import { join } from "node:path"; |
| 8 | + |
| 9 | +const repoRoot = join(import.meta.dir, "..", ".."); |
| 10 | +const indexTs = join(repoRoot, "src", "index.ts"); |
| 11 | +const benchRoot = join(repoRoot, "fixtures", "minimal"); |
| 12 | +let bunBin: string | null = null; |
| 13 | + |
| 14 | +async function runCli( |
| 15 | + args: string[], |
| 16 | + env: Record<string, string> = {}, |
| 17 | +): Promise<{ exitCode: number; out: string; err: string }> { |
| 18 | + if (bunBin === null) { |
| 19 | + throw new Error( |
| 20 | + "cmd-test-bench-e2e.test: bunBin not initialised by beforeAll.", |
| 21 | + ); |
| 22 | + } |
| 23 | + const proc = Bun.spawn([bunBin, indexTs, ...args], { |
| 24 | + cwd: repoRoot, |
| 25 | + stdout: "pipe", |
| 26 | + stderr: "pipe", |
| 27 | + env: { ...process.env, CODEMAP_ROOT: benchRoot, ...env }, |
| 28 | + }); |
| 29 | + const exitCode = await proc.exited; |
| 30 | + const out = await new Response(proc.stdout).text(); |
| 31 | + const err = await new Response(proc.stderr).text(); |
| 32 | + return { exitCode, out, err }; |
| 33 | +} |
| 34 | + |
| 35 | +beforeAll(async () => { |
| 36 | + bunBin = Bun.which("bun"); |
| 37 | + if (!bunBin || !existsSync(indexTs)) { |
| 38 | + throw new Error( |
| 39 | + `cmd-test-bench-e2e.test: cannot locate Bun (${bunBin}) or src entry (${indexTs}).`, |
| 40 | + ); |
| 41 | + } |
| 42 | + const idx = await runCli(["--full"]); |
| 43 | + expect(idx.exitCode).toBe(0); |
| 44 | +}, 120_000); |
| 45 | + |
| 46 | +describe("in-repo test bench CLI smoke — fixtures/minimal", () => { |
| 47 | + it("show returns symbol matches", async () => { |
| 48 | + const r = await runCli(["show", "usePermissions", "--json"]); |
| 49 | + expect(r.exitCode).toBe(0); |
| 50 | + expect(r.err).toBe(""); |
| 51 | + const payload = JSON.parse(r.out) as { matches: unknown[] }; |
| 52 | + expect(payload.matches.length).toBeGreaterThan(0); |
| 53 | + }); |
| 54 | + |
| 55 | + it("snippet returns source for a symbol", async () => { |
| 56 | + const r = await runCli(["snippet", "usePermissions", "--json"]); |
| 57 | + expect(r.exitCode).toBe(0); |
| 58 | + const payload = JSON.parse(r.out) as { |
| 59 | + matches: Array<{ source?: string; missing: boolean }>; |
| 60 | + }; |
| 61 | + expect(payload.matches[0]?.missing).toBe(false); |
| 62 | + expect(payload.matches[0]?.source).toContain("usePermissions"); |
| 63 | + }); |
| 64 | + |
| 65 | + it("impact returns neighborhood matches", async () => { |
| 66 | + const r = await runCli([ |
| 67 | + "impact", |
| 68 | + "usePermissions", |
| 69 | + "--json", |
| 70 | + "--depth", |
| 71 | + "1", |
| 72 | + ]); |
| 73 | + expect(r.exitCode).toBe(0); |
| 74 | + const payload = JSON.parse(r.out) as { matches: unknown[] }; |
| 75 | + expect(payload.matches.length).toBeGreaterThan(0); |
| 76 | + }); |
| 77 | + |
| 78 | + it("validate reports no drift on a fresh index", async () => { |
| 79 | + const r = await runCli(["validate", "--json"]); |
| 80 | + expect(r.exitCode).toBe(0); |
| 81 | + expect(JSON.parse(r.out)).toEqual([]); |
| 82 | + }); |
| 83 | + |
| 84 | + it("query --recipe shop-symbols returns project-local recipe rows", async () => { |
| 85 | + const r = await runCli(["query", "--recipe", "shop-symbols", "--json"]); |
| 86 | + expect(r.exitCode).toBe(0); |
| 87 | + const rows = JSON.parse(r.out) as Array<{ |
| 88 | + name: string; |
| 89 | + file_path: string; |
| 90 | + actions?: unknown[]; |
| 91 | + }>; |
| 92 | + expect(rows.some((row) => row.name === "ProductCard")).toBe(true); |
| 93 | + expect(rows[0]?.actions?.length).toBeGreaterThan(0); |
| 94 | + }); |
| 95 | + |
| 96 | + it("query --format sarif emits SARIF for boundary-violations", async () => { |
| 97 | + const r = await runCli([ |
| 98 | + "query", |
| 99 | + "--recipe", |
| 100 | + "boundary-violations", |
| 101 | + "--format", |
| 102 | + "sarif", |
| 103 | + ]); |
| 104 | + expect(r.exitCode).toBe(0); |
| 105 | + expect(r.err).toBe(""); |
| 106 | + const payload = JSON.parse(r.out) as { version: string; runs: unknown[] }; |
| 107 | + expect(payload.version).toBe("2.1.0"); |
| 108 | + expect(payload.runs.length).toBeGreaterThan(0); |
| 109 | + }); |
| 110 | +}); |
0 commit comments