@hop-top/kit 0.5.0-alpha.5 → 0.5.0-alpha.7

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.
Files changed (70) hide show
  1. package/CHANGELOG.md +43 -0
  2. package/LICENSE-cobra.txt +174 -0
  3. package/NOTICE +15 -0
  4. package/README.md +10 -10
  5. package/dist/{alias-BOj0-VAr.d.ts → alias-BsJ2jGqQ.d.ts} +9 -1
  6. package/dist/alias.d.ts +1 -1
  7. package/dist/alias.js +4 -2
  8. package/dist/chunk-263IZ6Z6.js +915 -0
  9. package/dist/{chunk-S45UHXQF.js → chunk-DIDN4X5C.js} +8 -6
  10. package/dist/{chunk-ZC5TZYZJ.js → chunk-GHPJJV5L.js} +7 -3
  11. package/dist/chunk-JKMQSAJP.js +174 -0
  12. package/dist/chunk-JZ22ZUFA.js +1131 -0
  13. package/dist/chunk-NLMS7HGP.js +178 -0
  14. package/dist/chunk-QQQYCWSC.js +240 -0
  15. package/dist/chunk-XJTRY453.js +868 -0
  16. package/dist/{cli-8rfITnv4.d.ts → cli-C5B5tnpA.d.ts} +1 -60
  17. package/dist/cli.d.ts +2 -1
  18. package/dist/cli.js +5 -3
  19. package/dist/completion-sDKVXPUL.d.ts +109 -0
  20. package/dist/completion-values-By-mnjNt.d.ts +142 -0
  21. package/dist/completion-values.d.ts +1 -0
  22. package/dist/completion-values.js +29 -0
  23. package/dist/completion.d.ts +3 -0
  24. package/dist/completion.js +34 -0
  25. package/dist/compliance-AxFf-uMy.d.ts +60 -0
  26. package/dist/compliance.d.ts +1 -0
  27. package/dist/compliance.js +17 -0
  28. package/dist/config-CaK60ByQ.d.ts +241 -0
  29. package/dist/config.d.ts +1 -1
  30. package/dist/config.js +4 -2
  31. package/dist/{index-DEyjGoo2.d.ts → index-DCe1boBz.d.ts} +1 -1
  32. package/dist/index.d.ts +10 -4
  33. package/dist/index.js +40 -20
  34. package/dist/log-CkaxMX3U.d.ts +115 -0
  35. package/dist/log.d.ts +2 -0
  36. package/dist/log.js +14 -0
  37. package/dist/parity-CvMuYcVI.d.ts +61 -0
  38. package/dist/toolspec-UrWnoJ3Y.d.ts +336 -0
  39. package/dist/toolspec.d.ts +2 -0
  40. package/dist/toolspec.js +36 -0
  41. package/dist/tui/index.d.ts +3 -2
  42. package/package.json +43 -3
  43. package/src/alias.test.ts +110 -1
  44. package/src/alias.ts +19 -4
  45. package/src/cli.ts +5 -0
  46. package/src/completion-scripts.ts +16 -0
  47. package/src/completion-values.test.ts +70 -10
  48. package/src/completion-values.ts +170 -66
  49. package/src/completion.conformance.test.ts +297 -0
  50. package/src/completion.test.ts +256 -145
  51. package/src/completion.ts +764 -121
  52. package/src/config-merge.test.ts +425 -0
  53. package/src/config.test.ts +41 -0
  54. package/src/config.ts +265 -18
  55. package/src/index.test.ts +2 -1
  56. package/src/index.ts +8 -1
  57. package/src/log.test.ts +270 -1
  58. package/src/log.ts +172 -44
  59. package/src/published-types.test.ts +18 -0
  60. package/src/toolspec-internal.ts +197 -0
  61. package/src/toolspec-manifest.test.ts +82 -0
  62. package/src/toolspec-manifest.ts +404 -0
  63. package/src/toolspec-spec-schemas.ts +12 -0
  64. package/src/toolspec-spec.test.ts +273 -0
  65. package/src/toolspec-spec.ts +458 -0
  66. package/src/toolspec.from-command.test.ts +216 -0
  67. package/src/toolspec.ts +159 -0
  68. package/test/toolspec/fixture.ts +73 -0
  69. package/dist/chunk-IQXEISMC.js +0 -142
  70. package/dist/config-Bm9fBXch.d.ts +0 -134
