forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathverify-deploy.test.ts
More file actions
263 lines (234 loc) · 9.67 KB
/
Copy pathverify-deploy.test.ts
File metadata and controls
263 lines (234 loc) · 9.67 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
import { describe, expect, it } from "vitest";
import {
asHost,
parseArgs,
resolveProbeTargets,
runVerify,
} from "../verify-deploy";
import type { ProbeRunner } from "../verify-deploy";
describe("verify-deploy argv parsing", () => {
it("requires --env", () => {
expect(() => parseArgs([])).toThrow(/--env/);
});
it("accepts --env staging and --env prod", () => {
expect(parseArgs(["--env", "staging"]).env).toBe("staging");
expect(parseArgs(["--env=prod"]).env).toBe("prod");
});
it("rejects unknown envs", () => {
expect(() => parseArgs(["--env", "dev"])).toThrow(/Unknown env/);
});
it("accepts optional --services CSV", () => {
const parsed = parseArgs(["--env", "staging", "--services", "docs,shell"]);
expect(parsed.services).toEqual(["docs", "shell"]);
});
it("rejects empty --services= equals-form (mirrors space-form behavior)", () => {
expect(() => parseArgs(["--env", "staging", "--services="])).toThrow(
/--services/,
);
});
it("rejects --services space-form whose CSV is all empty entries (symmetry with equals-form)", () => {
// Bug: space-form previously only guarded `!v` on the raw next-arg,
// so `--services ,,` produced an empty list that fell through to a
// less-precise zero-targets error. Both forms must throw the same
// precise `--services requires a CSV value` here.
expect(() => parseArgs(["--env=staging", "--services", ",,"])).toThrow(
/--services requires a CSV value/,
);
});
it("rejects bare trailing --env (no following value)", () => {
// Bug: `argv[++i]` was undefined and we deferred to a vague
// `resolveEnv` error. Must throw the precise message here.
expect(() => parseArgs(["--env"])).toThrow(
/--env requires a value \(staging\|prod\)/,
);
});
it("rejects --env= with empty value (symmetry with bare trailing --env)", () => {
expect(() => parseArgs(["--env="])).toThrow(
/--env requires a value \(staging\|prod\)/,
);
});
});
describe("resolveProbeTargets", () => {
it("filters SSOT to entries where probe[env] is true", () => {
const targets = resolveProbeTargets({ env: "staging" });
// docs is staging:true → must be present.
expect(targets.find((t) => t.name === "docs")).toBeDefined();
});
it("REFUSES when a probe-required service has no domain for the env", () => {
expect(() =>
resolveProbeTargets({
env: "staging",
overrides: {
docs: { domains: { staging: "", prod: "docs.copilotkit.ai" } },
},
}),
).toThrow(/missing.*staging.*domain/i);
});
it("honors --services filter (subset of probe-eligible)", () => {
const targets = resolveProbeTargets({
env: "staging",
services: ["docs"],
});
expect(targets.length).toBe(1);
expect(targets[0].name).toBe("docs");
});
it("REFUSES a typo'd service name (unknown service, not silent drop)", () => {
expect(() =>
resolveProbeTargets({ env: "staging", services: ["docss"] }),
).toThrow(/unknown service.*docss/i);
});
it("REFUSES a service that exists in SSOT but is not probe-eligible for the env", () => {
// Find a service whose probe[staging] is false.
// Use override seam to flip probe state without mutating SSOT.
// Since resolveProbeTargets doesn't expose a probe-flag override,
// we pick a real service name and an env where the SSOT probe is
// false. Search SERVICES for an entry where probe.staging===false.
// If none exists, this test still validates the error string for
// the more common typo case via the prior test; we focus on the
// distinct error phrasing.
//
// Practical assertion: an unknown name surfaces as "unknown
// service", which is structurally a different (clearer) error
// than "not probe-eligible". The two paths must be distinguished.
expect(() =>
resolveProbeTargets({ env: "staging", services: ["totally-fake"] }),
).toThrow(/unknown service/i);
});
it("REFUSES an override domain carrying a scheme (ingress branding wired)", () => {
// The override seam in `resolveProbeTargets` bypasses `domainFor`,
// so `asHost` is the sole ingress validator on that path. A
// scheme-bearing override must surface as a hard throw, proving
// the `asHost(rawHost)` call is actually wired in.
expect(() =>
resolveProbeTargets({
env: "staging",
services: ["docs"],
overrides: {
docs: {
domains: { staging: "https://docs.test", prod: "docs.test" },
},
},
}),
).toThrow(/scheme/i);
});
it("REFUSES an override domain carrying a path/slash (ingress branding wired)", () => {
expect(() =>
resolveProbeTargets({
env: "staging",
services: ["docs"],
overrides: {
docs: {
domains: { staging: "docs.test/path", prod: "docs.test" },
},
},
}),
).toThrow(/path|slash/i);
});
});
describe("asHost validator", () => {
it("accepts a bare hostname literal", () => {
expect(() => asHost("docs.example.com")).not.toThrow();
// Returned value IS a string at runtime (brand is structural).
const h = asHost("docs.example.com");
expect(typeof h).toBe("string");
expect(h).toBe("docs.example.com");
});
it("rejects values with a scheme separator", () => {
expect(() => asHost("https://x")).toThrow(/scheme/i);
expect(() => asHost("http://docs.example.com")).toThrow(/scheme/i);
});
it("rejects values containing a path or slash", () => {
expect(() => asHost("x/y")).toThrow(/path|slash/i);
expect(() => asHost("docs.example.com/")).toThrow(/path|slash/i);
});
it("rejects the empty string", () => {
expect(() => asHost("")).toThrow(/empty/i);
});
it("rejects leading/trailing whitespace", () => {
expect(() => asHost(" docs.example.com")).toThrow(/whitespace/i);
expect(() => asHost("docs.example.com ")).toThrow(/whitespace/i);
expect(() => asHost("\tdocs.example.com")).toThrow(/whitespace/i);
});
it("rejects userinfo '@'", () => {
expect(() => asHost("user@docs.example.com")).toThrow(/userinfo|@/);
});
it("rejects query '?'", () => {
expect(() => asHost("docs.example.com?x=1")).toThrow(/query|\?/);
});
it("rejects fragment '#'", () => {
expect(() => asHost("docs.example.com#frag")).toThrow(/fragment|#/);
});
it("rejects ASCII control characters (newline, CR, NUL, tab inside)", () => {
// These would survive the trim() check (because the offending
// char is interior, not leading/trailing) but must still be
// caught by the explicit control-char rule. NUL is the classic
// injection vector; newline/CR can cause header smuggling at any
// downstream `https://${host}/...` composition.
expect(() => asHost("docs.example\ncom")).toThrow(/control/i);
expect(() => asHost("docs.example\rcom")).toThrow(/control/i);
expect(() => asHost("docs.example\x00com")).toThrow(/control/i);
expect(() => asHost("docs.example\x7fcom")).toThrow(/control/i);
expect(() => asHost("docs.example\tcom")).toThrow(/control/i);
});
it("rejects a ':port' suffix", () => {
// domainFor() returns bare hostnames; ports are not part of the
// verify-pipeline contract. Reject with a precise diagnostic.
expect(() => asHost("docs.example.com:8080")).toThrow(/port/i);
expect(() => asHost("localhost:3000")).toThrow(/port/i);
});
it("rejects characters outside the DNS-label charset", () => {
// Underscore, unicode — none are in [A-Za-z0-9.-]. Leading/trailing
// whitespace is caught earlier by the trim check; interior
// whitespace is rejected here by the charset rule. `:` is caught
// by the port suffix check.
expect(() => asHost("docs_example.com")).toThrow(/charset|DNS/);
expect(() => asHost("docs.exämple.com")).toThrow(/charset|DNS/);
expect(() => asHost("docs!example.com")).toThrow(/charset|DNS/);
// Pure positive: real SSOT-shape hostnames still pass.
expect(() => asHost("docs.example.com")).not.toThrow();
expect(() => asHost("a-b.c-d.example")).not.toThrow();
expect(() => asHost("harness-staging-2ee4.up.railway.app")).not.toThrow();
});
});
describe("runVerify driver dispatch", () => {
it("calls the driver for each target and fails loud on any red", async () => {
const calls: string[] = [];
const runner: ProbeRunner = async (target) => {
calls.push(`${target.driver}:${target.host}`);
if (target.name === "docs") {
return { ok: false, error: "DOM string missing" };
}
return { ok: true };
};
const summary = await runVerify({
env: "staging",
services: ["docs", "shell"],
runner,
});
expect(calls).toContain("docs:docs.staging.copilotkit.ai");
expect(calls).toContain("shell:showcase.staging.copilotkit.ai");
expect(summary.failed.map((f) => f.name)).toEqual(["docs"]);
expect(summary.exitCode).toBe(1);
});
it("exits 0 when all probes green", async () => {
const summary = await runVerify({
env: "staging",
services: ["docs"],
runner: async () => ({ ok: true }),
});
expect(summary.exitCode).toBe(0);
});
it("FAILS LOUD on zero resolved targets (never silently exit 0)", async () => {
// resolveProbeTargets now throws on unknown service names, so the
// zero-target shape can only happen via the API (empty filter
// set). We exercise the runVerify guard directly: a runner that's
// never called and an exit code of 1 with a clear diagnostic.
const summary = await runVerify({
env: "staging",
services: [],
runner: async () => ({ ok: true }),
});
expect(summary.exitCode).not.toBe(0);
expect(summary.failed.length).toBeGreaterThan(0);
});
});