@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,1985 +0,0 @@
1
- /**
2
- * Unit tests for feature schema validation, with focus on variablesSchema and
3
- * variable value validation (defaultValue, disabledValue, variations, variableOverrides,
4
- * rules, force). Ensures every place a variable value can be set is validated against
5
- * the variable's schema.
6
- */
7
- import type { Attribute, Schema } from "@featurevisor/types";
8
- import { z } from "zod";
9
-
10
- import type { ProjectConfig } from "../config";
11
- import { getConditionsZodSchema } from "./conditionSchema";
12
- import { getFeatureZodSchema } from "./featureSchema";
13
-
14
- /** Minimal project config for feature schema tests (no file paths used for variable validation). */
15
- function minimalProjectConfig(overrides: Partial<ProjectConfig> = {}): ProjectConfig {
16
- return {
17
- featuresDirectoryPath: "",
18
- segmentsDirectoryPath: "",
19
- attributesDirectoryPath: "",
20
- groupsDirectoryPath: "",
21
- schemasDirectoryPath: "",
22
- targetsDirectoryPath: "",
23
- testsDirectoryPath: "",
24
- stateDirectoryPath: "",
25
- datafilesDirectoryPath: "",
26
- datafileNamePattern: "",
27
- revisionFileName: "",
28
- catalogDirectoryPath: "",
29
- setsDirectoryPath: "",
30
- environments: ["staging", "production"],
31
- sets: false,
32
- namespaceCharacter: ".",
33
- tags: ["all"],
34
- adapter: {},
35
- plugins: [],
36
- defaultBucketBy: "userId",
37
- parser: "yml",
38
- prettyState: true,
39
- prettyDatafile: false,
40
- stringify: true,
41
- enforceCatchAllRule: false,
42
- ...overrides,
43
- };
44
- }
45
-
46
- /** Attributes and segments that appear in conditions; keep small for tests. */
47
- const TEST_ATTRIBUTES: Record<string, Attribute> = {
48
- userId: {
49
- description: "User ID",
50
- type: "string",
51
- },
52
- country: {
53
- description: "Country",
54
- type: "string",
55
- },
56
- device: {
57
- description: "Device",
58
- type: "string",
59
- },
60
- permissions: {
61
- description: "Permissions",
62
- type: "array",
63
- items: {
64
- type: "string",
65
- enum: ["read", "write", "admin"],
66
- },
67
- },
68
- version: {
69
- description: "Version",
70
- oneOf: [{ type: "string" }, { type: "double" }],
71
- },
72
- traits: {
73
- description: "Traits",
74
- type: "object",
75
- },
76
- };
77
- const TEST_ATTRIBUTE_KEYS: [string, ...string[]] = [
78
- "userId",
79
- "country",
80
- "device",
81
- "permissions",
82
- "version",
83
- "traits",
84
- ];
85
- const TEST_SEGMENTS: [string, ...string[]] = ["*", "countries.germany", "countries.france"];
86
- const TEST_FEATURES: [string, ...string[]] = ["testFeature"];
87
- const TEST_SCHEMA_KEYS = ["link", "slugSchema"];
88
-
89
- /** Resolved schema for "slugSchema": string with pattern and length. */
90
- const slugSchemaResolved: Schema = {
91
- type: "string",
92
- minLength: 1,
93
- maxLength: 30,
94
- pattern: "^[a-z0-9-]+$",
95
- };
96
-
97
- /** Resolved schema for "link": object with title and url. */
98
- const linkSchemaResolved: Schema = {
99
- type: "object",
100
- required: ["title", "url"],
101
- properties: {
102
- title: { type: "string" },
103
- url: { type: "string" },
104
- },
105
- };
106
-
107
- const TEST_SCHEMAS_BY_KEY: Record<string, Schema> = {
108
- link: linkSchemaResolved,
109
- slugSchema: slugSchemaResolved,
110
- };
111
-
112
- function getFeatureSchema(projectConfigOverrides: Partial<ProjectConfig> = {}) {
113
- const projectConfig = minimalProjectConfig(projectConfigOverrides);
114
- const conditionsZodSchema = getConditionsZodSchema(projectConfig, TEST_ATTRIBUTES);
115
- return getFeatureZodSchema(
116
- projectConfig,
117
- conditionsZodSchema,
118
- TEST_ATTRIBUTE_KEYS,
119
- TEST_SEGMENTS,
120
- TEST_FEATURES,
121
- TEST_SCHEMA_KEYS,
122
- TEST_SCHEMAS_BY_KEY,
123
- );
124
- }
125
-
126
- /** Base feature shape required by getFeatureZodSchema (description, tags, bucketBy, rules). */
127
- function baseFeature(overrides: Record<string, unknown> = {}) {
128
- return {
129
- description: "Test feature",
130
- tags: ["all"],
131
- bucketBy: "userId",
132
- rules: {
133
- staging: [{ key: "r1", segments: "*", percentage: 100 }],
134
- production: [{ key: "r1", segments: "*", percentage: 100 }],
135
- },
136
- ...overrides,
137
- };
138
- }
139
-
140
- function parseFeature(
141
- feature: unknown,
142
- projectConfigOverrides: Partial<ProjectConfig> = {},
143
- ): z.ZodSafeParseResult<unknown> {
144
- return getFeatureSchema(projectConfigOverrides).safeParse(feature);
145
- }
146
-
147
- function expectParseSuccess(
148
- feature: unknown,
149
- projectConfigOverrides: Partial<ProjectConfig> = {},
150
- ): void {
151
- const result = parseFeature(feature, projectConfigOverrides);
152
- expect(result.success).toBe(true);
153
- if (!result.success) {
154
- const err = (result as z.ZodSafeParseError<unknown>).error;
155
- const msg = err.issues.map((i) => `${i.path.join(".")}: ${i.message}`).join("; ");
156
- throw new Error(`Expected success but got: ${msg}`);
157
- }
158
- }
159
-
160
- function expectParseFailure(
161
- feature: unknown,
162
- messageSubstring?: string,
163
- projectConfigOverrides: Partial<ProjectConfig> = {},
164
- ): z.ZodError {
165
- const result = parseFeature(feature, projectConfigOverrides);
166
- expect(result.success).toBe(false);
167
- if (result.success) throw new Error("Expected parse failure");
168
- const err = (result as z.ZodSafeParseError<unknown>).error;
169
- if (messageSubstring) {
170
- const messages = err.issues
171
- .map((i) => (typeof i.message === "string" ? i.message : ""))
172
- .join(" ");
173
- expect(messages).toContain(messageSubstring);
174
- }
175
- return err;
176
- }
177
-
178
- /** Assert that an intentional mistake produces an error at the expected path with expected message. */
179
- function expectErrorSurfaces(
180
- feature: unknown,
181
- opts: { pathContains: string[]; messageContains: string },
182
- ): void {
183
- const err = expectParseFailure(feature, opts.messageContains);
184
- const pathStrings = err.issues.map((i) => i.path.join("."));
185
- const hasMatchingPath = pathStrings.some((p) =>
186
- opts.pathContains.every((seg) => p.includes(seg)),
187
- );
188
- expect(hasMatchingPath).toBe(true);
189
- }
190
-
191
- describe("featureSchema.ts :: getFeatureZodSchema (variablesSchema and variable values)", () => {
192
- describe("tags", () => {
193
- it("accepts missing tags", () => {
194
- const feature = baseFeature();
195
- delete feature.tags;
196
-
197
- expectParseSuccess(feature);
198
- });
199
-
200
- it("accepts tags defined in project config", () => {
201
- expectParseSuccess(baseFeature({ tags: ["all", "beta"] }), { tags: ["all", "beta"] });
202
- });
203
-
204
- it("rejects tags missing from project config", () => {
205
- expectParseFailure(baseFeature({ tags: ["unknown"] }), 'Unknown tag "unknown"');
206
- });
207
-
208
- it("rejects duplicate tags", () => {
209
- expectParseFailure(baseFeature({ tags: ["all", "all"] }), "Duplicate tags found: all, all");
210
- });
211
- });
212
-
213
- describe("rule segments", () => {
214
- it("rejects empty and/or/not group segment arrays", () => {
215
- expectParseFailure(
216
- baseFeature({
217
- rules: {
218
- staging: [{ key: "r1", segments: { and: [] }, percentage: 100 }],
219
- production: [{ key: "r1", segments: "*", percentage: 100 }],
220
- },
221
- }),
222
- );
223
-
224
- expectParseFailure(
225
- baseFeature({
226
- rules: {
227
- staging: [{ key: "r1", segments: { or: [] }, percentage: 100 }],
228
- production: [{ key: "r1", segments: "*", percentage: 100 }],
229
- },
230
- }),
231
- );
232
-
233
- expectParseFailure(
234
- baseFeature({
235
- rules: {
236
- staging: [{ key: "r1", segments: { not: [] }, percentage: 100 }],
237
- production: [{ key: "r1", segments: "*", percentage: 100 }],
238
- },
239
- }),
240
- );
241
- });
242
- });
243
-
244
- describe("attribute-aware rule and force conditions", () => {
245
- it("accepts force conditions using semver operators on oneOf attributes", () => {
246
- expectParseSuccess(
247
- baseFeature({
248
- variations: [
249
- { value: "control", weight: 50 },
250
- { value: "treatment", weight: 50 },
251
- ],
252
- force: {
253
- staging: [
254
- {
255
- conditions: [
256
- { attribute: "version", operator: "semverGreaterThan", value: "5.0.0" },
257
- ],
258
- variation: "treatment",
259
- },
260
- ],
261
- production: [],
262
- },
263
- }),
264
- );
265
- });
266
-
267
- it("accepts force conditions using flat object nested paths", () => {
268
- expectParseSuccess(
269
- baseFeature({
270
- variations: [
271
- { value: "control", weight: 50 },
272
- { value: "treatment", weight: 50 },
273
- ],
274
- force: {
275
- staging: [
276
- {
277
- conditions: [
278
- { attribute: "traits.favoriteTheme", operator: "equals", value: "dark" },
279
- ],
280
- variation: "treatment",
281
- },
282
- ],
283
- production: [],
284
- },
285
- }),
286
- );
287
- });
288
-
289
- it("rejects force conditions when oneOf attribute value matches no branch", () => {
290
- expectParseFailure(
291
- baseFeature({
292
- variations: [
293
- { value: "control", weight: 50 },
294
- { value: "treatment", weight: 50 },
295
- ],
296
- force: {
297
- staging: [
298
- {
299
- conditions: [{ attribute: "version", operator: "equals", value: true }],
300
- variation: "treatment",
301
- },
302
- ],
303
- production: [],
304
- },
305
- }),
306
- 'Value does not match the schema of attribute "version"',
307
- );
308
- });
309
-
310
- it("rejects force conditions when array item value violates enum constraints", () => {
311
- expectParseFailure(
312
- baseFeature({
313
- variations: [
314
- { value: "control", weight: 50 },
315
- { value: "treatment", weight: 50 },
316
- ],
317
- force: {
318
- staging: [
319
- {
320
- conditions: [{ attribute: "permissions", operator: "includes", value: "delete" }],
321
- variation: "treatment",
322
- },
323
- ],
324
- production: [],
325
- },
326
- }),
327
- 'Value does not match the item schema of attribute "permissions"',
328
- );
329
- });
330
- });
331
-
332
- describe("variablesSchema: schema reference", () => {
333
- it("accepts variable with schema reference and valid defaultValue", () => {
334
- expectParseSuccess(
335
- baseFeature({
336
- variablesSchema: {
337
- myLink: {
338
- schema: "link",
339
- defaultValue: { title: "Home", url: "/" },
340
- },
341
- },
342
- }),
343
- );
344
- });
345
-
346
- it("rejects variable with unknown schema reference", () => {
347
- expectParseFailure(
348
- baseFeature({
349
- variablesSchema: {
350
- myLink: {
351
- schema: "nonexistentSchema",
352
- defaultValue: { title: "Home", url: "/" },
353
- },
354
- },
355
- }),
356
- "Unknown schema",
357
- );
358
- });
359
-
360
- it("rejects variable with schema reference when schema could not be loaded (missing from schemasByKey)", () => {
361
- const projectConfig = minimalProjectConfig();
362
- const conditionsZodSchema = getConditionsZodSchema(projectConfig, TEST_ATTRIBUTES);
363
- const schemaWithEmptySchemasByKey = getFeatureZodSchema(
364
- projectConfig,
365
- conditionsZodSchema,
366
- TEST_ATTRIBUTE_KEYS,
367
- TEST_SEGMENTS,
368
- TEST_FEATURES,
369
- ["link"],
370
- {},
371
- );
372
- const result = schemaWithEmptySchemasByKey.safeParse(
373
- baseFeature({
374
- variablesSchema: {
375
- myLink: {
376
- schema: "link",
377
- defaultValue: { title: "Home", url: "/" },
378
- },
379
- },
380
- }),
381
- );
382
- expect(result.success).toBe(false);
383
- if (result.success) return;
384
- const messages = (result as z.ZodSafeParseError<unknown>).error.issues
385
- .map((i) => (typeof i.message === "string" ? i.message : ""))
386
- .join(" ");
387
- expect(messages).toContain("could not be loaded");
388
- expect(messages).toContain("link");
389
- expect(messages).toContain("myLink");
390
- });
391
-
392
- it("rejects variable with schema reference when defaultValue does not match schema", () => {
393
- expectParseFailure(
394
- baseFeature({
395
- variablesSchema: {
396
- myLink: {
397
- schema: "link",
398
- defaultValue: { title: "Home" }, // missing required "url"
399
- },
400
- },
401
- }),
402
- );
403
- });
404
- });
405
-
406
- describe("variablesSchema: inline schema and defaultValue", () => {
407
- it("accepts inline string variable with valid defaultValue", () => {
408
- expectParseSuccess(
409
- baseFeature({
410
- variablesSchema: {
411
- label: { type: "string", defaultValue: "hello" },
412
- },
413
- }),
414
- );
415
- });
416
-
417
- it("accepts inline integer with min/max and defaultValue in range", () => {
418
- expectParseSuccess(
419
- baseFeature({
420
- variablesSchema: {
421
- level: {
422
- type: "integer",
423
- minimum: 1,
424
- maximum: 10,
425
- defaultValue: 5,
426
- },
427
- },
428
- }),
429
- );
430
- });
431
-
432
- it("rejects inline integer defaultValue below minimum", () => {
433
- expectParseFailure(
434
- baseFeature({
435
- variablesSchema: {
436
- level: {
437
- type: "integer",
438
- minimum: 1,
439
- maximum: 10,
440
- defaultValue: 0,
441
- },
442
- },
443
- }),
444
- "minimum",
445
- );
446
- });
447
-
448
- it("rejects inline integer defaultValue above maximum", () => {
449
- expectParseFailure(
450
- baseFeature({
451
- variablesSchema: {
452
- level: {
453
- type: "integer",
454
- minimum: 1,
455
- maximum: 10,
456
- defaultValue: 11,
457
- },
458
- },
459
- }),
460
- "maximum",
461
- );
462
- });
463
-
464
- it("rejects inline string defaultValue that violates minLength", () => {
465
- expectParseFailure(
466
- baseFeature({
467
- variablesSchema: {
468
- slug: {
469
- type: "string",
470
- minLength: 3,
471
- defaultValue: "ab",
472
- },
473
- },
474
- }),
475
- "minLength",
476
- );
477
- });
478
-
479
- it("rejects inline string defaultValue that violates maxLength", () => {
480
- expectParseFailure(
481
- baseFeature({
482
- variablesSchema: {
483
- slug: {
484
- type: "string",
485
- maxLength: 5,
486
- defaultValue: "toolong",
487
- },
488
- },
489
- }),
490
- "maxLength",
491
- );
492
- });
493
-
494
- it("rejects inline string defaultValue that does not match pattern", () => {
495
- expectParseFailure(
496
- baseFeature({
497
- variablesSchema: {
498
- slug: {
499
- type: "string",
500
- pattern: "^[a-z0-9-]+$",
501
- defaultValue: "INVALID",
502
- },
503
- },
504
- }),
505
- "pattern",
506
- );
507
- });
508
-
509
- it("rejects inline array defaultValue with length below minItems", () => {
510
- expectParseFailure(
511
- baseFeature({
512
- variablesSchema: {
513
- tags: {
514
- type: "array",
515
- items: { type: "string" },
516
- minItems: 2,
517
- defaultValue: ["one"],
518
- },
519
- },
520
- }),
521
- "minItems",
522
- );
523
- });
524
-
525
- it("rejects inline array defaultValue with length above maxItems", () => {
526
- expectParseFailure(
527
- baseFeature({
528
- variablesSchema: {
529
- tags: {
530
- type: "array",
531
- items: { type: "string" },
532
- maxItems: 2,
533
- defaultValue: ["a", "b", "c"],
534
- },
535
- },
536
- }),
537
- "maxItems",
538
- );
539
- });
540
-
541
- it("rejects inline array defaultValue with duplicate items when uniqueItems is true", () => {
542
- expectParseFailure(
543
- baseFeature({
544
- variablesSchema: {
545
- codes: {
546
- type: "array",
547
- items: { type: "string" },
548
- uniqueItems: true,
549
- defaultValue: ["x", "x"],
550
- },
551
- },
552
- }),
553
- "duplicate",
554
- );
555
- });
556
-
557
- it("rejects inline const variable when defaultValue does not equal const", () => {
558
- expectParseFailure(
559
- baseFeature({
560
- variablesSchema: {
561
- status: {
562
- type: "string",
563
- const: "active",
564
- defaultValue: "inactive",
565
- },
566
- },
567
- }),
568
- "constant",
569
- );
570
- });
571
-
572
- it("rejects inline enum variable when defaultValue is not in enum", () => {
573
- expectParseFailure(
574
- baseFeature({
575
- variablesSchema: {
576
- theme: {
577
- type: "string",
578
- enum: ["light", "dark"],
579
- defaultValue: "blue",
580
- },
581
- },
582
- }),
583
- );
584
- });
585
-
586
- it("accepts inline oneOf variable when defaultValue matches exactly one branch", () => {
587
- expectParseSuccess(
588
- baseFeature({
589
- variablesSchema: {
590
- idOrLink: {
591
- oneOf: [{ type: "string" }, { schema: "link" }],
592
- defaultValue: "ref-123",
593
- },
594
- },
595
- }),
596
- );
597
- });
598
-
599
- it("rejects when variable has both schema reference and inline type", () => {
600
- expectParseFailure(
601
- baseFeature({
602
- variablesSchema: {
603
- myLink: {
604
- schema: "link",
605
- type: "object",
606
- defaultValue: { title: "Home", url: "/" },
607
- },
608
- },
609
- }),
610
- "schema",
611
- );
612
- });
613
-
614
- it("rejects reserved variable key 'variation'", () => {
615
- expectParseFailure(
616
- baseFeature({
617
- variablesSchema: {
618
- variation: { type: "string", defaultValue: "control" },
619
- },
620
- }),
621
- "reserved",
622
- );
623
- });
624
- });
625
-
626
- describe("variablesSchema: disabledValue", () => {
627
- it("accepts valid disabledValue matching variable schema", () => {
628
- expectParseSuccess(
629
- baseFeature({
630
- variablesSchema: {
631
- label: {
632
- type: "string",
633
- defaultValue: "on",
634
- disabledValue: "off",
635
- },
636
- },
637
- }),
638
- );
639
- });
640
-
641
- it("rejects disabledValue that does not match variable schema", () => {
642
- expectParseFailure(
643
- baseFeature({
644
- variablesSchema: {
645
- level: {
646
- type: "integer",
647
- minimum: 1,
648
- maximum: 10,
649
- defaultValue: 5,
650
- disabledValue: 99,
651
- },
652
- },
653
- }),
654
- "maximum",
655
- );
656
- });
657
- });
658
-
659
- describe("variations: variables", () => {
660
- it("accepts variation variables that match variablesSchema", () => {
661
- expectParseSuccess(
662
- baseFeature({
663
- variablesSchema: {
664
- label: { type: "string", defaultValue: "default" },
665
- },
666
- variations: [
667
- { value: "control", weight: 50 },
668
- { value: "treatment", weight: 50, variables: { label: "treatment-label" } },
669
- ],
670
- }),
671
- );
672
- });
673
-
674
- it("rejects variation variable value that does not match schema", () => {
675
- expectParseFailure(
676
- baseFeature({
677
- variablesSchema: {
678
- level: { type: "integer", minimum: 1, maximum: 10, defaultValue: 1 },
679
- },
680
- variations: [
681
- { value: "control", weight: 50 },
682
- { value: "treatment", weight: 50, variables: { level: 100 } },
683
- ],
684
- }),
685
- "maximum",
686
- );
687
- });
688
-
689
- it("rejects when variation uses a variable key not defined in variablesSchema", () => {
690
- expectParseFailure(
691
- baseFeature({
692
- variablesSchema: {
693
- label: { type: "string", defaultValue: "default" },
694
- },
695
- variations: [
696
- { value: "control", weight: 50 },
697
- { value: "treatment", weight: 50, variables: { unknownVar: "x" } },
698
- ],
699
- }),
700
- "not defined in",
701
- );
702
- });
703
- });
704
-
705
- describe("variations: variableOverrides", () => {
706
- it("accepts variableOverrides with values matching variable schema", () => {
707
- expectParseSuccess(
708
- baseFeature({
709
- variablesSchema: {
710
- slug: {
711
- type: "string",
712
- pattern: "^[a-z0-9-]+$",
713
- minLength: 1,
714
- maxLength: 20,
715
- defaultValue: "home",
716
- },
717
- },
718
- variations: [
719
- { value: "control", weight: 50 },
720
- {
721
- value: "treatment",
722
- weight: 50,
723
- variables: { slug: "treatment" },
724
- variableOverrides: {
725
- slug: [
726
- { segments: "countries.germany", value: "de" },
727
- { segments: "countries.france", value: "fr" },
728
- ],
729
- },
730
- },
731
- ],
732
- }),
733
- );
734
- });
735
-
736
- it("rejects variableOverride value that does not match schema", () => {
737
- expectParseFailure(
738
- baseFeature({
739
- variablesSchema: {
740
- slug: {
741
- type: "string",
742
- pattern: "^[a-z]+$",
743
- defaultValue: "home",
744
- },
745
- },
746
- variations: [
747
- { value: "control", weight: 50 },
748
- {
749
- value: "treatment",
750
- weight: 50,
751
- variableOverrides: {
752
- slug: [{ segments: "*", value: "UPPERCASE" }],
753
- },
754
- },
755
- ],
756
- }),
757
- "pattern",
758
- );
759
- });
760
-
761
- it("rejects variableOverride for variable key not in variablesSchema", () => {
762
- expectParseFailure(
763
- baseFeature({
764
- variablesSchema: {
765
- label: { type: "string", defaultValue: "default" },
766
- },
767
- variations: [
768
- { value: "control", weight: 50 },
769
- {
770
- value: "treatment",
771
- weight: 50,
772
- variableOverrides: {
773
- notDefined: [{ segments: "*", value: "x" }],
774
- },
775
- },
776
- ],
777
- }),
778
- "not defined in",
779
- );
780
- });
781
-
782
- it("validates variableOverrides even when variation has no variables", () => {
783
- expectParseFailure(
784
- baseFeature({
785
- variablesSchema: {
786
- slug: { type: "string", maxLength: 5, defaultValue: "home" },
787
- },
788
- variations: [
789
- { value: "control", weight: 50 },
790
- {
791
- value: "treatment",
792
- weight: 50,
793
- variableOverrides: {
794
- slug: [{ segments: "*", value: "toolongvalue" }],
795
- },
796
- },
797
- ],
798
- }),
799
- "maxLength",
800
- );
801
- });
802
-
803
- it("accepts plain-key partial object overrides for structured object schemas", () => {
804
- expectParseSuccess(
805
- baseFeature({
806
- variablesSchema: {
807
- config: {
808
- type: "object",
809
- properties: {
810
- theme: { type: "string" },
811
- compact: { type: "boolean" },
812
- },
813
- required: ["theme", "compact"],
814
- defaultValue: { theme: "light", compact: true },
815
- },
816
- },
817
- variations: [
818
- { value: "control", weight: 50 },
819
- {
820
- value: "treatment",
821
- weight: 50,
822
- variableOverrides: {
823
- config: [{ segments: "*", value: { theme: "dark" } }],
824
- },
825
- },
826
- ],
827
- }),
828
- );
829
- });
830
- });
831
-
832
- describe("rules: variables", () => {
833
- it("accepts rule variables that match variablesSchema", () => {
834
- expectParseSuccess(
835
- baseFeature({
836
- variablesSchema: {
837
- title: { type: "string", defaultValue: "Default" },
838
- },
839
- rules: {
840
- staging: [
841
- { key: "r1", segments: "*", percentage: 100 },
842
- {
843
- key: "r2",
844
- segments: "countries.germany",
845
- percentage: 100,
846
- variables: { title: "Germany" },
847
- },
848
- ],
849
- production: [{ key: "r1", segments: "*", percentage: 100 }],
850
- },
851
- }),
852
- );
853
- });
854
-
855
- it("rejects rule variable value that does not match schema", () => {
856
- expectParseFailure(
857
- baseFeature({
858
- variablesSchema: {
859
- count: { type: "integer", minimum: 0, maximum: 10, defaultValue: 0 },
860
- },
861
- rules: {
862
- staging: [
863
- { key: "r1", segments: "*", percentage: 100 },
864
- {
865
- key: "r2",
866
- segments: "countries.germany",
867
- percentage: 100,
868
- variables: { count: 100 },
869
- },
870
- ],
871
- production: [{ key: "r1", segments: "*", percentage: 100 }],
872
- },
873
- }),
874
- "maximum",
875
- );
876
- });
877
-
878
- it("rejects rule variable key not defined in variablesSchema", () => {
879
- expectParseFailure(
880
- baseFeature({
881
- variablesSchema: {
882
- title: { type: "string", defaultValue: "Default" },
883
- },
884
- rules: {
885
- staging: [
886
- { key: "r1", segments: "*", percentage: 100 },
887
- {
888
- key: "r2",
889
- segments: "countries.germany",
890
- percentage: 100,
891
- variables: { unknownVar: "x" },
892
- },
893
- ],
894
- production: [{ key: "r1", segments: "*", percentage: 100 }],
895
- },
896
- }),
897
- "not defined in",
898
- );
899
- });
900
-
901
- it("rejects rule variable key when feature has no variablesSchema", () => {
902
- expectParseFailure(
903
- baseFeature({
904
- rules: {
905
- staging: [{ key: "r1", segments: "*", percentage: 100 }],
906
- production: [
907
- {
908
- key: "r1",
909
- segments: "*",
910
- percentage: 80,
911
- variables: { blah123: "some value" },
912
- },
913
- ],
914
- },
915
- }),
916
- "not defined in",
917
- );
918
- });
919
- });
920
-
921
- describe("rules: variableOverrides", () => {
922
- it("accepts rule variableOverrides with values matching variablesSchema", () => {
923
- expectParseSuccess(
924
- baseFeature({
925
- variablesSchema: {
926
- config: {
927
- type: "object",
928
- properties: {
929
- title: { type: "string" },
930
- nested: {
931
- type: "object",
932
- properties: {
933
- count: { type: "integer" },
934
- },
935
- },
936
- },
937
- defaultValue: {
938
- title: "default",
939
- nested: { count: 0 },
940
- },
941
- },
942
- },
943
- rules: {
944
- staging: [
945
- {
946
- key: "r1",
947
- segments: "*",
948
- percentage: 100,
949
- variableOverrides: {
950
- config: [
951
- {
952
- segments: "countries.germany",
953
- value: {
954
- "nested.count": 5,
955
- },
956
- },
957
- ],
958
- },
959
- },
960
- ],
961
- production: [{ key: "r1", segments: "*", percentage: 100 }],
962
- },
963
- }),
964
- );
965
- });
966
-
967
- it("rejects rule variableOverrides for unknown variable key", () => {
968
- expectParseFailure(
969
- baseFeature({
970
- variablesSchema: {
971
- config: { type: "string", defaultValue: "x" },
972
- },
973
- rules: {
974
- staging: [
975
- {
976
- key: "r1",
977
- segments: "*",
978
- percentage: 100,
979
- variableOverrides: {
980
- unknown: [{ segments: "*", value: "x" }],
981
- },
982
- },
983
- ],
984
- production: [{ key: "r1", segments: "*", percentage: 100 }],
985
- },
986
- }),
987
- "not defined in",
988
- );
989
- });
990
-
991
- it("rejects mutation notation in rule variableOverrides key", () => {
992
- expectParseFailure(
993
- baseFeature({
994
- variablesSchema: {
995
- config: {
996
- type: "object",
997
- properties: { title: { type: "string" } },
998
- defaultValue: { title: "x" },
999
- },
1000
- },
1001
- rules: {
1002
- staging: [
1003
- {
1004
- key: "r1",
1005
- segments: "*",
1006
- percentage: 100,
1007
- variableOverrides: {
1008
- "config.title": [{ segments: "*", value: "y" }],
1009
- },
1010
- },
1011
- ],
1012
- production: [{ key: "r1", segments: "*", percentage: 100 }],
1013
- },
1014
- }),
1015
- "must be a declared variable key",
1016
- );
1017
- });
1018
-
1019
- it("rejects rule variableOverride path not present in schema", () => {
1020
- expectParseFailure(
1021
- baseFeature({
1022
- variablesSchema: {
1023
- config: {
1024
- type: "object",
1025
- properties: { title: { type: "string" } },
1026
- defaultValue: { title: "x" },
1027
- },
1028
- },
1029
- rules: {
1030
- staging: [
1031
- {
1032
- key: "r1",
1033
- segments: "*",
1034
- percentage: 100,
1035
- variableOverrides: {
1036
- config: [
1037
- {
1038
- segments: "*",
1039
- value: {
1040
- "nested.missing": "x",
1041
- },
1042
- },
1043
- ],
1044
- },
1045
- },
1046
- ],
1047
- production: [{ key: "r1", segments: "*", percentage: 100 }],
1048
- },
1049
- }),
1050
- "invalid for variable",
1051
- );
1052
- });
1053
-
1054
- it("rejects non-null payload for :remove in rule variableOverrides", () => {
1055
- expectParseFailure(
1056
- baseFeature({
1057
- variablesSchema: {
1058
- config: {
1059
- type: "object",
1060
- properties: { title: { type: "string" }, optional: { type: "string" } },
1061
- defaultValue: { title: "x", optional: "y" },
1062
- },
1063
- },
1064
- rules: {
1065
- staging: [
1066
- {
1067
- key: "r1",
1068
- segments: "*",
1069
- percentage: 100,
1070
- variableOverrides: {
1071
- config: [
1072
- {
1073
- conditions: [{ attribute: "country", operator: "equals", value: "de" }],
1074
- value: {
1075
- "optional:remove": "not-null",
1076
- },
1077
- },
1078
- ],
1079
- },
1080
- },
1081
- ],
1082
- production: [{ key: "r1", segments: "*", percentage: 100 }],
1083
- },
1084
- }),
1085
- "must use `null` as value",
1086
- );
1087
- });
1088
- });
1089
-
1090
- describe("force: variables", () => {
1091
- it("accepts force variables that match variablesSchema", () => {
1092
- expectParseSuccess(
1093
- baseFeature({
1094
- variablesSchema: {
1095
- title: { type: "string", defaultValue: "Default" },
1096
- },
1097
- variations: [
1098
- { value: "control", weight: 50 },
1099
- { value: "treatment", weight: 50 },
1100
- ],
1101
- force: {
1102
- staging: [
1103
- {
1104
- conditions: [{ attribute: "userId", operator: "equals", value: "u1" }],
1105
- variation: "treatment",
1106
- variables: { title: "Forced" },
1107
- },
1108
- ],
1109
- production: [],
1110
- },
1111
- }),
1112
- );
1113
- });
1114
-
1115
- it("rejects force variable value that does not match schema", () => {
1116
- expectParseFailure(
1117
- baseFeature({
1118
- variablesSchema: {
1119
- level: { type: "integer", minimum: 1, maximum: 5, defaultValue: 1 },
1120
- },
1121
- variations: [
1122
- { value: "control", weight: 50 },
1123
- { value: "treatment", weight: 50 },
1124
- ],
1125
- force: {
1126
- staging: [
1127
- {
1128
- conditions: [{ attribute: "userId", operator: "equals", value: "u1" }],
1129
- variation: "control",
1130
- variables: { level: 10 },
1131
- },
1132
- ],
1133
- production: [],
1134
- },
1135
- }),
1136
- "maximum",
1137
- );
1138
- });
1139
-
1140
- it("rejects force variable key not defined in variablesSchema", () => {
1141
- expectParseFailure(
1142
- baseFeature({
1143
- variablesSchema: {
1144
- title: { type: "string", defaultValue: "Default" },
1145
- },
1146
- variations: [
1147
- { value: "control", weight: 50 },
1148
- { value: "treatment", weight: 50 },
1149
- ],
1150
- force: {
1151
- staging: [
1152
- {
1153
- conditions: [{ attribute: "userId", operator: "equals", value: "u1" }],
1154
- variation: "control",
1155
- variables: { notDefined: "x" },
1156
- },
1157
- ],
1158
- production: [],
1159
- },
1160
- }),
1161
- "not defined in",
1162
- );
1163
- });
1164
- });
1165
-
1166
- describe("environment maps", () => {
1167
- it("allows direct rules, force, and expose when environments are omitted", () => {
1168
- expectParseSuccess(
1169
- baseFeature({
1170
- expose: true,
1171
- force: [{ segments: "*", variation: "control" }],
1172
- variations: [
1173
- { value: "control", weight: 50 },
1174
- { value: "treatment", weight: 50 },
1175
- ],
1176
- rules: [{ key: "r1", segments: "*", percentage: 100 }],
1177
- }),
1178
- { environments: undefined },
1179
- );
1180
- });
1181
-
1182
- it("allows a subset of configured environments in rules, force, and expose", () => {
1183
- expectParseSuccess(
1184
- baseFeature({
1185
- expose: {
1186
- staging: true,
1187
- },
1188
- force: {
1189
- staging: [{ segments: "*", variation: "control" }],
1190
- },
1191
- variations: [
1192
- { value: "control", weight: 50 },
1193
- { value: "treatment", weight: 50 },
1194
- ],
1195
- rules: {
1196
- staging: [{ key: "r1", segments: "*", percentage: 100 }],
1197
- },
1198
- }),
1199
- );
1200
- });
1201
-
1202
- it("rejects direct rules when environments are configured", () => {
1203
- const err = expectParseFailure(
1204
- baseFeature({
1205
- rules: [{ key: "r1", segments: "*", percentage: 100 }],
1206
- }),
1207
- );
1208
-
1209
- expect(err.issues.some((issue) => issue.path.join(".").includes("rules"))).toBe(true);
1210
- });
1211
-
1212
- it("rejects unknown environment keys in rules", () => {
1213
- const err = expectParseFailure(
1214
- baseFeature({
1215
- rules: {
1216
- qa: [{ key: "r1", segments: "*", percentage: 100 }],
1217
- },
1218
- }),
1219
- );
1220
-
1221
- expect(err.issues.some((issue) => issue.path.join(".").includes("rules"))).toBe(true);
1222
- expect(err.issues.some((issue) => issue.code === "invalid_key")).toBe(true);
1223
- });
1224
- });
1225
-
1226
- describe("oneOf: value must match exactly one branch", () => {
1227
- it("rejects defaultValue that matches no oneOf branch", () => {
1228
- expectParseFailure(
1229
- baseFeature({
1230
- variablesSchema: {
1231
- idOrLink: {
1232
- oneOf: [{ type: "string" }, { schema: "link" }],
1233
- defaultValue: 42, // number matches neither
1234
- },
1235
- },
1236
- }),
1237
- );
1238
- });
1239
-
1240
- it("rejects defaultValue that matches more than one oneOf branch when one is string", () => {
1241
- // If we had two branches that both accept the same value, we'd get "matched more than one"
1242
- expectParseSuccess(
1243
- baseFeature({
1244
- variablesSchema: {
1245
- idOrLink: {
1246
- oneOf: [{ type: "string" }, { schema: "link" }],
1247
- defaultValue: "id-1",
1248
- },
1249
- },
1250
- }),
1251
- );
1252
- });
1253
- });
1254
-
1255
- describe("schema reference: defaultValue resolved and validated", () => {
1256
- it("rejects defaultValue for schema-ref variable when value violates resolved schema", () => {
1257
- expectParseFailure(
1258
- baseFeature({
1259
- variablesSchema: {
1260
- slug: {
1261
- schema: "slugSchema",
1262
- defaultValue: "INVALID-SLUG", // pattern is ^[a-z0-9-]+$
1263
- },
1264
- },
1265
- }),
1266
- );
1267
- });
1268
-
1269
- it("accepts defaultValue for schema-ref variable when value satisfies resolved schema", () => {
1270
- expectParseSuccess(
1271
- baseFeature({
1272
- variablesSchema: {
1273
- slug: {
1274
- schema: "slugSchema",
1275
- defaultValue: "valid-slug",
1276
- },
1277
- },
1278
- }),
1279
- );
1280
- });
1281
- });
1282
-
1283
- describe("complex cases (mirroring example-1 withSchema / withComplexSchema)", () => {
1284
- it("accepts inline object variable with properties and valid defaultValue (e.g. settings)", () => {
1285
- expectParseSuccess(
1286
- baseFeature({
1287
- variablesSchema: {
1288
- settings: {
1289
- type: "object",
1290
- properties: {
1291
- theme: { type: "string" },
1292
- compact: { type: "boolean" },
1293
- },
1294
- defaultValue: { theme: "light", compact: true },
1295
- },
1296
- },
1297
- }),
1298
- );
1299
- });
1300
-
1301
- it("accepts object variable with additionalProperties only and arbitrary keys", () => {
1302
- expectParseSuccess(
1303
- baseFeature({
1304
- variablesSchema: {
1305
- labels: {
1306
- type: "object",
1307
- additionalProperties: { type: "string" },
1308
- defaultValue: { title: "Welcome", subtitle: "Hello" },
1309
- },
1310
- },
1311
- }),
1312
- );
1313
- });
1314
-
1315
- it("accepts object variable with properties and additionalProperties together", () => {
1316
- expectParseSuccess(
1317
- baseFeature({
1318
- variablesSchema: {
1319
- metadata: {
1320
- type: "object",
1321
- properties: {
1322
- fixed: { type: "integer" },
1323
- },
1324
- additionalProperties: { type: "string" },
1325
- required: ["fixed"],
1326
- defaultValue: { fixed: 1, dynamicKey: "value" },
1327
- },
1328
- },
1329
- }),
1330
- );
1331
- });
1332
-
1333
- it("rejects object variable when unknown key is present and additionalProperties is not defined", () => {
1334
- expectParseFailure(
1335
- baseFeature({
1336
- variablesSchema: {
1337
- settings: {
1338
- type: "object",
1339
- properties: {
1340
- theme: { type: "string" },
1341
- },
1342
- defaultValue: { theme: "light", subtitle: "hello" },
1343
- },
1344
- },
1345
- }),
1346
- "Unknown property",
1347
- );
1348
- });
1349
-
1350
- it("accepts object variable with additionalProperties that references reusable schema", () => {
1351
- expectParseSuccess(
1352
- baseFeature({
1353
- variablesSchema: {
1354
- linksByLocale: {
1355
- type: "object",
1356
- additionalProperties: { schema: "link" },
1357
- defaultValue: {
1358
- en: { title: "Home", url: "/" },
1359
- de: { title: "Start", url: "/de" },
1360
- },
1361
- },
1362
- },
1363
- }),
1364
- );
1365
- });
1366
-
1367
- it("rejects object variable when additionalProperties value does not match schema", () => {
1368
- expectParseFailure(
1369
- baseFeature({
1370
- variablesSchema: {
1371
- linksByLocale: {
1372
- type: "object",
1373
- additionalProperties: { schema: "link" },
1374
- defaultValue: {
1375
- en: { title: "Home" },
1376
- },
1377
- },
1378
- },
1379
- }),
1380
- "Missing required property",
1381
- );
1382
- });
1383
-
1384
- it("rejects inline object variable when defaultValue is missing required property", () => {
1385
- expectParseFailure(
1386
- baseFeature({
1387
- variablesSchema: {
1388
- settings: {
1389
- type: "object",
1390
- properties: {
1391
- theme: { type: "string" },
1392
- compact: { type: "boolean" },
1393
- },
1394
- required: ["theme"],
1395
- defaultValue: { compact: true },
1396
- },
1397
- },
1398
- }),
1399
- );
1400
- });
1401
-
1402
- it("accepts inline array variable with items schema ref (e.g. linkPair)", () => {
1403
- expectParseSuccess(
1404
- baseFeature({
1405
- variablesSchema: {
1406
- linkPair: {
1407
- type: "array",
1408
- items: { schema: "link" },
1409
- defaultValue: [
1410
- { title: "First", url: "/first" },
1411
- { title: "Second", url: "/second" },
1412
- ],
1413
- },
1414
- },
1415
- }),
1416
- );
1417
- });
1418
-
1419
- it("rejects inline array with items schema ref when an item does not match schema", () => {
1420
- expectParseFailure(
1421
- baseFeature({
1422
- variablesSchema: {
1423
- linkPair: {
1424
- type: "array",
1425
- items: { schema: "link" },
1426
- defaultValue: [{ title: "Only title, missing url" }],
1427
- },
1428
- },
1429
- }),
1430
- );
1431
- });
1432
-
1433
- it("accepts oneOf variable when defaultValue matches object branch (e.g. refOrLink as link)", () => {
1434
- expectParseSuccess(
1435
- baseFeature({
1436
- variablesSchema: {
1437
- refOrLink: {
1438
- oneOf: [{ type: "string" }, { schema: "link" }],
1439
- defaultValue: { title: "Home", url: "/" },
1440
- },
1441
- },
1442
- }),
1443
- );
1444
- });
1445
-
1446
- it("accepts object variable with nested const in property (e.g. statusInfo.kind const active)", () => {
1447
- expectParseSuccess(
1448
- baseFeature({
1449
- variablesSchema: {
1450
- statusInfo: {
1451
- type: "object",
1452
- properties: {
1453
- kind: { type: "string", const: "active" },
1454
- label: { type: "string" },
1455
- },
1456
- required: ["kind", "label"],
1457
- defaultValue: { kind: "active", label: "Default" },
1458
- },
1459
- },
1460
- }),
1461
- );
1462
- });
1463
-
1464
- it("rejects object variable when nested const property has wrong value", () => {
1465
- expectParseFailure(
1466
- baseFeature({
1467
- variablesSchema: {
1468
- statusInfo: {
1469
- type: "object",
1470
- properties: {
1471
- kind: { type: "string", const: "active" },
1472
- label: { type: "string" },
1473
- },
1474
- required: ["kind", "label"],
1475
- defaultValue: { kind: "inactive", label: "Default" },
1476
- },
1477
- },
1478
- }),
1479
- );
1480
- });
1481
-
1482
- it("accepts object variable with nested enum in property (e.g. themeConfig.theme)", () => {
1483
- expectParseSuccess(
1484
- baseFeature({
1485
- variablesSchema: {
1486
- themeConfig: {
1487
- type: "object",
1488
- properties: {
1489
- theme: { type: "string", enum: ["light", "dark", "system"] },
1490
- label: { type: "string" },
1491
- },
1492
- required: ["theme", "label"],
1493
- defaultValue: { theme: "light", label: "Default" },
1494
- },
1495
- },
1496
- }),
1497
- );
1498
- });
1499
-
1500
- it("rejects object variable when nested enum property has invalid value", () => {
1501
- expectParseFailure(
1502
- baseFeature({
1503
- variablesSchema: {
1504
- themeConfig: {
1505
- type: "object",
1506
- properties: {
1507
- theme: { type: "string", enum: ["light", "dark", "system"] },
1508
- label: { type: "string" },
1509
- },
1510
- required: ["theme", "label"],
1511
- defaultValue: { theme: "invalid", label: "Default" },
1512
- },
1513
- },
1514
- }),
1515
- );
1516
- });
1517
-
1518
- it("accepts rule variables with object value (e.g. singleLink, themeColor in rules)", () => {
1519
- expectParseSuccess(
1520
- baseFeature({
1521
- variablesSchema: {
1522
- singleLink: {
1523
- schema: "link",
1524
- defaultValue: { title: "Home", url: "/" },
1525
- },
1526
- },
1527
- rules: {
1528
- staging: [
1529
- {
1530
- key: "r1",
1531
- segments: "*",
1532
- percentage: 100,
1533
- variables: {
1534
- singleLink: { title: "DE Link", url: "/de" },
1535
- },
1536
- },
1537
- ],
1538
- production: [{ key: "r1", segments: "*", percentage: 100 }],
1539
- },
1540
- }),
1541
- );
1542
- });
1543
-
1544
- it("rejects rule variable when object value does not match schema", () => {
1545
- expectParseFailure(
1546
- baseFeature({
1547
- variablesSchema: {
1548
- singleLink: {
1549
- schema: "link",
1550
- defaultValue: { title: "Home", url: "/" },
1551
- },
1552
- },
1553
- rules: {
1554
- staging: [
1555
- {
1556
- key: "r1",
1557
- segments: "*",
1558
- percentage: 100,
1559
- variables: {
1560
- singleLink: { title: "Missing url" },
1561
- },
1562
- },
1563
- ],
1564
- production: [{ key: "r1", segments: "*", percentage: 100 }],
1565
- },
1566
- }),
1567
- );
1568
- });
1569
- });
1570
-
1571
- describe("errors surface properly: intentional mistakes produce correct path and message", () => {
1572
- it("unknown schema ref: error path points to variable and message says Unknown schema", () => {
1573
- expectErrorSurfaces(
1574
- baseFeature({
1575
- variablesSchema: {
1576
- myLink: {
1577
- schema: "nonexistent",
1578
- defaultValue: { title: "Home", url: "/" },
1579
- },
1580
- },
1581
- }),
1582
- {
1583
- pathContains: ["variablesSchema", "myLink", "schema"],
1584
- messageContains: "Unknown schema",
1585
- },
1586
- );
1587
- });
1588
-
1589
- it("defaultValue below minimum: error path includes defaultValue, message mentions minimum", () => {
1590
- expectErrorSurfaces(
1591
- baseFeature({
1592
- variablesSchema: {
1593
- level: {
1594
- type: "integer",
1595
- minimum: 10,
1596
- maximum: 100,
1597
- defaultValue: 5,
1598
- },
1599
- },
1600
- }),
1601
- { pathContains: ["variablesSchema", "level", "defaultValue"], messageContains: "minimum" },
1602
- );
1603
- });
1604
-
1605
- it("defaultValue above maximum: error path includes defaultValue, message mentions maximum", () => {
1606
- expectErrorSurfaces(
1607
- baseFeature({
1608
- variablesSchema: {
1609
- level: {
1610
- type: "integer",
1611
- minimum: 0,
1612
- maximum: 10,
1613
- defaultValue: 99,
1614
- },
1615
- },
1616
- }),
1617
- { pathContains: ["variablesSchema", "level", "defaultValue"], messageContains: "maximum" },
1618
- );
1619
- });
1620
-
1621
- it("variation uses undeclared variable: error path points to variations.*.variables, message says not defined", () => {
1622
- expectErrorSurfaces(
1623
- baseFeature({
1624
- variablesSchema: {
1625
- label: { type: "string", defaultValue: "x" },
1626
- },
1627
- variations: [
1628
- { value: "control", weight: 50 },
1629
- { value: "treatment", weight: 50, variables: { typoVar: "y" } },
1630
- ],
1631
- }),
1632
- {
1633
- pathContains: ["variations", "variables", "typoVar"],
1634
- messageContains: "not defined in",
1635
- },
1636
- );
1637
- });
1638
-
1639
- it("variableOverrides value violates pattern: error path points to variableOverrides.*.value", () => {
1640
- expectErrorSurfaces(
1641
- baseFeature({
1642
- variablesSchema: {
1643
- slug: {
1644
- type: "string",
1645
- pattern: "^[a-z-]+$",
1646
- defaultValue: "home",
1647
- },
1648
- },
1649
- variations: [
1650
- { value: "control", weight: 50 },
1651
- {
1652
- value: "treatment",
1653
- weight: 50,
1654
- variableOverrides: {
1655
- slug: [{ segments: "*", value: "INVALID-UPPERCASE" }],
1656
- },
1657
- },
1658
- ],
1659
- }),
1660
- {
1661
- pathContains: ["variableOverrides", "slug", "value"],
1662
- messageContains: "pattern",
1663
- },
1664
- );
1665
- });
1666
-
1667
- it("rule variable wrong type: error path points to rules.*.variables, message surfaces constraint", () => {
1668
- expectErrorSurfaces(
1669
- baseFeature({
1670
- variablesSchema: {
1671
- count: {
1672
- type: "integer",
1673
- minimum: 0,
1674
- maximum: 10,
1675
- defaultValue: 0,
1676
- },
1677
- },
1678
- rules: {
1679
- staging: [
1680
- {
1681
- key: "r1",
1682
- segments: "*",
1683
- percentage: 100,
1684
- variables: { count: 100 },
1685
- },
1686
- ],
1687
- production: [{ key: "r1", segments: "*", percentage: 100 }],
1688
- },
1689
- }),
1690
- {
1691
- pathContains: ["rules", "variables", "count"],
1692
- messageContains: "maximum",
1693
- },
1694
- );
1695
- });
1696
-
1697
- it("disabledValue violates schema: error path includes disabledValue", () => {
1698
- expectErrorSurfaces(
1699
- baseFeature({
1700
- variablesSchema: {
1701
- level: {
1702
- type: "integer",
1703
- minimum: 1,
1704
- maximum: 5,
1705
- defaultValue: 1,
1706
- disabledValue: 99,
1707
- },
1708
- },
1709
- }),
1710
- {
1711
- pathContains: ["variablesSchema", "level", "disabledValue"],
1712
- messageContains: "maximum",
1713
- },
1714
- );
1715
- });
1716
-
1717
- it("reserved key variation: error path points to variablesSchema.variation", () => {
1718
- expectErrorSurfaces(
1719
- baseFeature({
1720
- variablesSchema: {
1721
- variation: { type: "string", defaultValue: "control" },
1722
- },
1723
- }),
1724
- {
1725
- pathContains: ["variablesSchema", "variation"],
1726
- messageContains: "reserved",
1727
- },
1728
- );
1729
- });
1730
-
1731
- it("object defaultValue missing required property: error path includes defaultValue and key, message says Missing required", () => {
1732
- expectErrorSurfaces(
1733
- baseFeature({
1734
- variablesSchema: {
1735
- settings: {
1736
- type: "object",
1737
- properties: { theme: { type: "string" }, requiredKey: { type: "string" } },
1738
- required: ["requiredKey"],
1739
- defaultValue: { theme: "light" },
1740
- },
1741
- },
1742
- }),
1743
- {
1744
- pathContains: ["variablesSchema", "settings", "defaultValue"],
1745
- messageContains: "Missing required",
1746
- },
1747
- );
1748
- });
1749
-
1750
- it("nested const property wrong: error path points into object value", () => {
1751
- expectErrorSurfaces(
1752
- baseFeature({
1753
- variablesSchema: {
1754
- statusInfo: {
1755
- type: "object",
1756
- properties: {
1757
- kind: { type: "string", const: "active" },
1758
- label: { type: "string" },
1759
- },
1760
- required: ["kind", "label"],
1761
- defaultValue: { kind: "inactive", label: "x" },
1762
- },
1763
- },
1764
- }),
1765
- {
1766
- pathContains: ["variablesSchema", "statusInfo", "defaultValue"],
1767
- messageContains: "constant",
1768
- },
1769
- );
1770
- });
1771
-
1772
- it("force variable not in variablesSchema: error path points to force.*.variables", () => {
1773
- expectErrorSurfaces(
1774
- baseFeature({
1775
- variablesSchema: {
1776
- title: { type: "string", defaultValue: "Default" },
1777
- },
1778
- variations: [
1779
- { value: "control", weight: 50 },
1780
- { value: "treatment", weight: 50 },
1781
- ],
1782
- force: {
1783
- staging: [
1784
- {
1785
- conditions: [{ attribute: "userId", operator: "equals", value: "u1" }],
1786
- variation: "control",
1787
- variables: { typoKey: "x" },
1788
- },
1789
- ],
1790
- production: [],
1791
- },
1792
- }),
1793
- {
1794
- pathContains: ["force", "variables", "typoKey"],
1795
- messageContains: "not defined in",
1796
- },
1797
- );
1798
- });
1799
- });
1800
-
1801
- describe("mutation notation in variables", () => {
1802
- it("accepts valid dot-notation mutation keys in rules.variables", () => {
1803
- expectParseSuccess(
1804
- baseFeature({
1805
- variablesSchema: {
1806
- config: {
1807
- type: "object",
1808
- properties: {
1809
- theme: { type: "string" },
1810
- width: { type: "integer" },
1811
- },
1812
- defaultValue: { theme: "light", width: 100 },
1813
- },
1814
- },
1815
- rules: {
1816
- staging: [
1817
- {
1818
- key: "r1",
1819
- segments: "*",
1820
- percentage: 100,
1821
- variables: { "config.theme": "dark", "config.width": 1200 },
1822
- },
1823
- ],
1824
- production: [{ key: "r1", segments: "*", percentage: 100 }],
1825
- },
1826
- }),
1827
- );
1828
- });
1829
-
1830
- it("accepts valid array-index mutation keys in rules.variables", () => {
1831
- expectParseSuccess(
1832
- baseFeature({
1833
- variablesSchema: {
1834
- tags: {
1835
- type: "array",
1836
- items: { type: "string" },
1837
- defaultValue: ["a", "b"],
1838
- },
1839
- },
1840
- rules: {
1841
- staging: [
1842
- { key: "r1", segments: "*", percentage: 100, variables: { "tags[0]": "first" } },
1843
- ],
1844
- production: [{ key: "r1", segments: "*", percentage: 100 }],
1845
- },
1846
- }),
1847
- );
1848
- });
1849
-
1850
- it("rejects mutation key when root variable is not in variablesSchema", () => {
1851
- expectErrorSurfaces(
1852
- baseFeature({
1853
- variablesSchema: {
1854
- title: { type: "string", defaultValue: "Default" },
1855
- },
1856
- rules: {
1857
- staging: [
1858
- { key: "r1", segments: "*", percentage: 100, variables: { "unknownVar.foo": "x" } },
1859
- ],
1860
- production: [{ key: "r1", segments: "*", percentage: 100 }],
1861
- },
1862
- }),
1863
- {
1864
- pathContains: ["rules", "variables", "unknownVar.foo"],
1865
- messageContains: "not defined in",
1866
- },
1867
- );
1868
- });
1869
-
1870
- it("rejects invalid path (property not in schema)", () => {
1871
- expectErrorSurfaces(
1872
- baseFeature({
1873
- variablesSchema: {
1874
- config: {
1875
- type: "object",
1876
- properties: { theme: { type: "string" } },
1877
- defaultValue: { theme: "light" },
1878
- },
1879
- },
1880
- rules: {
1881
- staging: [
1882
- {
1883
- key: "r1",
1884
- segments: "*",
1885
- percentage: 100,
1886
- variables: { "config.unknownProp": "x" },
1887
- },
1888
- ],
1889
- production: [{ key: "r1", segments: "*", percentage: 100 }],
1890
- },
1891
- }),
1892
- { pathContains: ["rules", "variables"], messageContains: "path does not exist" },
1893
- );
1894
- });
1895
-
1896
- it("rejects :append on non-array (object property)", () => {
1897
- expectErrorSurfaces(
1898
- baseFeature({
1899
- variablesSchema: {
1900
- config: {
1901
- type: "object",
1902
- properties: { theme: { type: "string" } },
1903
- defaultValue: { theme: "light" },
1904
- },
1905
- },
1906
- rules: {
1907
- staging: [
1908
- { key: "r1", segments: "*", percentage: 100, variables: { "config:append": "x" } },
1909
- ],
1910
- production: [{ key: "r1", segments: "*", percentage: 100 }],
1911
- },
1912
- }),
1913
- { pathContains: ["rules", "variables"], messageContains: "only allowed on array" },
1914
- );
1915
- });
1916
-
1917
- it("rejects :remove on required object property", () => {
1918
- expectErrorSurfaces(
1919
- baseFeature({
1920
- variablesSchema: {
1921
- config: {
1922
- type: "object",
1923
- properties: {
1924
- theme: { type: "string" },
1925
- compact: { type: "boolean" },
1926
- },
1927
- required: ["compact"],
1928
- defaultValue: { theme: "light", compact: true },
1929
- },
1930
- },
1931
- rules: {
1932
- staging: [
1933
- {
1934
- key: "r1",
1935
- segments: "*",
1936
- percentage: 100,
1937
- variables: { "config.compact:remove": null },
1938
- },
1939
- ],
1940
- production: [{ key: "r1", segments: "*", percentage: 100 }],
1941
- },
1942
- }),
1943
- {
1944
- pathContains: ["rules", "variables"],
1945
- messageContains: "Cannot remove required property",
1946
- },
1947
- );
1948
- });
1949
-
1950
- it("additionalProperties type mismatch: error path includes dynamic object key", () => {
1951
- expectErrorSurfaces(
1952
- baseFeature({
1953
- variablesSchema: {
1954
- labels: {
1955
- type: "object",
1956
- additionalProperties: { type: "string" },
1957
- defaultValue: {
1958
- title: 123,
1959
- },
1960
- },
1961
- },
1962
- }),
1963
- {
1964
- pathContains: ["variablesSchema", "labels", "defaultValue", "title"],
1965
- messageContains: "type string",
1966
- },
1967
- );
1968
- });
1969
-
1970
- it("rejects variable schema with schema reference mixed with inline additionalProperties", () => {
1971
- expectParseFailure(
1972
- baseFeature({
1973
- variablesSchema: {
1974
- myLink: {
1975
- schema: "link",
1976
- additionalProperties: { type: "string" },
1977
- defaultValue: { title: "Home", url: "/" },
1978
- },
1979
- },
1980
- }),
1981
- "additionalProperties",
1982
- );
1983
- });
1984
- });
1985
- });