@@ -0,0 +1,425 @@
1
+ /**
2
+ * Tests for the exported layer merge (mergeLayers), its policy options, and
3
+ * load()'s parse and merge options.
4
+ */
5
+
6
+ import * as fs from "fs";
7
+ import * as os from "os";
8
+ import * as path from "path";
9
+ import { describe, it, expect } from "vitest";
10
+ import { load, mergeLayers, type ArrayMergeFn, type MergeOptions } from "./config";
11
+
12
+ function tmpFile(ext: string, content: string): string {
13
+ const p = path.join(os.tmpdir(), `config-merge-${Math.random().toString(36).slice(2)}${ext}`);
14
+ fs.writeFileSync(p, content, "utf8");
15
+ return p;
16
+ }
17
+
18
+ function cleanup(...paths: string[]): void {
19
+ for (const p of paths) {
20
+ try { fs.unlinkSync(p); } catch { /* ignore */ }
21
+ }
22
+ }
23
+
24
+ // ---------------------------------------------------------------------------
25
+ // Defaults: same rules as load()
26
+ // ---------------------------------------------------------------------------
27
+
28
+ describe("mergeLayers defaults", () => {
29
+ it("returns a fresh object and never mutates the layers", () => {
30
+ const a = { db: { host: "x", port: 1 }, tags: ["a"] };
31
+ const b = { db: { port: 2 } };
32
+ const got = mergeLayers([a, b]);
33
+ expect(got).toEqual({ db: { host: "x", port: 2 }, tags: ["a"] });
34
+ expect(a).toEqual({ db: { host: "x", port: 1 }, tags: ["a"] });
35
+ expect(got.db).not.toBe(a.db);
36
+ expect(got.tags).not.toBe(a.tags);
37
+ });
38
+
39
+ it("merges recursively, replaces arrays, null clears objects/arrays but not scalars", () => {
40
+ const got = mergeLayers([
41
+ { a: { b: { c: 1, d: 2 } }, tags: ["x"], m: { k: 1 }, name: "n", list: [1] },
42
+ { a: { b: { d: 3 } }, tags: ["y"], m: null, name: null, list: null, fresh: null },
43
+ ]);
44
+ expect(got).toEqual({
45
+ a: { b: { c: 1, d: 3 } },
46
+ tags: ["y"],
47
+ m: null,
48
+ name: "n",
49
+ list: null,
50
+ fresh: null,
51
+ });
52
+ });
53
+
54
+ it("skips null, undefined and non-mapping layers", () => {
55
+ expect(mergeLayers([{ a: 1 }, null, undefined, ["x"], "s", 3, { b: 2 }])).toEqual({ a: 1, b: 2 });
56
+ expect(mergeLayers([])).toEqual({});
57
+ });
58
+
59
+ it("matches load() on the same layers", () => {
60
+ const bodies = ["db:\n host: x\n port: 1\ntags: [a]\nn: 1\n", "db:\n port: 2\ntags: [b]\nn: ~\nm: ~\n"];
61
+ const files = bodies.map((b) => tmpFile(".yaml", b));
62
+ try {
63
+ const viaLoad = load({}, { systemConfigPath: files[0], userConfigPath: files[1] });
64
+ const viaMerge = mergeLayers([
65
+ { db: { host: "x", port: 1 }, tags: ["a"], n: 1 },
66
+ { db: { port: 2 }, tags: ["b"], n: null, m: null },
67
+ ]);
68
+ expect(viaMerge).toEqual(viaLoad);
69
+ } finally {
70
+ cleanup(...files);
71
+ }
72
+ });
73
+
74
+ it("does not let __proto__ keys reach Object.prototype", () => {
75
+ const layer = JSON.parse('{"__proto__": {"polluted": 1}}') as object;
76
+ const got = mergeLayers([layer, layer]);
77
+ expect(({} as Record<string, unknown>).polluted).toBeUndefined();
78
+ expect(Object.getPrototypeOf(got)).toBe(Object.prototype);
79
+ });
80
+
81
+ it("rejects a cyclic layer instead of recursing forever", () => {
82
+ const cyc: Record<string, unknown> = { a: 1 };
83
+ cyc.self = cyc;
84
+ expect(() => mergeLayers([cyc])).toThrow(/config: layer 0: nesting exceeded maxDepth \(100\)/);
85
+ });
86
+ });
87
+
88
+ // ---------------------------------------------------------------------------
89
+ // nullPolicy
90
+ // ---------------------------------------------------------------------------
91
+
92
+ describe("mergeLayers nullPolicy", () => {
93
+ it("keep-lower: null never replaces a lower value of any type", () => {
94
+ const got = mergeLayers(
95
+ [
96
+ { m: { a: 1 }, tags: ["x"], name: "n", deep: { inner: { k: "v" } } },
97
+ { m: null, tags: null, name: null, deep: { inner: null } },
98
+ ],
99
+ { nullPolicy: "keep-lower" },
100
+ );
101
+ expect(got).toEqual({ m: { a: 1 }, tags: ["x"], name: "n", deep: { inner: { k: "v" } } });
102
+ });
103
+
104
+ it("keep-lower: null at a key no lower layer set leaves it absent", () => {
105
+ const got = mergeLayers([{ a: 1 }, { fresh: null }], { nullPolicy: "keep-lower" });
106
+ expect(got).toEqual({ a: 1 });
107
+ expect(Object.prototype.hasOwnProperty.call(got, "fresh")).toBe(false);
108
+ });
109
+
110
+ it("keep-lower: nulls inside a subtree copied whole are kept", () => {
111
+ const got = mergeLayers([{}, { db: { host: null } }], { nullPolicy: "keep-lower" });
112
+ expect(got).toEqual({ db: { host: null } });
113
+ });
114
+
115
+ it("clear is the default", () => {
116
+ const layers = [{ m: { a: 1 } }, { m: null }];
117
+ expect(mergeLayers(layers, { nullPolicy: "clear" })).toEqual(mergeLayers(layers));
118
+ });
119
+ });
120
+
121
+ // ---------------------------------------------------------------------------
122
+ // maxDepth
123
+ // ---------------------------------------------------------------------------
124
+
125
+ describe("mergeLayers maxDepth", () => {
126
+ const lower = { a: { x: 1, n: { p: 1, q: 1 } }, top: 1 };
127
+ const higher = { a: { y: 2, n: { q: 2 } } };
128
+
129
+ it("Infinity (default) merges at every depth", () => {
130
+ expect(mergeLayers([lower, higher])).toEqual({ a: { x: 1, y: 2, n: { p: 1, q: 2 } }, top: 1 });
131
+ expect(mergeLayers([lower, higher], { maxDepth: Infinity })).toEqual(mergeLayers([lower, higher]));
132
+ });
133
+
134
+ it("1 merges objects one level below the root; deeper objects replace", () => {
135
+ expect(mergeLayers([lower, higher], { maxDepth: 1 })).toEqual({
136
+ a: { x: 1, y: 2, n: { q: 2 } },
137
+ top: 1,
138
+ });
139
+ });
140
+
141
+ it("0 merges root keys only; every nested object replaces", () => {
142
+ expect(mergeLayers([lower, higher], { maxDepth: 0 })).toEqual({ a: { y: 2, n: { q: 2 } }, top: 1 });
143
+ });
144
+
145
+ it("rejects a negative or non-integer depth", () => {
146
+ expect(() => mergeLayers([], { maxDepth: -1 })).toThrow(/maxDepth/);
147
+ expect(() => mergeLayers([], { maxDepth: 1.5 })).toThrow(/maxDepth/);
148
+ expect(() => mergeLayers([], { maxDepth: NaN })).toThrow(/maxDepth/);
149
+ });
150
+ });
151
+
152
+ // ---------------------------------------------------------------------------
153
+ // arrayPolicy
154
+ // ---------------------------------------------------------------------------
155
+
156
+ describe("mergeLayers arrayPolicy", () => {
157
+ it("concat appends the higher array to the lower one", () => {
158
+ const got = mergeLayers([{ t: ["a", "b"] }, { t: ["b", "c"] }, { t: ["d"] }], { arrayPolicy: "concat" });
159
+ expect(got).toEqual({ t: ["a", "b", "b", "c", "d"] });
160
+ });
161
+
162
+ it("union keeps the first occurrence of each item, lower items first", () => {
163
+ const got = mergeLayers([{ t: ["a", "b", "a"] }, { t: ["c", "b"] }], { arrayPolicy: "union" });
164
+ expect(got).toEqual({ t: ["a", "b", "c"] });
165
+ });
166
+
167
+ it("union compares objects and arrays structurally, key order ignored", () => {
168
+ const got = mergeLayers(
169
+ [
170
+ { t: [{ k: 1, j: 2 }, [1, 2], NaN] },
171
+ { t: [{ j: 2, k: 1 }, [1, 2], [2, 1], { k: 1 }, NaN, "1", 1] },
172
+ ],
173
+ { arrayPolicy: "union" },
174
+ );
175
+ expect(got).toEqual({ t: [{ k: 1, j: 2 }, [1, 2], NaN, [2, 1], { k: 1 }, "1", 1] });
176
+ });
177
+
178
+ it("concat and union act only when both sides are arrays; otherwise the higher value replaces", () => {
179
+ const opts: MergeOptions = { arrayPolicy: "union" };
180
+ expect(mergeLayers([{}, { t: ["a", "a"] }], opts)).toEqual({ t: ["a", "a"] });
181
+ expect(mergeLayers([{ t: "s" }, { t: ["a"] }], opts)).toEqual({ t: ["a"] });
182
+ expect(mergeLayers([{ t: ["a"] }, { t: "s" }], opts)).toEqual({ t: "s" });
183
+ });
184
+
185
+ it("applies at every merged depth, not inside objects that replace", () => {
186
+ const layers = [{ a: { t: [1], n: { t: [1] } } }, { a: { t: [2], n: { t: [2] } } }];
187
+ expect(mergeLayers(layers, { arrayPolicy: "concat", maxDepth: 1 })).toEqual({
188
+ a: { t: [1, 2], n: { t: [2] } },
189
+ });
190
+ });
191
+
192
+ it("does not share items with the layers", () => {
193
+ const item = { k: 1 };
194
+ const got = mergeLayers([{ t: [] }, { t: [item] }], { arrayPolicy: "concat" });
195
+ expect((got.t as unknown[])[0]).toEqual(item);
196
+ expect((got.t as unknown[])[0]).not.toBe(item);
197
+ });
198
+
199
+ it("per-path override by dotted path wins over the default", () => {
200
+ const got = mergeLayers(
201
+ [
202
+ { hints: ["a"], other: ["a"], scope: { hints: ["a"] } },
203
+ { hints: ["b", "a"], other: ["b"], scope: { hints: ["b"] } },
204
+ ],
205
+ { arrayPolicyByPath: { hints: "union", "scope.hints": "concat" } },
206
+ );
207
+ expect(got).toEqual({ hints: ["a", "b"], other: ["b"], scope: { hints: ["a", "b"] } });
208
+ });
209
+
210
+ it("per-path custom function gets lower, higher and the path", () => {
211
+ const calls: [unknown[], unknown[], string][] = [];
212
+ const got = mergeLayers(
213
+ [{ a: { t: [1, 2] } }, { a: { t: [3] } }],
214
+ {
215
+ arrayPolicy: "concat",
216
+ arrayPolicyByPath: {
217
+ "a.t": (lower, higher, p) => {
218
+ calls.push([lower, higher, p]);
219
+ return [...higher, ...lower];
220
+ },
221
+ },
222
+ },
223
+ );
224
+ expect(got).toEqual({ a: { t: [3, 1, 2] } });
225
+ expect(calls).toEqual([[[1, 2], [3], "a.t"]]);
226
+ });
227
+
228
+ it("a per-path entry only matches the exact path", () => {
229
+ const got = mergeLayers([{ a: { t: [1] }, t: [1] }, { a: { t: [2] }, t: [2] }], {
230
+ arrayPolicyByPath: { t: "concat" },
231
+ });
232
+ expect(got).toEqual({ a: { t: [2] }, t: [1, 2] });
233
+ });
234
+
235
+ it("a * segment matches exactly one key at that position", () => {
236
+ const got = mergeLayers(
237
+ [
238
+ { scopes: { a: { hints: ["x"] }, b: { hints: ["x"], deep: { hints: ["x"] } } }, hints: ["x"] },
239
+ { scopes: { a: { hints: ["y"] }, b: { hints: ["y"], deep: { hints: ["y"] } } }, hints: ["y"] },
240
+ ],
241
+ { arrayPolicyByPath: { "scopes.*.hints": "concat" } },
242
+ );
243
+ expect(got).toEqual({
244
+ scopes: { a: { hints: ["x", "y"] }, b: { hints: ["x", "y"], deep: { hints: ["y"] } } },
245
+ hints: ["y"],
246
+ });
247
+ });
248
+
249
+ it("a * segment also matches a literal * key; a partial * is literal", () => {
250
+ const got = mergeLayers(
251
+ [{ m: { "*": [1], ab: [1] }, n: { "a*": [1], ab: [1] } }, { m: { "*": [2], ab: [2] }, n: { "a*": [2], ab: [2] } }],
252
+ { arrayPolicyByPath: { "m.*": "concat", "n.a*": "concat" } },
253
+ );
254
+ expect(got).toEqual({ m: { "*": [1, 2], ab: [1, 2] }, n: { "a*": [1, 2], ab: [2] } });
255
+ });
256
+
257
+ it("the function form gets the concrete path, not the pattern", () => {
258
+ const paths: string[] = [];
259
+ mergeLayers([{ s: { a: { t: [1] } } }, { s: { a: { t: [2] } } }], {
260
+ arrayPolicyByPath: { "s.*.t": (l, h, p) => (paths.push(p), [...l, ...h]) },
261
+ });
262
+ expect(paths).toEqual(["s.a.t"]);
263
+ });
264
+
265
+ it("precedence: exact path, then fewest *, then lexically smallest pattern", () => {
266
+ const layers = [{ a: { b: { c: [1, 1] } } }, { a: { b: { c: [2, 1] } } }];
267
+ const tag = (name: string) => (): unknown => name;
268
+ const pick = (byPath: Record<string, ArrayMergeFn>): unknown =>
269
+ mergeLayers(layers, { arrayPolicyByPath: byPath }).a;
270
+ // exact beats any wildcard, whatever the definition order
271
+ expect(pick({ "*.*.*": tag("3"), "a.*.c": tag("1"), "a.b.c": tag("exact") })).toEqual({ b: { c: "exact" } });
272
+ // fewer * beats more *, whatever the definition order
273
+ expect(pick({ "*.*.*": tag("3"), "*.b.*": tag("2"), "a.*.c": tag("1") })).toEqual({ b: { c: "1" } });
274
+ expect(pick({ "a.*.c": tag("1"), "*.*.*": tag("3") })).toEqual({ b: { c: "1" } });
275
+ // same number of *: lexically smallest pattern ("*" sorts before letters)
276
+ expect(pick({ "a.*.c": tag("a.*.c"), "*.b.c": tag("*.b.c") })).toEqual({ b: { c: "*.b.c" } });
277
+ expect(pick({ "*.b.c": tag("*.b.c"), "a.*.c": tag("a.*.c") })).toEqual({ b: { c: "*.b.c" } });
278
+ expect(pick({ "a.b.*": tag("a.b.*"), "a.*.c": tag("a.*.c") })).toEqual({ b: { c: "a.*.c" } });
279
+ });
280
+
281
+ it("a pattern never matches a path with a different number of segments", () => {
282
+ const got = mergeLayers([{ a: { t: [1] }, t: [1] }, { a: { t: [2] }, t: [2] }], {
283
+ arrayPolicyByPath: { "*": "concat", "*.*.*": "concat" },
284
+ });
285
+ expect(got).toEqual({ a: { t: [2] }, t: [1, 2] });
286
+ });
287
+
288
+ it("rejects a ** segment", () => {
289
+ expect(() => mergeLayers([], { arrayPolicyByPath: { "a.**.t": "concat" } })).toThrow(
290
+ /arrayPolicyByPath\["a\.\*\*\.t"\]: \*\* is not supported/,
291
+ );
292
+ });
293
+
294
+ it("rejects an unknown policy", () => {
295
+ expect(() => mergeLayers([], { arrayPolicy: "merge" as never })).toThrow(/arrayPolicy/);
296
+ expect(() => mergeLayers([], { arrayPolicyByPath: { t: "nope" as never } })).toThrow(/arrayPolicyByPath/);
297
+ expect(() => mergeLayers([], { nullPolicy: "drop" as never })).toThrow(/nullPolicy/);
298
+ });
299
+ });
300
+
301
+ // ---------------------------------------------------------------------------
302
+ // load(): parse and merge options
303
+ // ---------------------------------------------------------------------------
304
+
305
+ describe("load parse option", () => {
306
+ it("a parser function handles every file and receives text and path", () => {
307
+ const f = tmpFile(".rc", '{"a": 1, "a": 2}');
308
+ const seen: string[] = [];
309
+ try {
310
+ const got = load<Record<string, unknown>>({}, {
311
+ projectConfigPath: f,
312
+ parse: (text, p) => {
313
+ seen.push(p);
314
+ return JSON.parse(text) as unknown;
315
+ },
316
+ });
317
+ expect(got).toEqual({ a: 2 }); // JSON.parse: duplicate keys, last wins
318
+ expect(seen).toEqual([f]);
319
+ } finally {
320
+ cleanup(f);
321
+ }
322
+ });
323
+
324
+ it("a parser map picks by extension and falls back to YAML", () => {
325
+ const sys = tmpFile(".yaml", "a: 1\nlist: [x]\n");
326
+ const usr = tmpFile(".JSON", '{"b": 2, "list": ["y"]}');
327
+ const proj = tmpFile(".js", "module.exports = { c: 3 };\n");
328
+ try {
329
+ const got = load<Record<string, unknown>>({}, {
330
+ systemConfigPath: sys,
331
+ userConfigPath: usr,
332
+ projectConfigPath: proj,
333
+ parse: {
334
+ ".json": (text) => JSON.parse(text) as unknown,
335
+ ".js": (_text, p) => ({ c: p.endsWith(".js") ? 3 : 0 }),
336
+ },
337
+ merge: { arrayPolicy: "union" },
338
+ });
339
+ expect(got).toEqual({ a: 1, b: 2, c: 3, list: ["x", "y"] });
340
+ } finally {
341
+ cleanup(sys, usr, proj);
342
+ }
343
+ });
344
+
345
+ it("an extensionless dotfile matches the empty-string key", () => {
346
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), "config-merge-"));
347
+ const f = path.join(dir, ".toolrc");
348
+ fs.writeFileSync(f, '{"a": 1, "a": 2}', "utf8");
349
+ try {
350
+ expect(load({}, { projectConfigPath: f, parse: { "": (t) => JSON.parse(t) as unknown } })).toEqual({ a: 2 });
351
+ } finally {
352
+ fs.rmSync(dir, { recursive: true, force: true });
353
+ }
354
+ });
355
+
356
+ it("wraps parser errors with the file path", () => {
357
+ const f = tmpFile(".json", '{"a": 1,}');
358
+ try {
359
+ expect(() => load({}, { projectConfigPath: f, parse: { ".json": (t) => JSON.parse(t) as unknown } })).toThrow(
360
+ new RegExp(`config: failed to parse ${f.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}: `),
361
+ );
362
+ } finally {
363
+ cleanup(f);
364
+ }
365
+ });
366
+
367
+ it("still skips missing files and ignores a non-mapping result", () => {
368
+ const f = tmpFile(".json", "a: [unclosed");
369
+ try {
370
+ const got = load({ keep: 1 }, {
371
+ systemConfigPath: "/nonexistent/config-merge-test.json",
372
+ projectConfigPath: f,
373
+ parse: () => [1, 2],
374
+ });
375
+ expect(got).toEqual({ keep: 1 });
376
+ } finally {
377
+ cleanup(f);
378
+ }
379
+ });
380
+
381
+ it("applies the bounds check to parser output", () => {
382
+ const f = tmpFile(".json", "{}");
383
+ const cyc: Record<string, unknown> = {};
384
+ cyc.self = cyc;
385
+ try {
386
+ expect(() => load({}, { projectConfigPath: f, parse: () => cyc })).toThrow(/failed to parse .*nesting exceeded/);
387
+ } finally {
388
+ cleanup(f);
389
+ }
390
+ });
391
+ });
392
+
393
+ describe("load merge option", () => {
394
+ it("applies merge policies to file layers, then envOverride assigns plainly", () => {
395
+ const sys = tmpFile(".yaml", "db:\n host: h\n opts: {a: 1}\ntags: [a]\nname: n\n");
396
+ const usr = tmpFile(".yaml", "db:\n opts: {b: 2}\ntags: [b]\nname: ~\n");
397
+ try {
398
+ const got = load<Record<string, unknown>>({}, {
399
+ systemConfigPath: sys,
400
+ userConfigPath: usr,
401
+ merge: { nullPolicy: "keep-lower", maxDepth: 1, arrayPolicy: "union" },
402
+ envOverride(c) {
403
+ c.tags = ["env"];
404
+ c.name = null;
405
+ },
406
+ });
407
+ expect(got).toEqual({ db: { host: "h", opts: { b: 2 } }, tags: ["env"], name: null });
408
+ } finally {
409
+ cleanup(sys, usr);
410
+ }
411
+ });
412
+
413
+ it("merges file layers into dst, which acts as the lowest layer", () => {
414
+ const f = tmpFile(".yaml", "tags: [b]\nm: ~\n");
415
+ try {
416
+ const got = load({ tags: ["a"], m: { k: 1 } }, {
417
+ projectConfigPath: f,
418
+ merge: { arrayPolicy: "concat", nullPolicy: "keep-lower" },
419
+ });
420
+ expect(got).toEqual({ tags: ["a", "b"], m: { k: 1 } });
421
+ } finally {
422
+ cleanup(f);
423
+ }
424
+ });
425
+ });
@@ -242,6 +242,47 @@ describe("load merge rules", () => {
242
242
  });
