|
| 1 | +import { execFileSync } from "node:child_process"; |
| 2 | +import { copyFileSync, mkdirSync, readFileSync, rmSync } from "node:fs"; |
| 3 | +import { tmpdir } from "node:os"; |
| 4 | +import { dirname, join } from "node:path"; |
| 5 | +import { fileURLToPath } from "node:url"; |
| 6 | +import { afterEach, describe, expect, it } from "vitest"; |
| 7 | +import { runCli } from "../src/index"; |
| 8 | + |
| 9 | +const fixtures = join(dirname(fileURLToPath(import.meta.url)), "../../knowledge/test/fixtures/state-space"); |
| 10 | +const roots: string[] = []; |
| 11 | +afterEach(() => { |
| 12 | + for (const root of roots.splice(0)) rmSync(root, { recursive: true, force: true }); |
| 13 | +}); |
| 14 | + |
| 15 | +describe("codedecay state-space CLI", () => { |
| 16 | + it("evaluates a stale-cache fixture in a child repository", async () => { |
| 17 | + const root = createRepo(); |
| 18 | + mkdirSync(join(root, "experiments"), { recursive: true }); |
| 19 | + copyFileSync(join(fixtures, "stale-cache.json"), join(root, "experiments", "stale-cache.json")); |
| 20 | + const result = await run([ |
| 21 | + "state-space", |
| 22 | + "--cwd", |
| 23 | + root, |
| 24 | + "--experiment", |
| 25 | + "experiments/stale-cache.json", |
| 26 | + "--format", |
| 27 | + "json", |
| 28 | + "--output", |
| 29 | + "reports/state-space.json" |
| 30 | + ]); |
| 31 | + const report = JSON.parse(readFileSync(join(root, "reports", "state-space.json"), "utf8")) as { |
| 32 | + verdict: string; |
| 33 | + fullyVerified: boolean; |
| 34 | + coverage: { exhaustive: boolean }; |
| 35 | + }; |
| 36 | + expect(result).toEqual({ exitCode: 0, stdout: "", stderr: "" }); |
| 37 | + expect(report.verdict).toBe("confirmed-regression"); |
| 38 | + expect(report.fullyVerified).toBe(false); |
| 39 | + expect(report.coverage.exhaustive).toBe(false); |
| 40 | + }); |
| 41 | + |
| 42 | + it("exposes state-space help", async () => { |
| 43 | + const result = await run(["state-space", "--help"]); |
| 44 | + expect(result.exitCode).toBe(0); |
| 45 | + expect(result.stdout).toContain("CodeDecay state-space"); |
| 46 | + }); |
| 47 | +}); |
| 48 | + |
| 49 | +async function run(args: string[]): Promise<{ exitCode: number; stdout: string; stderr: string }> { |
| 50 | + let stdout = ""; |
| 51 | + let stderr = ""; |
| 52 | + const exitCode = await runCli(args, { |
| 53 | + stdout: (text) => { |
| 54 | + stdout += text; |
| 55 | + }, |
| 56 | + stderr: (text) => { |
| 57 | + stderr += text; |
| 58 | + } |
| 59 | + }); |
| 60 | + return { exitCode, stdout, stderr }; |
| 61 | +} |
| 62 | + |
| 63 | +function createRepo(): string { |
| 64 | + const root = join(tmpdir(), `codedecay-state-space-cli-${Date.now()}-${Math.random().toString(16).slice(2)}`); |
| 65 | + mkdirSync(root, { recursive: true }); |
| 66 | + execFileSync("git", ["init", "-q"], { cwd: root }); |
| 67 | + roots.push(root); |
| 68 | + return root; |
| 69 | +} |
0 commit comments