@hop-top/kit 0.5.0-alpha.1 → 0.5.0-alpha.11
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/CHANGELOG.md +275 -0
- package/LICENSE +1 -1
- package/LICENSE-cobra.txt +174 -0
- package/NOTICE +15 -0
- package/README.md +55 -259
- package/dist/aim-DZHwwiIc.d.ts +139 -0
- package/dist/aim.d.ts +1 -116
- package/dist/aim.js +13 -414
- package/dist/alias-D9_qFlyP.d.ts +174 -0
- package/dist/alias.d.ts +2 -73
- package/dist/alias.js +27 -207
- package/dist/api-DAexsu2H.d.ts +77 -0
- package/dist/api.d.ts +1 -62
- package/dist/api.js +10 -164
- package/dist/auth-2-jfgiHn.d.ts +34 -0
- package/dist/auth.d.ts +1 -26
- package/dist/auth.js +6 -40
- package/dist/chunk-3FIKGFKI.js +54 -0
- package/dist/chunk-4FMALWYB.js +669 -0
- package/dist/chunk-4GPCWBBA.js +143 -0
- package/dist/chunk-4UE5TCMQ.js +104 -0
- package/dist/chunk-65VPWMPV.js +1134 -0
- package/dist/chunk-6HFAEZ52.js +29 -0
- package/dist/chunk-6SVOHNIC.js +17 -0
- package/dist/chunk-BDNNPRX6.js +41 -0
- package/dist/chunk-BRNEKPWX.js +37 -0
- package/dist/chunk-BXJ2J27B.js +219 -0
- package/dist/chunk-CSJWB6D3.js +151 -0
- package/dist/chunk-CXBZTONY.js +65 -0
- package/dist/chunk-EE4M2UOF.js +818 -0
- package/dist/chunk-GFRAYYRD.js +817 -0
- package/dist/chunk-IVQHLI56.js +1890 -0
- package/dist/chunk-IZ6Q4SE3.js +178 -0
- package/dist/chunk-JKMQSAJP.js +174 -0
- package/dist/chunk-K7E34Q3A.js +1445 -0
- package/dist/chunk-KVG7YCTS.js +115 -0
- package/dist/chunk-L23O2FFE.js +51 -0
- package/dist/chunk-MFVUENZR.js +1549 -0
- package/dist/chunk-ML7D5DWY.js +263 -0
- package/dist/chunk-MQXYDYRD.js +968 -0
- package/dist/chunk-POR5L2DG.js +247 -0
- package/dist/chunk-QJVSKKRQ.js +369 -0
- package/dist/chunk-QQQYCWSC.js +240 -0
- package/dist/chunk-R5U7XKVJ.js +16 -0
- package/dist/chunk-RYKLVOVK.js +309 -0
- package/dist/chunk-SNVGDYX7.js +187 -0
- package/dist/chunk-T6OZMPPV.js +68 -0
- package/dist/chunk-UKZIAQA7.js +26 -0
- package/dist/chunk-VGOA5Y7S.js +251 -0
- package/dist/chunk-VWGP4NVJ.js +513 -0
- package/dist/chunk-WBVO43HU.js +355 -0
- package/dist/chunk-WUOAQVOA.js +86 -0
- package/dist/chunk-XJTRY453.js +868 -0
- package/dist/chunk-YOBAUCVN.js +583 -0
- package/dist/cli-RSRk1M36.d.ts +707 -0
- package/dist/cli.d.ts +7 -309
- package/dist/cli.js +125 -945
- package/dist/completion-csVun6SP.d.ts +112 -0
- package/dist/completion-values-By-mnjNt.d.ts +142 -0
- package/dist/completion-values.d.ts +1 -0
- package/dist/completion-values.js +29 -0
- package/dist/completion.d.ts +3 -0
- package/dist/completion.js +34 -0
- package/dist/compliance-AxFf-uMy.d.ts +60 -0
- package/dist/compliance.d.ts +1 -0
- package/dist/compliance.js +17 -0
- package/dist/config-CaK60ByQ.d.ts +241 -0
- package/dist/config.d.ts +1 -97
- package/dist/config.js +8 -78
- package/dist/errcorrect-BcX1Ft0K.d.ts +44 -0
- package/dist/errcorrect.d.ts +1 -37
- package/dist/errcorrect.js +8 -68
- package/dist/error-Do8Qji43.d.ts +231 -0
- package/dist/id/index.d.ts +2 -83
- package/dist/id/index.js +14 -65
- package/dist/index-BKJZClJx.d.ts +1486 -0
- package/dist/index-C3Zw4wDJ.d.ts +359 -0
- package/dist/index-CGE5yLBj.d.ts +328 -0
- package/dist/index-VLDcI5NF.d.ts +95 -0
- package/dist/index.d.ts +37 -0
- package/dist/index.js +132 -0
- package/dist/llm-DYuR7_uX.d.ts +1069 -0
- package/dist/llm-anthropic-Cd_I2xXB.d.ts +67 -0
- package/dist/llm-anthropic.d.ts +2 -0
- package/dist/llm-anthropic.js +13 -0
- package/dist/llm-openai-DyFrucmV.d.ts +87 -0
- package/dist/llm-openai.d.ts +2 -0
- package/dist/llm-openai.js +19 -0
- package/dist/llm.d.ts +1 -188
- package/dist/llm.js +101 -356
- package/dist/log-CkaxMX3U.d.ts +115 -0
- package/dist/log.d.ts +2 -0
- package/dist/log.js +14 -0
- package/dist/mcp/index.d.ts +4 -0
- package/dist/mcp/index.js +185 -0
- package/dist/netpolicy-Brx5XZUB.d.ts +151 -0
- package/dist/netpolicy.d.ts +1 -0
- package/dist/netpolicy.js +25 -0
- package/dist/output-CjZWmL8s.d.ts +420 -0
- package/dist/output.d.ts +3 -213
- package/dist/output.js +135 -447
- package/dist/parity-CvMuYcVI.d.ts +61 -0
- package/dist/progress-CAzyxeWl.d.ts +38 -0
- package/dist/progress.d.ts +1 -30
- package/dist/progress.js +6 -55
- package/dist/provenance-SMFHm5gx.d.ts +30 -0
- package/dist/provenance.d.ts +1 -24
- package/dist/provenance.js +6 -31
- package/dist/routellm-B0nv59mH.d.ts +111 -0
- package/dist/routellm.d.ts +2 -64
- package/dist/routellm.js +13 -255
- package/dist/rpc-D31Lg0Kc.d.ts +32 -0
- package/dist/rpc.d.ts +2 -25
- package/dist/rpc.js +4 -98
- package/dist/safety-CUaYEjZc.d.ts +30 -0
- package/dist/safety.d.ts +1 -24
- package/dist/safety.js +6 -43
- package/dist/scope-BrhYB4ij.d.ts +141 -0
- package/dist/scope.d.ts +1 -121
- package/dist/scope.js +20 -377
- package/dist/serve-D8yWMrTc.d.ts +646 -0
- package/dist/serve.d.ts +3 -0
- package/dist/serve.js +75 -0
- package/dist/sqlstore.js +6 -39
- package/dist/stream-CrjeCp0M.d.ts +44 -0
- package/dist/stream.d.ts +1 -35
- package/dist/stream.js +8 -51
- package/dist/telemetry/index.d.ts +1 -315
- package/dist/telemetry/index.js +26 -599
- package/dist/toolspec-DcPwwoAZ.d.ts +368 -0
- package/dist/toolspec.d.ts +2 -0
- package/dist/toolspec.js +37 -0
- package/dist/tui/index.d.ts +6 -298
- package/dist/tui/index.js +17 -381
- package/dist/upgrade-C3RWGwRi.d.ts +125 -0
- package/dist/upgrade.d.ts +1 -95
- package/dist/upgrade.js +6 -114
- package/dist/xdg-DB7Shoib.d.ts +179 -0
- package/dist/xdg.d.ts +1 -169
- package/dist/xdg.js +14 -128
- package/package.json +192 -24
- package/src/README.md +15 -0
- package/src/alias.conformance.test.ts +250 -0
- package/src/alias.test.ts +206 -2
- package/src/alias.ts +196 -21
- package/src/cli.help-command.test.ts +105 -0
- package/src/cli.offline.test.ts +93 -0
- package/src/cli.parity.test.ts +192 -0
- package/src/cli.root.conformance.test.ts +166 -0
- package/src/cli.test.ts +95 -1
- package/src/cli.ts +234 -25
- package/src/completion-scripts.ts +16 -0
- package/src/completion-values.test.ts +76 -16
- package/src/completion-values.ts +170 -66
- package/src/completion.conformance.test.ts +298 -0
- package/src/completion.test.ts +268 -145
- package/src/completion.ts +772 -121
- package/src/compliance.test.ts +196 -2
- package/src/compliance.ts +440 -20
- package/src/config-merge.test.ts +425 -0
- package/src/config.test.ts +228 -1
- package/src/config.ts +429 -16
- package/src/delegation.test.ts +299 -0
- package/src/delegation.ts +807 -0
- package/src/gen/crud_pb.ts +7 -7
- package/src/id/README.md +38 -0
- package/src/id/contract.test.ts +1 -1
- package/src/id/index.test.ts +1 -1
- package/src/id/index.ts +3 -2
- package/src/index.test.ts +71 -0
- package/src/index.ts +46 -0
- package/src/llm-anthropic.conformance.test.ts +1135 -0
- package/src/llm-anthropic.test.ts +152 -0
- package/src/llm-anthropic.ts +940 -0
- package/src/llm-choice-hermetic.test.ts +335 -0
- package/src/llm-config.test.ts +232 -0
- package/src/llm-config.ts +106 -0
- package/src/llm-endpoint.ts +254 -0
- package/src/llm-error.ts +17 -0
- package/src/llm-errors.ts +152 -0
- package/src/llm-file.ts +126 -0
- package/src/llm-http.ts +317 -0
- package/src/llm-keys.ts +167 -0
- package/src/llm-media.conformance.test.ts +87 -0
- package/src/llm-media.test.ts +279 -0
- package/src/llm-media.ts +322 -0
- package/src/llm-openai.conformance.test.ts +407 -0
- package/src/llm-openai.test.ts +510 -0
- package/src/llm-openai.ts +708 -0
- package/src/llm-payload.ts +155 -0
- package/src/llm-quote.ts +46 -0
- package/src/llm-routellm.conformance.test.ts +44 -0
- package/src/llm-toolnames.conformance.test.ts +112 -0
- package/src/llm-toolnames.test.ts +132 -0
- package/src/llm-toolnames.ts +298 -0
- package/src/llm-uri.ts +99 -0
- package/src/llm.test.ts +43 -15
- package/src/llm.toolcall.test.ts +175 -0
- package/src/llm.ts +602 -190
- package/src/log.parity.test.ts +105 -0
- package/src/log.test.ts +270 -1
- package/src/log.ts +228 -49
- package/src/mcp/README.md +63 -0
- package/src/mcp/args-separator.test.ts +164 -0
- package/src/mcp/args.test.ts +223 -0
- package/src/mcp/args.ts +160 -0
- package/src/mcp/audit.test.ts +520 -0
- package/src/mcp/audit.ts +70 -0
- package/src/mcp/bridge.test.ts +250 -0
- package/src/mcp/bridge.ts +286 -0
- package/src/mcp/conformance.test.ts +357 -0
- package/src/mcp/dispatch.ts +359 -0
- package/src/mcp/identity.test.ts +166 -0
- package/src/mcp/identity.ts +72 -0
- package/src/mcp/index.ts +176 -0
- package/src/mcp/legacy-notify.test.ts +59 -0
- package/src/mcp/legacy.ts +367 -0
- package/src/mcp/mcp.test.ts +707 -0
- package/src/mcp/modern-confirm.test.ts +520 -0
- package/src/mcp/modern-confirm.ts +484 -0
- package/src/mcp/modern.ts +788 -0
- package/src/mcp/stdio.test.ts +313 -0
- package/src/mcp/stdio.ts +254 -0
- package/src/mcp/tasks.ts +82 -0
- package/src/mcp/testtree.ts +250 -0
- package/src/mcp/toolname.test.ts +147 -0
- package/src/mcp/types.ts +689 -0
- package/src/netpolicy.test.ts +280 -0
- package/src/netpolicy.ts +256 -0
- package/src/output/README.md +46 -0
- package/src/output/builtins.ts +5 -5
- package/src/output/dispatch.ts +10 -2
- package/src/output/error.ts +459 -0
- package/src/output/formatter.ts +29 -5
- package/src/output/formatters/README.md +50 -0
- package/src/output/formatters/csv.ts +48 -12
- package/src/output/formatters/json.ts +4 -25
- package/src/output/formatters/project.ts +34 -0
- package/src/output/formatters/table.ts +8 -6
- package/src/output/formatters/text.ts +7 -3
- package/src/output/formatters/yaml.ts +5 -22
- package/src/output/projection.test.ts +298 -0
- package/src/output/projection.ts +33 -59
- package/src/output/template.ts +11 -21
- package/src/output.surface.test.ts +41 -0
- package/src/output.ts +64 -3
- package/src/plan.ts +89 -0
- package/src/policy.ts +472 -0
- package/src/published-types.test.ts +133 -0
- package/src/routellm.test.ts +209 -28
- package/src/routellm.toolcall.test.ts +521 -0
- package/src/routellm.ts +213 -177
- package/src/router/README.md +44 -0
- package/src/router/server.test.ts +41 -0
- package/src/router/server.ts +11 -0
- package/src/safety.test.ts +19 -9
- package/src/scope-defaults.json +29 -29
- package/src/serve.test.ts +1257 -0
- package/src/serve.ts +1665 -0
- package/src/sideeffect.ts +105 -0
- package/src/telemetry/README.md +48 -0
- package/src/telemetry/client.test.ts +31 -0
- package/src/telemetry/client.ts +12 -6
- package/src/telemetry/offline.test.ts +64 -0
- package/src/toolspec-internal.ts +279 -0
- package/src/toolspec-manifest.test.ts +167 -0
- package/src/toolspec-manifest.ts +415 -0
- package/src/toolspec-spec-schemas.ts +12 -0
- package/src/toolspec-spec.test.ts +273 -0
- package/src/toolspec-spec.ts +458 -0
- package/src/toolspec.from-command.test.ts +350 -0
- package/src/toolspec.test.ts +2 -2
- package/src/toolspec.ts +222 -0
- package/src/triton/README.md +34 -0
- package/src/tui/README.md +42 -0
- package/src/tui/parity.test.ts +120 -31
- package/src/tui/parity.ts +58 -7
- package/src/upgrade.test.ts +291 -0
- package/src/upgrade.ts +116 -7
- package/src/wizard.test.ts +7 -7
- package/test/llm/conformance-helpers.ts +100 -0
- package/test/output/csv.test.ts +164 -0
- package/test/output/dispatch.test.ts +1 -2
- package/test/output/error.test.ts +277 -0
- package/test/output/smoke-capture.ts +2 -2
- package/test/output/smoke.test.ts +2 -2
- package/test/output/taxonomy-contract.test.ts +148 -0
- package/test/setup-env.ts +16 -0
- package/test/toolspec/fixture.ts +79 -0
- package/tsconfig.json +3 -1
- package/tsconfig.test.json +10 -0
- package/vitest.config.ts +9 -0
- package/src/tui/parity.json +0 -74
|
@@ -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
|
+
});
|
package/src/config.test.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Tests for layered config loader (config.ts).
|
|
3
|
-
* Covers: merge order, missing-file skip, bad YAML error, envOverride, empty opts
|
|
3
|
+
* Covers: merge order, missing-file skip, bad YAML error, envOverride, empty opts,
|
|
4
|
+
* and the per-layer merge rules (objects recursive, arrays/scalars replace,
|
|
5
|
+
* null, type conflicts, non-plain objects).
|
|
4
6
|
*/
|
|
5
7
|
|
|
6
8
|
import * as fs from "fs";
|
|
@@ -108,3 +110,228 @@ describe("load", () => {
|
|
|
108
110
|
expect(result).toBe(dst);
|
|
109
111
|
});
|
|
110
112
|
});
|
|
113
|
+
|
|
114
|
+
// ---------------------------------------------------------------------------
|
|
115
|
+
// Merge rules
|
|
116
|
+
// ---------------------------------------------------------------------------
|
|
117
|
+
|
|
118
|
+
/** Writes each YAML body to a temp file, loads them in order, cleans up. */
|
|
119
|
+
function loadLayers<T extends object>(dst: T, ...bodies: string[]): T {
|
|
120
|
+
const files = bodies.map(tmpYaml);
|
|
121
|
+
try {
|
|
122
|
+
const [systemConfigPath, userConfigPath, projectConfigPath] = files;
|
|
123
|
+
return load(dst, { systemConfigPath, userConfigPath, projectConfigPath });
|
|
124
|
+
} finally {
|
|
125
|
+
cleanup(...files);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
describe("load merge rules", () => {
|
|
130
|
+
it("merges nested maps across layers instead of replacing them", () => {
|
|
131
|
+
const got = loadLayers<Record<string, unknown>>(
|
|
132
|
+
{},
|
|
133
|
+
"",
|
|
134
|
+
"db:\n host: x\n port: 1\n",
|
|
135
|
+
"db:\n port: 2\n",
|
|
136
|
+
);
|
|
137
|
+
expect(got).toEqual({ db: { host: "x", port: 2 } });
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
it("merges at every depth and keeps dst defaults", () => {
|
|
141
|
+
const dst = { a: { b: { c: 1, d: 2 }, keep: true } };
|
|
142
|
+
loadLayers(dst, "a:\n b:\n d: 3\n e: 4\n");
|
|
143
|
+
expect(dst).toEqual({ a: { b: { c: 1, d: 3, e: 4 }, keep: true } });
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
it("an empty mapping leaves the object as is", () => {
|
|
147
|
+
const dst = { m: { a: 1 } };
|
|
148
|
+
loadLayers(dst, "m: {}\n");
|
|
149
|
+
expect(dst).toEqual({ m: { a: 1 } });
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it("replaces arrays wholesale, including with an empty one", () => {
|
|
153
|
+
const dst = { tags: ["d"] };
|
|
154
|
+
loadLayers(dst, "tags: [a, b]\n", "tags: [c]\n");
|
|
155
|
+
expect(dst.tags).toEqual(["c"]);
|
|
156
|
+
loadLayers(dst, "tags: []\n");
|
|
157
|
+
expect(dst.tags).toEqual([]);
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
it("replaces scalars", () => {
|
|
161
|
+
const dst = { name: "d", port: 1, debug: false };
|
|
162
|
+
loadLayers(dst, "name: a\nport: 2\n", "name: b\ndebug: true\n");
|
|
163
|
+
expect(dst).toEqual({ name: "b", port: 2, debug: true });
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
it("null leaves a scalar untouched", () => {
|
|
167
|
+
const dst = { name: "d", port: 1 };
|
|
168
|
+
loadLayers(dst, "name: a\n", "name: ~\nport: null\n");
|
|
169
|
+
expect(dst).toEqual({ name: "a", port: 1 });
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
it("null clears objects and arrays", () => {
|
|
173
|
+
const dst = { m: { a: 1 }, tags: ["x"], deep: { inner: { k: "v" } } };
|
|
174
|
+
loadLayers(dst, "m: ~\ntags: ~\ndeep:\n inner: ~\n");
|
|
175
|
+
expect(dst).toEqual({ m: null, tags: null, deep: { inner: null } });
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
it("null sets a key that is absent or already null", () => {
|
|
179
|
+
const dst: Record<string, unknown> = { n: null };
|
|
180
|
+
loadLayers(dst, "n: ~\nfresh: ~\n");
|
|
181
|
+
expect(dst).toEqual({ n: null, fresh: null });
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
it("type conflicts: the later value replaces the earlier one", () => {
|
|
185
|
+
const dst: Record<string, unknown> = {
|
|
186
|
+
objToScalar: { a: 1 },
|
|
187
|
+
scalarToObj: 5,
|
|
188
|
+
objToArray: { a: 1 },
|
|
189
|
+
arrayToObj: [1],
|
|
190
|
+
};
|
|
191
|
+
loadLayers(
|
|
192
|
+
dst,
|
|
193
|
+
"objToScalar: 7\nscalarToObj: {b: 2}\nobjToArray: [x]\narrayToObj: {c: 3}\n",
|
|
194
|
+
);
|
|
195
|
+
expect(dst).toEqual({
|
|
196
|
+
objToScalar: 7,
|
|
197
|
+
scalarToObj: { b: 2 },
|
|
198
|
+
objToArray: ["x"],
|
|
199
|
+
arrayToObj: { c: 3 },
|
|
200
|
+
});
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
it("does not merge into non-plain objects; replaces them", () => {
|
|
204
|
+
class Endpoint {
|
|
205
|
+
host = "h";
|
|
206
|
+
port = 1;
|
|
207
|
+
}
|
|
208
|
+
const dst: Record<string, unknown> = {
|
|
209
|
+
when: new Date(0),
|
|
210
|
+
ep: new Endpoint(),
|
|
211
|
+
map: new Map([["k", "v"]]),
|
|
212
|
+
};
|
|
213
|
+
loadLayers(dst, "when: {y: 1}\nep: {port: 2}\nmap: {k: w}\n");
|
|
214
|
+
expect(dst).toEqual({ when: { y: 1 }, ep: { port: 2 }, map: { k: "w" } });
|
|
215
|
+
expect(Object.getPrototypeOf(dst.ep)).toBe(Object.prototype);
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
it("a YAML timestamp (Date) replaces a plain object", () => {
|
|
219
|
+
const dst: Record<string, unknown> = { when: { y: 1 } };
|
|
220
|
+
loadLayers(dst, "when: 2026-01-02\n");
|
|
221
|
+
expect(dst.when).toBeInstanceOf(Date);
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
it("aliased mappings are copied, so later merges do not leak across keys", () => {
|
|
225
|
+
const dst: Record<string, unknown> = {};
|
|
226
|
+
loadLayers(dst, "a: &x {k: v}\nb: *x\n", "a:\n k: changed\n");
|
|
227
|
+
expect(dst).toEqual({ a: { k: "changed" }, b: { k: "v" } });
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
it("does not let __proto__ keys reach Object.prototype", () => {
|
|
231
|
+
const dst: Record<string, unknown> = {};
|
|
232
|
+
loadLayers(dst, "__proto__:\n polluted: yes\n", "__proto__:\n polluted2: yes\n");
|
|
233
|
+
expect(({} as Record<string, unknown>).polluted).toBeUndefined();
|
|
234
|
+
expect(({} as Record<string, unknown>).polluted2).toBeUndefined();
|
|
235
|
+
expect(Object.getPrototypeOf(dst)).toBe(Object.prototype);
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
it("ignores a document whose root is not a mapping", () => {
|
|
239
|
+
const dst = { a: 1 };
|
|
240
|
+
loadLayers(dst, "- x\n- y\n", "just a string\n");
|
|
241
|
+
expect(dst).toEqual({ a: 1 });
|
|
242
|
+
});
|
|
243
|
+
});
|
|
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
|
+
|
|
286
|
+
// ---------------------------------------------------------------------------
|
|
287
|
+
// Resource bounds
|
|
288
|
+
// ---------------------------------------------------------------------------
|
|
289
|
+
|
|
290
|
+
/** `levels` anchors, each a list of `width` aliases of the previous one. */
|
|
291
|
+
function aliasBomb(levels: number, width = 9): string {
|
|
292
|
+
const row = (ref: string): string => `[${Array(width).fill(ref).join(", ")}]`;
|
|
293
|
+
let out = `a0: &a0 ${row("lol")}\n`;
|
|
294
|
+
for (let i = 1; i < levels; i++) out += `a${i}: &a${i} ${row(`*a${i - 1}`)}\n`;
|
|
295
|
+
return out;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
describe("load resource bounds", () => {
|
|
299
|
+
it("fails fast on an exponential alias bomb", () => {
|
|
300
|
+
const started = Date.now();
|
|
301
|
+
expect(() => loadLayers({}, aliasBomb(10))).toThrow(
|
|
302
|
+
/failed to parse .*: document contains excessive aliasing/,
|
|
303
|
+
);
|
|
304
|
+
expect(Date.now() - started).toBeLessThan(2000);
|
|
305
|
+
});
|
|
306
|
+
|
|
307
|
+
it("fails on the shared config-v1 excessive aliasing vector", () => {
|
|
308
|
+
const corpus = JSON.parse(
|
|
309
|
+
fs.readFileSync(path.join(__dirname, "../../../contracts/config-v1/load.json"), "utf8"),
|
|
310
|
+
) as { cases: { name: string; files: string[] }[] };
|
|
311
|
+
const c = corpus.cases.find((x) => x.name === "excessive aliasing");
|
|
312
|
+
expect(c).toBeDefined();
|
|
313
|
+
expect(() => loadLayers({}, ...(c?.files ?? []))).toThrow(/excessive aliasing/);
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
it("allows ordinary alias use", () => {
|
|
317
|
+
const got = loadLayers<Record<string, unknown>>({}, aliasBomb(2) + "x: *a1\n");
|
|
318
|
+
expect((got.x as unknown[]).length).toBe(9);
|
|
319
|
+
});
|
|
320
|
+
|
|
321
|
+
it("fails on nesting deeper than the limit", () => {
|
|
322
|
+
const deep = "x: " + "[".repeat(150) + "]".repeat(150) + "\n";
|
|
323
|
+
expect(() => loadLayers({}, deep)).toThrow(/failed to parse .*: nesting exceeded maxDepth \(100\)/);
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
it("fails on alias chains that nest deeper than the limit", () => {
|
|
327
|
+
let doc = "a0: &a0 [x]\n";
|
|
328
|
+
for (let i = 1; i < 150; i++) doc += `a${i}: &a${i} [*a${i - 1}]\n`;
|
|
329
|
+
expect(() => loadLayers({}, doc)).toThrow(/failed to parse .*: nesting exceeded maxDepth \(100\)/);
|
|
330
|
+
});
|
|
331
|
+
|
|
332
|
+
it("accepts nesting at the limit", () => {
|
|
333
|
+
const ok = "x: " + "[".repeat(98) + "]".repeat(98) + "\n";
|
|
334
|
+
expect(() => loadLayers({}, ok)).not.toThrow();
|
|
335
|
+
});
|
|
336
|
+
});
|
|
337
|
+
|