|
| 1 | +import { mkdtempSync, writeFileSync } from "node:fs"; |
| 2 | +import { tmpdir } from "node:os"; |
| 3 | +import { join } from "node:path"; |
| 4 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 5 | +import { runDenyHooks } from "../../packages/loopover-miner/lib/deny-hooks-cli.js"; |
| 6 | +import { initDenyHookSynthesisStore } from "../../packages/loopover-miner/lib/deny-hook-synthesis.js"; |
| 7 | +import { resolveAttemptHouseRulesConfig } from "../../packages/loopover-miner/lib/attempt-cli.js"; |
| 8 | + |
| 9 | +// #8806: the operate half of the deny-hook loop — refresh (explicit --history file) → approve → the |
| 10 | +// attempt-side resolver picks the approved rule up. The end-to-end test below is the loop the audit found |
| 11 | +// severed: pre-#8806 nothing invoked refreshProposals and nothing read resolveEffectiveRules. |
| 12 | +describe("loopover-miner deny-hooks (#8806)", () => { |
| 13 | + let configDir: string; |
| 14 | + const savedEnv = { configDir: process.env.LOOPOVER_MINER_CONFIG_DIR, dbOverride: process.env.LOOPOVER_MINER_DENY_HOOK_SYNTHESIS_DB }; |
| 15 | + |
| 16 | + beforeEach(() => { |
| 17 | + configDir = mkdtempSync(join(tmpdir(), "miner-deny-hooks-cli-")); |
| 18 | + process.env.LOOPOVER_MINER_CONFIG_DIR = configDir; |
| 19 | + delete process.env.LOOPOVER_MINER_DENY_HOOK_SYNTHESIS_DB; |
| 20 | + }); |
| 21 | + afterEach(() => { |
| 22 | + if (savedEnv.configDir === undefined) delete process.env.LOOPOVER_MINER_CONFIG_DIR; |
| 23 | + else process.env.LOOPOVER_MINER_CONFIG_DIR = savedEnv.configDir; |
| 24 | + if (savedEnv.dbOverride === undefined) delete process.env.LOOPOVER_MINER_DENY_HOOK_SYNTHESIS_DB; |
| 25 | + else process.env.LOOPOVER_MINER_DENY_HOOK_SYNTHESIS_DB = savedEnv.dbOverride; |
| 26 | + vi.restoreAllMocks(); |
| 27 | + }); |
| 28 | + |
| 29 | + function writeHistory(): string { |
| 30 | + const path = join(configDir, "history.json"); |
| 31 | + writeFileSync( |
| 32 | + path, |
| 33 | + JSON.stringify([ |
| 34 | + { blockerCodes: ["guardrail_hold"], changedPaths: ["CHANGELOG.md"] }, |
| 35 | + { blockerCodes: ["guardrail_hold"], changedPaths: ["CHANGELOG.md"] }, |
| 36 | + ]), |
| 37 | + ); |
| 38 | + return path; |
| 39 | + } |
| 40 | + |
| 41 | + it("END-TO-END: refresh --history → approve → the attempt-side resolver enforces the approved rule", () => { |
| 42 | + const log = vi.spyOn(console, "log").mockImplementation(() => undefined); |
| 43 | + expect(runDenyHooks(["refresh", "acme/widgets", "--history", writeHistory(), "--json"])).toBe(0); |
| 44 | + const { proposals } = JSON.parse(String(log.mock.calls.at(-1)?.[0])) as { proposals: Array<{ id: string }> }; |
| 45 | + expect(proposals.length).toBeGreaterThan(0); |
| 46 | + |
| 47 | + expect(runDenyHooks(["approve", "acme/widgets", proposals[0]!.id])).toBe(0); |
| 48 | + |
| 49 | + // The enforce half: buildAttemptDeps' resolver (same default store path) now includes the approved rule. |
| 50 | + const baseline = resolveAttemptHouseRulesConfig("other/repo"); |
| 51 | + const withApproved = resolveAttemptHouseRulesConfig("acme/widgets"); |
| 52 | + expect(withApproved?.rules.length).toBe((baseline?.rules.length ?? 0) + 1); |
| 53 | + }); |
| 54 | + |
| 55 | + it("list renders proposals with status and the effective-rule count", () => { |
| 56 | + const log = vi.spyOn(console, "log").mockImplementation(() => undefined); |
| 57 | + runDenyHooks(["refresh", "acme/widgets", "--history", writeHistory()]); |
| 58 | + expect(runDenyHooks(["list", "acme/widgets", "--json"])).toBe(0); |
| 59 | + const payload = JSON.parse(String(log.mock.calls.at(-1)?.[0])) as { proposals: unknown[]; effectiveRuleCount: number }; |
| 60 | + expect(payload.proposals.length).toBeGreaterThan(0); |
| 61 | + expect(payload.effectiveRuleCount).toBeGreaterThan(0); |
| 62 | + // Human output too (both list arms + the empty-repo arm). |
| 63 | + expect(runDenyHooks(["list", "acme/widgets"])).toBe(0); |
| 64 | + expect(runDenyHooks(["list", "empty/repo"])).toBe(0); |
| 65 | + }); |
| 66 | + |
| 67 | + it("reject marks a proposal rejected — it never reaches the effective rules", () => { |
| 68 | + const log = vi.spyOn(console, "log").mockImplementation(() => undefined); |
| 69 | + runDenyHooks(["refresh", "acme/widgets", "--history", writeHistory(), "--json"]); |
| 70 | + const { proposals } = JSON.parse(String(log.mock.calls.at(-1)?.[0])) as { proposals: Array<{ id: string }> }; |
| 71 | + expect(runDenyHooks(["reject", "acme/widgets", proposals[0]!.id])).toBe(0); |
| 72 | + const store = initDenyHookSynthesisStore(); |
| 73 | + try { |
| 74 | + const baselineCount = store.resolveEffectiveRules("other/repo").length; |
| 75 | + expect(store.resolveEffectiveRules("acme/widgets").length).toBe(baselineCount); // defaults only |
| 76 | + } finally { |
| 77 | + store.close(); |
| 78 | + } |
| 79 | + }); |
| 80 | + |
| 81 | + it("usage/argument errors exit 2; a bad history file exits 1 with the parse error", () => { |
| 82 | + const error = vi.spyOn(console, "error").mockImplementation(() => undefined); |
| 83 | + vi.spyOn(console, "log").mockImplementation(() => undefined); |
| 84 | + expect(runDenyHooks([])).toBe(2); // no subcommand |
| 85 | + expect(runDenyHooks(["list"])).toBe(2); // no repo |
| 86 | + expect(runDenyHooks(["refresh", "acme/widgets"])).toBe(2); // no --history |
| 87 | + expect(runDenyHooks(["approve", "acme/widgets"])).toBe(2); // no proposal id |
| 88 | + expect(runDenyHooks(["bogus", "acme/widgets"])).toBe(2); // unknown subcommand |
| 89 | + const badPath = join(configDir, "bad.json"); |
| 90 | + writeFileSync(badPath, JSON.stringify({ not: "an array" })); |
| 91 | + expect(runDenyHooks(["refresh", "acme/widgets", "--history", badPath])).toBe(1); |
| 92 | + expect(error).toHaveBeenCalledWith(expect.stringContaining("JSON array")); |
| 93 | + }); |
| 94 | +}); |
0 commit comments