@featurevisor/core 3.1.0 → 3.3.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 (213) 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/linter/testSchema.d.ts +4 -0
  13. package/lib/linter/testSchema.js +46 -4
  14. package/lib/linter/testSchema.js.map +1 -1
  15. package/lib/promoter/index.js +62 -1
  16. package/lib/promoter/index.js.map +1 -1
  17. package/lib/tester/testFeature.js +17 -16
  18. package/lib/tester/testFeature.js.map +1 -1
  19. package/package.json +10 -7
  20. package/CHANGELOG.md +0 -2246
  21. package/coverage/clover.xml +0 -5456
  22. package/coverage/coverage-final.json +0 -64
  23. package/coverage/lcov-report/base.css +0 -224
  24. package/coverage/lcov-report/block-navigation.js +0 -87
  25. package/coverage/lcov-report/favicon.png +0 -0
  26. package/coverage/lcov-report/index.html +0 -371
  27. package/coverage/lcov-report/prettify.css +0 -1
  28. package/coverage/lcov-report/prettify.js +0 -2
  29. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  30. package/coverage/lcov-report/sorter.js +0 -210
  31. package/coverage/lcov-report/src/assess-distribution/index.html +0 -116
  32. package/coverage/lcov-report/src/assess-distribution/index.ts.html +0 -754
  33. package/coverage/lcov-report/src/benchmark/index.html +0 -116
  34. package/coverage/lcov-report/src/benchmark/index.ts.html +0 -793
  35. package/coverage/lcov-report/src/builder/allocator.ts.html +0 -220
  36. package/coverage/lcov-report/src/builder/applyContextToConditions.ts.html +0 -502
  37. package/coverage/lcov-report/src/builder/applyContextToDatafile.ts.html +0 -772
  38. package/coverage/lcov-report/src/builder/applyContextToSegments.ts.html +0 -580
  39. package/coverage/lcov-report/src/builder/buildDatafile.ts.html +0 -2053
  40. package/coverage/lcov-report/src/builder/buildProject.ts.html +0 -1117
  41. package/coverage/lcov-report/src/builder/buildRuntimeDatafiles.ts.html +0 -256
  42. package/coverage/lcov-report/src/builder/getFeatureRanges.ts.html +0 -268
  43. package/coverage/lcov-report/src/builder/hashes.ts.html +0 -412
  44. package/coverage/lcov-report/src/builder/index.html +0 -311
  45. package/coverage/lcov-report/src/builder/index.ts.html +0 -91
  46. package/coverage/lcov-report/src/builder/mutateVariables.ts.html +0 -421
  47. package/coverage/lcov-report/src/builder/mutator.ts.html +0 -796
  48. package/coverage/lcov-report/src/builder/revision.ts.html +0 -151
  49. package/coverage/lcov-report/src/builder/traffic.ts.html +0 -661
  50. package/coverage/lcov-report/src/cli/cli.ts.html +0 -400
  51. package/coverage/lcov-report/src/cli/index.html +0 -131
  52. package/coverage/lcov-report/src/cli/plugins.ts.html +0 -214
  53. package/coverage/lcov-report/src/config/index.html +0 -131
  54. package/coverage/lcov-report/src/config/index.ts.html +0 -88
  55. package/coverage/lcov-report/src/config/projectConfig.ts.html +0 -925
  56. package/coverage/lcov-report/src/datasource/adapter.ts.html +0 -253
  57. package/coverage/lcov-report/src/datasource/datasource.ts.html +0 -1000
  58. package/coverage/lcov-report/src/datasource/filesystemAdapter.ts.html +0 -1576
  59. package/coverage/lcov-report/src/datasource/index.html +0 -161
  60. package/coverage/lcov-report/src/datasource/index.ts.html +0 -94
  61. package/coverage/lcov-report/src/evaluate/index.html +0 -116
  62. package/coverage/lcov-report/src/evaluate/index.ts.html +0 -814
  63. package/coverage/lcov-report/src/find-duplicate-segments/findDuplicateSegments.ts.html +0 -313
  64. package/coverage/lcov-report/src/find-duplicate-segments/index.html +0 -131
  65. package/coverage/lcov-report/src/find-duplicate-segments/index.ts.html +0 -307
  66. package/coverage/lcov-report/src/find-usage/index.html +0 -116
  67. package/coverage/lcov-report/src/find-usage/index.ts.html +0 -1780
  68. package/coverage/lcov-report/src/generate-code/index.html +0 -131
  69. package/coverage/lcov-report/src/generate-code/index.ts.html +0 -403
  70. package/coverage/lcov-report/src/generate-code/typescript.ts.html +0 -1909
  71. package/coverage/lcov-report/src/index.html +0 -131
  72. package/coverage/lcov-report/src/info/index.html +0 -116
  73. package/coverage/lcov-report/src/info/index.ts.html +0 -472
  74. package/coverage/lcov-report/src/init/index.html +0 -116
  75. package/coverage/lcov-report/src/init/index.ts.html +0 -280
  76. package/coverage/lcov-report/src/linter/attributeSchema.ts.html +0 -1963
  77. package/coverage/lcov-report/src/linter/checkCircularDependency.ts.html +0 -220
  78. package/coverage/lcov-report/src/linter/checkPercentageExceedingSlot.ts.html +0 -268
  79. package/coverage/lcov-report/src/linter/conditionSchema.ts.html +0 -2224
  80. package/coverage/lcov-report/src/linter/featureSchema.ts.html +0 -5851
  81. package/coverage/lcov-report/src/linter/groupSchema.ts.html +0 -226
  82. package/coverage/lcov-report/src/linter/index.html +0 -326
  83. package/coverage/lcov-report/src/linter/index.ts.html +0 -88
  84. package/coverage/lcov-report/src/linter/lintProject.ts.html +0 -2704
  85. package/coverage/lcov-report/src/linter/mutationNotation.ts.html +0 -1411
  86. package/coverage/lcov-report/src/linter/printError.ts.html +0 -289
  87. package/coverage/lcov-report/src/linter/schema.ts.html +0 -1558
  88. package/coverage/lcov-report/src/linter/segmentSchema.ts.html +0 -139
  89. package/coverage/lcov-report/src/linter/targetSchema.ts.html +0 -223
  90. package/coverage/lcov-report/src/linter/testSchema.ts.html +0 -529
  91. package/coverage/lcov-report/src/linter/zodHelpers.ts.html +0 -133
  92. package/coverage/lcov-report/src/list/index.html +0 -131
  93. package/coverage/lcov-report/src/list/index.ts.html +0 -2065
  94. package/coverage/lcov-report/src/list/matrix.ts.html +0 -721
  95. package/coverage/lcov-report/src/promoter/index.html +0 -116
  96. package/coverage/lcov-report/src/promoter/index.ts.html +0 -3883
  97. package/coverage/lcov-report/src/sets.ts.html +0 -313
  98. package/coverage/lcov-report/src/targeting.ts.html +0 -403
  99. package/coverage/lcov-report/src/tester/cliFormat.ts.html +0 -133
  100. package/coverage/lcov-report/src/tester/helpers.ts.html +0 -313
  101. package/coverage/lcov-report/src/tester/index.html +0 -206
  102. package/coverage/lcov-report/src/tester/prettyDuration.ts.html +0 -187
  103. package/coverage/lcov-report/src/tester/printTestResult.ts.html +0 -337
  104. package/coverage/lcov-report/src/tester/testFeature.ts.html +0 -1186
  105. package/coverage/lcov-report/src/tester/testProject.ts.html +0 -1018
  106. package/coverage/lcov-report/src/tester/testSegment.ts.html +0 -316
  107. package/coverage/lcov-report/src/utils/extractKeys.ts.html +0 -493
  108. package/coverage/lcov-report/src/utils/git.ts.html +0 -559
  109. package/coverage/lcov-report/src/utils/index.html +0 -161
  110. package/coverage/lcov-report/src/utils/index.ts.html +0 -94
  111. package/coverage/lcov-report/src/utils/pretty.ts.html +0 -121
  112. package/coverage/lcov.info +0 -11615
  113. package/jest.config.js +0 -22
  114. package/json-schema/README.md +0 -5
  115. package/json-schema/attribute.json +0 -40
  116. package/json-schema/feature.json +0 -394
  117. package/json-schema/segment.json +0 -137
  118. package/src/assess-distribution/index.ts +0 -223
  119. package/src/benchmark/index.ts +0 -236
  120. package/src/builder/allocator.spec.ts +0 -105
  121. package/src/builder/allocator.ts +0 -45
  122. package/src/builder/applyContextToConditions.spec.ts +0 -2677
  123. package/src/builder/applyContextToConditions.ts +0 -139
  124. package/src/builder/applyContextToDatafile.spec.ts +0 -2626
  125. package/src/builder/applyContextToDatafile.ts +0 -229
  126. package/src/builder/applyContextToSegments.spec.ts +0 -1593
  127. package/src/builder/applyContextToSegments.ts +0 -165
  128. package/src/builder/buildDatafile.spec.ts +0 -445
  129. package/src/builder/buildDatafile.ts +0 -656
  130. package/src/builder/buildProject.ts +0 -344
  131. package/src/builder/buildRuntimeDatafiles.ts +0 -57
  132. package/src/builder/getFeatureRanges.ts +0 -61
  133. package/src/builder/hashes.ts +0 -109
  134. package/src/builder/index.ts +0 -2
  135. package/src/builder/mutateVariables.spec.ts +0 -1179
  136. package/src/builder/mutateVariables.ts +0 -112
  137. package/src/builder/mutator.spec.ts +0 -413
  138. package/src/builder/mutator.ts +0 -237
  139. package/src/builder/revision.spec.ts +0 -22
  140. package/src/builder/revision.ts +0 -22
  141. package/src/builder/traffic.spec.ts +0 -946
  142. package/src/builder/traffic.ts +0 -192
  143. package/src/cli/cli.ts +0 -105
  144. package/src/cli/index.ts +0 -2
  145. package/src/cli/plugins.ts +0 -43
  146. package/src/config/index.ts +0 -1
  147. package/src/config/projectConfig.spec.ts +0 -194
  148. package/src/config/projectConfig.ts +0 -280
  149. package/src/datasource/adapter.ts +0 -56
  150. package/src/datasource/datasource.ts +0 -305
  151. package/src/datasource/filesystemAdapter.spec.ts +0 -212
  152. package/src/datasource/filesystemAdapter.ts +0 -497
  153. package/src/datasource/index.ts +0 -3
  154. package/src/dependencies.ts +0 -13
  155. package/src/evaluate/index.ts +0 -243
  156. package/src/find-duplicate-segments/findDuplicateSegments.ts +0 -76
  157. package/src/find-duplicate-segments/index.ts +0 -74
  158. package/src/find-usage/index.ts +0 -565
  159. package/src/generate-code/index.ts +0 -106
  160. package/src/generate-code/typescript.spec.ts +0 -214
  161. package/src/generate-code/typescript.ts +0 -608
  162. package/src/index.ts +0 -20
  163. package/src/info/index.ts +0 -129
  164. package/src/init/index.ts +0 -65
  165. package/src/linter/attributeSchema.spec.ts +0 -342
  166. package/src/linter/attributeSchema.ts +0 -626
  167. package/src/linter/checkCircularDependency.ts +0 -45
  168. package/src/linter/checkPercentageExceedingSlot.ts +0 -61
  169. package/src/linter/conditionSchema.spec.ts +0 -510
  170. package/src/linter/conditionSchema.ts +0 -713
  171. package/src/linter/featureSchema.spec.ts +0 -1985
  172. package/src/linter/featureSchema.ts +0 -1922
  173. package/src/linter/groupSchema.ts +0 -47
  174. package/src/linter/index.ts +0 -1
  175. package/src/linter/lintProject.spec.ts +0 -336
  176. package/src/linter/lintProject.ts +0 -873
  177. package/src/linter/mutationNotation.spec.ts +0 -704
  178. package/src/linter/mutationNotation.ts +0 -442
  179. package/src/linter/printError.ts +0 -68
  180. package/src/linter/schema.spec.ts +0 -690
  181. package/src/linter/schema.ts +0 -491
  182. package/src/linter/segmentSchema.spec.ts +0 -358
  183. package/src/linter/segmentSchema.ts +0 -18
  184. package/src/linter/targetSchema.spec.ts +0 -159
  185. package/src/linter/targetSchema.ts +0 -46
  186. package/src/linter/testSchema.spec.ts +0 -258
  187. package/src/linter/testSchema.ts +0 -148
  188. package/src/linter/zodHelpers.ts +0 -16
  189. package/src/list/index.spec.ts +0 -135
  190. package/src/list/index.ts +0 -660
  191. package/src/list/matrix.spec.ts +0 -49
  192. package/src/list/matrix.ts +0 -212
  193. package/src/promoter/index.spec.ts +0 -617
  194. package/src/promoter/index.ts +0 -1266
  195. package/src/sets.ts +0 -76
  196. package/src/targeting.spec.ts +0 -74
  197. package/src/targeting.ts +0 -106
  198. package/src/tester/cliFormat.ts +0 -16
  199. package/src/tester/helpers.spec.ts +0 -149
  200. package/src/tester/helpers.ts +0 -76
  201. package/src/tester/index.ts +0 -2
  202. package/src/tester/prettyDuration.ts +0 -34
  203. package/src/tester/printTestResult.ts +0 -84
  204. package/src/tester/testFeature.ts +0 -367
  205. package/src/tester/testProject.spec.ts +0 -50
  206. package/src/tester/testProject.ts +0 -311
  207. package/src/tester/testSegment.ts +0 -77
  208. package/src/utils/extractKeys.ts +0 -136
  209. package/src/utils/git.ts +0 -158
  210. package/src/utils/index.ts +0 -3
  211. package/src/utils/pretty.ts +0 -12
  212. package/tsconfig.cjs.json +0 -13
  213. package/tsconfig.typecheck.json +0 -9
