forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathredeploy-env.test.ts
More file actions
313 lines (279 loc) · 10.4 KB
/
Copy pathredeploy-env.test.ts
File metadata and controls
313 lines (279 loc) · 10.4 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
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { parseArgs, runRedeploy, resolveTargetServices } from "./redeploy-env";
import { SERVICES } from "./railway-envs";
describe("runRedeploy", () => {
let consoleErrSpy: ReturnType<typeof vi.spyOn>;
let consoleLogSpy: ReturnType<typeof vi.spyOn>;
let stdoutWriteSpy: ReturnType<typeof vi.spyOn>;
let summary: string;
beforeEach(() => {
consoleErrSpy = vi.spyOn(console, "error").mockImplementation(() => {});
consoleLogSpy = vi.spyOn(console, "log").mockImplementation(() => {});
// runRedeploy's per-service progress lines go to process.stdout.write
// (NOT console.log); spy on that too or tests spam the terminal.
stdoutWriteSpy = vi
.spyOn(process.stdout, "write")
.mockImplementation(() => true);
summary = "";
});
afterEach(() => {
consoleErrSpy.mockRestore();
consoleLogSpy.mockRestore();
stdoutWriteSpy.mockRestore();
});
const appendSummary = (s: string) => {
summary += s + "\n";
};
it("default scope (no --services) targets the 26 CI-built services (incl. pocketbase), NOT all 27", async () => {
const seenNames: string[] = [];
const redeploy = vi.fn(async (serviceId: string) => {
// Reverse-lookup the SSOT name from serviceId so the test can
// assert exact membership rather than counting opaquely.
const name = Object.entries(SERVICES).find(
([, e]) => e.serviceId === serviceId,
)?.[0];
if (name) seenNames.push(name);
return { ok: true as const };
});
const result = await runRedeploy({
env: "staging",
redeploy,
appendSummary,
// services omitted → default = CI_BUILT_SERVICES
});
expect(result.exitCode).toBe(0);
expect(result.attempted).toBe(26);
expect(result.succeeded).toBe(26);
expect(redeploy).toHaveBeenCalledTimes(26);
// pocketbase is now CI-built, so it IS in the default redeploy scope.
expect(seenNames).toContain("pocketbase");
// webhooks remains out-of-band.
expect(seenNames).not.toContain("webhooks");
});
it("default whole-env staging redeploy NEVER bounces webhooks (out-of-band)", async () => {
// Anti-regression: webhooks is released by its own repo's workflow
// and must stay out of the default redeploy scope. pocketbase, by
// contrast, is now CI-built and IS legitimately in the default scope.
const seenIds = new Set<string>();
const redeploy = vi.fn(async (serviceId: string) => {
seenIds.add(serviceId);
return { ok: true as const };
});
await runRedeploy({ env: "staging", redeploy, appendSummary });
expect(seenIds.has(SERVICES.webhooks.serviceId)).toBe(false);
expect(seenIds.has(SERVICES.pocketbase.serviceId)).toBe(true);
});
it("explicit --services list targets exactly that subset", async () => {
const seenIds: string[] = [];
const redeploy = vi.fn(async (serviceId: string) => {
seenIds.push(serviceId);
return { ok: true as const };
});
const result = await runRedeploy({
env: "staging",
redeploy,
appendSummary,
services: ["showcase-mastra", "showcase-ag2"],
});
expect(result.attempted).toBe(2);
expect(seenIds).toEqual([
SERVICES["showcase-ag2"].serviceId, // alphabetical iteration
SERVICES["showcase-mastra"].serviceId,
]);
});
it("targets the staging env id", async () => {
const calls: Array<{ environmentId: string }> = [];
const redeploy = vi.fn(async (_svc: string, environmentId: string) => {
calls.push({ environmentId });
return { ok: true as const };
});
await runRedeploy({
env: "staging",
redeploy,
appendSummary,
services: ["showcase-mastra"],
});
for (const c of calls) {
expect(c.environmentId).toBe("8edfef02-ea09-4a20-8689-261f21cc2849");
}
});
it("targets the prod env id when env=prod", async () => {
const calls: Array<{ environmentId: string }> = [];
const redeploy = vi.fn(async (_svc: string, environmentId: string) => {
calls.push({ environmentId });
return { ok: true as const };
});
await runRedeploy({
env: "prod",
redeploy,
appendSummary,
services: ["showcase-mastra"],
});
for (const c of calls) {
expect(c.environmentId).toBe("b14919f4-6417-429f-848d-c6ae2201e04f");
}
});
it("treats per-service failures as non-fatal and exits 0", async () => {
// Deterministic: fail on a specific named service so the test does
// not depend on the size of the default scope.
const failTarget = SERVICES["showcase-mastra"].serviceId;
const redeploy = vi.fn(async (serviceId: string) => {
if (serviceId === failTarget) {
return { ok: false as const, error: "boom" };
}
return { ok: true as const };
});
const result = await runRedeploy({
env: "staging",
redeploy,
appendSummary,
});
expect(result.exitCode).toBe(0);
expect(result.failed).toBe(1);
expect(result.succeeded + result.failed).toBe(result.attempted);
// Failure must be visible in the summary.
expect(summary).toMatch(/FAIL/);
expect(summary).toMatch(/boom/);
});
it("per-service catch records non-Error throws (null, string) as failures without crashing", async () => {
// Catch block previously did `(e as Error).message ?? String(e)`,
// which throws TypeError when e is null.
const failTarget = SERVICES["showcase-mastra"].serviceId;
const redeploy = vi.fn(async (serviceId: string) => {
if (serviceId === failTarget) {
// eslint-disable-next-line @typescript-eslint/no-throw-literal
throw null;
}
return { ok: true as const };
});
const result = await runRedeploy({
env: "staging",
redeploy,
appendSummary,
});
expect(result.exitCode).toBe(0);
expect(result.failed).toBe(1);
expect(summary).toMatch(/FAIL/);
});
it("env=prod returns non-zero exitCode on per-service failure", async () => {
// Prod isn't wired yet, but the design must NOT silently swallow
// prod per-service failures the way staging intentionally does.
const redeploy = vi.fn(async () => ({
ok: false as const,
error: "kaboom",
}));
const result = await runRedeploy({
env: "prod",
redeploy,
appendSummary,
services: ["showcase-mastra"],
});
expect(result.failed).toBe(1);
expect(result.exitCode).not.toBe(0);
});
it("env=prod returns exitCode 0 when all services succeed", async () => {
const redeploy = vi.fn(async () => ({ ok: true as const }));
const result = await runRedeploy({
env: "prod",
redeploy,
appendSummary,
services: ["showcase-mastra"],
});
expect(result.exitCode).toBe(0);
});
it("reports zero failures with empty failure list in summary", async () => {
const redeploy = vi.fn(async () => ({ ok: true as const }));
await runRedeploy({ env: "staging", redeploy, appendSummary });
expect(summary).toMatch(/0 failed/);
});
it("throws on a --services entry that doesn't resolve to a known SSOT key", async () => {
const redeploy = vi.fn();
await expect(
runRedeploy({
env: "staging",
redeploy,
appendSummary,
services: ["nonsense"],
}),
).rejects.toThrow(/Unknown service/);
expect(redeploy).not.toHaveBeenCalled();
});
it("rejects unknown env names", async () => {
const redeploy = vi.fn();
await expect(
runRedeploy({ env: "dev" as never, redeploy, appendSummary }),
).rejects.toThrow(/Unknown env/);
expect(redeploy).not.toHaveBeenCalled();
});
});
describe("resolveTargetServices", () => {
it("accepts SSOT keys verbatim and resolves CI dispatch_names too", () => {
// The workflow passes detect-changes.outputs.matrix .dispatch_name
// values, but a human operator might pass SSOT keys directly.
// Both forms must resolve.
const resolved = resolveTargetServices([
"showcase-mastra", // already an SSOT key
"mastra", // dispatch_name → showcase-mastra
"shell-dashboard", // dispatch_name → dashboard
"showcase-aimock", // dispatch_name → aimock
]);
expect(resolved).toEqual(["showcase-mastra", "dashboard", "aimock"]);
// Dedupes the duplicate showcase-mastra ↔ mastra.
});
it("throws on inputs that match neither SSOT keys nor dispatch_names", () => {
expect(() => resolveTargetServices(["mastra", "garbage"])).toThrow(
/Unknown service "garbage"/,
);
});
it("returns the CI_BUILT_SERVICES set sorted when given undefined", () => {
const resolved = resolveTargetServices(undefined);
expect(resolved.length).toBe(26);
// pocketbase is now CI-built and part of the default scope.
expect(resolved).toContain("pocketbase");
// webhooks remains out-of-band.
expect(resolved).not.toContain("webhooks");
});
});
describe("parseArgs", () => {
it("parses bare env name", () => {
expect(parseArgs(["staging"])).toEqual({ env: "staging" });
});
it("parses --services with CSV value", () => {
expect(parseArgs(["staging", "--services", "mastra,ag2"])).toEqual({
env: "staging",
services: ["mastra", "ag2"],
});
});
it("parses --services=csv form", () => {
expect(parseArgs(["staging", "--services=mastra,ag2"])).toEqual({
env: "staging",
services: ["mastra", "ag2"],
});
});
it("throws when --services has no following value (bare flag at end)", () => {
expect(() => parseArgs(["staging", "--services"])).toThrow(
/--services requires a non-empty comma-separated value/,
);
});
it("throws when --services value is the empty string", () => {
expect(() => parseArgs(["staging", "--services", ""])).toThrow(
/--services requires a non-empty comma-separated value/,
);
});
it("throws when --services value is only commas/whitespace", () => {
expect(() => parseArgs(["staging", "--services", ","])).toThrow(
/--services requires a non-empty comma-separated value/,
);
});
it("throws when --services= value empties after split/filter", () => {
expect(() => parseArgs(["staging", "--services="])).toThrow(
/--services requires a non-empty comma-separated value/,
);
});
it("throws on unknown argument", () => {
expect(() => parseArgs(["staging", "--bogus"])).toThrow(/Unknown argument/);
});
it("throws on empty argv", () => {
expect(() => parseArgs([])).toThrow();
});
});