@htilabs/platform-common-models 0.8.8 → 0.8.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/config/index.cjs +191 -0
- package/dist/config/index.cjs.map +1 -0
- package/dist/config/index.d.cts +101 -0
- package/dist/config/index.d.ts +101 -0
- package/dist/config/index.js +165 -0
- package/dist/config/index.js.map +1 -0
- package/dist/{cvcEntry-DaDrEuTa.d.cts → cvcEntry-BCj1OWqL.d.cts} +36 -36
- package/dist/{cvcEntry-DaDrEuTa.d.ts → cvcEntry-BCj1OWqL.d.ts} +36 -36
- package/dist/cvcs/index.cjs +7253 -6350
- package/dist/cvcs/index.cjs.map +1 -1
- package/dist/cvcs/index.d.cts +4459 -3451
- package/dist/cvcs/index.d.ts +4459 -3451
- package/dist/cvcs/index.js +7246 -6350
- package/dist/cvcs/index.js.map +1 -1
- package/dist/{index-EPjPjR0z.d.cts → index-CoVr1jIV.d.cts} +1 -1
- package/dist/{index-C0J3f169.d.ts → index-I-DiX9eQ.d.ts} +1 -1
- package/dist/index.cjs +3218 -2201
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +290 -148
- package/dist/index.d.ts +290 -148
- package/dist/index.js +7433 -6430
- package/dist/index.js.map +1 -1
- package/dist/models/index.d.cts +2 -2
- package/dist/models/index.d.ts +2 -2
- package/dist/schemas/index.d.cts +11 -11
- package/dist/schemas/index.d.ts +11 -11
- package/package.json +7 -1
- package/src/__tests__/__snapshots__/cvc-sot.snap +31 -3
- package/src/__tests__/cvc-sot-snapshot.test.ts +10 -2
- package/src/__tests__/cvc-structural.test.ts +2 -2
- package/src/__tests__/runtime-contract-validator.test.ts +556 -0
- package/src/config/fixtures/valid.yaml +90 -0
- package/src/config/index.ts +352 -0
- package/src/cvcs/assertionDomain.v1.0.0.json +18 -0
- package/src/cvcs/childChangeType.v1.0.0.json +152 -0
- package/src/cvcs/hivStatus.v1.0.0.json +71 -0
- package/src/cvcs/immigrationLegalStatusChangeType.v1.0.0.json +143 -0
- package/src/cvcs/index.ts +15 -1
- package/src/cvcs/mentalHealthStatus.v1.0.0.json +71 -0
- package/src/cvcs/programEnrollmentChangeType.v1.0.0.json +134 -0
- package/src/cvcs/romanticRelationshipChangeType.v1.0.0.json +161 -0
- package/src/cvcs/{serviceCategories.v1.1.0.json → serviceCategories.v1.0.0.json} +1091 -1091
- package/src/cvcs/specialNeedsDisabilities.v1.0.0.json +143 -0
- package/src/generated/cvc-enums.ts +300 -144
|
@@ -0,0 +1,556 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Runtime Contract Validator — unit tests
|
|
3
|
+
*
|
|
4
|
+
* Covers:
|
|
5
|
+
* - Type coercion matrix (string, boolean, number, enum, url, json)
|
|
6
|
+
* - required fail-fast (single + aggregate)
|
|
7
|
+
* - Default application per env
|
|
8
|
+
* - client_visible filtering in createConfigEndpoint
|
|
9
|
+
* - secret exclusion in createConfigEndpoint
|
|
10
|
+
* - Build-phase variable skip at runtime (R3.4)
|
|
11
|
+
*
|
|
12
|
+
* Uses the fixture at src/config/fixtures/valid.yaml.
|
|
13
|
+
* Tests call validateAgainstContract with throwOnFailure: true to avoid
|
|
14
|
+
* process.exit(1) in the test runner.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import { readFileSync, writeFileSync, unlinkSync } from "node:fs";
|
|
18
|
+
import { join } from "node:path";
|
|
19
|
+
import { tmpdir } from "node:os";
|
|
20
|
+
import { fileURLToPath } from "node:url";
|
|
21
|
+
import { dirname } from "node:path";
|
|
22
|
+
|
|
23
|
+
import { describe, it, expect } from "vitest";
|
|
24
|
+
|
|
25
|
+
import {
|
|
26
|
+
validateAgainstContract,
|
|
27
|
+
createConfigEndpoint,
|
|
28
|
+
} from "../config/index.js";
|
|
29
|
+
|
|
30
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
31
|
+
const __dirname = dirname(__filename);
|
|
32
|
+
|
|
33
|
+
const FIXTURE_PATH = join(__dirname, "../config/fixtures/valid.yaml");
|
|
34
|
+
|
|
35
|
+
// ─── helpers ─────────────────────────────────────────────────────────────────
|
|
36
|
+
|
|
37
|
+
/** Write a temp YAML file and return its path. Caller must delete. */
|
|
38
|
+
function writeTempYaml(content: string): string {
|
|
39
|
+
const p = join(tmpdir(), `test-runtime-${Date.now()}.yaml`);
|
|
40
|
+
writeFileSync(p, content, "utf8");
|
|
41
|
+
return p;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Save and restore process.env around a test. */
|
|
45
|
+
function withEnv(
|
|
46
|
+
vars: Record<string, string | undefined>,
|
|
47
|
+
fn: () => void,
|
|
48
|
+
): void {
|
|
49
|
+
const saved: Record<string, string | undefined> = {};
|
|
50
|
+
for (const k of Object.keys(vars)) {
|
|
51
|
+
saved[k] = process.env[k];
|
|
52
|
+
}
|
|
53
|
+
for (const [k, v] of Object.entries(vars)) {
|
|
54
|
+
if (v === undefined) {
|
|
55
|
+
delete process.env[k];
|
|
56
|
+
} else {
|
|
57
|
+
process.env[k] = v;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
try {
|
|
61
|
+
fn();
|
|
62
|
+
} finally {
|
|
63
|
+
for (const [k, v] of Object.entries(saved)) {
|
|
64
|
+
if (v === undefined) {
|
|
65
|
+
delete process.env[k];
|
|
66
|
+
} else {
|
|
67
|
+
process.env[k] = v;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// ─── Type coercion matrix ─────────────────────────────────────────────────────
|
|
74
|
+
|
|
75
|
+
describe("validateAgainstContract — type coercion", () => {
|
|
76
|
+
it("coerces boolean true variants", () => {
|
|
77
|
+
withEnv(
|
|
78
|
+
{ HOST: "0.0.0.0", DB_SECRET: "mongodb://localhost", ENABLED: "true" },
|
|
79
|
+
() => {
|
|
80
|
+
const cfg = validateAgainstContract(FIXTURE_PATH, {
|
|
81
|
+
throwOnFailure: true,
|
|
82
|
+
env: "local",
|
|
83
|
+
});
|
|
84
|
+
expect(cfg["ENABLED"]).toBe(true);
|
|
85
|
+
},
|
|
86
|
+
);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it("coerces boolean false variant", () => {
|
|
90
|
+
withEnv(
|
|
91
|
+
{ HOST: "0.0.0.0", DB_SECRET: "mongodb://localhost", ENABLED: "false" },
|
|
92
|
+
() => {
|
|
93
|
+
const cfg = validateAgainstContract(FIXTURE_PATH, {
|
|
94
|
+
throwOnFailure: true,
|
|
95
|
+
env: "local",
|
|
96
|
+
});
|
|
97
|
+
expect(cfg["ENABLED"]).toBe(false);
|
|
98
|
+
},
|
|
99
|
+
);
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
it("coerces boolean with numeric 1/0", () => {
|
|
103
|
+
withEnv(
|
|
104
|
+
{ HOST: "0.0.0.0", DB_SECRET: "mongodb://localhost", ENABLED: "1" },
|
|
105
|
+
() => {
|
|
106
|
+
const cfg = validateAgainstContract(FIXTURE_PATH, {
|
|
107
|
+
throwOnFailure: true,
|
|
108
|
+
env: "local",
|
|
109
|
+
});
|
|
110
|
+
expect(cfg["ENABLED"]).toBe(true);
|
|
111
|
+
},
|
|
112
|
+
);
|
|
113
|
+
withEnv(
|
|
114
|
+
{ HOST: "0.0.0.0", DB_SECRET: "mongodb://localhost", ENABLED: "0" },
|
|
115
|
+
() => {
|
|
116
|
+
const cfg = validateAgainstContract(FIXTURE_PATH, {
|
|
117
|
+
throwOnFailure: true,
|
|
118
|
+
env: "local",
|
|
119
|
+
});
|
|
120
|
+
expect(cfg["ENABLED"]).toBe(false);
|
|
121
|
+
},
|
|
122
|
+
);
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
it("rejects invalid boolean", () => {
|
|
126
|
+
withEnv(
|
|
127
|
+
{ HOST: "0.0.0.0", DB_SECRET: "mongodb://localhost", ENABLED: "yes" },
|
|
128
|
+
() => {
|
|
129
|
+
expect(() =>
|
|
130
|
+
validateAgainstContract(FIXTURE_PATH, {
|
|
131
|
+
throwOnFailure: true,
|
|
132
|
+
env: "local",
|
|
133
|
+
}),
|
|
134
|
+
).toThrow(/ENABLED.*boolean/);
|
|
135
|
+
},
|
|
136
|
+
);
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
it("coerces number", () => {
|
|
140
|
+
withEnv(
|
|
141
|
+
{ HOST: "0.0.0.0", DB_SECRET: "mongodb://localhost", PORT: "8080" },
|
|
142
|
+
() => {
|
|
143
|
+
const cfg = validateAgainstContract(FIXTURE_PATH, {
|
|
144
|
+
throwOnFailure: true,
|
|
145
|
+
env: "local",
|
|
146
|
+
});
|
|
147
|
+
expect(cfg["PORT"]).toBe(8080);
|
|
148
|
+
},
|
|
149
|
+
);
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it("rejects invalid number", () => {
|
|
153
|
+
withEnv(
|
|
154
|
+
{
|
|
155
|
+
HOST: "0.0.0.0",
|
|
156
|
+
DB_SECRET: "mongodb://localhost",
|
|
157
|
+
PORT: "not-a-number",
|
|
158
|
+
},
|
|
159
|
+
() => {
|
|
160
|
+
expect(() =>
|
|
161
|
+
validateAgainstContract(FIXTURE_PATH, {
|
|
162
|
+
throwOnFailure: true,
|
|
163
|
+
env: "local",
|
|
164
|
+
}),
|
|
165
|
+
).toThrow(/PORT.*number/);
|
|
166
|
+
},
|
|
167
|
+
);
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
it("coerces valid enum value", () => {
|
|
171
|
+
withEnv(
|
|
172
|
+
{
|
|
173
|
+
HOST: "0.0.0.0",
|
|
174
|
+
DB_SECRET: "mongodb://localhost",
|
|
175
|
+
NODE_ENV: "production",
|
|
176
|
+
},
|
|
177
|
+
() => {
|
|
178
|
+
const cfg = validateAgainstContract(FIXTURE_PATH, {
|
|
179
|
+
throwOnFailure: true,
|
|
180
|
+
env: "local",
|
|
181
|
+
});
|
|
182
|
+
expect(cfg["NODE_ENV"]).toBe("production");
|
|
183
|
+
},
|
|
184
|
+
);
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
it("rejects invalid enum value", () => {
|
|
188
|
+
withEnv(
|
|
189
|
+
{
|
|
190
|
+
HOST: "0.0.0.0",
|
|
191
|
+
DB_SECRET: "mongodb://localhost",
|
|
192
|
+
NODE_ENV: "staging",
|
|
193
|
+
},
|
|
194
|
+
() => {
|
|
195
|
+
expect(() =>
|
|
196
|
+
validateAgainstContract(FIXTURE_PATH, {
|
|
197
|
+
throwOnFailure: true,
|
|
198
|
+
env: "local",
|
|
199
|
+
}),
|
|
200
|
+
).toThrow(/NODE_ENV.*development.*production.*test/);
|
|
201
|
+
},
|
|
202
|
+
);
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
it("coerces valid URL", () => {
|
|
206
|
+
withEnv(
|
|
207
|
+
{
|
|
208
|
+
HOST: "0.0.0.0",
|
|
209
|
+
DB_SECRET: "mongodb://localhost",
|
|
210
|
+
API_URL: "https://api.example.com",
|
|
211
|
+
},
|
|
212
|
+
() => {
|
|
213
|
+
const cfg = validateAgainstContract(FIXTURE_PATH, {
|
|
214
|
+
throwOnFailure: true,
|
|
215
|
+
env: "local",
|
|
216
|
+
});
|
|
217
|
+
expect(cfg["API_URL"]).toBe("https://api.example.com");
|
|
218
|
+
},
|
|
219
|
+
);
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
it("rejects invalid URL", () => {
|
|
223
|
+
withEnv(
|
|
224
|
+
{
|
|
225
|
+
HOST: "0.0.0.0",
|
|
226
|
+
DB_SECRET: "mongodb://localhost",
|
|
227
|
+
API_URL: "not-a-url",
|
|
228
|
+
},
|
|
229
|
+
() => {
|
|
230
|
+
expect(() =>
|
|
231
|
+
validateAgainstContract(FIXTURE_PATH, {
|
|
232
|
+
throwOnFailure: true,
|
|
233
|
+
env: "local",
|
|
234
|
+
}),
|
|
235
|
+
).toThrow(/API_URL.*URL/);
|
|
236
|
+
},
|
|
237
|
+
);
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
it("coerces valid JSON", () => {
|
|
241
|
+
withEnv(
|
|
242
|
+
{
|
|
243
|
+
HOST: "0.0.0.0",
|
|
244
|
+
DB_SECRET: "mongodb://localhost",
|
|
245
|
+
DEBUG_JSON: '{"key":"value"}',
|
|
246
|
+
},
|
|
247
|
+
() => {
|
|
248
|
+
const cfg = validateAgainstContract(FIXTURE_PATH, {
|
|
249
|
+
throwOnFailure: true,
|
|
250
|
+
env: "local",
|
|
251
|
+
});
|
|
252
|
+
expect(cfg["DEBUG_JSON"]).toEqual({ key: "value" });
|
|
253
|
+
},
|
|
254
|
+
);
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
it("rejects invalid JSON", () => {
|
|
258
|
+
withEnv(
|
|
259
|
+
{
|
|
260
|
+
HOST: "0.0.0.0",
|
|
261
|
+
DB_SECRET: "mongodb://localhost",
|
|
262
|
+
DEBUG_JSON: "{bad-json}",
|
|
263
|
+
},
|
|
264
|
+
() => {
|
|
265
|
+
expect(() =>
|
|
266
|
+
validateAgainstContract(FIXTURE_PATH, {
|
|
267
|
+
throwOnFailure: true,
|
|
268
|
+
env: "local",
|
|
269
|
+
}),
|
|
270
|
+
).toThrow(/DEBUG_JSON.*JSON/);
|
|
271
|
+
},
|
|
272
|
+
);
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
it("passes string through unchanged", () => {
|
|
276
|
+
withEnv(
|
|
277
|
+
{ HOST: "my-host", DB_SECRET: "mongodb://localhost" },
|
|
278
|
+
() => {
|
|
279
|
+
const cfg = validateAgainstContract(FIXTURE_PATH, {
|
|
280
|
+
throwOnFailure: true,
|
|
281
|
+
env: "local",
|
|
282
|
+
});
|
|
283
|
+
expect(cfg["HOST"]).toBe("my-host");
|
|
284
|
+
},
|
|
285
|
+
);
|
|
286
|
+
});
|
|
287
|
+
});
|
|
288
|
+
|
|
289
|
+
// ─── Required fail-fast ───────────────────────────────────────────────────────
|
|
290
|
+
|
|
291
|
+
describe("validateAgainstContract — required fail-fast", () => {
|
|
292
|
+
it("fails when a required var is missing and has no matching default", () => {
|
|
293
|
+
withEnv({ HOST: undefined, DB_SECRET: undefined, NODE_ENV: undefined }, () => {
|
|
294
|
+
expect(() =>
|
|
295
|
+
validateAgainstContract(FIXTURE_PATH, {
|
|
296
|
+
throwOnFailure: true,
|
|
297
|
+
env: "prod",
|
|
298
|
+
}),
|
|
299
|
+
).toThrow(/required but not set/);
|
|
300
|
+
});
|
|
301
|
+
});
|
|
302
|
+
|
|
303
|
+
it("aggregates multiple missing vars in one error", () => {
|
|
304
|
+
const yaml = `
|
|
305
|
+
service: multi-fail
|
|
306
|
+
version: 1
|
|
307
|
+
environments: [local, prod]
|
|
308
|
+
variables:
|
|
309
|
+
ALPHA:
|
|
310
|
+
type: string
|
|
311
|
+
required: true
|
|
312
|
+
secret: false
|
|
313
|
+
description: Alpha var.
|
|
314
|
+
BETA:
|
|
315
|
+
type: string
|
|
316
|
+
required: true
|
|
317
|
+
secret: false
|
|
318
|
+
description: Beta var.
|
|
319
|
+
GAMMA:
|
|
320
|
+
type: string
|
|
321
|
+
required: true
|
|
322
|
+
secret: false
|
|
323
|
+
description: Gamma var.
|
|
324
|
+
`;
|
|
325
|
+
const p = writeTempYaml(yaml);
|
|
326
|
+
try {
|
|
327
|
+
withEnv({ ALPHA: undefined, BETA: undefined, GAMMA: undefined }, () => {
|
|
328
|
+
let caught: Error | undefined;
|
|
329
|
+
try {
|
|
330
|
+
validateAgainstContract(p, { throwOnFailure: true, env: "prod" });
|
|
331
|
+
} catch (err) {
|
|
332
|
+
caught = err as Error;
|
|
333
|
+
}
|
|
334
|
+
expect(caught).toBeDefined();
|
|
335
|
+
expect(caught?.message).toContain("ALPHA");
|
|
336
|
+
expect(caught?.message).toContain("BETA");
|
|
337
|
+
expect(caught?.message).toContain("GAMMA");
|
|
338
|
+
});
|
|
339
|
+
} finally {
|
|
340
|
+
unlinkSync(p);
|
|
341
|
+
}
|
|
342
|
+
});
|
|
343
|
+
|
|
344
|
+
it("succeeds when a required var is satisfied by env default", () => {
|
|
345
|
+
// HOST has default.local = "0.0.0.0"
|
|
346
|
+
withEnv({ HOST: undefined, DB_SECRET: "mongodb://localhost" }, () => {
|
|
347
|
+
expect(() =>
|
|
348
|
+
validateAgainstContract(FIXTURE_PATH, {
|
|
349
|
+
throwOnFailure: true,
|
|
350
|
+
env: "local",
|
|
351
|
+
}),
|
|
352
|
+
).not.toThrow();
|
|
353
|
+
});
|
|
354
|
+
});
|
|
355
|
+
});
|
|
356
|
+
|
|
357
|
+
// ─── Default application per env ────────────────────────────────────────────
|
|
358
|
+
|
|
359
|
+
describe("validateAgainstContract — default per env", () => {
|
|
360
|
+
it("applies default for the explicit env", () => {
|
|
361
|
+
withEnv(
|
|
362
|
+
{ NODE_ENV: undefined, HOST: "0.0.0.0", DB_SECRET: "mongodb://localhost" },
|
|
363
|
+
() => {
|
|
364
|
+
const cfg = validateAgainstContract(FIXTURE_PATH, {
|
|
365
|
+
throwOnFailure: true,
|
|
366
|
+
env: "test",
|
|
367
|
+
});
|
|
368
|
+
// NODE_ENV has default.test = "test"
|
|
369
|
+
expect(cfg["NODE_ENV"]).toBe("test");
|
|
370
|
+
},
|
|
371
|
+
);
|
|
372
|
+
});
|
|
373
|
+
|
|
374
|
+
it("applies default.local when env is local", () => {
|
|
375
|
+
withEnv(
|
|
376
|
+
{
|
|
377
|
+
NODE_ENV: undefined,
|
|
378
|
+
HOST: undefined,
|
|
379
|
+
DB_SECRET: "mongodb://localhost",
|
|
380
|
+
},
|
|
381
|
+
() => {
|
|
382
|
+
const cfg = validateAgainstContract(FIXTURE_PATH, {
|
|
383
|
+
throwOnFailure: true,
|
|
384
|
+
env: "local",
|
|
385
|
+
});
|
|
386
|
+
// HOST has default.local = "0.0.0.0"
|
|
387
|
+
expect(cfg["HOST"]).toBe("0.0.0.0");
|
|
388
|
+
},
|
|
389
|
+
);
|
|
390
|
+
});
|
|
391
|
+
|
|
392
|
+
it("process.env takes priority over default", () => {
|
|
393
|
+
withEnv(
|
|
394
|
+
{ HOST: "override-host", DB_SECRET: "mongodb://localhost" },
|
|
395
|
+
() => {
|
|
396
|
+
const cfg = validateAgainstContract(FIXTURE_PATH, {
|
|
397
|
+
throwOnFailure: true,
|
|
398
|
+
env: "local",
|
|
399
|
+
});
|
|
400
|
+
expect(cfg["HOST"]).toBe("override-host");
|
|
401
|
+
},
|
|
402
|
+
);
|
|
403
|
+
});
|
|
404
|
+
});
|
|
405
|
+
|
|
406
|
+
// ─── Build-phase skip ────────────────────────────────────────────────────────
|
|
407
|
+
|
|
408
|
+
describe("validateAgainstContract — build-phase skip (R3.4)", () => {
|
|
409
|
+
it("does not require build-phase vars at runtime", () => {
|
|
410
|
+
// BUILD_ARG is phase: build in the fixture; it MUST NOT be required at runtime
|
|
411
|
+
withEnv(
|
|
412
|
+
{ HOST: "0.0.0.0", DB_SECRET: "mongodb://localhost", BUILD_ARG: undefined },
|
|
413
|
+
() => {
|
|
414
|
+
expect(() =>
|
|
415
|
+
validateAgainstContract(FIXTURE_PATH, {
|
|
416
|
+
throwOnFailure: true,
|
|
417
|
+
env: "local",
|
|
418
|
+
}),
|
|
419
|
+
).not.toThrow();
|
|
420
|
+
},
|
|
421
|
+
);
|
|
422
|
+
});
|
|
423
|
+
|
|
424
|
+
it("does not include build-phase vars in returned config", () => {
|
|
425
|
+
withEnv(
|
|
426
|
+
{
|
|
427
|
+
HOST: "0.0.0.0",
|
|
428
|
+
DB_SECRET: "mongodb://localhost",
|
|
429
|
+
BUILD_ARG: "some-build-value",
|
|
430
|
+
},
|
|
431
|
+
() => {
|
|
432
|
+
const cfg = validateAgainstContract(FIXTURE_PATH, {
|
|
433
|
+
throwOnFailure: true,
|
|
434
|
+
env: "local",
|
|
435
|
+
});
|
|
436
|
+
expect(Object.keys(cfg)).not.toContain("BUILD_ARG");
|
|
437
|
+
},
|
|
438
|
+
);
|
|
439
|
+
});
|
|
440
|
+
});
|
|
441
|
+
|
|
442
|
+
// ─── createConfigEndpoint — client_visible filtering ─────────────────────────
|
|
443
|
+
|
|
444
|
+
describe("createConfigEndpoint — client_visible filtering (R7.2)", () => {
|
|
445
|
+
it("returns only client_visible vars in the Response body", async () => {
|
|
446
|
+
withEnv(
|
|
447
|
+
{
|
|
448
|
+
HOST: "0.0.0.0",
|
|
449
|
+
DB_SECRET: "mongodb://localhost",
|
|
450
|
+
PUBLIC_API_URL: "https://api.public.com",
|
|
451
|
+
NODE_ENV: "development",
|
|
452
|
+
},
|
|
453
|
+
() => {
|
|
454
|
+
const handler = createConfigEndpoint(FIXTURE_PATH);
|
|
455
|
+
const result = handler();
|
|
456
|
+
expect(result).toBeInstanceOf(Response);
|
|
457
|
+
const res = result as Response;
|
|
458
|
+
return res.json().then((body: unknown) => {
|
|
459
|
+
const b = body as Record<string, unknown>;
|
|
460
|
+
// PUBLIC_API_URL is client_visible: true
|
|
461
|
+
expect(b).toHaveProperty("PUBLIC_API_URL");
|
|
462
|
+
// HOST is NOT client_visible
|
|
463
|
+
expect(b).not.toHaveProperty("HOST");
|
|
464
|
+
// NODE_ENV is NOT client_visible
|
|
465
|
+
expect(b).not.toHaveProperty("NODE_ENV");
|
|
466
|
+
});
|
|
467
|
+
},
|
|
468
|
+
);
|
|
469
|
+
});
|
|
470
|
+
});
|
|
471
|
+
|
|
472
|
+
// ─── createConfigEndpoint — secret exclusion ─────────────────────────────────
|
|
473
|
+
|
|
474
|
+
describe("createConfigEndpoint — secret exclusion (R7.3)", () => {
|
|
475
|
+
it("never exposes secret vars even if client_visible were set", () => {
|
|
476
|
+
// Build a temp yaml with a secret + client_visible var (spec violation,
|
|
477
|
+
// but we test the endpoint enforces R7.3 as a hard gate regardless).
|
|
478
|
+
const yaml = `
|
|
479
|
+
service: secret-test
|
|
480
|
+
version: 1
|
|
481
|
+
environments: [local]
|
|
482
|
+
variables:
|
|
483
|
+
SECRET_VAR:
|
|
484
|
+
type: string
|
|
485
|
+
required: false
|
|
486
|
+
secret: true
|
|
487
|
+
client_visible: false
|
|
488
|
+
description: A secret that must never be exposed.
|
|
489
|
+
default:
|
|
490
|
+
local: local-placeholder
|
|
491
|
+
PUBLIC_VAR:
|
|
492
|
+
type: string
|
|
493
|
+
required: false
|
|
494
|
+
secret: false
|
|
495
|
+
client_visible: true
|
|
496
|
+
description: A public value.
|
|
497
|
+
default:
|
|
498
|
+
local: hello
|
|
499
|
+
`;
|
|
500
|
+
const p = writeTempYaml(yaml);
|
|
501
|
+
try {
|
|
502
|
+
withEnv({ SECRET_VAR: "should-not-appear", PUBLIC_VAR: "visible" }, () => {
|
|
503
|
+
const handler = createConfigEndpoint(p);
|
|
504
|
+
const result = handler();
|
|
505
|
+
expect(result).toBeInstanceOf(Response);
|
|
506
|
+
const res = result as Response;
|
|
507
|
+
return res.json().then((body: unknown) => {
|
|
508
|
+
const b = body as Record<string, unknown>;
|
|
509
|
+
expect(b).not.toHaveProperty("SECRET_VAR");
|
|
510
|
+
expect(b).toHaveProperty("PUBLIC_VAR", "visible");
|
|
511
|
+
});
|
|
512
|
+
});
|
|
513
|
+
} finally {
|
|
514
|
+
unlinkSync(p);
|
|
515
|
+
}
|
|
516
|
+
});
|
|
517
|
+
});
|
|
518
|
+
|
|
519
|
+
// ─── createConfigEndpoint — cache (O(1) after warm-up) ───────────────────────
|
|
520
|
+
|
|
521
|
+
describe("createConfigEndpoint — caching", () => {
|
|
522
|
+
it("returns the same payload on repeated calls", async () => {
|
|
523
|
+
withEnv(
|
|
524
|
+
{ HOST: "0.0.0.0", DB_SECRET: "mongodb://localhost" },
|
|
525
|
+
async () => {
|
|
526
|
+
const handler = createConfigEndpoint(FIXTURE_PATH);
|
|
527
|
+
const r1 = handler() as Response;
|
|
528
|
+
const r2 = handler() as Response;
|
|
529
|
+
const b1 = await r1.json();
|
|
530
|
+
const b2 = await r2.json();
|
|
531
|
+
expect(JSON.stringify(b1)).toBe(JSON.stringify(b2));
|
|
532
|
+
},
|
|
533
|
+
);
|
|
534
|
+
});
|
|
535
|
+
});
|
|
536
|
+
|
|
537
|
+
// ─── require() smoke test (CJS dual-publish check) ───────────────────────────
|
|
538
|
+
// This test is intentionally a no-op at the module level — actual CJS smoke
|
|
539
|
+
// runs via `node --require` after build. The test below validates the ESM
|
|
540
|
+
// surface exports the expected symbols (the tsup dual build makes both equal).
|
|
541
|
+
|
|
542
|
+
describe("module exports — surface check", () => {
|
|
543
|
+
it("exports validateAgainstContract and createConfigEndpoint", () => {
|
|
544
|
+
expect(typeof validateAgainstContract).toBe("function");
|
|
545
|
+
expect(typeof createConfigEndpoint).toBe("function");
|
|
546
|
+
});
|
|
547
|
+
});
|
|
548
|
+
|
|
549
|
+
// ─── Fixture file readable ───────────────────────────────────────────────────
|
|
550
|
+
|
|
551
|
+
describe("fixture", () => {
|
|
552
|
+
it("fixture file exists and is readable", () => {
|
|
553
|
+
const content = readFileSync(FIXTURE_PATH, "utf8");
|
|
554
|
+
expect(content).toContain("service: test-svc");
|
|
555
|
+
});
|
|
556
|
+
});
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
service: test-svc
|
|
2
|
+
version: 1
|
|
3
|
+
environments:
|
|
4
|
+
- local
|
|
5
|
+
- dev
|
|
6
|
+
- test
|
|
7
|
+
- prod
|
|
8
|
+
|
|
9
|
+
variables:
|
|
10
|
+
NODE_ENV:
|
|
11
|
+
type: enum
|
|
12
|
+
values: [development, production, test]
|
|
13
|
+
required: false
|
|
14
|
+
secret: false
|
|
15
|
+
description: Node runtime mode.
|
|
16
|
+
default:
|
|
17
|
+
local: development
|
|
18
|
+
test: test
|
|
19
|
+
prod: production
|
|
20
|
+
|
|
21
|
+
PORT:
|
|
22
|
+
type: number
|
|
23
|
+
required: false
|
|
24
|
+
secret: false
|
|
25
|
+
description: Listen port.
|
|
26
|
+
default:
|
|
27
|
+
local: 3000
|
|
28
|
+
|
|
29
|
+
HOST:
|
|
30
|
+
type: string
|
|
31
|
+
required: true
|
|
32
|
+
secret: false
|
|
33
|
+
description: Bind address.
|
|
34
|
+
default:
|
|
35
|
+
local: 0.0.0.0
|
|
36
|
+
|
|
37
|
+
DEBUG_JSON:
|
|
38
|
+
type: json
|
|
39
|
+
required: false
|
|
40
|
+
secret: false
|
|
41
|
+
description: Debug JSON payload.
|
|
42
|
+
|
|
43
|
+
API_URL:
|
|
44
|
+
type: url
|
|
45
|
+
required: false
|
|
46
|
+
secret: false
|
|
47
|
+
description: External API URL.
|
|
48
|
+
default:
|
|
49
|
+
local: http://localhost:4000
|
|
50
|
+
|
|
51
|
+
DB_SECRET:
|
|
52
|
+
type: string
|
|
53
|
+
required: true
|
|
54
|
+
secret: true
|
|
55
|
+
description: Database connection secret.
|
|
56
|
+
default:
|
|
57
|
+
local: mongodb://localhost:27017
|
|
58
|
+
|
|
59
|
+
ENABLED:
|
|
60
|
+
type: boolean
|
|
61
|
+
required: false
|
|
62
|
+
secret: false
|
|
63
|
+
description: Feature flag.
|
|
64
|
+
default:
|
|
65
|
+
local: true
|
|
66
|
+
|
|
67
|
+
THEME:
|
|
68
|
+
type: enum
|
|
69
|
+
values: [light, dark, system]
|
|
70
|
+
required: false
|
|
71
|
+
secret: false
|
|
72
|
+
description: UI theme.
|
|
73
|
+
|
|
74
|
+
PUBLIC_API_URL:
|
|
75
|
+
type: url
|
|
76
|
+
required: false
|
|
77
|
+
secret: false
|
|
78
|
+
client_visible: true
|
|
79
|
+
description: Public API URL shown in browser.
|
|
80
|
+
default:
|
|
81
|
+
local: http://localhost:4000
|
|
82
|
+
|
|
83
|
+
BUILD_ARG:
|
|
84
|
+
type: string
|
|
85
|
+
required: false
|
|
86
|
+
secret: false
|
|
87
|
+
phase: build
|
|
88
|
+
description: Build-time argument.
|
|
89
|
+
default:
|
|
90
|
+
local: build-value
|