forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.test.ts
More file actions
344 lines (325 loc) · 11.2 KB
/
Copy pathserver.test.ts
File metadata and controls
344 lines (325 loc) · 11.2 KB
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
import { describe, it, expect } from "vitest";
import { buildServer } from "./server.js";
import { logger } from "../logger.js";
import { createMetricsRegistry } from "./metrics.js";
import type { PbClient } from "../storage/pb-client.js";
function fakePb(healthy: boolean): PbClient {
return {
getOne: async () => null,
getFirst: async () => null,
list: async () => ({
page: 1,
perPage: 0,
totalPages: 0,
totalItems: 0,
items: [],
}),
create: async () => ({}) as never,
update: async () => ({}) as never,
upsertByField: async () => ({}) as never,
delete: async () => {},
deleteByFilter: async () => 0,
health: async () => healthy,
createBackup: async () => {},
downloadBackup: async () => new Uint8Array(),
deleteBackup: async () => {},
};
}
describe("http/server", () => {
it("GET /health returns 200 when pb up, loop alive, rules>0", async () => {
const app = buildServer({
pb: fakePb(true),
logger,
ruleCount: () => 1,
loopAlive: () => true,
schedulerJobCount: () => 1,
});
const res = await app.request("/health");
expect(res.status).toBe(200);
const body = (await res.json()) as {
status: string;
pb: string;
rules: number;
};
expect(body.status).toBe("ok");
expect(body.pb).toBe("ok");
expect(body.rules).toBe(1);
});
it("GET /health returns 503 with loop:no-jobs when scheduler has zero entries", async () => {
// Regression: if rule-loader crashes or loads zero rules, the HTTP
// server still reports healthy because loopAlive/schedulerStarted
// don't care about job count. Require schedulerJobCount > 0 so
// this pathological state surfaces in /health.
const app = buildServer({
pb: fakePb(true),
logger,
ruleCount: () => 1,
loopAlive: () => true,
schedulerStarted: () => true,
schedulerJobCount: () => 0,
});
const res = await app.request("/health");
expect(res.status).toBe(503);
const body = (await res.json()) as {
loop: string;
status: string;
schedulerJobs: number;
};
expect(body.loop).toBe("no-jobs");
expect(body.schedulerJobs).toBe(0);
expect(body.status).toBe("degraded");
});
it("GET /health returns 503 with loop:stopped when schedulerIsStopped is true even if alive was never flipped", async () => {
const app = buildServer({
pb: fakePb(true),
logger,
ruleCount: () => 1,
loopAlive: () => true, // legacy flag, still true
schedulerStarted: () => true,
schedulerIsStopped: () => true, // but scheduler.stop() completed
schedulerJobCount: () => 0,
});
const res = await app.request("/health");
expect(res.status).toBe(503);
const body = (await res.json()) as { loop: string };
expect(body.loop).toBe("stopped");
});
it("GET /health returns 200 when all scheduler signals are healthy", async () => {
const app = buildServer({
pb: fakePb(true),
logger,
ruleCount: () => 3,
loopAlive: () => true,
schedulerStarted: () => true,
schedulerIsStopped: () => false,
schedulerJobCount: () => 5,
});
const res = await app.request("/health");
expect(res.status).toBe(200);
const body = (await res.json()) as {
loop: string;
schedulerJobs: number;
};
expect(body.loop).toBe("ok");
expect(body.schedulerJobs).toBe(5);
});
it("GET /health returns 503 when pb down", async () => {
const app = buildServer({
pb: fakePb(false),
logger,
ruleCount: () => 1,
loopAlive: () => true,
schedulerJobCount: () => 1,
});
const res = await app.request("/health");
expect(res.status).toBe(503);
});
it("GET /health returns 503 when no rules loaded", async () => {
const app = buildServer({
pb: fakePb(true),
logger,
ruleCount: () => 0,
loopAlive: () => true,
schedulerJobCount: () => 1,
});
const res = await app.request("/health");
expect(res.status).toBe(503);
});
it("GET /health returns 503 when loop not alive", async () => {
const app = buildServer({
pb: fakePb(true),
logger,
ruleCount: () => 1,
loopAlive: () => false,
schedulerJobCount: () => 1,
});
const res = await app.request("/health");
expect(res.status).toBe(503);
});
it("GET /health reports loop:starting (503) when scheduler has not started", async () => {
const app = buildServer({
pb: fakePb(true),
logger,
ruleCount: () => 1,
loopAlive: () => true,
schedulerStarted: () => false,
schedulerJobCount: () => 1,
});
const res = await app.request("/health");
expect(res.status).toBe(503);
const body = (await res.json()) as { loop: string; status: string };
expect(body.loop).toBe("starting");
expect(body.status).toBe("degraded");
});
it("GET /health reports loop:ok when scheduler has started and is alive", async () => {
const app = buildServer({
pb: fakePb(true),
logger,
ruleCount: () => 1,
loopAlive: () => true,
schedulerStarted: () => true,
schedulerJobCount: () => 1,
});
const res = await app.request("/health");
expect(res.status).toBe(200);
const body = (await res.json()) as { loop: string };
expect(body.loop).toBe("ok");
});
it("GET /health reports loop:stopped (503) when loop explicitly stopped even if started", async () => {
const app = buildServer({
pb: fakePb(true),
logger,
ruleCount: () => 1,
loopAlive: () => false,
schedulerStarted: () => true,
schedulerJobCount: () => 1,
});
const res = await app.request("/health");
expect(res.status).toBe(503);
const body = (await res.json()) as { loop: string };
expect(body.loop).toBe("stopped");
});
it("GET /health (control-plane role) returns 200 with rules=0 when pb/loop/scheduler ok", async () => {
// The control-plane is a scheduler/queue/aggregator — it legitimately
// owns NO probe rules, only the single fleet-job-producer scheduler
// entry. The default `rules > 0` gate is wrong for that role; with
// role:"control-plane" the endpoint reports healthy on its real
// liveness signals (pb ok, scheduler started + alive, schedulerJobs>0)
// WITHOUT requiring rules.
const app = buildServer({
pb: fakePb(true),
logger,
role: "control-plane",
ruleCount: () => 0,
loopAlive: () => true,
schedulerStarted: () => true,
schedulerIsStopped: () => false,
schedulerJobCount: () => 1,
});
const res = await app.request("/health");
expect(res.status).toBe(200);
const body = (await res.json()) as {
status: string;
rules: number;
loop: string;
schedulerJobs: number;
};
expect(body.status).toBe("ok");
expect(body.rules).toBe(0);
expect(body.loop).toBe("ok");
expect(body.schedulerJobs).toBe(1);
});
it("GET /health (control-plane role) still returns 503 when pb is down", async () => {
const app = buildServer({
pb: fakePb(false),
logger,
role: "control-plane",
ruleCount: () => 0,
loopAlive: () => true,
schedulerStarted: () => true,
schedulerJobCount: () => 1,
});
const res = await app.request("/health");
expect(res.status).toBe(503);
const body = (await res.json()) as { status: string; pb: string };
expect(body.status).toBe("degraded");
expect(body.pb).toBe("down");
});
it("GET /health (control-plane role) still returns 503 when scheduler has no jobs", async () => {
// Even role-aware, the control-plane MUST surface a dead scheduler: if
// the fleet-job-producer entry is missing (schedulerJobs==0) nothing
// ticks, so /health must report degraded regardless of the rules gate.
const app = buildServer({
pb: fakePb(true),
logger,
role: "control-plane",
ruleCount: () => 0,
loopAlive: () => true,
schedulerStarted: () => true,
schedulerJobCount: () => 0,
});
const res = await app.request("/health");
expect(res.status).toBe(503);
const body = (await res.json()) as { status: string; loop: string };
expect(body.status).toBe("degraded");
expect(body.loop).toBe("no-jobs");
});
it("GET /health (control-plane role) still returns 503 when loop not alive", async () => {
const app = buildServer({
pb: fakePb(true),
logger,
role: "control-plane",
ruleCount: () => 0,
loopAlive: () => false,
schedulerStarted: () => true,
schedulerJobCount: () => 1,
});
const res = await app.request("/health");
expect(res.status).toBe(503);
const body = (await res.json()) as { status: string };
expect(body.status).toBe("degraded");
});
it("GET /health (worker/default role) still requires rules>0 (regression guard)", async () => {
// The role-aware change must NOT loosen the worker/legacy path: with no
// role (or a non-control-plane role) the rules>0 gate stays in force.
const app = buildServer({
pb: fakePb(true),
logger,
ruleCount: () => 0,
loopAlive: () => true,
schedulerStarted: () => true,
schedulerJobCount: () => 1,
});
const res = await app.request("/health");
expect(res.status).toBe(503);
});
it("GET /metrics exposes Prometheus-format counters when metrics is provided", async () => {
const metrics = createMetricsRegistry();
metrics.inc("probe_runs", { dimension: "smoke" });
metrics.inc("hmac_failures");
const app = buildServer({
pb: fakePb(true),
logger,
ruleCount: () => 1,
loopAlive: () => true,
schedulerJobCount: () => 1,
metrics,
});
const res = await app.request("/metrics");
expect(res.status).toBe(200);
expect(res.headers.get("content-type")).toContain("text/plain");
const body = await res.text();
expect(body).toContain('showcase_harness_probe_runs{dimension="smoke"} 1');
expect(body).toContain("showcase_harness_hmac_failures 1");
});
it("GET /metrics returns 404 when metrics registry is absent", async () => {
const app = buildServer({
pb: fakePb(true),
logger,
ruleCount: () => 1,
loopAlive: () => true,
schedulerJobCount: () => 1,
});
const res = await app.request("/metrics");
expect(res.status).toBe(404);
});
it("buildServer throws synchronously when schedulerJobCount is not supplied", () => {
// Fail-loud discipline: the previous behaviour treated a missing
// `schedulerJobCount` as "OK by default" (jobCountOk = true), so an
// orchestrator that forgot to wire the callback would silently report
// /health: 200 with zero cron jobs. Production must always supply it;
// surface the misconfiguration as a hard boot-time failure rather than
// a quiet `loop: ok` lie.
expect(() =>
// @ts-expect-error — schedulerJobCount is now required; this call
// must fail to compile AND fail at runtime so misconfigured boot
// paths cannot reach a misleading /health response.
buildServer({
pb: fakePb(true),
logger,
ruleCount: () => 1,
loopAlive: () => true,
}),
).toThrow(/schedulerJobCount/);
});
});