|
| 1 | +import { describe, it } from "node:test"; |
| 2 | +import assert from "node:assert/strict"; |
| 3 | +import { parseAxmeGate, checkGit } from "../src/storage/safety.js"; |
| 4 | +import type { SafetyRules } from "../src/storage/safety.js"; |
| 5 | + |
| 6 | +const defaultRules: SafetyRules = { |
| 7 | + git: { protectedBranches: ["main", "master"], allowForcePush: false, allowDirectPushToMain: false }, |
| 8 | + bash: { deniedPrefixes: [], deniedCommands: [] }, |
| 9 | + filesystem: { deniedPaths: [] }, |
| 10 | +}; |
| 11 | + |
| 12 | +describe("parseAxmeGate", () => { |
| 13 | + it("parses valid gate metadata", () => { |
| 14 | + const result = parseAxmeGate('git commit -m "fix" #!axme pr=37 repo=AxmeAI/axme-code'); |
| 15 | + assert.deepEqual(result, { pr: "37", repo: "AxmeAI/axme-code" }); |
| 16 | + }); |
| 17 | + |
| 18 | + it("parses pr=none", () => { |
| 19 | + const result = parseAxmeGate('git commit -m "init" #!axme pr=none repo=AxmeAI/axme-code'); |
| 20 | + assert.deepEqual(result, { pr: "none", repo: "AxmeAI/axme-code" }); |
| 21 | + }); |
| 22 | + |
| 23 | + it("returns null for no gate marker", () => { |
| 24 | + assert.equal(parseAxmeGate('git commit -m "fix"'), null); |
| 25 | + }); |
| 26 | + |
| 27 | + it("returns null for incomplete gate (missing repo)", () => { |
| 28 | + assert.equal(parseAxmeGate('git commit -m "fix" #!axme pr=37'), null); |
| 29 | + }); |
| 30 | + |
| 31 | + it("returns null for incomplete gate (missing pr)", () => { |
| 32 | + assert.equal(parseAxmeGate('git commit -m "fix" #!axme repo=AxmeAI/axme-code'), null); |
| 33 | + }); |
| 34 | + |
| 35 | + it("works with git push", () => { |
| 36 | + const result = parseAxmeGate('git push -u origin feat/xxx #!axme pr=42 repo=AxmeAI/axme-cli'); |
| 37 | + assert.deepEqual(result, { pr: "42", repo: "AxmeAI/axme-cli" }); |
| 38 | + }); |
| 39 | + |
| 40 | + it("works with git -C path", () => { |
| 41 | + const result = parseAxmeGate('git -C /path/to/repo commit -m "msg" #!axme pr=10 repo=AxmeAI/test'); |
| 42 | + assert.deepEqual(result, { pr: "10", repo: "AxmeAI/test" }); |
| 43 | + }); |
| 44 | +}); |
| 45 | + |
| 46 | +describe("checkGit with axme gate", () => { |
| 47 | + it("blocks git commit without #!axme gate", () => { |
| 48 | + const v = checkGit(defaultRules, 'git commit -m "test"'); |
| 49 | + assert.equal(v.allowed, false); |
| 50 | + assert.ok(v.reason!.includes("#!axme")); |
| 51 | + assert.ok(v.reason!.includes("BLOCKED")); |
| 52 | + }); |
| 53 | + |
| 54 | + it("blocks git push without #!axme gate", () => { |
| 55 | + const v = checkGit(defaultRules, "git push origin feat/xxx"); |
| 56 | + assert.equal(v.allowed, false); |
| 57 | + assert.ok(v.reason!.includes("#!axme")); |
| 58 | + }); |
| 59 | + |
| 60 | + it("allows git commit with pr=none", () => { |
| 61 | + const v = checkGit(defaultRules, 'git commit -m "init" #!axme pr=none repo=AxmeAI/axme-code'); |
| 62 | + assert.equal(v.allowed, true); |
| 63 | + }); |
| 64 | + |
| 65 | + it("allows git push with pr=none", () => { |
| 66 | + const v = checkGit(defaultRules, 'git push -u origin feat/xxx #!axme pr=none repo=AxmeAI/axme-code'); |
| 67 | + assert.equal(v.allowed, true); |
| 68 | + }); |
| 69 | + |
| 70 | + it("blocks invalid PR number", () => { |
| 71 | + const v = checkGit(defaultRules, 'git commit -m "x" #!axme pr=abc repo=AxmeAI/axme-code'); |
| 72 | + assert.equal(v.allowed, false); |
| 73 | + assert.ok(v.reason!.includes("Invalid PR number")); |
| 74 | + }); |
| 75 | + |
| 76 | + it("does not require gate on git add", () => { |
| 77 | + const v = checkGit(defaultRules, "git add src/file.ts"); |
| 78 | + assert.equal(v.allowed, true); |
| 79 | + }); |
| 80 | + |
| 81 | + it("does not require gate on git status", () => { |
| 82 | + const v = checkGit(defaultRules, "git status"); |
| 83 | + assert.equal(v.allowed, true); |
| 84 | + }); |
| 85 | + |
| 86 | + it("does not require gate on git branch", () => { |
| 87 | + const v = checkGit(defaultRules, "git branch --show-current"); |
| 88 | + assert.equal(v.allowed, true); |
| 89 | + }); |
| 90 | + |
| 91 | + it("does not require gate on git diff", () => { |
| 92 | + const v = checkGit(defaultRules, "git diff --stat"); |
| 93 | + assert.equal(v.allowed, true); |
| 94 | + }); |
| 95 | + |
| 96 | + it("does not require gate on git log", () => { |
| 97 | + const v = checkGit(defaultRules, "git log --oneline -5"); |
| 98 | + assert.equal(v.allowed, true); |
| 99 | + }); |
| 100 | + |
| 101 | + it("skips gate check when skipMergedCheck is true", () => { |
| 102 | + const v = checkGit(defaultRules, 'git commit -m "x"', undefined, true); |
| 103 | + assert.equal(v.allowed, true); |
| 104 | + }); |
| 105 | + |
| 106 | + it("still blocks force push even with valid gate", () => { |
| 107 | + const v = checkGit(defaultRules, 'git push --force origin main #!axme pr=none repo=AxmeAI/axme-code'); |
| 108 | + assert.equal(v.allowed, false); |
| 109 | + assert.ok(v.reason!.includes("Force push")); |
| 110 | + }); |
| 111 | + |
| 112 | + it("still blocks direct push to main even with valid gate", () => { |
| 113 | + const v = checkGit(defaultRules, 'git push origin main #!axme pr=none repo=AxmeAI/axme-code'); |
| 114 | + assert.equal(v.allowed, false); |
| 115 | + assert.ok(v.reason!.includes("Direct push to main")); |
| 116 | + }); |
| 117 | + |
| 118 | + it("works with git -C prefix", () => { |
| 119 | + const v = checkGit(defaultRules, 'git -C /home/user/repo commit -m "fix" #!axme pr=none repo=AxmeAI/test'); |
| 120 | + assert.equal(v.allowed, true); |
| 121 | + }); |
| 122 | +}); |
0 commit comments