CodeIssuesDiscussionsWikiPull RequestsProjectsCommitsActionsReleasesContributorsPulse● GatesSecuritySettingsDeploymentsPipelineInsightsAgents✨ Explain✨ Ask AI✨ Workspace✨ Spec✨ Tests▓ Debt Map✨ NL Search🏛 Archaeology
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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 | /**
* Block J6 — Ruleset evaluator + route-auth tests.
*
* Evaluator is pure, so most of the coverage is in this file. DB-backed CRUD
* relies on a live DB so it's covered in integration.
*/
import { describe, it, expect } from "bun:test";
import app from "../app";
import {
RULE_TYPES,
__internal,
evaluatePush,
globToRegex,
parseParams,
} from "../lib/rulesets";
describe("rulesets — globToRegex", () => {
it("handles literal paths", () => {
expect(globToRegex("README.md").test("README.md")).toBe(true);
expect(globToRegex("README.md").test("other.md")).toBe(false);
});
it("* matches one segment", () => {
expect(globToRegex("src/*.ts").test("src/app.ts")).toBe(true);
expect(globToRegex("src/*.ts").test("src/a/b.ts")).toBe(false);
});
it("** matches anything including slashes", () => {
expect(globToRegex("docs/**").test("docs/a/b/c.md")).toBe(true);
expect(globToRegex("**/secret.txt").test("a/b/secret.txt")).toBe(true);
});
it("escapes regex specials", () => {
expect(globToRegex("a+b.txt").test("a+b.txt")).toBe(true);
expect(globToRegex("a+b.txt").test("axb.txt")).toBe(false);
});
});
describe("rulesets — parseParams", () => {
it("round-trips JSON", () => {
expect(parseParams('{"pattern":"^feat:"}')).toEqual({
pattern: "^feat:",
});
});
it("returns {} on garbage", () => {
expect(parseParams("not json")).toEqual({});
expect(parseParams("")).toEqual({});
});
});
function rs(
enforcement: "active" | "evaluate" | "disabled",
rules: Array<{ ruleType: string; params: any }>
) {
return {
id: "rs-1",
repositoryId: "r",
name: "n",
enforcement,
createdBy: null,
createdAt: new Date(),
updatedAt: new Date(),
rules: rules.map((r, i) => ({
id: `rule-${i}`,
rulesetId: "rs-1",
ruleType: r.ruleType,
params: JSON.stringify(r.params),
createdAt: new Date(),
})),
} as any;
}
describe("rulesets — evaluatePush commit_message_pattern", () => {
it("require=true blocks non-matching messages under active", () => {
const result = evaluatePush(
[
rs("active", [
{
ruleType: "commit_message_pattern",
params: { pattern: "^(feat|fix|chore):" },
},
]),
],
{
kind: "push",
refType: "branch",
refName: "main",
commits: [{ sha: "abc123", message: "random junk" }],
}
);
expect(result.allowed).toBe(false);
expect(result.violations[0].ruleType).toBe("commit_message_pattern");
});
it("evaluate mode warns but allows", () => {
const result = evaluatePush(
[
rs("evaluate", [
{
ruleType: "commit_message_pattern",
params: { pattern: "^(feat|fix|chore):" },
},
]),
],
{
kind: "push",
refType: "branch",
refName: "main",
commits: [{ sha: "abc123", message: "random" }],
}
);
expect(result.allowed).toBe(true);
expect(result.violations.length).toBe(1);
expect(result.violations[0].enforcement).toBe("evaluate");
});
it("disabled mode produces zero violations", () => {
const result = evaluatePush(
[
rs("disabled", [
{
ruleType: "commit_message_pattern",
params: { pattern: "^feat:" },
},
]),
],
{
kind: "push",
refType: "branch",
refName: "main",
commits: [{ sha: "abc", message: "anything" }],
}
);
expect(result.allowed).toBe(true);
expect(result.violations.length).toBe(0);
});
it("require=false blocks when pattern matches (forbidden)", () => {
const result = evaluatePush(
[
rs("active", [
{
ruleType: "commit_message_pattern",
params: { pattern: "wip", flags: "i", require: false },
},
]),
],
{
kind: "push",
refType: "branch",
refName: "main",
commits: [{ sha: "abc", message: "WIP push" }],
}
);
expect(result.allowed).toBe(false);
});
});
describe("rulesets — evaluatePush branch / tag patterns", () => {
it("branch must match required pattern", () => {
const r = evaluatePush(
[
rs("active", [
{
ruleType: "branch_name_pattern",
params: { pattern: "^release/" },
},
]),
],
{
kind: "push",
refType: "branch",
refName: "feature/x",
commits: [],
}
);
expect(r.allowed).toBe(false);
});
it("branch rule is a no-op for tag pushes", () => {
const r = evaluatePush(
[
rs("active", [
{
ruleType: "branch_name_pattern",
params: { pattern: "^release/" },
},
]),
],
{ kind: "push", refType: "tag", refName: "v1.0", commits: [] }
);
expect(r.allowed).toBe(true);
});
it("tag must match semver-ish", () => {
const r = evaluatePush(
[
rs("active", [
{
ruleType: "tag_name_pattern",
params: { pattern: "^v\\d+\\.\\d+\\.\\d+$" },
},
]),
],
{ kind: "push", refType: "tag", refName: "v1.0", commits: [] }
);
expect(r.allowed).toBe(false);
});
});
describe("rulesets — evaluatePush blocked_file_paths", () => {
it("blocks changes to matching paths", () => {
const r = evaluatePush(
[
rs("active", [
{
ruleType: "blocked_file_paths",
params: { paths: ["secrets/**", "*.pem"] },
},
]),
],
{
kind: "push",
refType: "branch",
refName: "main",
commits: [
{ sha: "a", message: "x", changedPaths: ["secrets/db.env"] },
],
}
);
expect(r.allowed).toBe(false);
});
it("allows unrelated path changes", () => {
const r = evaluatePush(
[
rs("active", [
{
ruleType: "blocked_file_paths",
params: { paths: ["secrets/**"] },
},
]),
],
{
kind: "push",
refType: "branch",
refName: "main",
commits: [{ sha: "a", message: "x", changedPaths: ["src/app.ts"] }],
}
);
expect(r.allowed).toBe(true);
});
});
describe("rulesets — evaluatePush max_file_size + force push", () => {
it("blocks oversize blobs", () => {
const r = evaluatePush(
[
rs("active", [
{
ruleType: "max_file_size",
params: { bytes: 1024 },
},
]),
],
{
kind: "push",
refType: "branch",
refName: "main",
commits: [{ sha: "a", message: "x", maxBlobSize: 2048 }],
}
);
expect(r.allowed).toBe(false);
});
it("blocks force push when configured", () => {
const r = evaluatePush(
[rs("active", [{ ruleType: "forbid_force_push", params: {} }])],
{
kind: "push",
refType: "branch",
refName: "main",
commits: [],
forcePush: true,
}
);
expect(r.allowed).toBe(false);
});
});
describe("rulesets — RULE_TYPES surface", () => {
it("exports all rule types", () => {
expect(RULE_TYPES).toContain("commit_message_pattern");
expect(RULE_TYPES).toContain("branch_name_pattern");
expect(RULE_TYPES).toContain("tag_name_pattern");
expect(RULE_TYPES).toContain("blocked_file_paths");
expect(RULE_TYPES).toContain("max_file_size");
expect(RULE_TYPES).toContain("forbid_force_push");
expect(RULE_TYPES.length).toBe(6);
});
});
describe("rulesets — __internal evalRule edge cases", () => {
it("empty pattern is a no-op", () => {
const msgs = __internal.evalRule(
{
id: "r1",
rulesetId: "s",
ruleType: "commit_message_pattern",
params: JSON.stringify({ pattern: "" }),
createdAt: new Date(),
} as any,
{
kind: "push",
refType: "branch",
refName: "main",
commits: [{ sha: "a", message: "nothing" }],
}
);
expect(msgs.length).toBe(0);
});
it("invalid regex is a no-op", () => {
const msgs = __internal.evalRule(
{
id: "r1",
rulesetId: "s",
ruleType: "commit_message_pattern",
params: JSON.stringify({ pattern: "(" }),
createdAt: new Date(),
} as any,
{
kind: "push",
refType: "branch",
refName: "main",
commits: [{ sha: "a", message: "x" }],
}
);
expect(msgs.length).toBe(0);
});
});
describe("rulesets — route auth", () => {
it("GET /:o/:r/settings/rulesets without auth → 302 /login", async () => {
const res = await app.request("/alice/repo/settings/rulesets");
expect(res.status).toBe(302);
expect(res.headers.get("location") || "").toContain("/login");
});
it("POST create without auth → 302 /login", async () => {
const res = await app.request("/alice/repo/settings/rulesets", {
method: "POST",
});
expect(res.status).toBe(302);
expect(res.headers.get("location") || "").toContain("/login");
});
it("POST delete without auth → 302 /login", async () => {
const res = await app.request(
"/alice/repo/settings/rulesets/00000000-0000-0000-0000-000000000000/delete",
{ method: "POST" }
);
expect(res.status).toBe(302);
expect(res.headers.get("location") || "").toContain("/login");
});
});
|