1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
import { describe, it, expect } from "bun:test";
import app from "../app";
import { evaluateProtection } from "../lib/branch-protection";
import type { BranchProtection } from "../db/schema";
function rule(overrides: Partial<BranchProtection> = {}): BranchProtection {
return {
id: "rule-1",
repositoryId: "repo-1",
pattern: "main",
requirePullRequest: true,
requireGreenGates: true,
requireAiApproval: false,
requireHumanReview: false,
requiredApprovals: 0,
allowForcePush: false,
allowDeletion: false,
createdAt: new Date(),
updatedAt: new Date(),
...overrides,
} as BranchProtection;
}
describe("evaluateProtection — required checks matrix", () => {
const green = {
aiApproved: true,
humanApprovalCount: 1,
gateResultGreen: true,
hasFailedGates: false,
};
it("allows when no required checks are configured", () => {
const d = evaluateProtection(rule(), green, []);
expect(d.allowed).toBe(true);
expect(d.reasons.length).toBe(0);
expect(d.missingChecks).toBeUndefined();
});
it("allows when all required checks are in passingCheckNames", () => {
const d = evaluateProtection(
rule(),
{ ...green, passingCheckNames: ["GateTest", "AI Review", "CI"] },
["GateTest", "AI Review"]
);
expect(d.allowed).toBe(true);
expect(d.missingChecks).toBeUndefined();
});
it("blocks when a required check is missing", () => {
const d = evaluateProtection(
rule(),
{ ...green, passingCheckNames: ["GateTest"] },
["GateTest", "AI Review"]
);
expect(d.allowed).toBe(false);
expect(d.missingChecks).toEqual(["AI Review"]);
expect(d.reasons.join(" ")).toContain("AI Review");
});
it("blocks when passingCheckNames is empty but checks are required", () => {
const d = evaluateProtection(
rule(),
{ ...green, passingCheckNames: [] },
["GateTest"]
);
expect(d.allowed).toBe(false);
expect(d.missingChecks).toEqual(["GateTest"]);
});
it("still reports other failures alongside missing checks", () => {
const d = evaluateProtection(
rule({ requireAiApproval: true, requiredApprovals: 2 }),
{
aiApproved: false,
humanApprovalCount: 0,
gateResultGreen: true,
hasFailedGates: false,
passingCheckNames: [],
},
["CI"]
);
expect(d.allowed).toBe(false);
expect(d.reasons.length).toBeGreaterThanOrEqual(3);
expect(d.missingChecks).toEqual(["CI"]);
});
});
describe("required-checks — route smoke", () => {
it("GET protection/:id/checks without auth → 302 /login", async () => {
const res = await app.request(
"/any/repo/gates/protection/abc/checks"
);
expect(res.status).toBe(302);
const loc = res.headers.get("location") || "";
expect(loc.startsWith("/login")).toBe(true);
});
it("POST protection/:id/checks without auth → 302 /login", async () => {
const res = await app.request(
"/any/repo/gates/protection/abc/checks",
{
method: "POST",
body: new URLSearchParams({ checkName: "CI" }),
headers: { "content-type": "application/x-www-form-urlencoded" },
}
);
expect(res.status).toBe(302);
const loc = res.headers.get("location") || "";
expect(loc.startsWith("/login")).toBe(true);
});
it("POST protection/:id/checks/:cid/delete without auth → 302 /login", async () => {
const res = await app.request(
"/any/repo/gates/protection/abc/checks/xyz/delete",
{ method: "POST" }
);
expect(res.status).toBe(302);
const loc = res.headers.get("location") || "";
expect(loc.startsWith("/login")).toBe(true);
});
});
|