@featurevisor/core 3.0.1 → 3.2.0

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 (208) hide show
  1. package/lib/datasource/datasource.d.ts +1 -1
  2. package/lib/datasource/datasource.js +4 -14
  3. package/lib/datasource/datasource.js.map +1 -1
  4. package/lib/datasource/requiredFeatures.d.ts +7 -0
  5. package/lib/datasource/requiredFeatures.js +38 -0
  6. package/lib/datasource/requiredFeatures.js.map +1 -0
  7. package/lib/linter/checkCircularDependency.d.ts +1 -1
  8. package/lib/linter/checkCircularDependency.js +3 -22
  9. package/lib/linter/checkCircularDependency.js.map +1 -1
  10. package/lib/linter/conditionSchema.js +27 -3
  11. package/lib/linter/conditionSchema.js.map +1 -1
  12. package/lib/tester/testFeature.js +17 -16
  13. package/lib/tester/testFeature.js.map +1 -1
  14. package/package.json +10 -7
  15. package/CHANGELOG.md +0 -2238
  16. package/coverage/clover.xml +0 -5456
  17. package/coverage/coverage-final.json +0 -64
  18. package/coverage/lcov-report/base.css +0 -224
  19. package/coverage/lcov-report/block-navigation.js +0 -87
  20. package/coverage/lcov-report/favicon.png +0 -0
  21. package/coverage/lcov-report/index.html +0 -371
  22. package/coverage/lcov-report/prettify.css +0 -1
  23. package/coverage/lcov-report/prettify.js +0 -2
  24. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  25. package/coverage/lcov-report/sorter.js +0 -210
  26. package/coverage/lcov-report/src/assess-distribution/index.html +0 -116
  27. package/coverage/lcov-report/src/assess-distribution/index.ts.html +0 -754
  28. package/coverage/lcov-report/src/benchmark/index.html +0 -116
  29. package/coverage/lcov-report/src/benchmark/index.ts.html +0 -793
  30. package/coverage/lcov-report/src/builder/allocator.ts.html +0 -220
  31. package/coverage/lcov-report/src/builder/applyContextToConditions.ts.html +0 -502
  32. package/coverage/lcov-report/src/builder/applyContextToDatafile.ts.html +0 -772
  33. package/coverage/lcov-report/src/builder/applyContextToSegments.ts.html +0 -580
  34. package/coverage/lcov-report/src/builder/buildDatafile.ts.html +0 -2053
  35. package/coverage/lcov-report/src/builder/buildProject.ts.html +0 -1117
  36. package/coverage/lcov-report/src/builder/buildRuntimeDatafiles.ts.html +0 -256
  37. package/coverage/lcov-report/src/builder/getFeatureRanges.ts.html +0 -268
  38. package/coverage/lcov-report/src/builder/hashes.ts.html +0 -412
  39. package/coverage/lcov-report/src/builder/index.html +0 -311
  40. package/coverage/lcov-report/src/builder/index.ts.html +0 -91
  41. package/coverage/lcov-report/src/builder/mutateVariables.ts.html +0 -421
  42. package/coverage/lcov-report/src/builder/mutator.ts.html +0 -796
  43. package/coverage/lcov-report/src/builder/revision.ts.html +0 -151
  44. package/coverage/lcov-report/src/builder/traffic.ts.html +0 -661
  45. package/coverage/lcov-report/src/cli/cli.ts.html +0 -400
  46. package/coverage/lcov-report/src/cli/index.html +0 -131
  47. package/coverage/lcov-report/src/cli/plugins.ts.html +0 -214
  48. package/coverage/lcov-report/src/config/index.html +0 -131
  49. package/coverage/lcov-report/src/config/index.ts.html +0 -88
  50. package/coverage/lcov-report/src/config/projectConfig.ts.html +0 -925
  51. package/coverage/lcov-report/src/datasource/adapter.ts.html +0 -253
  52. package/coverage/lcov-report/src/datasource/datasource.ts.html +0 -1000
  53. package/coverage/lcov-report/src/datasource/filesystemAdapter.ts.html +0 -1576
  54. package/coverage/lcov-report/src/datasource/index.html +0 -161
  55. package/coverage/lcov-report/src/datasource/index.ts.html +0 -94
  56. package/coverage/lcov-report/src/evaluate/index.html +0 -116
  57. package/coverage/lcov-report/src/evaluate/index.ts.html +0 -814
  58. package/coverage/lcov-report/src/find-duplicate-segments/findDuplicateSegments.ts.html +0 -313
  59. package/coverage/lcov-report/src/find-duplicate-segments/index.html +0 -131
  60. package/coverage/lcov-report/src/find-duplicate-segments/index.ts.html +0 -307
  61. package/coverage/lcov-report/src/find-usage/index.html +0 -116
  62. package/coverage/lcov-report/src/find-usage/index.ts.html +0 -1780
  63. package/coverage/lcov-report/src/generate-code/index.html +0 -131
  64. package/coverage/lcov-report/src/generate-code/index.ts.html +0 -403
  65. package/coverage/lcov-report/src/generate-code/typescript.ts.html +0 -1909
  66. package/coverage/lcov-report/src/index.html +0 -131
  67. package/coverage/lcov-report/src/info/index.html +0 -116
  68. package/coverage/lcov-report/src/info/index.ts.html +0 -472
  69. package/coverage/lcov-report/src/init/index.html +0 -116
  70. package/coverage/lcov-report/src/init/index.ts.html +0 -280
  71. package/coverage/lcov-report/src/linter/attributeSchema.ts.html +0 -1963
  72. package/coverage/lcov-report/src/linter/checkCircularDependency.ts.html +0 -220
  73. package/coverage/lcov-report/src/linter/checkPercentageExceedingSlot.ts.html +0 -268
  74. package/coverage/lcov-report/src/linter/conditionSchema.ts.html +0 -2224
  75. package/coverage/lcov-report/src/linter/featureSchema.ts.html +0 -5851
  76. package/coverage/lcov-report/src/linter/groupSchema.ts.html +0 -226
  77. package/coverage/lcov-report/src/linter/index.html +0 -326
  78. package/coverage/lcov-report/src/linter/index.ts.html +0 -88
  79. package/coverage/lcov-report/src/linter/lintProject.ts.html +0 -2704
  80. package/coverage/lcov-report/src/linter/mutationNotation.ts.html +0 -1411
  81. package/coverage/lcov-report/src/linter/printError.ts.html +0 -289
  82. package/coverage/lcov-report/src/linter/schema.ts.html +0 -1558
  83. package/coverage/lcov-report/src/linter/segmentSchema.ts.html +0 -139
  84. package/coverage/lcov-report/src/linter/targetSchema.ts.html +0 -223
  85. package/coverage/lcov-report/src/linter/testSchema.ts.html +0 -529
  86. package/coverage/lcov-report/src/linter/zodHelpers.ts.html +0 -133
  87. package/coverage/lcov-report/src/list/index.html +0 -131
  88. package/coverage/lcov-report/src/list/index.ts.html +0 -2065
  89. package/coverage/lcov-report/src/list/matrix.ts.html +0 -721
  90. package/coverage/lcov-report/src/promoter/index.html +0 -116
  91. package/coverage/lcov-report/src/promoter/index.ts.html +0 -3883
  92. package/coverage/lcov-report/src/sets.ts.html +0 -313
  93. package/coverage/lcov-report/src/targeting.ts.html +0 -403
  94. package/coverage/lcov-report/src/tester/cliFormat.ts.html +0 -133
  95. package/coverage/lcov-report/src/tester/helpers.ts.html +0 -313
  96. package/coverage/lcov-report/src/tester/index.html +0 -206
  97. package/coverage/lcov-report/src/tester/prettyDuration.ts.html +0 -187
  98. package/coverage/lcov-report/src/tester/printTestResult.ts.html +0 -337
  99. package/coverage/lcov-report/src/tester/testFeature.ts.html +0 -1186
  100. package/coverage/lcov-report/src/tester/testProject.ts.html +0 -1018
  101. package/coverage/lcov-report/src/tester/testSegment.ts.html +0 -316
  102. package/coverage/lcov-report/src/utils/extractKeys.ts.html +0 -493
  103. package/coverage/lcov-report/src/utils/git.ts.html +0 -559
  104. package/coverage/lcov-report/src/utils/index.html +0 -161
  105. package/coverage/lcov-report/src/utils/index.ts.html +0 -94
  106. package/coverage/lcov-report/src/utils/pretty.ts.html +0 -121
  107. package/coverage/lcov.info +0 -11615
  108. package/jest.config.js +0 -22
  109. package/json-schema/README.md +0 -5
  110. package/json-schema/attribute.json +0 -40
  111. package/json-schema/feature.json +0 -394
  112. package/json-schema/segment.json +0 -137
  113. package/src/assess-distribution/index.ts +0 -223
  114. package/src/benchmark/index.ts +0 -236
  115. package/src/builder/allocator.spec.ts +0 -105
  116. package/src/builder/allocator.ts +0 -45
  117. package/src/builder/applyContextToConditions.spec.ts +0 -2677
  118. package/src/builder/applyContextToConditions.ts +0 -139
  119. package/src/builder/applyContextToDatafile.spec.ts +0 -2626
  120. package/src/builder/applyContextToDatafile.ts +0 -229
  121. package/src/builder/applyContextToSegments.spec.ts +0 -1593
  122. package/src/builder/applyContextToSegments.ts +0 -165
  123. package/src/builder/buildDatafile.spec.ts +0 -445
  124. package/src/builder/buildDatafile.ts +0 -656
  125. package/src/builder/buildProject.ts +0 -344
  126. package/src/builder/buildRuntimeDatafiles.ts +0 -57
  127. package/src/builder/getFeatureRanges.ts +0 -61
  128. package/src/builder/hashes.ts +0 -109
  129. package/src/builder/index.ts +0 -2
  130. package/src/builder/mutateVariables.spec.ts +0 -1179
  131. package/src/builder/mutateVariables.ts +0 -112
  132. package/src/builder/mutator.spec.ts +0 -413
  133. package/src/builder/mutator.ts +0 -237
  134. package/src/builder/revision.spec.ts +0 -22
  135. package/src/builder/revision.ts +0 -22
  136. package/src/builder/traffic.spec.ts +0 -946
  137. package/src/builder/traffic.ts +0 -192
  138. package/src/cli/cli.ts +0 -105
  139. package/src/cli/index.ts +0 -2
  140. package/src/cli/plugins.ts +0 -43
  141. package/src/config/index.ts +0 -1
  142. package/src/config/projectConfig.spec.ts +0 -194
  143. package/src/config/projectConfig.ts +0 -280
  144. package/src/datasource/adapter.ts +0 -56
  145. package/src/datasource/datasource.ts +0 -305
  146. package/src/datasource/filesystemAdapter.spec.ts +0 -212
  147. package/src/datasource/filesystemAdapter.ts +0 -497
  148. package/src/datasource/index.ts +0 -3
  149. package/src/dependencies.ts +0 -13
  150. package/src/evaluate/index.ts +0 -243
  151. package/src/find-duplicate-segments/findDuplicateSegments.ts +0 -76
  152. package/src/find-duplicate-segments/index.ts +0 -74
  153. package/src/find-usage/index.ts +0 -565
  154. package/src/generate-code/index.ts +0 -106
  155. package/src/generate-code/typescript.spec.ts +0 -214
  156. package/src/generate-code/typescript.ts +0 -608
  157. package/src/index.ts +0 -20
  158. package/src/info/index.ts +0 -129
  159. package/src/init/index.ts +0 -65
  160. package/src/linter/attributeSchema.spec.ts +0 -342
  161. package/src/linter/attributeSchema.ts +0 -626
  162. package/src/linter/checkCircularDependency.ts +0 -45
  163. package/src/linter/checkPercentageExceedingSlot.ts +0 -61
  164. package/src/linter/conditionSchema.spec.ts +0 -510
  165. package/src/linter/conditionSchema.ts +0 -713
  166. package/src/linter/featureSchema.spec.ts +0 -1985
  167. package/src/linter/featureSchema.ts +0 -1922
  168. package/src/linter/groupSchema.ts +0 -47
  169. package/src/linter/index.ts +0 -1
  170. package/src/linter/lintProject.spec.ts +0 -336
  171. package/src/linter/lintProject.ts +0 -873
  172. package/src/linter/mutationNotation.spec.ts +0 -704
  173. package/src/linter/mutationNotation.ts +0 -442
  174. package/src/linter/printError.ts +0 -68
  175. package/src/linter/schema.spec.ts +0 -690
  176. package/src/linter/schema.ts +0 -491
  177. package/src/linter/segmentSchema.spec.ts +0 -358
  178. package/src/linter/segmentSchema.ts +0 -18
  179. package/src/linter/targetSchema.spec.ts +0 -159
  180. package/src/linter/targetSchema.ts +0 -46
  181. package/src/linter/testSchema.spec.ts +0 -258
  182. package/src/linter/testSchema.ts +0 -148
  183. package/src/linter/zodHelpers.ts +0 -16
  184. package/src/list/index.spec.ts +0 -135
  185. package/src/list/index.ts +0 -660
  186. package/src/list/matrix.spec.ts +0 -49
  187. package/src/list/matrix.ts +0 -212
  188. package/src/promoter/index.spec.ts +0 -617
  189. package/src/promoter/index.ts +0 -1266
  190. package/src/sets.ts +0 -76
  191. package/src/targeting.spec.ts +0 -74
  192. package/src/targeting.ts +0 -106
  193. package/src/tester/cliFormat.ts +0 -16
  194. package/src/tester/helpers.spec.ts +0 -149
  195. package/src/tester/helpers.ts +0 -76
  196. package/src/tester/index.ts +0 -2
  197. package/src/tester/prettyDuration.ts +0 -34
  198. package/src/tester/printTestResult.ts +0 -84
  199. package/src/tester/testFeature.ts +0 -367
  200. package/src/tester/testProject.spec.ts +0 -50
  201. package/src/tester/testProject.ts +0 -311
  202. package/src/tester/testSegment.ts +0 -77
  203. package/src/utils/extractKeys.ts +0 -136
  204. package/src/utils/git.ts +0 -158
  205. package/src/utils/index.ts +0 -3
  206. package/src/utils/pretty.ts +0 -12
  207. package/tsconfig.cjs.json +0 -13
  208. package/tsconfig.typecheck.json +0 -9
