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
|
import { describe, it, expect } from "bun:test";
import app from "../app";
import {
SIGNAL_SOURCES,
extractTopFrame,
hashError,
isValidSha,
sanitiseKind,
sanitiseSeverity,
sanitiseSource,
__internal,
} from "../lib/prod-signals";
describe("prod-signals — isValidSha", () => {
it("accepts 7–64 hex chars", () => {
expect(isValidSha("abcdef1")).toBe(true);
expect(isValidSha("a".repeat(40))).toBe(true);
expect(isValidSha("a".repeat(64))).toBe(true);
expect(isValidSha("DEADBEEF1234")).toBe(true);
});
it("rejects too short / too long / bad chars / empty / null", () => {
expect(isValidSha("abc123")).toBe(false);
expect(isValidSha("a".repeat(65))).toBe(false);
expect(isValidSha("xyz12345")).toBe(false);
expect(isValidSha("")).toBe(false);
expect(isValidSha(null)).toBe(false);
expect(isValidSha(undefined)).toBe(false);
expect(isValidSha(1234567 as any)).toBe(false);
});
});
describe("prod-signals — hashError", () => {
it("is deterministic for the same inputs", () => {
const a = hashError("TypeError: x is null", "at foo (bar.ts:1:1)");
const b = hashError("TypeError: x is null", "at foo (bar.ts:1:1)");
expect(a).toBe(b);
});
it("returns a 16-hex-char string", () => {
const h = hashError("boom", "at foo (bar.ts)");
expect(h.length).toBe(__internal.HASH_LEN);
expect(/^[a-f0-9]+$/.test(h)).toBe(true);
});
it("differs when top frame differs", () => {
const a = hashError("boom", "at foo (a.ts:1:1)");
const b = hashError("boom", "at bar (c.ts:1:1)");
expect(a).not.toBe(b);
});
it("collapses volatile details in messages (whitespace, hex pointers, line:col)", () => {
const a = hashError(
"Cannot read property 'x' of null at 0xdeadbeef:12:34",
"at foo (a.ts:10:5)"
);
const b = hashError(
"Cannot read property 'x' of null at 0xfeedface:99:1",
"at foo (a.ts:10:5)"
);
expect(a).toBe(b);
});
it("tolerates null / undefined inputs", () => {
const h = hashError(null as any, undefined as any);
expect(h.length).toBe(__internal.HASH_LEN);
});
});
describe("prod-signals — extractTopFrame", () => {
it("returns the first non-empty, non-node_modules line", () => {
const st = [
"",
" at Module._compile (node:internal/modules)",
" at ./node_modules/foo/index.js:3:10",
" at ./src/app.ts:42:5",
].join("\n");
expect(extractTopFrame(st)).toBe(
"at Module._compile (node:internal/modules)"
);
});
it("skips node_modules frames", () => {
const st = [
" at ./node_modules/react/cjs/react.js:10:1",
" at ./src/App.tsx:15:3",
].join("\n");
expect(extractTopFrame(st)).toBe("at ./src/App.tsx:15:3");
});
it("caps at FRAME_MAX", () => {
const line = "at foo " + "x".repeat(2000);
const top = extractTopFrame(line);
expect(top.length).toBe(__internal.FRAME_MAX);
});
it("returns empty string on malformed / empty input", () => {
expect(extractTopFrame("")).toBe("");
expect(extractTopFrame(null)).toBe("");
expect(extractTopFrame(undefined)).toBe("");
expect(extractTopFrame(" \n\n ")).toBe("");
});
});
describe("prod-signals — sanitiseSource", () => {
it("accepts the allow-listed sources", () => {
for (const s of SIGNAL_SOURCES) expect(sanitiseSource(s)).toBe(s);
});
it("lower-cases and trims", () => {
expect(sanitiseSource(" CRONTECH ")).toBe("crontech");
expect(sanitiseSource("Gatetest")).toBe("gatetest");
});
it("falls back to 'manual' for unknown / missing", () => {
expect(sanitiseSource("datadog")).toBe("manual");
expect(sanitiseSource("")).toBe("manual");
expect(sanitiseSource(null)).toBe("manual");
expect(sanitiseSource(undefined)).toBe("manual");
expect(sanitiseSource(42 as any)).toBe("manual");
});
});
describe("prod-signals — sanitiseKind", () => {
it("accepts canonical kinds", () => {
expect(sanitiseKind("runtime_error")).toBe("runtime_error");
expect(sanitiseKind("test_failure")).toBe("test_failure");
expect(sanitiseKind("deploy_failure")).toBe("deploy_failure");
expect(sanitiseKind("performance")).toBe("performance");
expect(sanitiseKind("security")).toBe("security");
});
it("defaults unknown kinds to runtime_error", () => {
expect(sanitiseKind("weird")).toBe("runtime_error");
expect(sanitiseKind(null)).toBe("runtime_error");
expect(sanitiseKind(undefined)).toBe("runtime_error");
});
});
describe("prod-signals — sanitiseSeverity", () => {
it("accepts canonical severities", () => {
expect(sanitiseSeverity("info")).toBe("info");
expect(sanitiseSeverity("warning")).toBe("warning");
expect(sanitiseSeverity("error")).toBe("error");
expect(sanitiseSeverity("critical")).toBe("critical");
});
it("defaults to 'error'", () => {
expect(sanitiseSeverity("")).toBe("error");
expect(sanitiseSeverity("fatal")).toBe("error");
expect(sanitiseSeverity(null)).toBe("error");
expect(sanitiseSeverity(undefined)).toBe("error");
});
});
describe("prod-signals — route auth", () => {
it("POST /api/v1/signals/error without auth → 401 JSON (or 404 pre-mount)", async () => {
const res = await app.request("/api/v1/signals/error", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({
repo: "alice/demo",
commit_sha: "abc1234",
source: "crontech",
kind: "runtime_error",
message: "boom",
}),
});
expect([401, 404]).toContain(res.status);
});
it("POST with invalid bearer token → 401 JSON (or 404 pre-mount)", async () => {
const res = await app.request("/api/v1/signals/error", {
method: "POST",
headers: {
"content-type": "application/json",
authorization: "Bearer glc_not_a_real_token",
},
body: JSON.stringify({
repo: "alice/demo",
commit_sha: "abc1234",
source: "manual",
kind: "runtime_error",
message: "boom",
}),
});
expect([401, 404]).toContain(res.status);
});
it("POST dismiss without auth → 401 (or 404 pre-mount)", async () => {
const res = await app.request(
"/api/v1/signals/00000000-0000-0000-0000-000000000000/dismiss",
{ method: "POST" }
);
expect([401, 404]).toContain(res.status);
});
it("POST resolve without auth → 401 (or 404 pre-mount)", async () => {
const res = await app.request(
"/api/v1/signals/00000000-0000-0000-0000-000000000000/resolve",
{
method: "POST",
headers: { "content-type": "application/json" },
body: "{}",
}
);
expect([401, 404]).toContain(res.status);
});
it("GET repo signals returns JSON (200 / 403 / 404 / 500 depending on env)", async () => {
const res = await app.request("/api/v1/repos/alice/demo/signals");
expect([200, 403, 404, 500]).toContain(res.status);
});
it("GET commit signals with invalid sha → 400 (or 404 pre-mount)", async () => {
const res = await app.request(
"/api/v1/repos/alice/demo/commits/NOT_HEX/signals"
);
expect([400, 404]).toContain(res.status);
});
});
|