@@ -1,704 +0,0 @@
1
- /**
2
- * Exhaustive unit tests for mutation notation parsing and validation:
3
- * isMutationKey, parseMutationKey, resolveSchemaAtPath, parsePathMapKey, validateMutationKey.
4
- */
5
- import type { Schema } from "@featurevisor/types";
6
-
7
- import {
8
- isMutationKey,
9
- parseMutationKey,
10
- parsePathMapKey,
11
- resolveSchemaAtPath,
12
- validateMutationKey,
13
- } from "./mutationNotation";
14
-
15
- describe("mutationNotation.ts", () => {
16
- describe("isMutationKey", () => {
17
- it("returns false for empty string", () => {
18
- expect(isMutationKey("")).toBe(false);
19
- });
20
-
21
- it("returns false for whitespace-only", () => {
22
- expect(isMutationKey(" ")).toBe(false);
23
- });
24
-
25
- it("returns false for plain variable name (no path, no operation)", () => {
26
- expect(isMutationKey("foo")).toBe(false);
27
- expect(isMutationKey("title")).toBe(false);
28
- expect(isMutationKey("config")).toBe(false);
29
- });
30
-
31
- it("returns true when key contains dot (path)", () => {
32
- expect(isMutationKey("foo.bar")).toBe(true);
33
- expect(isMutationKey("config.width")).toBe(true);
34
- expect(isMutationKey("a.b.c")).toBe(true);
35
- });
36
-
37
- it("returns true when key contains bracket (array index or selector)", () => {
38
- expect(isMutationKey("items[0]")).toBe(true);
39
- expect(isMutationKey("tags[1]")).toBe(true);
40
- expect(isMutationKey("items[id=1]")).toBe(true);
41
- });
42
-
43
- it("returns true when key ends with :append", () => {
44
- expect(isMutationKey("tags:append")).toBe(true);
45
- expect(isMutationKey("items:append")).toBe(true);
46
- });
47
-
48
- it("returns true when key ends with :prepend", () => {
49
- expect(isMutationKey("tags:prepend")).toBe(true);
50
- });
51
-
52
- it("returns true when key ends with :after", () => {
53
- expect(isMutationKey("items[id=1]:after")).toBe(true);
54
- });
55
-
56
- it("returns true when key ends with :before", () => {
57
- expect(isMutationKey("items[id=1]:before")).toBe(true);
58
- });
59
-
60
- it("returns true when key ends with :remove", () => {
61
- expect(isMutationKey("config.compact:remove")).toBe(true);
62
- expect(isMutationKey("items[0]:remove")).toBe(true);
63
- });
64
-
65
- it("trims input before checking", () => {
66
- expect(isMutationKey(" config.width ")).toBe(true);
67
- expect(isMutationKey(" tags:append ")).toBe(true);
68
- });
69
- });
70
-
71
- describe("parseMutationKey", () => {
72
- it("returns null for empty string", () => {
73
- expect(parseMutationKey("")).toBeNull();
74
- });
75
-
76
- it("returns null for whitespace-only", () => {
77
- expect(parseMutationKey(" ")).toBeNull();
78
- });
79
-
80
- it("parses plain variable name as rootKey only, operation set", () => {
81
- const r = parseMutationKey("foo");
82
- expect(r).not.toBeNull();
83
- expect(r!.rootKey).toBe("foo");
84
- expect(r!.pathSegments).toEqual([]);
85
- expect(r!.allSegments).toEqual([{ key: "foo" }]);
86
- expect(r!.operation).toBe("set");
87
- });
88
-
89
- it("parses dot path: rootKey and single path segment", () => {
90
- const r = parseMutationKey("config.width");
91
- expect(r).not.toBeNull();
92
- expect(r!.rootKey).toBe("config");
93
- expect(r!.pathSegments).toEqual([{ key: "width" }]);
94
- expect(r!.allSegments).toEqual([{ key: "config" }, { key: "width" }]);
95
- expect(r!.operation).toBe("set");
96
- });
97
-
98
- it("parses deep dot path", () => {
99
- const r = parseMutationKey("settings.display.fontSize");
100
- expect(r).not.toBeNull();
101
- expect(r!.rootKey).toBe("settings");
102
- expect(r!.pathSegments).toEqual([{ key: "display" }, { key: "fontSize" }]);
103
- expect(r!.allSegments).toEqual([
104
- { key: "settings" },
105
- { key: "display" },
106
- { key: "fontSize" },
107
- ]);
108
- });
109
-
110
- it("parses array index notation", () => {
111
- const r = parseMutationKey("items[0]");
112
- expect(r).not.toBeNull();
113
- expect(r!.rootKey).toBe("items");
114
- expect(r!.pathSegments).toEqual([{ key: "", index: 0 }]);
115
- expect(r!.allSegments).toEqual([{ key: "items", index: 0 }]);
116
- expect(r!.operation).toBe("set");
117
- });
118
-
119
- it("parses array index then property", () => {
120
- const r = parseMutationKey("items[0].name");
121
- expect(r).not.toBeNull();
122
- expect(r!.rootKey).toBe("items");
123
- expect(r!.pathSegments).toEqual([{ key: "", index: 0 }, { key: "name" }]);
124
- expect(r!.allSegments).toEqual([{ key: "items", index: 0 }, { key: "name" }]);
125
- });
126
-
127
- it("parses selector notation", () => {
128
- const r = parseMutationKey("items[id=1]");
129
- expect(r).not.toBeNull();
130
- expect(r!.rootKey).toBe("items");
131
- expect(r!.pathSegments).toEqual([{ key: "", selector: { prop: "id", value: "1" } }]);
132
- expect(r!.allSegments).toEqual([{ key: "items", selector: { prop: "id", value: "1" } }]);
133
- });
134
-
135
- it("parses key starting with bracket (root-level array segment)", () => {
136
- const r = parseMutationKey("[0].name");
137
- expect(r).not.toBeNull();
138
- expect(r!.rootKey).toBe("");
139
- expect(r!.pathSegments).toEqual([{ key: "", index: 0 }, { key: "name" }]);
140
- expect(r!.allSegments).toEqual([{ key: "", index: 0 }, { key: "name" }]);
141
- });
142
-
143
- it("parses :append operation", () => {
144
- const r = parseMutationKey("tags:append");
145
- expect(r).not.toBeNull();
146
- expect(r!.rootKey).toBe("tags");
147
- expect(r!.pathSegments).toEqual([]);
148
- expect(r!.operation).toBe("append");
149
- });
150
-
151
- it("parses :prepend operation", () => {
152
- const r = parseMutationKey("tags:prepend");
153
- expect(r).not.toBeNull();
154
- expect(r!.rootKey).toBe("tags");
155
- expect(r!.operation).toBe("prepend");
156
- });
157
-
158
- it("parses :remove on property", () => {
159
- const r = parseMutationKey("config.compact:remove");
160
- expect(r).not.toBeNull();
161
- expect(r!.rootKey).toBe("config");
162
- expect(r!.pathSegments).toEqual([{ key: "compact" }]);
163
- expect(r!.operation).toBe("remove");
164
- });
165
-
166
- it("parses :remove on array index", () => {
167
- const r = parseMutationKey("items[2]:remove");
168
- expect(r).not.toBeNull();
169
- expect(r!.rootKey).toBe("items");
170
- expect(r!.pathSegments).toEqual([{ key: "", index: 2 }]);
171
- expect(r!.operation).toBe("remove");
172
- });
173
-
174
- it("parses :after with selector", () => {
175
- const r = parseMutationKey("items[id=2]:after");
176
- expect(r).not.toBeNull();
177
- expect(r!.rootKey).toBe("items");
178
- expect(r!.pathSegments).toEqual([{ key: "", selector: { prop: "id", value: "2" } }]);
179
- expect(r!.operation).toBe("after");
180
- });
181
-
182
- it("parses :before with selector", () => {
183
- const r = parseMutationKey("items[id=2]:before");
184
- expect(r).not.toBeNull();
185
- expect(r!.operation).toBe("before");
186
- });
187
-
188
- it("trims input", () => {
189
- const r = parseMutationKey(" config.width ");
190
- expect(r).not.toBeNull();
191
- expect(r!.rootKey).toBe("config");
192
- expect(r!.pathSegments).toEqual([{ key: "width" }]);
193
- });
194
- });
195
-
196
- describe("resolveSchemaAtPath", () => {
197
- const objectSchema: Schema = {
198
- type: "object",
199
- properties: {
200
- width: { type: "integer" },
201
- theme: { type: "string" },
202
- nested: {
203
- type: "object",
204
- properties: {
205
- deep: { type: "string" },
206
- },
207
- },
208
- },
209
- };
210
-
211
- const arraySchema: Schema = {
212
- type: "array",
213
- items: {
214
- type: "object",
215
- properties: {
216
- id: { type: "string" },
217
- name: { type: "string" },
218
- },
219
- },
220
- };
221
-
222
- it("returns null when variableSchema is null", () => {
223
- expect(resolveSchemaAtPath(null, [])).toBeNull();
224
- expect(resolveSchemaAtPath(null, [{ key: "width" }])).toBeNull();
225
- });
226
-
227
- it("returns resolved schema for empty path (root)", () => {
228
- const result = resolveSchemaAtPath(objectSchema, []);
229
- expect(result).toEqual(objectSchema);
230
- });
231
-
232
- it("resolves schema ref when variableSchema has schema key and schemasByKey provided", () => {
233
- const refSchema: Schema = { type: "object", properties: { x: { type: "string" } } };
234
- const variableSchema = { schema: "MyRef" as const };
235
- const result = resolveSchemaAtPath(variableSchema, [], { MyRef: refSchema });
236
- expect(result).toEqual(refSchema);
237
- });
238
-
239
- it("returns unresolved ref object when schema ref is missing in schemasByKey (no resolution)", () => {
240
- const variableSchema = { schema: "Missing" as const };
241
- const result = resolveSchemaAtPath(variableSchema, [], {});
242
- expect(result).toEqual(variableSchema);
243
- });
244
-
245
- it("resolves one level of path (object property)", () => {
246
- const result = resolveSchemaAtPath(objectSchema, [{ key: "width" }]);
247
- expect(result).toEqual({ type: "integer" });
248
- });
249
-
250
- it("resolves nested path", () => {
251
- const result = resolveSchemaAtPath(objectSchema, [{ key: "nested" }, { key: "deep" }]);
252
- expect(result).toEqual({ type: "string" });
253
- });
254
-
255
- it("returns null when path property does not exist", () => {
256
- const result = resolveSchemaAtPath(objectSchema, [{ key: "unknown" }]);
257
- expect(result).toBeNull();
258
- });
259
-
260
- it("resolves unknown object key via additionalProperties", () => {
261
- const result = resolveSchemaAtPath(
262
- {
263
- type: "object",
264
- additionalProperties: { type: "string" },
265
- },
266
- [{ key: "dynamicKey" }],
267
- );
268
- expect(result).toEqual({ type: "string" });
269
- });
270
-
271
- it("returns null when stepping into non-object", () => {
272
- const result = resolveSchemaAtPath(objectSchema, [{ key: "width" }, { key: "foo" }]);
273
- expect(result).toBeNull();
274
- });
275
-
276
- it("resolves array items schema for index segment", () => {
277
- const result = resolveSchemaAtPath(arraySchema, [{ key: "", index: 0 }]);
278
- expect(result).toEqual({
279
- type: "object",
280
- properties: { id: { type: "string" }, name: { type: "string" } },
281
- });
282
- });
283
-
284
- it("resolves array item then property", () => {
285
- const result = resolveSchemaAtPath(arraySchema, [{ key: "", index: 0 }, { key: "name" }]);
286
- expect(result).toEqual({ type: "string" });
287
- });
288
-
289
- it("returns null when stepping into non-array with index segment", () => {
290
- const result = resolveSchemaAtPath(objectSchema, [{ key: "", index: 0 }]);
291
- expect(result).toBeNull();
292
- });
293
-
294
- it("returns null when schema has oneOf (path through oneOf invalid)", () => {
295
- const oneOfSchema: Schema = {
296
- oneOf: [{ type: "string" }, { type: "integer" }],
297
- };
298
- const result = resolveSchemaAtPath(oneOfSchema, [{ key: "any" }]);
299
- expect(result).toBeNull();
300
- });
301
-
302
- it("resolves nested ref in properties when schemasByKey provided", () => {
303
- const linkSchema: Schema = { type: "object", properties: { url: { type: "string" } } };
304
- const root: Schema = {
305
- type: "object",
306
- properties: {
307
- mainLink: { schema: "Link" },
308
- },
309
- };
310
- const result = resolveSchemaAtPath(root, [{ key: "mainLink" }], { Link: linkSchema });
311
- expect(result).toEqual(linkSchema);
312
- });
313
- });
314
-
315
- describe("parsePathMapKey", () => {
316
- it("returns null for empty string", () => {
317
- expect(parsePathMapKey("")).toBeNull();
318
- });
319
-
320
- it("returns null when parseMutationKey returns null", () => {
321
- expect(parsePathMapKey(" ")).toBeNull();
322
- });
323
-
324
- it("parses dot path into segments", () => {
325
- expect(parsePathMapKey("display.fontSize")).toEqual([
326
- { key: "display" },
327
- { key: "fontSize" },
328
- ]);
329
- });
330
-
331
- it("parses bracket-first path", () => {
332
- expect(parsePathMapKey("[0].name")).toEqual([{ key: "", index: 0 }, { key: "name" }]);
333
- });
334
-
335
- it("parses single key", () => {
336
- expect(parsePathMapKey("label")).toEqual([{ key: "label" }]);
337
- });
338
-
339
- it("parses deep path", () => {
340
- expect(parsePathMapKey("config.nested.deep")).toEqual([
341
- { key: "config" },
342
- { key: "nested" },
343
- { key: "deep" },
344
- ]);
345
- });
346
-
347
- it("parses path with array index in middle", () => {
348
- expect(parsePathMapKey("rows[1].label")).toEqual([
349
- { key: "rows", index: 1 },
350
- { key: "label" },
351
- ]);
352
- });
353
- });
354
-
355
- describe("validateMutationKey", () => {
356
- const objectSchema: Schema = {
357
- type: "object",
358
- properties: {
359
- theme: { type: "string" },
360
- compact: { type: "boolean" },
361
- width: { type: "integer" },
362
- },
363
- required: ["compact"],
364
- };
365
-
366
- const arraySchema: Schema = {
367
- type: "array",
368
- items: { type: "string" },
369
- };
370
-
371
- const arrayOfObjectsSchema: Schema = {
372
- type: "array",
373
- items: {
374
- type: "object",
375
- properties: { id: { type: "string" }, name: { type: "string" } },
376
- },
377
- };
378
-
379
- const nestedObjectSchema: Schema = {
380
- type: "object",
381
- properties: {
382
- config: {
383
- type: "object",
384
- properties: {
385
- width: { type: "integer" },
386
- optionalKey: { type: "string" },
387
- },
388
- required: ["width"],
389
- },
390
- },
391
- };
392
-
393
- const variableSchemaByKey: Record<string, Schema> = {
394
- config: objectSchema,
395
- tags: arraySchema,
396
- items: arrayOfObjectsSchema,
397
- settings: nestedObjectSchema,
398
- partialMutationShowcase: {
399
- type: "object",
400
- properties: {
401
- rows: {
402
- type: "array",
403
- items: {
404
- type: "object",
405
- properties: {
406
- id: { type: "integer" },
407
- label: { type: "string" },
408
- },
409
- },
410
- },
411
- },
412
- },
413
- };
414
-
415
- it("returns invalid when key is empty (parseMutationKey null)", () => {
416
- const r = validateMutationKey("", variableSchemaByKey);
417
- expect(r.valid).toBe(false);
418
- expect(r.error).toContain("Invalid mutation notation");
419
- expect(r.operation).toBe("set");
420
- });
421
-
422
- it("returns invalid when key has no root (e.g. [0] only)", () => {
423
- const r = validateMutationKey("[0]", variableSchemaByKey);
424
- expect(r.valid).toBe(false);
425
- expect(r.error).toContain("must start with a variable name");
426
- });
427
-
428
- it("returns invalid when root variable is not in variablesSchema", () => {
429
- const r = validateMutationKey("unknownVar.foo", variableSchemaByKey);
430
- expect(r.valid).toBe(false);
431
- expect(r.error).toContain("not defined in");
432
- expect(r.rootKey).toBe("unknownVar");
433
- });
434
-
435
- it("returns invalid when variable has schema ref but path fails (unresolved ref has no properties)", () => {
436
- const schemas: Record<string, Schema> = {
437
- myVar: { schema: "MissingRef" },
438
- };
439
- const r = validateMutationKey("myVar.foo", schemas, {});
440
- expect(r.valid).toBe(false);
441
- expect(r.error).toMatch(/path does not exist|could not be loaded/);
442
- expect(r.error).toContain("myVar");
443
- });
444
-
445
- it("returns invalid when root schema is oneOf and path has segments", () => {
446
- const schemas: Record<string, Schema> = {
447
- oneOfVar: { oneOf: [{ type: "string" }, { type: "integer" }] },
448
- };
449
- const r = validateMutationKey("oneOfVar.foo", schemas);
450
- expect(r.valid).toBe(false);
451
- expect(r.error).toContain("oneOf");
452
- expect(r.error).toContain("path resolution not defined");
453
- });
454
-
455
- describe("operation: set", () => {
456
- it("valid when setting whole variable (no path)", () => {
457
- const r = validateMutationKey("config", variableSchemaByKey);
458
- expect(r.valid).toBe(true);
459
- expect(r.operation).toBe("set");
460
- expect(r.valueSchema).toEqual(objectSchema);
461
- });
462
-
463
- it("valid when path exists in schema", () => {
464
- const r = validateMutationKey("config.width", variableSchemaByKey);
465
- expect(r.valid).toBe(true);
466
- expect(r.valueSchema).toEqual({ type: "integer" });
467
- });
468
-
469
- it("invalid when path does not exist", () => {
470
- const r = validateMutationKey("config.nonexistent", variableSchemaByKey);
471
- expect(r.valid).toBe(false);
472
- expect(r.error).toContain("path does not exist");
473
- });
474
-
475
- it("valid when path uses additionalProperties", () => {
476
- const schemas: Record<string, Schema> = {
477
- labels: {
478
- type: "object",
479
- additionalProperties: { type: "string" },
480
- },
481
- };
482
- const r = validateMutationKey("labels.headline", schemas);
483
- expect(r.valid).toBe(true);
484
- expect(r.valueSchema).toEqual({ type: "string" });
485
- });
486
-
487
- it("invalid when target schema is oneOf", () => {
488
- const schemas: Record<string, Schema> = {
489
- x: {
490
- type: "object",
491
- properties: {
492
- choice: { oneOf: [{ type: "string" }, { type: "integer" }] },
493
- },
494
- },
495
- };
496
- const r = validateMutationKey("x.choice", schemas);
497
- expect(r.valid).toBe(false);
498
- expect(r.error).toContain("oneOf");
499
- expect(r.error).toContain("mutation target must be a single schema");
500
- });
501
- });
502
-
503
- describe("operation: append / prepend", () => {
504
- it("valid on root array variable", () => {
505
- const rAppend = validateMutationKey("tags:append", variableSchemaByKey);
506
- expect(rAppend.valid).toBe(true);
507
- expect(rAppend.operation).toBe("append");
508
- expect(rAppend.valueSchema).toEqual({ type: "string" });
509
-
510
- const rPrepend = validateMutationKey("tags:prepend", variableSchemaByKey);
511
- expect(rPrepend.valid).toBe(true);
512
- expect(rPrepend.operation).toBe("prepend");
513
- expect(rPrepend.valueSchema).toEqual({ type: "string" });
514
- });
515
-
516
- it("invalid when path does not exist", () => {
517
- const r = validateMutationKey("config.nested:append", variableSchemaByKey);
518
- expect(r.valid).toBe(false);
519
- expect(r.error).toContain("path does not exist");
520
- });
521
-
522
- it("invalid when path points to non-array", () => {
523
- const r = validateMutationKey("config.width:append", variableSchemaByKey);
524
- expect(r.valid).toBe(false);
525
- expect(r.error).toContain("only allowed on array");
526
- expect(r.error).toContain("does not point to an array");
527
- });
528
-
529
- it("valid when array items is oneOf (valueSchema is the oneOf schema)", () => {
530
- const oneOfItems: Schema = {
531
- oneOf: [{ type: "string" }, { type: "integer" }],
532
- };
533
- const schemas: Record<string, Schema> = {
534
- arr: {
535
- type: "array",
536
- items: oneOfItems,
537
- },
538
- };
539
- const r = validateMutationKey("arr:append", schemas);
540
- expect(r.valid).toBe(true);
541
- expect(r.valueSchema).toEqual(oneOfItems);
542
- });
543
-
544
- it("valid on nested array path when schema has array at that path", () => {
545
- const schemas: Record<string, Schema> = {
546
- root: {
547
- type: "object",
548
- properties: {
549
- list: { type: "array", items: { type: "string" } },
550
- },
551
- },
552
- };
553
- const r = validateMutationKey("root.list:append", schemas);
554
- expect(r.valid).toBe(true);
555
- expect(r.valueSchema).toEqual({ type: "string" });
556
- });
557
- });
558
-
559
- describe("operation: after / before", () => {
560
- it("valid on array with index segment", () => {
561
- const r = validateMutationKey("items[1]:after", variableSchemaByKey);
562
- expect(r.valid).toBe(true);
563
- expect(r.operation).toBe("after");
564
- expect(r.valueSchema).toEqual({
565
- type: "object",
566
- properties: { id: { type: "string" }, name: { type: "string" } },
567
- });
568
- });
569
-
570
- it("valid on array with selector segment", () => {
571
- const r = validateMutationKey("items[id=2]:before", variableSchemaByKey);
572
- expect(r.valid).toBe(true);
573
- expect(r.operation).toBe("before");
574
- });
575
-
576
- it("valid on nested array selector segment in object variable", () => {
577
- const r = validateMutationKey(
578
- "partialMutationShowcase.rows[id=1]:after",
579
- variableSchemaByKey,
580
- );
581
- expect(r.valid).toBe(true);
582
- expect(r.operation).toBe("after");
583
- expect(r.valueSchema).toEqual({
584
- type: "object",
585
- properties: {
586
- id: { type: "integer" },
587
- label: { type: "string" },
588
- },
589
- });
590
- });
591
-
592
- it("invalid when path does not exist or does not point to array element", () => {
593
- const r = validateMutationKey("config.fake[0]:after", variableSchemaByKey);
594
- expect(r.valid).toBe(false);
595
- expect(r.error).toMatch(/path does not exist|does not point to an array/);
596
- });
597
-
598
- it("invalid when array items is oneOf", () => {
599
- const schemas: Record<string, Schema> = {
600
- arr: {
601
- type: "array",
602
- items: { oneOf: [{ type: "string" }] },
603
- },
604
- };
605
- const r = validateMutationKey("arr[0]:after", schemas);
606
- expect(r.valid).toBe(false);
607
- expect(r.error).toContain("oneOf");
608
- });
609
- });
610
-
611
- describe("operation: remove", () => {
612
- it("valid when removing optional object property", () => {
613
- const r = validateMutationKey("settings.config.optionalKey:remove", variableSchemaByKey);
614
- expect(r.valid).toBe(true);
615
- expect(r.operation).toBe("remove");
616
- expect(r.valueSchema).toBeNull();
617
- });
618
-
619
- it("invalid when removing required object property", () => {
620
- const r = validateMutationKey("config.compact:remove", variableSchemaByKey);
621
- expect(r.valid).toBe(false);
622
- expect(r.error).toContain("Cannot remove required property");
623
- expect(r.error).toContain("compact");
624
- expect(r.error).toContain("required");
625
- });
626
-
627
- it("invalid when removing required nested property", () => {
628
- const r = validateMutationKey("settings.config.width:remove", variableSchemaByKey);
629
- expect(r.valid).toBe(false);
630
- expect(r.error).toContain("Cannot remove required property");
631
- expect(r.error).toContain("width");
632
- });
633
-
634
- it("valid when removing array element by index", () => {
635
- const r = validateMutationKey("items[0]:remove", variableSchemaByKey);
636
- expect(r.valid).toBe(true);
637
- expect(r.operation).toBe("remove");
638
- expect(r.valueSchema).toBeNull();
639
- });
640
-
641
- it("valid when removing array element by selector", () => {
642
- const r = validateMutationKey("items[id=1]:remove", variableSchemaByKey);
643
- expect(r.valid).toBe(true);
644
- });
645
-
646
- it("valid when removing nested array element by selector", () => {
647
- const r = validateMutationKey(
648
- "partialMutationShowcase.rows[id=2]:remove",
649
- variableSchemaByKey,
650
- );
651
- expect(r.valid).toBe(true);
652
- });
653
-
654
- it("invalid when path does not exist", () => {
655
- const r = validateMutationKey("config.bad:remove", variableSchemaByKey);
656
- expect(r.valid).toBe(false);
657
- expect(r.error).toContain("path does not exist");
658
- });
659
- });
660
-
661
- describe("with schema ref resolution", () => {
662
- it("valid when variable schema is ref and schemasByKey resolves it", () => {
663
- const resolvedSchema: Schema = {
664
- type: "object",
665
- properties: { title: { type: "string" }, url: { type: "string" } },
666
- };
667
- const schemas: Record<string, Schema> = {
668
- link: { schema: "Link" },
669
- };
670
- const r = validateMutationKey("link.title", schemas, { Link: resolvedSchema });
671
- expect(r.valid).toBe(true);
672
- expect(r.valueSchema).toEqual({ type: "string" });
673
- });
674
-
675
- it("valid set on resolved ref root", () => {
676
- const resolvedSchema: Schema = { type: "object", properties: { x: { type: "string" } } };
677
- const schemas: Record<string, Schema> = { v: { schema: "R" } };
678
- const r = validateMutationKey("v", schemas, { R: resolvedSchema });
679
- expect(r.valid).toBe(true);
680
- expect(r.valueSchema).toEqual(resolvedSchema);
681
- });
682
- });
683
-
684
- describe("edge cases", () => {
685
- it("valid for nested path set when all steps exist", () => {
686
- const r = validateMutationKey("settings.config.width", variableSchemaByKey);
687
- expect(r.valid).toBe(true);
688
- expect(r.valueSchema).toEqual({ type: "integer" });
689
- });
690
-
691
- it("invalid for path into primitive (no properties)", () => {
692
- const r = validateMutationKey("config.theme.foo", variableSchemaByKey);
693
- expect(r.valid).toBe(false);
694
- });
695
-
696
- it("returns correct pathSegments and rootKey in result", () => {
697
- const r = validateMutationKey("config.theme", variableSchemaByKey);
698
- expect(r.valid).toBe(true);
699
- expect(r.rootKey).toBe("config");
700
- expect(r.pathSegments).toEqual([{ key: "theme" }]);
701
- });
702
- });
703
- });
704
- });