@@ -1,1179 +0,0 @@
1
- import type { VariableSchema, VariableValue } from "@featurevisor/types";
2
- import {
3
- resolveMutationsForMultipleVariables,
4
- resolveMutationsForSingleVariable,
5
- } from "./mutateVariables";
6
-
7
- describe("mutateVariables", function () {
8
- describe("resolveMutationsForMultipleVariables", function () {
9
- test("is a function", function () {
10
- expect(resolveMutationsForMultipleVariables).toBeInstanceOf(Function);
11
- });
12
-
13
- describe("returns undefined when", function () {
14
- test("overrides is undefined", function () {
15
- const schema: Record<string, VariableSchema> = {
16
- foo: { type: "string", defaultValue: "default" },
17
- };
18
- expect(resolveMutationsForMultipleVariables(schema, undefined)).toBeUndefined();
19
- });
20
-
21
- test("overrides is null", function () {
22
- const schema: Record<string, VariableSchema> = {
23
- foo: { type: "string", defaultValue: "default" },
24
- };
25
- expect(
26
- resolveMutationsForMultipleVariables(
27
- schema,
28
- null as unknown as Record<string, VariableValue>,
29
- ),
30
- ).toBeUndefined();
31
- });
32
-
33
- test("overrides is empty object", function () {
34
- const schema: Record<string, VariableSchema> = {
35
- foo: { type: "string", defaultValue: "default" },
36
- };
37
- expect(resolveMutationsForMultipleVariables(schema, {})).toBeUndefined();
38
- });
39
-
40
- test("variablesSchema is undefined", function () {
41
- expect(resolveMutationsForMultipleVariables(undefined, { foo: "bar" })).toBeUndefined();
42
- });
43
-
44
- test("variablesSchema is empty object", function () {
45
- expect(resolveMutationsForMultipleVariables({}, { foo: "bar" })).toBeUndefined();
46
- });
47
-
48
- test("every override key refers to a variable not in schema", function () {
49
- const schema: Record<string, VariableSchema> = {
50
- foo: { type: "string", defaultValue: "x" },
51
- };
52
- expect(
53
- resolveMutationsForMultipleVariables(schema, { bar: "y", baz: "z" }),
54
- ).toBeUndefined();
55
- });
56
- });
57
-
58
- describe("returns only overridden variables (not all from schema)", function () {
59
- test("single override includes only that variable", function () {
60
- const variablesSchema: Record<string, VariableSchema> = {
61
- foo: { type: "string", defaultValue: "defaultFoo" },
62
- bar: { type: "string", defaultValue: "defaultBar" },
63
- baz: { type: "string", defaultValue: "defaultBaz" },
64
- };
65
- const result = resolveMutationsForMultipleVariables(variablesSchema, {
66
- bar: "overriddenBar",
67
- });
68
- expect(result).toEqual({ bar: "overriddenBar" });
69
- expect(Object.keys(result!).sort()).toEqual(["bar"]);
70
- });
71
-
72
- test("multiple overrides include only those variables", function () {
73
- const variablesSchema: Record<string, VariableSchema> = {
74
- a: { type: "string", defaultValue: "a" },
75
- b: { type: "string", defaultValue: "b" },
76
- c: { type: "string", defaultValue: "c" },
77
- };
78
- const result = resolveMutationsForMultipleVariables(variablesSchema, {
79
- a: "A",
80
- c: "C",
81
- });
82
- expect(result).toEqual({ a: "A", c: "C" });
83
- expect(Object.keys(result!).sort()).toEqual(["a", "c"]);
84
- });
85
- });
86
-
87
- describe("exact key override (whole variable replacement)", function () {
88
- test("replaces entire value with override", function () {
89
- const variablesSchema: Record<string, VariableSchema> = {
90
- foo: { type: "object", defaultValue: { a: 1, b: 2 } },
91
- };
92
- const result = resolveMutationsForMultipleVariables(variablesSchema, {
93
- foo: { x: 10, y: 20 },
94
- });
95
- expect(result).toEqual({ foo: { x: 10, y: 20 } });
96
- });
97
-
98
- test("replaces string default", function () {
99
- const variablesSchema: Record<string, VariableSchema> = {
100
- name: { type: "string", defaultValue: "default" },
101
- };
102
- const result = resolveMutationsForMultipleVariables(variablesSchema, { name: "custom" });
103
- expect(result).toEqual({ name: "custom" });
104
- });
105
-
106
- test("replaces number default", function () {
107
- const variablesSchema: Record<string, VariableSchema> = {
108
- count: { type: "integer", defaultValue: 0 },
109
- };
110
- const result = resolveMutationsForMultipleVariables(variablesSchema, { count: 42 });
111
- expect(result).toEqual({ count: 42 });
112
- });
113
-
114
- test("replaces array default", function () {
115
- const variablesSchema: Record<string, VariableSchema> = {
116
- list: { type: "array", defaultValue: [1, 2, 3] },
117
- };
118
- const result = resolveMutationsForMultipleVariables(variablesSchema, { list: [10, 20] });
119
- expect(result).toEqual({ list: [10, 20] });
120
- });
121
- });
122
-
123
- describe("dot-notation override (mutates default)", function () {
124
- test("sets nested path on object default", function () {
125
- const variablesSchema: Record<string, VariableSchema> = {
126
- config: {
127
- type: "object",
128
- defaultValue: { a: 1, b: 2, c: 3 },
129
- },
130
- };
131
- const result = resolveMutationsForMultipleVariables(variablesSchema, {
132
- "config.b": 20,
133
- });
134
- expect(result).toEqual({ config: { a: 1, b: 20, c: 3 } });
135
- });
136
-
137
- test("sets deep nested path", function () {
138
- const variablesSchema: Record<string, VariableSchema> = {
139
- foo: {
140
- type: "object",
141
- defaultValue: { level1: { level2: { value: "old" } } },
142
- },
143
- };
144
- const result = resolveMutationsForMultipleVariables(variablesSchema, {
145
- "foo.level1.level2.value": "new",
146
- });
147
- expect(result).toEqual({
148
- foo: { level1: { level2: { value: "new" } } },
149
- });
150
- });
151
-
152
- test("multiple dot-notation overrides for same variable merge", function () {
153
- const variablesSchema: Record<string, VariableSchema> = {
154
- obj: {
155
- type: "object",
156
- defaultValue: { a: 1, b: 2, c: 3 },
157
- },
158
- };
159
- const result = resolveMutationsForMultipleVariables(variablesSchema, {
160
- "obj.a": 10,
161
- "obj.c": 30,
162
- });
163
- expect(result).toEqual({ obj: { a: 10, b: 2, c: 30 } });
164
- });
165
- });
166
-
167
- describe("order of application", function () {
168
- test("full key then nested: nested wins on that path", function () {
169
- const variablesSchema: Record<string, VariableSchema> = {
170
- foo: { type: "object", defaultValue: { a: 1, b: 2 } },
171
- };
172
- const result = resolveMutationsForMultipleVariables(variablesSchema, {
173
- foo: { a: 100, b: 200 },
174
- "foo.b": 999,
175
- });
176
- expect(result).toEqual({ foo: { a: 100, b: 999 } });
177
- });
178
-
179
- test("nested then full key: full key applied first (shorter), then nested", function () {
180
- const variablesSchema: Record<string, VariableSchema> = {
181
- foo: { type: "object", defaultValue: { a: 1, b: 2 } },
182
- };
183
- const result = resolveMutationsForMultipleVariables(variablesSchema, {
184
- "foo.a": 10,
185
- foo: { x: 1 },
186
- });
187
- expect(result).toEqual({ foo: { x: 1, a: 10 } });
188
- });
189
- });
190
-
191
- describe("defaultValue handling", function () {
192
- test("uses clone of defaultValue (does not mutate schema)", function () {
193
- const defaultValue = { a: 1 };
194
- const variablesSchema: Record<string, VariableSchema> = {
195
- foo: { type: "object", defaultValue },
196
- };
197
- resolveMutationsForMultipleVariables(variablesSchema, { "foo.a": 2 });
198
- expect(defaultValue).toEqual({ a: 1 });
199
- });
200
-
201
- test("variable with undefined defaultValue and nested override leaves value undefined", function () {
202
- const variablesSchema: Record<string, VariableSchema> = {
203
- foo: { type: "object", defaultValue: undefined },
204
- };
205
- const result = resolveMutationsForMultipleVariables(variablesSchema, {
206
- "foo.a": 1,
207
- });
208
- expect(result).toEqual({ foo: undefined });
209
- });
210
-
211
- test("variable with null defaultValue and exact override", function () {
212
- const variablesSchema: Record<string, VariableSchema> = {
213
- foo: { type: "string", defaultValue: null },
214
- };
215
- const result = resolveMutationsForMultipleVariables(variablesSchema, { foo: "set" });
216
- expect(result).toEqual({ foo: "set" });
217
- });
218
- });
219
-
220
- describe("override key not in schema", function () {
221
- test("ignores unknown variable key (no entry in result)", function () {
222
- const variablesSchema: Record<string, VariableSchema> = {
223
- foo: { type: "string", defaultValue: "x" },
224
- };
225
- const result = resolveMutationsForMultipleVariables(variablesSchema, {
226
- foo: "ok",
227
- unknownVar: "ignored",
228
- });
229
- expect(result).toEqual({ foo: "ok" });
230
- });
231
-
232
- test("ignores dotted path whose first segment is not in schema", function () {
233
- const variablesSchema: Record<string, VariableSchema> = {
234
- foo: { type: "string", defaultValue: "x" },
235
- };
236
- const result = resolveMutationsForMultipleVariables(variablesSchema, {
237
- "other.deep.path": 1,
238
- });
239
- expect(result).toBeUndefined();
240
- });
241
- });
242
-
243
- describe("does not mutate inputs", function () {
244
- test("overrides object is not mutated", function () {
245
- const variablesSchema: Record<string, VariableSchema> = {
246
- foo: { type: "object", defaultValue: {} },
247
- };
248
- const overrides = { foo: { key: "value" } };
249
- resolveMutationsForMultipleVariables(variablesSchema, overrides);
250
- expect(overrides.foo).toEqual({ key: "value" });
251
- });
252
-
253
- test("schema defaultValues are not mutated", function () {
254
- const defaultValue = { nested: { x: 1 } };
255
- const variablesSchema: Record<string, VariableSchema> = {
256
- foo: { type: "object", defaultValue },
257
- };
258
- resolveMutationsForMultipleVariables(variablesSchema, { "foo.nested.x": 2 });
259
- expect(defaultValue).toEqual({ nested: { x: 1 } });
260
- });
261
- });
262
-
263
- describe("multiple variables with dot notation", function () {
264
- test("resolves each overridden variable independently", function () {
265
- const variablesSchema: Record<string, VariableSchema> = {
266
- theme: {
267
- type: "object",
268
- defaultValue: { primary: "blue", secondary: "gray" },
269
- },
270
- count: { type: "integer", defaultValue: 0 },
271
- };
272
- const result = resolveMutationsForMultipleVariables(variablesSchema, {
273
- "theme.primary": "red",
274
- count: 5,
275
- });
276
- expect(result).toEqual({
277
- theme: { primary: "red", secondary: "gray" },
278
- count: 5,
279
- });
280
- });
281
- });
282
-
283
- describe("edge cases", function () {
284
- test("override key with single segment (no dot)", function () {
285
- const variablesSchema: Record<string, VariableSchema> = {
286
- x: { type: "string", defaultValue: "default" },
287
- };
288
- const result = resolveMutationsForMultipleVariables(variablesSchema, { x: "y" });
289
- expect(result).toEqual({ x: "y" });
290
- });
291
-
292
- test("override key with multiple dots", function () {
293
- const variablesSchema: Record<string, VariableSchema> = {
294
- a: { type: "object", defaultValue: { b: { c: 0 } } },
295
- };
296
- const result = resolveMutationsForMultipleVariables(variablesSchema, {
297
- "a.b.c": 1,
298
- });
299
- expect(result).toEqual({ a: { b: { c: 1 } } });
300
- });
301
-
302
- test("same variable overridden by exact and dotted keys (shorter key applied first)", function () {
303
- const variablesSchema: Record<string, VariableSchema> = {
304
- v: { type: "object", defaultValue: { p: 1, q: 2 } },
305
- };
306
- const result = resolveMutationsForMultipleVariables(variablesSchema, {
307
- "v.p": 10,
308
- v: { p: 100, q: 200 },
309
- });
310
- expect(result).toEqual({ v: { p: 10, q: 200 } });
311
- });
312
-
313
- test("returns undefined when result is empty (all override keys unknown)", function () {
314
- const variablesSchema: Record<string, VariableSchema> = {
315
- known: { type: "string", defaultValue: "v" },
316
- };
317
- const result = resolveMutationsForMultipleVariables(variablesSchema, {
318
- unknown: "x",
319
- "another.thing": 1,
320
- });
321
- expect(result).toBeUndefined();
322
- });
323
- });
324
-
325
- describe("complex: deeply nested objects", function () {
326
- test("object with multiple branches, override one leaf", function () {
327
- const variablesSchema: Record<string, VariableSchema> = {
328
- root: {
329
- type: "object",
330
- defaultValue: {
331
- left: { a: 1, b: 2 },
332
- right: { x: 10, y: 20 },
333
- center: { p: 100, q: 200 },
334
- },
335
- },
336
- };
337
- const result = resolveMutationsForMultipleVariables(variablesSchema, {
338
- "root.right.y": 99,
339
- });
340
- expect(result).toEqual({
341
- root: {
342
- left: { a: 1, b: 2 },
343
- right: { x: 10, y: 99 },
344
- center: { p: 100, q: 200 },
345
- },
346
- });
347
- });
348
-
349
- test("override multiple leaves in different branches", function () {
350
- const variablesSchema: Record<string, VariableSchema> = {
351
- tree: {
352
- type: "object",
353
- defaultValue: {
354
- l: { l: { v: 1 }, r: { v: 2 } },
355
- r: { l: { v: 3 }, r: { v: 4 } },
356
- },
357
- },
358
- };
359
- const result = resolveMutationsForMultipleVariables(variablesSchema, {
360
- "tree.l.l.v": 10,
361
- "tree.r.r.v": 40,
362
- });
363
- expect(result).toEqual({
364
- tree: {
365
- l: { l: { v: 10 }, r: { v: 2 } },
366
- r: { l: { v: 3 }, r: { v: 40 } },
367
- },
368
- });
369
- });
370
-
371
- test("four levels deep with override at each level", function () {
372
- const variablesSchema: Record<string, VariableSchema> = {
373
- d: {
374
- type: "object",
375
- defaultValue: {
376
- l1: {
377
- l2: { l3: { l4: "deep" } },
378
- },
379
- },
380
- },
381
- };
382
- const result = resolveMutationsForMultipleVariables(variablesSchema, {
383
- "d.l1.l2.l3.l4": "updated",
384
- });
385
- expect(result).toEqual({
386
- d: {
387
- l1: {
388
- l2: { l3: { l4: "updated" } },
389
- },
390
- },
391
- });
392
- });
393
-
394
- test("replace whole branch with exact key then override nested under it", function () {
395
- const variablesSchema: Record<string, VariableSchema> = {
396
- cfg: {
397
- type: "object",
398
- defaultValue: {
399
- api: { url: "old", timeout: 5 },
400
- ui: { theme: "light" },
401
- },
402
- },
403
- };
404
- const result = resolveMutationsForMultipleVariables(variablesSchema, {
405
- cfg: { api: { url: "https://new", timeout: 10 }, ui: { theme: "dark" } },
406
- "cfg.api.timeout": 30,
407
- });
408
- expect(result).toEqual({
409
- cfg: {
410
- api: { url: "https://new", timeout: 30 },
411
- ui: { theme: "dark" },
412
- },
413
- });
414
- });
415
- });
416
-
417
- describe("complex: arrays", function () {
418
- test("override element at index", function () {
419
- const variablesSchema: Record<string, VariableSchema> = {
420
- list: { type: "array", defaultValue: [10, 20, 30] },
421
- };
422
- const result = resolveMutationsForMultipleVariables(variablesSchema, {
423
- "list[1]": 99,
424
- });
425
- expect(result).toEqual({ list: [10, 99, 30] });
426
- });
427
-
428
- test("override first and last index", function () {
429
- const variablesSchema: Record<string, VariableSchema> = {
430
- arr: { type: "array", defaultValue: ["a", "b", "c", "d"] },
431
- };
432
- const result = resolveMutationsForMultipleVariables(variablesSchema, {
433
- "arr[0]": "A",
434
- "arr[3]": "D",
435
- });
436
- expect(result).toEqual({ arr: ["A", "b", "c", "D"] });
437
- });
438
-
439
- test("array of primitives replaced entirely with exact key", function () {
440
- const variablesSchema: Record<string, VariableSchema> = {
441
- ids: { type: "array", defaultValue: [1, 2, 3] },
442
- };
443
- const result = resolveMutationsForMultipleVariables(variablesSchema, {
444
- ids: [100, 200, 300],
445
- });
446
- expect(result).toEqual({ ids: [100, 200, 300] });
447
- });
448
- });
449
-
450
- describe("complex: arrays of objects", function () {
451
- test("override property of object at index", function () {
452
- const variablesSchema: Record<string, VariableSchema> = {
453
- items: {
454
- type: "array",
455
- defaultValue: [
456
- { id: 1, name: "first" },
457
- { id: 2, name: "second" },
458
- { id: 3, name: "third" },
459
- ],
460
- },
461
- };
462
- const result = resolveMutationsForMultipleVariables(variablesSchema, {
463
- "items[0].name": "First",
464
- "items[2].id": 30,
465
- });
466
- expect(result).toEqual({
467
- items: [
468
- { id: 1, name: "First" },
469
- { id: 2, name: "second" },
470
- { id: 30, name: "third" },
471
- ],
472
- });
473
- });
474
-
475
- test("override nested property inside object at index", function () {
476
- const variablesSchema: Record<string, VariableSchema> = {
477
- users: {
478
- type: "array",
479
- defaultValue: [
480
- { id: 1, profile: { displayName: "Alice", role: "admin" } },
481
- { id: 2, profile: { displayName: "Bob", role: "user" } },
482
- ],
483
- },
484
- };
485
- const result = resolveMutationsForMultipleVariables(variablesSchema, {
486
- "users[0].profile.displayName": "Alicia",
487
- "users[1].profile.role": "editor",
488
- });
489
- expect(result).toEqual({
490
- users: [
491
- { id: 1, profile: { displayName: "Alicia", role: "admin" } },
492
- { id: 2, profile: { displayName: "Bob", role: "editor" } },
493
- ],
494
- });
495
- });
496
-
497
- test("override multiple properties of same object at index", function () {
498
- const variablesSchema: Record<string, VariableSchema> = {
499
- rows: {
500
- type: "array",
501
- defaultValue: [
502
- { a: 1, b: 2, c: 3 },
503
- { a: 4, b: 5, c: 6 },
504
- ],
505
- },
506
- };
507
- const result = resolveMutationsForMultipleVariables(variablesSchema, {
508
- "rows[1].a": 40,
509
- "rows[1].c": 60,
510
- });
511
- expect(result).toEqual({
512
- rows: [
513
- { a: 1, b: 2, c: 3 },
514
- { a: 40, b: 5, c: 60 },
515
- ],
516
- });
517
- });
518
-
519
- test("replace whole array of objects with exact key", function () {
520
- const variablesSchema: Record<string, VariableSchema> = {
521
- list: {
522
- type: "array",
523
- defaultValue: [{ id: 1 }, { id: 2 }],
524
- },
525
- };
526
- const result = resolveMutationsForMultipleVariables(variablesSchema, {
527
- list: [
528
- { id: 10, name: "x" },
529
- { id: 20, name: "y" },
530
- ],
531
- });
532
- expect(result).toEqual({
533
- list: [
534
- { id: 10, name: "x" },
535
- { id: 20, name: "y" },
536
- ],
537
- });
538
- });
539
- });
540
-
541
- describe("complex: object containing arrays of objects", function () {
542
- test("config with sections and items", function () {
543
- const variablesSchema: Record<string, VariableSchema> = {
544
- config: {
545
- type: "object",
546
- defaultValue: {
547
- title: "App",
548
- sections: [
549
- { id: "s1", label: "Section 1", items: [{ name: "a" }, { name: "b" }] },
550
- { id: "s2", label: "Section 2", items: [{ name: "c" }] },
551
- ],
552
- },
553
- },
554
- };
555
- const result = resolveMutationsForMultipleVariables(variablesSchema, {
556
- "config.title": "My App",
557
- "config.sections[0].label": "First",
558
- "config.sections[0].items[1].name": "B",
559
- "config.sections[1].items[0].name": "C",
560
- });
561
- expect(result).toEqual({
562
- config: {
563
- title: "My App",
564
- sections: [
565
- { id: "s1", label: "First", items: [{ name: "a" }, { name: "B" }] },
566
- { id: "s2", label: "Section 2", items: [{ name: "C" }] },
567
- ],
568
- },
569
- });
570
- });
571
-
572
- test("feature flags style: rules array with variables", function () {
573
- const variablesSchema: Record<string, VariableSchema> = {
574
- payload: {
575
- type: "object",
576
- defaultValue: {
577
- enabled: true,
578
- rules: [
579
- { id: "r1", percentage: 50, config: { theme: "light", size: "md" } },
580
- { id: "r2", percentage: 50, config: { theme: "dark", size: "sm" } },
581
- ],
582
- },
583
- },
584
- };
585
- const result = resolveMutationsForMultipleVariables(variablesSchema, {
586
- "payload.rules[0].config.theme": "blue",
587
- "payload.rules[1].percentage": 100,
588
- });
589
- expect(result).toEqual({
590
- payload: {
591
- enabled: true,
592
- rules: [
593
- { id: "r1", percentage: 50, config: { theme: "blue", size: "md" } },
594
- { id: "r2", percentage: 100, config: { theme: "dark", size: "sm" } },
595
- ],
596
- },
597
- });
598
- });
599
- });
600
-
601
- describe("complex: multiple variables with mixed structures", function () {
602
- test("two variables: one nested object, one array of objects", function () {
603
- const variablesSchema: Record<string, VariableSchema> = {
604
- settings: {
605
- type: "object",
606
- defaultValue: {
607
- theme: "light",
608
- layout: { sidebar: true, width: 240 },
609
- },
610
- },
611
- items: {
612
- type: "array",
613
- defaultValue: [
614
- { id: 1, label: "One" },
615
- { id: 2, label: "Two" },
616
- ],
617
- },
618
- };
619
- const result = resolveMutationsForMultipleVariables(variablesSchema, {
620
- "settings.layout.width": 320,
621
- "items[1].label": "Two Updated",
622
- });
623
- expect(result).toEqual({
624
- settings: {
625
- theme: "light",
626
- layout: { sidebar: true, width: 320 },
627
- },
628
- items: [
629
- { id: 1, label: "One" },
630
- { id: 2, label: "Two Updated" },
631
- ],
632
- });
633
- });
634
-
635
- test("three variables overridden with different depth", function () {
636
- const variablesSchema: Record<string, VariableSchema> = {
637
- flat: { type: "string", defaultValue: "v1" },
638
- nested: {
639
- type: "object",
640
- defaultValue: { a: { b: "old" } },
641
- },
642
- list: { type: "array", defaultValue: [{ x: 1 }, { x: 2 }] },
643
- };
644
- const result = resolveMutationsForMultipleVariables(variablesSchema, {
645
- flat: "V1",
646
- "nested.a.b": "new",
647
- "list[0].x": 10,
648
- });
649
- expect(result).toEqual({
650
- flat: "V1",
651
- nested: { a: { b: "new" } },
652
- list: [{ x: 10 }, { x: 2 }],
653
- });
654
- });
655
- });
656
-
657
- describe("complex: edge cases for arrays and nesting", function () {
658
- test("empty array default, override index (mutator creates/updates)", function () {
659
- const variablesSchema: Record<string, VariableSchema> = {
660
- arr: { type: "array", defaultValue: [] },
661
- };
662
- const result = resolveMutationsForMultipleVariables(variablesSchema, {
663
- "arr[0]": "first",
664
- });
665
- expect(result).toEqual({ arr: ["first"] });
666
- });
667
-
668
- test("object with empty array property, override inside array", function () {
669
- const variablesSchema: Record<string, VariableSchema> = {
670
- data: {
671
- type: "object",
672
- defaultValue: { tags: [] },
673
- },
674
- };
675
- const result = resolveMutationsForMultipleVariables(variablesSchema, {
676
- "data.tags[0]": "new-tag",
677
- });
678
- expect(result).toEqual({ data: { tags: ["new-tag"] } });
679
- });
680
-
681
- test("array of objects with nested array", function () {
682
- const variablesSchema: Record<string, VariableSchema> = {
683
- groups: {
684
- type: "array",
685
- defaultValue: [
686
- { name: "G1", ids: [1, 2, 3] },
687
- { name: "G2", ids: [4, 5] },
688
- ],
689
- },
690
- };
691
- const result = resolveMutationsForMultipleVariables(variablesSchema, {
692
- "groups[0].ids[1]": 20,
693
- "groups[1].name": "Group 2",
694
- });
695
- expect(result).toEqual({
696
- groups: [
697
- { name: "G1", ids: [1, 20, 3] },
698
- { name: "Group 2", ids: [4, 5] },
699
- ],
700
- });
701
- });
702
-
703
- test("deep chain: object with array of objects with nested object", function () {
704
- const variablesSchema: Record<string, VariableSchema> = {
705
- root: {
706
- type: "object",
707
- defaultValue: {
708
- items: [
709
- { id: 1, meta: { count: 0, label: "a" } },
710
- { id: 2, meta: { count: 0, label: "b" } },
711
- ],
712
- },
713
- },
714
- };
715
- const result = resolveMutationsForMultipleVariables(variablesSchema, {
716
- "root.items[0].meta.count": 5,
717
- "root.items[1].meta.label": "B",
718
- });
719
- expect(result).toEqual({
720
- root: {
721
- items: [
722
- { id: 1, meta: { count: 5, label: "a" } },
723
- { id: 2, meta: { count: 0, label: "B" } },
724
- ],
725
- },
726
- });
727
- });
728
- });
729
-
730
- describe("complex: many overrides on single variable", function () {
731
- test("single variable with 5+ dotted overrides", function () {
732
- const variablesSchema: Record<string, VariableSchema> = {
733
- state: {
734
- type: "object",
735
- defaultValue: {
736
- a: 1,
737
- b: 2,
738
- c: 3,
739
- d: 4,
740
- e: 5,
741
- f: 6,
742
- },
743
- },
744
- };
745
- const result = resolveMutationsForMultipleVariables(variablesSchema, {
746
- "state.a": 10,
747
- "state.c": 30,
748
- "state.e": 50,
749
- });
750
- expect(result).toEqual({
751
- state: { a: 10, b: 2, c: 30, d: 4, e: 50, f: 6 },
752
- });
753
- });
754
-
755
- test("single variable: mix of exact key and dotted overrides (order by length)", function () {
756
- const variablesSchema: Record<string, VariableSchema> = {
757
- v: {
758
- type: "object",
759
- defaultValue: { x: 0, y: 0 },
760
- },
761
- };
762
- const result = resolveMutationsForMultipleVariables(variablesSchema, {
763
- "v.x": 1,
764
- v: { x: 100, y: 200, z: 300 },
765
- "v.y": 2,
766
- });
767
- expect(result).toEqual({ v: { x: 1, y: 2, z: 300 } });
768
- });
769
- });
770
-
771
- describe("complex: schema with multiple variables, only some overridden", function () {
772
- test("six variables in schema, override two with complex paths", function () {
773
- const variablesSchema: Record<string, VariableSchema> = {
774
- a: { type: "string", defaultValue: "a" },
775
- b: { type: "object", defaultValue: { b1: 1, b2: 2 } },
776
- c: { type: "array", defaultValue: [1, 2, 3] },
777
- d: {
778
- type: "object",
779
- defaultValue: { nested: { value: "d" } },
780
- },
781
- e: {
782
- type: "array",
783
- defaultValue: [
784
- { id: 1, name: "e1" },
785
- { id: 2, name: "e2" },
786
- ],
787
- },
788
- f: { type: "integer", defaultValue: 0 },
789
- };
790
- const result = resolveMutationsForMultipleVariables(variablesSchema, {
791
- "b.b2": 20,
792
- "d.nested.value": "D",
793
- "e[0].name": "E1",
794
- });
795
- expect(result).toEqual({
796
- b: { b1: 1, b2: 20 },
797
- d: { nested: { value: "D" } },
798
- e: [
799
- { id: 1, name: "E1" },
800
- { id: 2, name: "e2" },
801
- ],
802
- });
803
- expect(Object.keys(result!).sort()).toEqual(["b", "d", "e"]);
804
- });
805
- });
806
- });
807
-
808
- describe("resolveMutationsForSingleVariable", function () {
809
- test("is a function", function () {
810
- expect(resolveMutationsForSingleVariable).toBeInstanceOf(Function);
811
- });
812
-
813
- describe("returns overrideValue unchanged when", function () {
814
- test("variablesSchema is undefined", function () {
815
- expect(resolveMutationsForSingleVariable(undefined, "foo", "bar")).toBe("bar");
816
- expect(resolveMutationsForSingleVariable(undefined, "foo", { a: 1 })).toEqual({ a: 1 });
817
- });
818
-
819
- test("variablesSchema is empty (no variableKey)", function () {
820
- expect(resolveMutationsForSingleVariable({}, "foo", "bar")).toBe("bar");
821
- });
822
-
823
- test("variableKey is not in variablesSchema", function () {
824
- const schema: Record<string, VariableSchema> = {
825
- bar: { type: "string", defaultValue: "x" },
826
- };
827
- expect(resolveMutationsForSingleVariable(schema, "foo", "value")).toBe("value");
828
- expect(resolveMutationsForSingleVariable(schema, "foo", { nested: 1 })).toEqual({
829
- nested: 1,
830
- });
831
- });
832
-
833
- test("overrideValue is null", function () {
834
- const schema: Record<string, VariableSchema> = {
835
- foo: { type: "string", defaultValue: "default" },
836
- };
837
- expect(resolveMutationsForSingleVariable(schema, "foo", null)).toBeNull();
838
- });
839
-
840
- test("overrideValue is undefined", function () {
841
- const schema: Record<string, VariableSchema> = {
842
- foo: { type: "string", defaultValue: "default" },
843
- };
844
- expect(resolveMutationsForSingleVariable(schema, "foo", undefined)).toBeUndefined();
845
- });
846
-
847
- test("overrideValue is a string (full replacement)", function () {
848
- const schema: Record<string, VariableSchema> = {
849
- name: { type: "string", defaultValue: "default" },
850
- };
851
- expect(resolveMutationsForSingleVariable(schema, "name", "custom")).toBe("custom");
852
- });
853
-
854
- test("overrideValue is a number (full replacement)", function () {
855
- const schema: Record<string, VariableSchema> = {
856
- count: { type: "integer", defaultValue: 0 },
857
- };
858
- expect(resolveMutationsForSingleVariable(schema, "count", 42)).toBe(42);
859
- });
860
-
861
- test("overrideValue is a boolean (full replacement)", function () {
862
- const schema: Record<string, VariableSchema> = {
863
- flag: { type: "boolean", defaultValue: false },
864
- };
865
- expect(resolveMutationsForSingleVariable(schema, "flag", true)).toBe(true);
866
- });
867
-
868
- test("overrideValue is an array (full replacement, not path map)", function () {
869
- const schema: Record<string, VariableSchema> = {
870
- list: { type: "array", defaultValue: [1, 2, 3] },
871
- };
872
- const arr = [10, 20];
873
- expect(resolveMutationsForSingleVariable(schema, "list", arr)).toEqual([10, 20]);
874
- expect(resolveMutationsForSingleVariable(schema, "list", [])).toEqual([]);
875
- });
876
- });
877
-
878
- describe("plain object as path map (merge with default)", function () {
879
- test("single path key merges with default", function () {
880
- const schema: Record<string, VariableSchema> = {
881
- theme: {
882
- type: "object",
883
- defaultValue: { primary: "blue", secondary: "gray" },
884
- },
885
- };
886
- const result = resolveMutationsForSingleVariable(schema, "theme", {
887
- primary: "red",
888
- });
889
- expect(result).toEqual({ primary: "red", secondary: "gray" });
890
- });
891
-
892
- test("multiple path keys merge with default", function () {
893
- const schema: Record<string, VariableSchema> = {
894
- config: {
895
- type: "object",
896
- defaultValue: { a: 1, b: 2, c: 3 },
897
- },
898
- };
899
- const result = resolveMutationsForSingleVariable(schema, "config", {
900
- a: 10,
901
- c: 30,
902
- });
903
- expect(result).toEqual({ a: 10, b: 2, c: 30 });
904
- });
905
-
906
- test("nested path in path map", function () {
907
- const schema: Record<string, VariableSchema> = {
908
- obj: {
909
- type: "object",
910
- defaultValue: { level1: { level2: { v: "old" } } },
911
- },
912
- };
913
- const result = resolveMutationsForSingleVariable(schema, "obj", {
914
- "level1.level2.v": "new",
915
- });
916
- expect(result).toEqual({ level1: { level2: { v: "new" } } });
917
- });
918
-
919
- test("exact variableKey key in path map replaces entire value", function () {
920
- const schema: Record<string, VariableSchema> = {
921
- theme: {
922
- type: "object",
923
- defaultValue: { primary: "blue", secondary: "gray" },
924
- },
925
- };
926
- const result = resolveMutationsForSingleVariable(schema, "theme", {
927
- theme: { primary: "red", accent: "yellow" },
928
- });
929
- expect(result).toEqual({ primary: "red", accent: "yellow" });
930
- });
931
-
932
- test("variableKey key and path keys: full value applied first then paths (order)", function () {
933
- const schema: Record<string, VariableSchema> = {
934
- v: {
935
- type: "object",
936
- defaultValue: { a: 1, b: 2 },
937
- },
938
- };
939
- const result = resolveMutationsForSingleVariable(schema, "v", {
940
- v: { a: 100, b: 200 },
941
- b: 999,
942
- });
943
- expect(result).toEqual({ a: 100, b: 999 });
944
- });
945
-
946
- test("array index in path map", function () {
947
- const schema: Record<string, VariableSchema> = {
948
- items: {
949
- type: "array",
950
- defaultValue: [
951
- { id: 1, name: "a" },
952
- { id: 2, name: "b" },
953
- ],
954
- },
955
- };
956
- const result = resolveMutationsForSingleVariable(schema, "items", {
957
- "[0].name": "A",
958
- "[1].id": 20,
959
- });
960
- expect(result).toEqual([
961
- { id: 1, name: "A" },
962
- { id: 20, name: "b" },
963
- ]);
964
- });
965
-
966
- test("deep path and array index", function () {
967
- const schema: Record<string, VariableSchema> = {
968
- data: {
969
- type: "object",
970
- defaultValue: {
971
- list: [{ x: 1 }, { x: 2 }],
972
- },
973
- },
974
- };
975
- const result = resolveMutationsForSingleVariable(schema, "data", {
976
- "list[0].x": 10,
977
- });
978
- expect(result).toEqual({
979
- list: [{ x: 10 }, { x: 2 }],
980
- });
981
- });
982
-
983
- test("empty path map returns default merged (no overrides)", function () {
984
- const schema: Record<string, VariableSchema> = {
985
- foo: { type: "object", defaultValue: { a: 1 } },
986
- };
987
- const result = resolveMutationsForSingleVariable(schema, "foo", {});
988
- expect(result).toEqual({});
989
- });
990
-
991
- test("uses provided baseValue instead of defaultValue for path mutations", function () {
992
- const schema: Record<string, VariableSchema> = {
993
- cfg: {
994
- type: "object",
995
- defaultValue: { source: "default", nested: { value: 1 } },
996
- },
997
- };
998
-
999
- const result = resolveMutationsForSingleVariable(
1000
- schema,
1001
- "cfg",
1002
- {
1003
- "nested.value": 2,
1004
- },
1005
- { source: "rule", nested: { value: 10 }, extra: true },
1006
- );
1007
-
1008
- expect(result).toEqual({
1009
- source: "rule",
1010
- nested: { value: 2 },
1011
- extra: true,
1012
- });
1013
- });
1014
-
1015
- test("baseValue with full replacement key still applies replacement first", function () {
1016
- const schema: Record<string, VariableSchema> = {
1017
- cfg: {
1018
- type: "object",
1019
- defaultValue: { a: 1, b: 2 },
1020
- },
1021
- };
1022
-
1023
- const result = resolveMutationsForSingleVariable(
1024
- schema,
1025
- "cfg",
1026
- {
1027
- cfg: { a: 100, c: 300 },
1028
- c: 999,
1029
- },
1030
- { a: 10, b: 20 },
1031
- );
1032
-
1033
- expect(result).toEqual({ a: 100, c: 999 });
1034
- });
1035
- });
1036
-
1037
- describe("does not mutate input", function () {
1038
- test("overrideValue object is not mutated", function () {
1039
- const schema: Record<string, VariableSchema> = {
1040
- foo: { type: "object", defaultValue: {} },
1041
- };
1042
- const pathMap = { a: 1, b: 2 };
1043
- resolveMutationsForSingleVariable(schema, "foo", pathMap);
1044
- expect(pathMap).toEqual({ a: 1, b: 2 });
1045
- });
1046
- });
1047
-
1048
- describe("edge cases", function () {
1049
- test("variableKey with same name as path segment", function () {
1050
- const schema: Record<string, VariableSchema> = {
1051
- theme: {
1052
- type: "object",
1053
- defaultValue: { theme: "light", color: "white" },
1054
- },
1055
- };
1056
- const result = resolveMutationsForSingleVariable(schema, "theme", {
1057
- color: "black",
1058
- });
1059
- expect(result).toEqual({ theme: "light", color: "black" });
1060
- });
1061
-
1062
- test("multiple variables in schema, only one resolved", function () {
1063
- const schema: Record<string, VariableSchema> = {
1064
- a: { type: "object", defaultValue: { x: 1 } },
1065
- b: { type: "object", defaultValue: { y: 2 } },
1066
- };
1067
- const result = resolveMutationsForSingleVariable(schema, "a", {
1068
- x: 10,
1069
- });
1070
- expect(result).toEqual({ x: 10 });
1071
- });
1072
-
1073
- test("path map with only variableKey key (full replacement)", function () {
1074
- const schema: Record<string, VariableSchema> = {
1075
- foo: { type: "object", defaultValue: { a: 1, b: 2 } },
1076
- };
1077
- const result = resolveMutationsForSingleVariable(schema, "foo", {
1078
- foo: { x: 1 },
1079
- });
1080
- expect(result).toEqual({ x: 1 });
1081
- });
1082
-
1083
- test("primitive default with path map (path overwrites)", function () {
1084
- const schema: Record<string, VariableSchema> = {
1085
- value: { type: "string", defaultValue: "default" },
1086
- };
1087
- const result = resolveMutationsForSingleVariable(schema, "value", {
1088
- value: "overridden",
1089
- });
1090
- expect(result).toEqual("overridden");
1091
- });
1092
- });
1093
-
1094
- describe("complex: object path maps", function () {
1095
- test("path map with three-level nested path", function () {
1096
- const schema: Record<string, VariableSchema> = {
1097
- cfg: {
1098
- type: "object",
1099
- defaultValue: { a: { b: { c: 0 } } },
1100
- },
1101
- };
1102
- const result = resolveMutationsForSingleVariable(schema, "cfg", {
1103
- "a.b.c": 1,
1104
- });
1105
- expect(result).toEqual({ a: { b: { c: 1 } } });
1106
- });
1107
-
1108
- test("path map with array index then property", function () {
1109
- const schema: Record<string, VariableSchema> = {
1110
- rows: {
1111
- type: "array",
1112
- defaultValue: [
1113
- { id: 1, label: "a" },
1114
- { id: 2, label: "b" },
1115
- ],
1116
- },
1117
- };
1118
- const result = resolveMutationsForSingleVariable(schema, "rows", {
1119
- "[0].label": "A",
1120
- "[1].id": 20,
1121
- });
1122
- expect(result).toEqual([
1123
- { id: 1, label: "A" },
1124
- { id: 20, label: "b" },
1125
- ]);
1126
- });
1127
-
1128
- test("path map full replacement then path (order: full first)", function () {
1129
- const schema: Record<string, VariableSchema> = {
1130
- v: {
1131
- type: "object",
1132
- defaultValue: { x: 0, y: 0 },
1133
- },
1134
- };
1135
- const result = resolveMutationsForSingleVariable(schema, "v", {
1136
- v: { x: 1, y: 2, z: 3 },
1137
- y: 20,
1138
- });
1139
- expect(result).toEqual({ x: 1, y: 20, z: 3 });
1140
- });
1141
-
1142
- test("path map with mixed path styles", function () {
1143
- const schema: Record<string, VariableSchema> = {
1144
- data: {
1145
- type: "object",
1146
- defaultValue: { count: 0, nested: { value: "old" } },
1147
- },
1148
- };
1149
- const result = resolveMutationsForSingleVariable(schema, "data", {
1150
- count: 5,
1151
- "nested.value": "new",
1152
- });
1153
- expect(result).toEqual({ count: 5, nested: { value: "new" } });
1154
- });
1155
- });
1156
-
1157
- describe("schema default handling", function () {
1158
- test("defaultValue null with path map leaves value undefined (mutator cannot mutate null)", function () {
1159
- const schema: Record<string, VariableSchema> = {
1160
- foo: { type: "object", defaultValue: null },
1161
- };
1162
- const result = resolveMutationsForSingleVariable(schema, "foo", {
1163
- a: 1,
1164
- });
1165
- expect(result).toBeUndefined();
1166
- });
1167
-
1168
- test("defaultValue undefined with path map leaves value undefined (mutator cannot mutate undefined)", function () {
1169
- const schema: Record<string, VariableSchema> = {
1170
- foo: { type: "object", defaultValue: undefined },
1171
- };
1172
- const result = resolveMutationsForSingleVariable(schema, "foo", {
1173
- a: 1,
1174
- });
1175
- expect(result).toBeUndefined();
1176
- });
1177
- });
1178
- });
1179
- });