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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
|
import { describe, it, expect } from "bun:test";
import app from "../app";
import { scanForSecrets, SECRET_PATTERNS } from "../lib/security-scan";
import {
parseCodeowners,
ownersForPath,
} from "../lib/codeowners";
import { generateCommitMessage } from "../lib/ai-generators";
import { isAiAvailable } from "../lib/ai-client";
import {
isAllowedEmoji,
isAllowedTarget,
ALLOWED_EMOJIS,
EMOJI_GLYPH,
} from "../lib/reactions";
import { sendEmail, absoluteUrl } from "../lib/email";
import { __internal as notifyInternal } from "../lib/notify";
describe("secret scanner", () => {
it("detects AWS access keys", () => {
const findings = scanForSecrets([
{
path: "config.env",
content: "AWS_ACCESS_KEY=AKIAZ2J4NPQR5LTMWXYZ\n",
},
]);
expect(findings.length).toBeGreaterThan(0);
expect(findings.some((f) => /AWS/i.test(f.type))).toBe(true);
});
it("detects Anthropic API keys", () => {
const findings = scanForSecrets([
{
path: "app.ts",
content:
'const key = "sk-ant-api03-QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ-AAAAAA";',
},
]);
expect(findings.some((f) => /anthropic/i.test(f.type))).toBe(true);
});
it("ignores binary/lock paths", () => {
const findings = scanForSecrets([
{
path: "package-lock.json",
content: "AKIAZ2J4NPQR5LTMWXYZ secret content",
},
]);
expect(findings.length).toBe(0);
});
it("does not match placeholder strings in test fixtures", () => {
const findings = scanForSecrets([
{
path: "test.js",
content:
'// example: AKIA" + "XAMPLE_PLACEHOLDER_KEY_FIXTURE"\nconst k = "FAKE_TEST_PLACEHOLDER";',
},
]);
expect(findings.every((f) => !/placeholder|fixture/i.test(f.snippet))).toBe(true);
});
it("has a rich library of rules", () => {
expect(SECRET_PATTERNS.length).toBeGreaterThanOrEqual(10);
});
});
describe("codeowners parser", () => {
it("parses simple rules", () => {
const rules = parseCodeowners(
"# top-level owner\n* @alice\nsrc/api/** @bob @carol\n/docs/ @alice\n"
);
expect(rules.length).toBe(3);
expect(rules[0].owners).toEqual(["alice"]);
expect(rules[1].owners).toEqual(["bob", "carol"]);
});
it("resolves last-matching rule wins", () => {
const rules = parseCodeowners("* @alice\nsrc/api/** @bob\n");
expect(ownersForPath("README.md", rules)).toEqual(["alice"]);
expect(ownersForPath("src/api/users.ts", rules)).toEqual(["bob"]);
});
it("anchors leading-slash patterns to repo root", () => {
const rules = parseCodeowners("/docs/ @alice\n");
expect(ownersForPath("docs/readme.md", rules)).toEqual(["alice"]);
expect(ownersForPath("src/docs/readme.md", rules)).toEqual([]);
});
it("ignores comments and blank lines", () => {
const rules = parseCodeowners(
"# comment\n\n \n# another\n* @ghost # trailing comment\n"
);
expect(rules.length).toBe(1);
expect(rules[0].owners).toEqual(["ghost"]);
});
});
describe("AI generator fallbacks", () => {
it("returns a safe fallback commit message when AI is unavailable", async () => {
if (isAiAvailable()) {
return;
}
const msg = await generateCommitMessage("");
expect(msg.length).toBeGreaterThan(0);
expect(msg).toMatch(/^\S+/);
});
});
describe("health + metrics endpoints", () => {
it("GET /healthz returns 200", async () => {
const res = await app.request("/healthz");
expect(res.status).toBe(200);
const body = await res.json();
expect(body.ok).toBe(true);
});
it("GET /metrics returns process metrics", async () => {
const res = await app.request("/metrics");
expect(res.status).toBe(200);
const body = await res.json();
expect(typeof body.uptimeMs).toBe("number");
expect(typeof body.heapUsed).toBe("number");
});
it("response carries X-Request-Id header", async () => {
const res = await app.request("/healthz");
expect(res.headers.get("X-Request-Id")).toBeTruthy();
});
});
describe("rate limiting", () => {
it("rate-limit headers appear on /api requests", async () => {
const res = await app.request("/api/users/nonexistent/repos");
const limit = res.headers.get("X-RateLimit-Limit");
expect(limit).toBeTruthy();
});
});
describe("shortcuts + search page", () => {
it("GET /shortcuts is public", async () => {
const res = await app.request("/shortcuts");
expect(res.status).toBe(200);
const html = await res.text();
expect(html).toContain("Keyboard shortcuts");
});
it("GET /search with no query shows the type tabs", async () => {
const res = await app.request("/search");
expect(res.status).toBe(200);
const html = await res.text();
expect(html).toContain("Repositories");
expect(html).toContain("Users");
});
});
describe("GateTest inbound hook", () => {
it("GET /api/hooks/ping is unauthenticated and reports service", async () => {
const res = await app.request("/api/hooks/ping");
expect(res.status).toBe(200);
const body = await res.json();
expect(body.ok).toBe(true);
expect(body.service).toBe("gluecron");
expect(Array.isArray(body.hooks)).toBe(true);
});
it("POST /api/hooks/gatetest rejects when no secret configured", async () => {
const prev = process.env.GATETEST_CALLBACK_SECRET;
const prevH = process.env.GATETEST_HMAC_SECRET;
delete process.env.GATETEST_CALLBACK_SECRET;
delete process.env.GATETEST_HMAC_SECRET;
const res = await app.request("/api/hooks/gatetest", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({ repository: "a/b", sha: "x", status: "passed" }),
});
expect(res.status).toBe(401);
if (prev) process.env.GATETEST_CALLBACK_SECRET = prev;
if (prevH) process.env.GATETEST_HMAC_SECRET = prevH;
});
it("POST /api/hooks/gatetest rejects bad bearer token", async () => {
const prev = process.env.GATETEST_CALLBACK_SECRET;
process.env.GATETEST_CALLBACK_SECRET = "real-secret-abc123";
const res = await app.request("/api/hooks/gatetest", {
method: "POST",
headers: {
"content-type": "application/json",
authorization: "Bearer wrong-token",
},
body: JSON.stringify({ repository: "a/b", sha: "x", status: "passed" }),
});
expect(res.status).toBe(401);
if (prev === undefined) delete process.env.GATETEST_CALLBACK_SECRET;
else process.env.GATETEST_CALLBACK_SECRET = prev;
});
it("POST /api/hooks/gatetest rejects malformed payload even when authed", async () => {
const prev = process.env.GATETEST_CALLBACK_SECRET;
process.env.GATETEST_CALLBACK_SECRET = "real-secret-abc123";
const res = await app.request("/api/hooks/gatetest", {
method: "POST",
headers: {
"content-type": "application/json",
authorization: "Bearer real-secret-abc123",
},
body: "not-json",
});
expect(res.status).toBe(400);
if (prev === undefined) delete process.env.GATETEST_CALLBACK_SECRET;
else process.env.GATETEST_CALLBACK_SECRET = prev;
});
it("POST /api/v1/gate-runs (backup) rejects without bearer", async () => {
const res = await app.request("/api/v1/gate-runs", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({ repository: "a/b", sha: "x", status: "passed" }),
});
expect(res.status).toBe(401);
});
});
describe("theme toggle", () => {
it("GET /theme/toggle sets a cookie and redirects", async () => {
const res = await app.request("/theme/toggle");
expect([301, 302, 303, 307]).toContain(res.status);
const setCookie = res.headers.get("set-cookie") || "";
expect(/theme=light/.test(setCookie)).toBe(true);
});
it("GET /theme/toggle flips an existing 'light' cookie back to dark", async () => {
const res = await app.request("/theme/toggle", {
headers: { cookie: "theme=light" },
});
const setCookie = res.headers.get("set-cookie") || "";
expect(/theme=dark/.test(setCookie)).toBe(true);
});
it("GET /theme/set?mode=light returns JSON when asked", async () => {
const res = await app.request("/theme/set?mode=light", {
headers: { accept: "application/json" },
});
expect(res.status).toBe(200);
const body = await res.json();
expect(body.ok).toBe(true);
expect(body.theme).toBe("light");
});
it("GET /theme/set rejects unknown modes", async () => {
const res = await app.request("/theme/set?mode=neon", {
headers: { accept: "application/json" },
});
expect(res.status).toBe(400);
});
it("home page includes the pre-paint theme script + data-theme attribute", async () => {
const res = await app.request("/");
const html = await res.text();
expect(html).toContain("data-theme");
expect(html).toContain("theme-icon-");
expect(html).toContain("document.cookie");
});
});
describe("reactions", () => {
it("allowed emojis and targets are self-consistent", () => {
expect(ALLOWED_EMOJIS.length).toBeGreaterThanOrEqual(6);
for (const e of ALLOWED_EMOJIS) {
expect(isAllowedEmoji(e)).toBe(true);
expect(EMOJI_GLYPH[e]).toBeTruthy();
}
expect(isAllowedEmoji("nope")).toBe(false);
expect(isAllowedTarget("issue")).toBe(true);
expect(isAllowedTarget("martian")).toBe(false);
});
it("POST /api/reactions/.../toggle requires auth", async () => {
const res = await app.request(
"/api/reactions/issue/00000000-0000-0000-0000-000000000000/thumbs_up/toggle",
{ method: "POST" }
);
expect([301, 302, 303, 307]).toContain(res.status);
});
it("GET /api/reactions/:type/:id returns empty summary when no reactions exist", async () => {
const res = await app.request(
"/api/reactions/issue/00000000-0000-0000-0000-000000000000"
);
expect(res.status).toBe(200);
const body = await res.json();
expect(body.ok).toBe(true);
expect(Array.isArray(body.reactions)).toBe(true);
});
it("rejects unknown target type on the listing endpoint", async () => {
const res = await app.request(
"/api/reactions/martian/00000000-0000-0000-0000-000000000000"
);
expect(res.status).toBe(400);
});
});
describe("audit log UI", () => {
it("GET /settings/audit redirects unauthenticated users to /login", async () => {
const res = await app.request("/settings/audit");
expect([301, 302, 303, 307]).toContain(res.status);
const loc = res.headers.get("location") || "";
expect(loc.startsWith("/login")).toBe(true);
});
});
describe("email", () => {
it("sendEmail in log mode never throws and returns ok", async () => {
const prev = process.env.EMAIL_PROVIDER;
process.env.EMAIL_PROVIDER = "log";
const res = await sendEmail({
to: "test@gluecron.local",
subject: "hello",
text: "body",
});
expect(res.ok).toBe(true);
expect(res.provider).toBe("log");
if (prev === undefined) delete process.env.EMAIL_PROVIDER;
else process.env.EMAIL_PROVIDER = prev;
});
it("sendEmail rejects invalid recipient without throwing", async () => {
const res = await sendEmail({
to: "not-an-email",
subject: "x",
text: "y",
});
expect(res.ok).toBe(false);
expect(res.skipped).toBeTruthy();
});
it("sendEmail rejects empty subject/body without throwing", async () => {
const res = await sendEmail({ to: "a@b.co", subject: "", text: "" });
expect(res.ok).toBe(false);
});
it("absoluteUrl joins paths against APP_BASE_URL", () => {
const prev = process.env.APP_BASE_URL;
process.env.APP_BASE_URL = "https://gluecron.example/";
expect(absoluteUrl("/x")).toBe("https://gluecron.example/x");
expect(absoluteUrl("x")).toBe("https://gluecron.example/x");
expect(absoluteUrl("https://other/y")).toBe("https://other/y");
if (prev === undefined) delete process.env.APP_BASE_URL;
else process.env.APP_BASE_URL = prev;
});
it("notify email-eligible set only includes user-opt-in kinds", () => {
for (const k of notifyInternal.EMAIL_ELIGIBLE) {
expect(notifyInternal.prefFor(k)).not.toBeNull();
}
expect(notifyInternal.EMAIL_ELIGIBLE.has("gate_passed" as any)).toBe(false);
expect(notifyInternal.EMAIL_ELIGIBLE.has("deploy_failed" as any)).toBe(
false
);
});
it("notify email subject is tagged and truncated", () => {
const subj = notifyInternal.subjectFor("gate_failed", "x".repeat(300));
expect(subj.startsWith("[gate failed]")).toBe(true);
expect(subj.length).toBeLessThanOrEqual(180);
});
});
describe("settings email preferences", () => {
it("GET /settings redirects unauthenticated users to /login", async () => {
const res = await app.request("/settings");
expect([301, 302, 303, 307]).toContain(res.status);
const loc = res.headers.get("location") || "";
expect(loc.startsWith("/login")).toBe(true);
});
it("POST /settings/notifications redirects unauthenticated users to /login", async () => {
const res = await app.request("/settings/notifications", {
method: "POST",
headers: { "content-type": "application/x-www-form-urlencoded" },
body: "notify_email_on_mention=1",
});
expect([301, 302, 303, 307]).toContain(res.status);
const loc = res.headers.get("location") || "";
expect(loc.startsWith("/login")).toBe(true);
});
});
|