diff --git a/scripts/check-import-graph.mjs b/scripts/check-import-graph.mjs index 8a9652bf..11f432b4 100644 --- a/scripts/check-import-graph.mjs +++ b/scripts/check-import-graph.mjs @@ -85,6 +85,39 @@ try { process.exit(2); } +if ( + typeof report !== "object" || + report === null || + typeof report.summary !== "object" || + report.summary === null || + !Number.isInteger(report.summary.totalCruised) || + report.summary.totalCruised < 0 || + !Number.isInteger(report.summary.totalDependenciesCruised) || + report.summary.totalDependenciesCruised < 0 || + !Array.isArray(report.summary.violations) || + report.summary.violations.some( + (violation) => + typeof violation !== "object" || + violation === null || + typeof violation.from !== "string" || + (violation.to !== undefined && typeof violation.to !== "string") || + typeof violation.rule !== "object" || + violation.rule === null || + typeof violation.rule.name !== "string" || + typeof violation.rule.severity !== "string" || + (violation.cycle !== undefined && + (!Array.isArray(violation.cycle) || + violation.cycle.some( + (segment) => + typeof segment !== "string" && + (typeof segment !== "object" || segment === null || typeof segment.name !== "string"), + ))), + ) +) { + console.error("import-graph check INCONCLUSIVE: dependency-cruiser report has no valid summary"); + process.exit(2); +} + const { totalCruised, totalDependenciesCruised, violations } = report.summary; // -------------------------------------------------------------------------- diff --git a/scripts/check-stateless.mjs b/scripts/check-stateless.mjs index b43d4fb0..05126ab5 100644 --- a/scripts/check-stateless.mjs +++ b/scripts/check-stateless.mjs @@ -64,11 +64,18 @@ if (findings.length > 0) { process.exit(1); } +const vitest = join(root, "node_modules/.bin/vitest"); +const statelessProbe = join(root, "test/stateless.test.ts"); +if (!existsSync(vitest)) inconclusive("vitest is not installed; run npm ci"); +if (!existsSync(statelessProbe)) inconclusive("test/stateless.test.ts is missing"); + const probe = spawnSync( - join(root, "node_modules/.bin/vitest"), + vitest, ["run", "test/stateless.test.ts", "--reporter=dot"], { cwd: root, encoding: "utf8", stdio: "inherit" }, ); +if (probe.error) inconclusive(`could not run the stateless probe: ${probe.error.message}`); +if (probe.signal) inconclusive(`stateless probe terminated by signal ${probe.signal}`); if (probe.status !== 0) process.exit(probe.status ?? 1); const packageJson = JSON.parse(readFileSync(join(root, "package.json"), "utf8")); diff --git a/test/check-import-graph.test.ts b/test/check-import-graph.test.ts index 8908c011..73de09e5 100644 --- a/test/check-import-graph.test.ts +++ b/test/check-import-graph.test.ts @@ -24,7 +24,7 @@ const script = join(repoRoot, "scripts", "check-import-graph.mjs"); /** Run the gate against a fixture root, with an optional floor override. */ function runAgainst(root: string, minModules?: number) { - const env: Record = { ...process.env, IMPORT_GRAPH_ROOT: root }; + const env: NodeJS.ProcessEnv = { ...process.env, IMPORT_GRAPH_ROOT: root }; if (minModules !== undefined) env.IMPORT_GRAPH_MIN_MODULES = String(minModules); return spawnSync("node", [script], { encoding: "utf8", env }); } @@ -129,6 +129,33 @@ describe("check-import-graph gate", () => { expect(run.stderr).toContain("could not parse"); }); + it.each([ + "{}", + "null", + '{"summary":null}', + '{"summary":{"totalCruised":"2","totalDependenciesCruised":1,"violations":[]}}', + '{"summary":{"totalCruised":2,"totalDependenciesCruised":-1,"violations":[]}}', + '{"summary":{"totalCruised":2,"totalDependenciesCruised":1,"violations":[{}]}}', + ])( + "is INCONCLUSIVE (exit 2) when dependency-cruiser returns malformed report %s", + (output) => { + writeCleanFixture(); + rmSync(join(root, "node_modules", ".bin"), { recursive: true, force: true }); + mkdirSync(join(root, "node_modules", ".bin"), { recursive: true }); + const fake = join(root, "node_modules", ".bin", "depcruise"); + writeFileSync(fake, `#!/bin/sh\nprintf '%s\\n' '${output}'\n`); + chmodSync(fake, 0o755); + + const run = runAgainst(root, 2); + + expect(run.status).toBe(2); + expect(run.stderr).toContain( + "import-graph check INCONCLUSIVE: dependency-cruiser report has no valid summary", + ); + expect(run.stderr).not.toContain("TypeError"); + }, + ); + // --- one mutant per rule ------------------------------------------------- it("FAILS (exit 1) naming src-no-node-builtins on a Node builtin import (FC-3)", () => { diff --git a/test/check-stateless.test.ts b/test/check-stateless.test.ts new file mode 100644 index 00000000..fd1a964c --- /dev/null +++ b/test/check-stateless.test.ts @@ -0,0 +1,104 @@ +import { spawnSync } from "node:child_process"; +import { chmodSync, copyFileSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { dirname, join } from "node:path"; +import { fileURLToPath } from "node:url"; +import { afterEach, beforeEach, describe, expect, it } from "vitest"; + +const repoRoot = dirname(dirname(fileURLToPath(import.meta.url))); +const script = join(repoRoot, "scripts", "check-stateless.mjs"); + +function executable(path: string, body: string) { + writeFileSync(path, `#!/bin/sh\n${body}\n`); + chmodSync(path, 0o755); +} + +describe("check-stateless gate", () => { + let root: string; + let bin: string; + + beforeEach(() => { + root = mkdtempSync(join(tmpdir(), "stateless-check-")); + bin = join(root, "node_modules", ".bin"); + mkdirSync(bin, { recursive: true }); + mkdirSync(join(root, "src")); + mkdirSync(join(root, "test")); + mkdirSync(join(root, "scripts")); + copyFileSync(script, join(root, "scripts", "check-stateless.mjs")); + mkdirSync(join(root, ".agents", "checks"), { recursive: true }); + for (const dependency of ["eslint", "@typescript-eslint/parser", "eslint-plugin-sonarjs"]) { + mkdirSync(join(root, "node_modules", dependency), { recursive: true }); + } + writeFileSync(join(root, "src", "index.ts"), "export const value = 1;\n"); + writeFileSync(join(root, "test", "stateless.test.ts"), "// fixture probe\n"); + writeFileSync(join(root, ".agents", "checks", "eslint.strict.config.js"), "export default [];\n"); + writeFileSync(join(root, "package.json"), JSON.stringify({ name: "stateless-fixture" })); + executable( + join(bin, "eslint"), + `printf '%s\\n' '[{"filePath":"${join(root, "src", "index.ts")}","messages":[]}]'`, + ); + executable(join(bin, "vitest"), "exit 0"); + }); + + afterEach(() => rmSync(root, { recursive: true, force: true })); + + function run() { + return spawnSync("node", [join(root, "scripts", "check-stateless.mjs")], { + encoding: "utf8", + }); + } + + it("passes only after lint and the process probe both succeed", () => { + const result = run(); + expect(result.status).toBe(0); + expect(result.stdout).toContain( + "No issues found (1 source files linted; stateless-fixture stateless probe passed)", + ); + }); + + it("returns exit 2 when the vitest executable is missing", () => { + rmSync(join(bin, "vitest")); + const result = run(); + expect(result.status).toBe(2); + expect(result.stderr).toContain("INCONCLUSIVE — vitest is not installed; run npm ci"); + }); + + it("returns exit 2 when the stateless probe is missing", () => { + rmSync(join(root, "test", "stateless.test.ts")); + const result = run(); + expect(result.status).toBe(2); + expect(result.stderr).toContain("INCONCLUSIVE — test/stateless.test.ts is missing"); + }); + + it("returns exit 2 when spawning the stateless probe fails", () => { + writeFileSync(join(bin, "vitest"), "#!/definitely/missing/interpreter\n"); + chmodSync(join(bin, "vitest"), 0o755); + const result = run(); + expect(result.status).toBe(2); + expect(result.stderr).toContain("INCONCLUSIVE — could not run the stateless probe: spawnSync"); + }); + + it("returns exit 2 when the stateless probe is terminated by a signal", () => { + executable(join(bin, "vitest"), "kill -TERM $$"); + const result = run(); + expect(result.status).toBe(2); + expect(result.stderr).toContain("INCONCLUSIVE — stateless probe terminated by signal SIGTERM"); + }); + + it("preserves a genuine lint finding as exit 1", () => { + executable( + join(bin, "eslint"), + `printf '%s\\n' '[{"filePath":"${join(root, "src", "index.ts")}","messages":[{"ruleId":"no-warning-comments","message":"finding"}]}]'`, + ); + const result = run(); + expect(result.status).toBe(1); + expect(result.stderr).toContain('"message": "finding"'); + }); + + it("preserves a failing stateless probe as exit 1", () => { + executable(join(bin, "vitest"), "exit 1"); + const result = run(); + expect(result.status).toBe(1); + expect(result.stdout).not.toContain("No issues found"); + }); +});