@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,61 +0,0 @@
1
- import type { Group, Rule } from "@featurevisor/types";
2
-
3
- import { Datasource } from "../datasource";
4
-
5
- // @NOTE: ideally in future, this check should be done from Feature level,
6
- // as well as Group level as done here
7
- export async function checkForFeatureExceedingGroupSlotPercentage(
8
- datasource: Datasource,
9
- group: Group,
10
- availableFeatureKeys: string[],
11
- ) {
12
- for (const slot of group.slots) {
13
- const maxPercentageForRule = slot.percentage;
14
-
15
- if (slot.feature) {
16
- const featureKey = slot.feature;
17
- const featureExists = availableFeatureKeys.indexOf(featureKey) > -1;
18
-
19
- if (!featureExists) {
20
- throw new Error(`Unknown feature "${featureKey}"`);
21
- }
22
-
23
- const parsedFeature = await datasource.readFeature(featureKey);
24
-
25
- const hasEnvironments =
26
- parsedFeature.rules &&
27
- !Array.isArray(parsedFeature.rules) &&
28
- Object.keys(parsedFeature.rules).length > 0;
29
-
30
- if (hasEnvironments && parsedFeature.rules) {
31
- // with environments
32
- const environmentKeys = Object.keys(parsedFeature.rules);
33
-
34
- for (const environmentKey of environmentKeys) {
35
- const rules = parsedFeature.rules[environmentKey];
36
-
37
- for (const rule of rules) {
38
- if (rule.percentage > maxPercentageForRule) {
39
- // @NOTE: this does not help with same feature belonging to multiple slots. fix that.
40
- throw new Error(
41
- `Feature ${featureKey}'s rule ${rule.key} in ${environmentKey} has a percentage of ${rule.percentage} which is greater than the maximum percentage of ${maxPercentageForRule} for the slot`,
42
- );
43
- }
44
- }
45
- }
46
- } else if (parsedFeature.rules) {
47
- // no environments
48
- const rules = parsedFeature.rules as Rule[];
49
-
50
- for (const rule of rules) {
51
- if (rule.percentage > maxPercentageForRule) {
52
- // @NOTE: this does not help with same feature belonging to multiple slots. fix that.
53
- throw new Error(
54
- `Feature ${featureKey}'s rule ${rule.key} has a percentage of ${rule.percentage} which is greater than the maximum percentage of ${maxPercentageForRule} for the slot`,
55
- );
56
- }
57
- }
58
- }
59
- }
60
- }
61
- }
@@ -1,510 +0,0 @@
1
- /**
2
- * Unit tests for condition schema validation (segment conditions, feature rules, etc.).
3
- * Covers getConditionsZodSchema: attribute path resolution, operator/value compatibility,
4
- * schema-aware value validation, regexFlags, nesting, and "*" (everyone).
5
- */
6
- import { z } from "zod";
7
-
8
- import type { Attribute } from "@featurevisor/types";
9
- import type { ProjectConfig } from "../config";
10
- import { getConditionsZodSchema } from "./conditionSchema";
11
-
12
- function minimalProjectConfig(): ProjectConfig {
13
- return {
14
- featuresDirectoryPath: "",
15
- segmentsDirectoryPath: "",
16
- attributesDirectoryPath: "",
17
- groupsDirectoryPath: "",
18
- schemasDirectoryPath: "",
19
- targetsDirectoryPath: "",
20
- testsDirectoryPath: "",
21
- stateDirectoryPath: "",
22
- datafilesDirectoryPath: "",
23
- datafileNamePattern: "",
24
- revisionFileName: "",
25
- catalogDirectoryPath: "",
26
- setsDirectoryPath: "",
27
- environments: ["staging", "production"],
28
- sets: false,
29
- namespaceCharacter: ".",
30
- tags: ["all"],
31
- adapter: {},
32
- plugins: [],
33
- defaultBucketBy: "userId",
34
- parser: "yml",
35
- prettyState: true,
36
- prettyDatafile: false,
37
- stringify: true,
38
- };
39
- }
40
-
41
- const TEST_ATTRIBUTES: Record<string, Attribute> = {
42
- userId: {
43
- description: "User ID",
44
- type: "string",
45
- },
46
- country: {
47
- description: "Country",
48
- type: "string",
49
- enum: ["de", "fr", "nl", "us"],
50
- },
51
- device: {
52
- description: "Device",
53
- type: "string",
54
- },
55
- email: {
56
- description: "Email",
57
- type: "string",
58
- pattern: "^[^@]+@[^@]+\\.[^@]+$",
59
- },
60
- age: {
61
- description: "Age",
62
- type: "integer",
63
- minimum: 18,
64
- },
65
- browser: {
66
- description: "Browser",
67
- type: "object",
68
- properties: {
69
- name: {
70
- type: "string",
71
- enum: ["chrome", "firefox", "safari"],
72
- },
73
- version: {
74
- type: "semver",
75
- },
76
- },
77
- },
78
- account: {
79
- description: "Account",
80
- type: "object",
81
- properties: {
82
- plan: {
83
- type: "string",
84
- enum: ["free", "pro"],
85
- },
86
- locale: {
87
- type: "string",
88
- },
89
- },
90
- },
91
- labels: {
92
- description: "Labels",
93
- type: "object",
94
- additionalProperties: {
95
- type: "string",
96
- },
97
- },
98
- permissions: {
99
- description: "Permissions",
100
- type: "array",
101
- items: {
102
- type: "string",
103
- enum: ["read", "write", "admin"],
104
- },
105
- },
106
- version: {
107
- description: "Version",
108
- oneOf: [{ type: "string" }, { type: "double" }],
109
- },
110
- traits: {
111
- description: "Traits",
112
- type: "object",
113
- },
114
- };
115
-
116
- function getConditionsSchema() {
117
- return getConditionsZodSchema(minimalProjectConfig(), TEST_ATTRIBUTES);
118
- }
119
-
120
- function parseConditions(input: unknown): z.ZodSafeParseResult<unknown> {
121
- return getConditionsSchema().safeParse(input);
122
- }
123
-
124
- function expectConditionsSuccess(input: unknown): void {
125
- const result = parseConditions(input);
126
- expect(result.success).toBe(true);
127
- if (!result.success) {
128
- const err = (result as z.ZodSafeParseError<unknown>).error;
129
- const msg = err.issues.map((i) => `${i.path.join(".")}: ${i.message}`).join("; ");
130
- throw new Error(`Expected conditions to pass: ${msg}`);
131
- }
132
- }
133
-
134
- function expectConditionsFailure(input: unknown, messageSubstring?: string): z.ZodError {
135
- const result = parseConditions(input);
136
- expect(result.success).toBe(false);
137
- if (result.success) throw new Error("Expected conditions to fail");
138
- const err = (result as z.ZodSafeParseError<unknown>).error;
139
- if (messageSubstring) {
140
- const messages = err.issues
141
- .map((i) => (typeof i.message === "string" ? i.message : ""))
142
- .join(" ");
143
- expect(messages).toContain(messageSubstring);
144
- }
145
- return err;
146
- }
147
-
148
- function expectConditionErrorSurfaces(
149
- input: unknown,
150
- opts: { pathContains: string[]; messageContains: string },
151
- ): void {
152
- const err = expectConditionsFailure(input, opts.messageContains);
153
- const pathStrings = err.issues.map((i) => i.path.join("."));
154
- const hasMatchingPath = pathStrings.some((p) =>
155
- opts.pathContains.every((seg) => p.includes(seg)),
156
- );
157
- expect(hasMatchingPath).toBe(true);
158
- }
159
-
160
- describe("conditionSchema.ts :: getConditionsZodSchema", () => {
161
- describe("attribute path resolution", () => {
162
- it("accepts direct attributes", () => {
163
- expectConditionsSuccess({ attribute: "userId", operator: "equals", value: "u1" });
164
- });
165
-
166
- it("accepts nested attributes from declared object properties", () => {
167
- expectConditionsSuccess({
168
- attribute: "browser.name",
169
- operator: "equals",
170
- value: "chrome",
171
- });
172
- });
173
-
174
- it("accepts dynamic nested attributes via additionalProperties", () => {
175
- expectConditionsSuccess({
176
- attribute: "labels.locale",
177
- operator: "equals",
178
- value: "nl-NL",
179
- });
180
- });
181
-
182
- it("accepts one-level nested attributes on flat objects", () => {
183
- expectConditionsSuccess({
184
- attribute: "traits.favoriteColor",
185
- operator: "equals",
186
- value: "blue",
187
- });
188
- });
189
-
190
- it("rejects unknown attributes", () => {
191
- expectConditionsFailure(
192
- {
193
- attribute: "unknownAttr",
194
- operator: "equals",
195
- value: "x",
196
- },
197
- "Unknown attribute",
198
- );
199
- });
200
-
201
- it("rejects paths that are too deep for flat objects", () => {
202
- expectConditionsFailure(
203
- {
204
- attribute: "traits.preferences.theme",
205
- operator: "equals",
206
- value: "dark",
207
- },
208
- "Unknown attribute",
209
- );
210
- });
211
- });
212
-
213
- describe("operator and value validation", () => {
214
- it("accepts schema-aware equality checks", () => {
215
- expectConditionsSuccess({
216
- attribute: "browser.name",
217
- operator: "equals",
218
- value: "firefox",
219
- });
220
- });
221
-
222
- it("rejects equality values outside enum constraints", () => {
223
- expectConditionsFailure(
224
- {
225
- attribute: "browser.name",
226
- operator: "equals",
227
- value: "edge",
228
- },
229
- 'Value does not match the schema of attribute "browser.name"',
230
- );
231
- });
232
-
233
- it("accepts number equality on numeric attributes", () => {
234
- expectConditionsSuccess({ attribute: "age", operator: "equals", value: 42 });
235
- });
236
-
237
- it("accepts null equality checks", () => {
238
- expectConditionsSuccess({ attribute: "userId", operator: "equals", value: null });
239
- });
240
-
241
- it("rejects array values with equals", () => {
242
- expectConditionsFailure(
243
- { attribute: "userId", operator: "equals", value: ["a", "b"] },
244
- "value has to be",
245
- );
246
- });
247
-
248
- it("accepts numeric operators on numeric attributes", () => {
249
- expectConditionsSuccess({
250
- attribute: "age",
251
- operator: "greaterThan",
252
- value: 21,
253
- });
254
- });
255
-
256
- it("rejects numeric operators on string attributes", () => {
257
- expectConditionsFailure(
258
- { attribute: "country", operator: "greaterThan", value: 10 },
259
- "can only be used with integer or double attributes",
260
- );
261
- });
262
-
263
- it("accepts string operators on string-like attributes", () => {
264
- expectConditionsSuccess({
265
- attribute: "email",
266
- operator: "contains",
267
- value: "@",
268
- });
269
- });
270
-
271
- it("rejects string operators on array attributes", () => {
272
- expectConditionsFailure(
273
- { attribute: "permissions", operator: "contains", value: "admin" },
274
- "can only be used with string-like attributes",
275
- );
276
- });
277
-
278
- it("accepts date operators with ISO strings", () => {
279
- expectConditionsSuccess({
280
- attribute: "device",
281
- operator: "before",
282
- value: "2025-12-31T23:59:59Z",
283
- });
284
- });
285
-
286
- it("rejects date operators with invalid strings", () => {
287
- expectConditionsFailure(
288
- { attribute: "device", operator: "after", value: "not-a-date" },
289
- "ISO 8601",
290
- );
291
- });
292
-
293
- it("accepts semver operators on semver attributes", () => {
294
- expectConditionsSuccess({
295
- attribute: "browser.version",
296
- operator: "semverEquals",
297
- value: "1.0.0",
298
- });
299
- });
300
-
301
- it("accepts semver operators on oneOf attributes when a string branch supports them", () => {
302
- expectConditionsSuccess({
303
- attribute: "version",
304
- operator: "semverGreaterThan",
305
- value: "5.0.0",
306
- });
307
- });
308
-
309
- it("accepts numeric equality on oneOf attributes when a numeric branch matches", () => {
310
- expectConditionsSuccess({
311
- attribute: "version",
312
- operator: "equals",
313
- value: 5.5,
314
- });
315
- });
316
- });
317
-
318
- describe("array-aware operators", () => {
319
- it("accepts in/notIn on scalar attributes", () => {
320
- expectConditionsSuccess({
321
- attribute: "country",
322
- operator: "in",
323
- value: ["de", "fr", "nl"],
324
- });
325
- });
326
-
327
- it("rejects non-array values for in/notIn", () => {
328
- expectConditionsFailure(
329
- { attribute: "country", operator: "notIn", value: "de" },
330
- "value must be an array",
331
- );
332
- });
333
-
334
- it("rejects in/notIn members that violate attribute schema", () => {
335
- expectConditionsFailure(
336
- { attribute: "country", operator: "in", value: ["de", "unknown"] },
337
- 'Value at index 1 does not match the schema of attribute "country"',
338
- );
339
- });
340
-
341
- it("accepts includes for arrays with typed string items", () => {
342
- expectConditionsSuccess({
343
- attribute: "permissions",
344
- operator: "includes",
345
- value: "admin",
346
- });
347
- });
348
-
349
- it("rejects includes values outside item enum constraints", () => {
350
- expectConditionsFailure(
351
- {
352
- attribute: "permissions",
353
- operator: "includes",
354
- value: "delete",
355
- },
356
- 'Value does not match the item schema of attribute "permissions"',
357
- );
358
- });
359
-
360
- it("rejects non-string includes values for string arrays", () => {
361
- expectConditionsFailure(
362
- {
363
- attribute: "permissions",
364
- operator: "includes",
365
- value: 42,
366
- },
367
- 'Value does not match the item schema of attribute "permissions"',
368
- );
369
- });
370
- });
371
-
372
- describe("regex flags", () => {
373
- it("accepts regexFlags with matches", () => {
374
- expectConditionsSuccess({
375
- attribute: "browser.version",
376
- operator: "matches",
377
- value: "1\\.0\\.0",
378
- regexFlags: "i",
379
- });
380
- });
381
-
382
- it("rejects invalid regexFlags", () => {
383
- expectConditionsFailure(
384
- {
385
- attribute: "browser.version",
386
- operator: "matches",
387
- value: "x",
388
- regexFlags: "invalid",
389
- },
390
- "regexFlags",
391
- );
392
- });
393
-
394
- it("rejects duplicate regexFlags", () => {
395
- expectConditionsFailure(
396
- {
397
- attribute: "browser.version",
398
- operator: "matches",
399
- value: "x",
400
- regexFlags: "ii",
401
- },
402
- "unique",
403
- );
404
- });
405
-
406
- it("rejects regexFlags when operator is not matches/notMatches", () => {
407
- expectConditionsFailure(
408
- {
409
- attribute: "userId",
410
- operator: "equals",
411
- value: "u1",
412
- regexFlags: "i",
413
- },
414
- "not needed",
415
- );
416
- });
417
- });
418
-
419
- describe("exists/notExists", () => {
420
- it("accepts exists without value", () => {
421
- expectConditionsSuccess({
422
- attribute: "userId",
423
- operator: "exists",
424
- });
425
- });
426
-
427
- it("rejects exists with value", () => {
428
- expectConditionsFailure(
429
- { attribute: "userId", operator: "exists", value: "x" },
430
- "value is not needed",
431
- );
432
- });
433
- });
434
-
435
- describe("structure", () => {
436
- it("accepts nested and/or/not conditions", () => {
437
- expectConditionsSuccess({
438
- or: [
439
- {
440
- and: [
441
- { attribute: "browser.name", operator: "equals", value: "chrome" },
442
- { attribute: "country", operator: "equals", value: "de" },
443
- ],
444
- },
445
- { attribute: "permissions", operator: "includes", value: "admin" },
446
- ],
447
- });
448
- });
449
-
450
- it("rejects empty and/or/not condition arrays", () => {
451
- expectConditionsFailure({ and: [] });
452
- expectConditionsFailure({ or: [] });
453
- expectConditionsFailure({ not: [] });
454
- });
455
-
456
- it("rejects invalid nested conditions", () => {
457
- expectConditionsFailure(
458
- {
459
- and: [
460
- { attribute: "userId", operator: "equals", value: "u1" },
461
- { attribute: "browser.name", operator: "equals", value: "edge" },
462
- ],
463
- },
464
- 'Value does not match the schema of attribute "browser.name"',
465
- );
466
- });
467
-
468
- it("accepts literal * as conditions", () => {
469
- expectConditionsSuccess("*");
470
- });
471
-
472
- it("accepts arrays of plain conditions", () => {
473
- expectConditionsSuccess([
474
- { attribute: "userId", operator: "equals", value: "u1" },
475
- { attribute: "country", operator: "equals", value: "de" },
476
- ]);
477
- });
478
-
479
- it("rejects and/or/not objects with extra keys", () => {
480
- const result = parseConditions({
481
- and: [{ attribute: "userId", operator: "equals", value: "u1" }],
482
- extraKey: true,
483
- });
484
- expect(result.success).toBe(false);
485
- });
486
- });
487
-
488
- describe("error surfacing", () => {
489
- it("reports unknown attributes at the attribute path", () => {
490
- expectConditionErrorSurfaces(
491
- { attribute: "typoAttr", operator: "equals", value: "x" },
492
- { pathContains: ["attribute"], messageContains: "Unknown attribute" },
493
- );
494
- });
495
-
496
- it("reports schema mismatches at the value path", () => {
497
- expectConditionErrorSurfaces(
498
- { attribute: "browser.name", operator: "equals", value: "edge" },
499
- { pathContains: ["value"], messageContains: "Value does not match the schema" },
500
- );
501
- });
502
-
503
- it("reports array item schema mismatches at the value path", () => {
504
- expectConditionErrorSurfaces(
505
- { attribute: "country", operator: "in", value: ["de", "edge"] },
506
- { pathContains: ["value"], messageContains: "Value at index 1 does not match" },
507
- );
508
- });
509
- });
510
- });