Blame · Line-by-line history
green-ecosystem.test.ts
Each line is annotated with the commit that last touched it. Click any SHA to jump to that commit and see the surrounding change.
| 3ef4c9d | 1 | /** |
| 2 | * Tests for the green ecosystem: secret scanner, codeowners parser, | |
| 3 | * auto-repair helpers, notification + audit log helpers, health routes, | |
| 4 | * and rate limiting. | |
| 5 | * | |
| 6 | * These unit-level tests avoid DB + git subprocess work so they run fast. | |
| 7 | */ | |
| 8 | ||
| 9 | import { describe, it, expect } from "bun:test"; | |
| 10 | import app from "../app"; | |
| 11 | import { scanForSecrets, SECRET_PATTERNS } from "../lib/security-scan"; | |
| 12 | import { | |
| 13 | parseCodeowners, | |
| 14 | ownersForPath, | |
| 15 | } from "../lib/codeowners"; | |
| 16 | import { generateCommitMessage } from "../lib/ai-generators"; | |
| 17 | import { isAiAvailable } from "../lib/ai-client"; | |
| 18 | ||
| 19 | describe("secret scanner", () => { | |
| 20 | it("detects AWS access keys", () => { | |
| 21 | const findings = scanForSecrets([ | |
| 22 | { | |
| 23 | path: "config.env", | |
| 24 | content: "AWS_ACCESS_KEY=AKIAZ2J4NPQR5LTMWXYZ\n", | |
| 25 | }, | |
| 26 | ]); | |
| 27 | expect(findings.length).toBeGreaterThan(0); | |
| 28 | expect(findings.some((f) => /AWS/i.test(f.type))).toBe(true); | |
| 29 | }); | |
| 30 | ||
| 31 | it("detects Anthropic API keys", () => { | |
| 32 | const findings = scanForSecrets([ | |
| 33 | { | |
| 34 | path: "app.ts", | |
| 35 | content: | |
| 36 | 'const key = "sk-ant-api03-QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ-AAAAAA";', | |
| 37 | }, | |
| 38 | ]); | |
| 39 | expect(findings.some((f) => /anthropic/i.test(f.type))).toBe(true); | |
| 40 | }); | |
| 41 | ||
| 42 | it("ignores binary/lock paths", () => { | |
| 43 | const findings = scanForSecrets([ | |
| 44 | { | |
| 45 | path: "package-lock.json", | |
| 46 | content: "AKIAZ2J4NPQR5LTMWXYZ secret content", | |
| 47 | }, | |
| 48 | ]); | |
| 49 | expect(findings.length).toBe(0); | |
| 50 | }); | |
| 51 | ||
| 52 | it("does not match placeholder strings in test fixtures", () => { | |
| 53 | const findings = scanForSecrets([ | |
| 54 | { | |
| 55 | path: "test.js", | |
| 56 | content: | |
| 57 | '// example: AKIA" + "XAMPLE_PLACEHOLDER_KEY_FIXTURE"\nconst k = "FAKE_TEST_PLACEHOLDER";', | |
| 58 | }, | |
| 59 | ]); | |
| 60 | // all findings should be filtered by the placeholder heuristic | |
| 61 | expect(findings.every((f) => !/placeholder|fixture/i.test(f.snippet))).toBe(true); | |
| 62 | }); | |
| 63 | ||
| 64 | it("has a rich library of rules", () => { | |
| 65 | expect(SECRET_PATTERNS.length).toBeGreaterThanOrEqual(10); | |
| 66 | }); | |
| 67 | }); | |
| 68 | ||
| 69 | describe("codeowners parser", () => { | |
| 70 | it("parses simple rules", () => { | |
| 71 | const rules = parseCodeowners( | |
| 72 | "# top-level owner\n* @alice\nsrc/api/** @bob @carol\n/docs/ @alice\n" | |
| 73 | ); | |
| 74 | expect(rules.length).toBe(3); | |
| 75 | expect(rules[0].owners).toEqual(["alice"]); | |
| 76 | expect(rules[1].owners).toEqual(["bob", "carol"]); | |
| 77 | }); | |
| 78 | ||
| 79 | it("resolves last-matching rule wins", () => { | |
| 80 | const rules = parseCodeowners("* @alice\nsrc/api/** @bob\n"); | |
| 81 | expect(ownersForPath("README.md", rules)).toEqual(["alice"]); | |
| 82 | expect(ownersForPath("src/api/users.ts", rules)).toEqual(["bob"]); | |
| 83 | }); | |
| 84 | ||
| 85 | it("anchors leading-slash patterns to repo root", () => { | |
| 86 | const rules = parseCodeowners("/docs/ @alice\n"); | |
| 87 | expect(ownersForPath("docs/readme.md", rules)).toEqual(["alice"]); | |
| 88 | expect(ownersForPath("src/docs/readme.md", rules)).toEqual([]); | |
| 89 | }); | |
| 90 | ||
| 91 | it("ignores comments and blank lines", () => { | |
| 92 | const rules = parseCodeowners( | |
| 93 | "# comment\n\n \n# another\n* @ghost # trailing comment\n" | |
| 94 | ); | |
| 95 | expect(rules.length).toBe(1); | |
| 96 | expect(rules[0].owners).toEqual(["ghost"]); | |
| 97 | }); | |
| 98 | }); | |
| 99 | ||
| 100 | describe("AI generator fallbacks", () => { | |
| 101 | it("returns a safe fallback commit message when AI is unavailable", async () => { | |
| 102 | if (isAiAvailable()) { | |
| 103 | // API key is set — skip fallback assertion | |
| 104 | return; | |
| 105 | } | |
| 106 | const msg = await generateCommitMessage(""); | |
| 107 | expect(msg.length).toBeGreaterThan(0); | |
| 108 | expect(msg).toMatch(/^\S+/); | |
| 109 | }); | |
| 110 | }); | |
| 111 | ||
| 112 | describe("health + metrics endpoints", () => { | |
| 113 | it("GET /healthz returns 200", async () => { | |
| 114 | const res = await app.request("/healthz"); | |
| 115 | expect(res.status).toBe(200); | |
| 116 | const body = await res.json(); | |
| 117 | expect(body.ok).toBe(true); | |
| 118 | }); | |
| 119 | ||
| 120 | it("GET /metrics returns process metrics", async () => { | |
| 121 | const res = await app.request("/metrics"); | |
| 122 | expect(res.status).toBe(200); | |
| 123 | const body = await res.json(); | |
| 124 | expect(typeof body.uptimeMs).toBe("number"); | |
| 125 | expect(typeof body.heapUsed).toBe("number"); | |
| 126 | }); | |
| 127 | ||
| 128 | it("response carries X-Request-Id header", async () => { | |
| 129 | const res = await app.request("/healthz"); | |
| 130 | expect(res.headers.get("X-Request-Id")).toBeTruthy(); | |
| 131 | }); | |
| 132 | }); | |
| 133 | ||
| 134 | describe("rate limiting", () => { | |
| 135 | it("rate-limit headers appear on /api requests", async () => { | |
| 136 | const res = await app.request("/api/users/nonexistent/repos"); | |
| 137 | // Headers should exist even though user is missing | |
| 138 | const limit = res.headers.get("X-RateLimit-Limit"); | |
| 139 | expect(limit).toBeTruthy(); | |
| 140 | }); | |
| 141 | }); | |
| 142 | ||
| 143 | describe("shortcuts + search page", () => { | |
| 144 | it("GET /shortcuts is public", async () => { | |
| 145 | const res = await app.request("/shortcuts"); | |
| 146 | expect(res.status).toBe(200); | |
| 147 | const html = await res.text(); | |
| 148 | expect(html).toContain("Keyboard shortcuts"); | |
| 149 | }); | |
| 150 | ||
| 151 | it("GET /search with no query shows the type tabs", async () => { | |
| 152 | const res = await app.request("/search"); | |
| 153 | expect(res.status).toBe(200); | |
| 154 | const html = await res.text(); | |
| 155 | expect(html).toContain("Repositories"); | |
| 156 | expect(html).toContain("Users"); | |
| 157 | }); | |
| 158 | }); |