243
243
  });
244
244
 
245
+ // ---------------------------------------------------------------------------
246
+ // Default parsing (pinned: no parse option means YAML for every file)
247
+ // ---------------------------------------------------------------------------
248
+
249
+ function tmpFile(ext: string, content: string): string {
250
+ const p = path.join(os.tmpdir(), `config-test-${Math.random().toString(36).slice(2)}${ext}`);
251
+ fs.writeFileSync(p, content, "utf8");
252
+ return p;
253
+ }
254
+
255
+ describe("load default parsing", () => {
256
+ it("throws on a duplicate key", () => {
257
+ expect(() => loadLayers({}, "a: 1\na: 2\n")).toThrow(/failed to parse .*duplicated mapping key/);
258
+ });
259
+
260
+ it("accepts a trailing comma in a flow collection", () => {
261
+ const got = loadLayers<Record<string, unknown>>({}, "{a: 1, tags: [x, y,],}\n");
262
+ expect(got).toEqual({ a: 1, tags: ["x", "y"] });
263
+ });
264
+
265
+ it("parses a .json file as YAML", () => {
266
+ const f = tmpFile(".json", '{"a": {"b": 1}, "tags": ["x"]}');
267
+ try {
268
+ expect(load({}, { projectConfigPath: f })).toEqual({ a: { b: 1 }, tags: ["x"] });
269
+ } finally {
270
+ cleanup(f);
271
+ }
272
+ });
273
+
274
+ it("reads a .js file as YAML text and never executes it", () => {
275
+ const scalar = tmpFile(".js", "'use strict'\n");
276
+ const junk = tmpFile(".js", "module.exports = { a: 1 };\n");
277
+ try {
278
+ expect(load({ keep: true }, { projectConfigPath: scalar })).toEqual({ keep: true });
279
+ expect(load({}, { projectConfigPath: junk })).toEqual({ "module.exports = { a": "1 };" });
280
+ } finally {
281
+ cleanup(scalar, junk);
282
+ }
283
+ });
284
+ });
285
+
245
286
  // ---------------------------------------------------------------------------
246
287
  // Resource bounds
247
288
  // ---------------------------------------------------------------------------