@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,358 +0,0 @@
1
- /**
2
- * Unit tests for segment schema validation (segments/*.yml).
3
- * Covers getSegmentZodSchema: description, conditions (plain, and/or/not, array, *),
4
- * optional archived, and strict (no extra keys).
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
- import { getSegmentZodSchema } from "./segmentSchema";
12
-
13
- function minimalProjectConfig(): ProjectConfig {
14
- return {
15
- featuresDirectoryPath: "",
16
- segmentsDirectoryPath: "",
17
- attributesDirectoryPath: "",
18
- groupsDirectoryPath: "",
19
- schemasDirectoryPath: "",
20
- targetsDirectoryPath: "",
21
- testsDirectoryPath: "",
22
- stateDirectoryPath: "",
23
- datafilesDirectoryPath: "",
24
- datafileNamePattern: "",
25
- revisionFileName: "",
26
- catalogDirectoryPath: "",
27
- setsDirectoryPath: "",
28
- environments: ["staging", "production"],
29
- sets: false,
30
- namespaceCharacter: ".",
31
- tags: ["all"],
32
- adapter: {},
33
- plugins: [],
34
- defaultBucketBy: "userId",
35
- parser: "yml",
36
- prettyState: true,
37
- prettyDatafile: false,
38
- stringify: true,
39
- };
40
- }
41
-
42
- const TEST_ATTRIBUTES: Record<string, Attribute> = {
43
- userId: {
44
- description: "User ID",
45
- type: "string",
46
- },
47
- country: {
48
- description: "Country",
49
- type: "string",
50
- },
51
- device: {
52
- description: "Device",
53
- type: "string",
54
- },
55
- permissions: {
56
- description: "Permissions",
57
- type: "array",
58
- items: {
59
- type: "string",
60
- enum: ["read", "write", "admin"],
61
- },
62
- },
63
- version: {
64
- description: "Version",
65
- oneOf: [{ type: "string" }, { type: "double" }],
66
- },
67
- traits: {
68
- description: "Traits",
69
- type: "object",
70
- },
71
- };
72
-
73
- function getSegmentSchema() {
74
- const projectConfig = minimalProjectConfig();
75
- const conditionsZodSchema = getConditionsZodSchema(projectConfig, TEST_ATTRIBUTES);
76
- return getSegmentZodSchema(projectConfig, conditionsZodSchema);
77
- }
78
-
79
- function parseSegment(input: unknown): z.ZodSafeParseResult<unknown> {
80
- return getSegmentSchema().safeParse(input);
81
- }
82
-
83
- function expectSegmentSuccess(input: unknown): void {
84
- const result = parseSegment(input);
85
- expect(result.success).toBe(true);
86
- if (!result.success) {
87
- const err = (result as z.ZodSafeParseError<unknown>).error;
88
- const msg = err.issues.map((i) => `${i.path.join(".")}: ${i.message}`).join("; ");
89
- throw new Error(`Expected segment to pass: ${msg}`);
90
- }
91
- }
92
-
93
- function expectSegmentFailure(input: unknown, messageSubstring?: string): z.ZodError {
94
- const result = parseSegment(input);
95
- expect(result.success).toBe(false);
96
- if (result.success) throw new Error("Expected segment to fail");
97
- const err = (result as z.ZodSafeParseError<unknown>).error;
98
- if (messageSubstring) {
99
- const messages = err.issues
100
- .map((i) => (typeof i.message === "string" ? i.message : ""))
101
- .join(" ");
102
- expect(messages).toContain(messageSubstring);
103
- }
104
- return err;
105
- }
106
-
107
- /** Assert that an intentional mistake produces an error at the expected path with expected message. */
108
- function expectSegmentErrorSurfaces(
109
- input: unknown,
110
- opts: { pathContains: string[]; messageContains: string },
111
- ): void {
112
- const err = expectSegmentFailure(input, opts.messageContains);
113
- const pathStrings = err.issues.map((i) => i.path.join("."));
114
- const hasMatchingPath = pathStrings.some((p) =>
115
- opts.pathContains.every((seg) => p.includes(seg)),
116
- );
117
- expect(hasMatchingPath).toBe(true);
118
- }
119
-
120
- describe("segmentSchema.ts :: getSegmentZodSchema", () => {
121
- describe("required fields", () => {
122
- it("accepts segment with description and conditions (plain condition)", () => {
123
- expectSegmentSuccess({
124
- description: "Users in Germany",
125
- conditions: {
126
- attribute: "country",
127
- operator: "equals",
128
- value: "de",
129
- },
130
- });
131
- });
132
-
133
- it("rejects segment without description", () => {
134
- const result = parseSegment({
135
- conditions: { attribute: "country", operator: "equals", value: "de" },
136
- });
137
- expect(result.success).toBe(false);
138
- if (!result.success) {
139
- const err = (result as z.ZodSafeParseError<unknown>).error;
140
- const path = err.issues.map((i) => i.path.join(".")).join(" ");
141
- expect(path).toContain("description");
142
- }
143
- });
144
-
145
- it("rejects segment without conditions", () => {
146
- const result = parseSegment({
147
- description: "A segment",
148
- });
149
- expect(result.success).toBe(false);
150
- });
151
- });
152
-
153
- describe("conditions: everyone (*)", () => {
154
- it("accepts segment with conditions *", () => {
155
- expectSegmentSuccess({
156
- description: "Everyone",
157
- conditions: "*",
158
- });
159
- });
160
- });
161
-
162
- describe("conditions: array of conditions", () => {
163
- it("accepts segment with conditions as array", () => {
164
- expectSegmentSuccess({
165
- description: "Germany or France",
166
- conditions: [
167
- { attribute: "country", operator: "equals", value: "de" },
168
- { attribute: "country", operator: "equals", value: "fr" },
169
- ],
170
- });
171
- });
172
- });
173
-
174
- describe("conditions: and / or / not", () => {
175
- it("accepts segment with and conditions", () => {
176
- expectSegmentSuccess({
177
- description: "Germany and mobile",
178
- conditions: {
179
- and: [
180
- { attribute: "country", operator: "equals", value: "de" },
181
- { attribute: "device", operator: "equals", value: "mobile" },
182
- ],
183
- },
184
- });
185
- });
186
-
187
- it("accepts segment with or conditions", () => {
188
- expectSegmentSuccess({
189
- description: "Germany or France",
190
- conditions: {
191
- or: [
192
- { attribute: "country", operator: "equals", value: "de" },
193
- { attribute: "country", operator: "equals", value: "fr" },
194
- ],
195
- },
196
- });
197
- });
198
-
199
- it("accepts segment with not conditions", () => {
200
- expectSegmentSuccess({
201
- description: "Not US",
202
- conditions: {
203
- not: [{ attribute: "country", operator: "equals", value: "us" }],
204
- },
205
- });
206
- });
207
-
208
- it("rejects segment when nested condition has unknown attribute", () => {
209
- expectSegmentFailure(
210
- {
211
- description: "Bad attr",
212
- conditions: {
213
- and: [
214
- { attribute: "userId", operator: "equals", value: "u1" },
215
- { attribute: "unknownAttr", operator: "equals", value: "x" },
216
- ],
217
- },
218
- },
219
- "Unknown attribute",
220
- );
221
- });
222
-
223
- it("accepts segment conditions using semver operators on oneOf attributes", () => {
224
- expectSegmentSuccess({
225
- description: "Version above v5",
226
- conditions: {
227
- and: [
228
- { attribute: "device", operator: "equals", value: "desktop" },
229
- { attribute: "version", operator: "semverGreaterThan", value: "5.0.0" },
230
- ],
231
- },
232
- });
233
- });
234
-
235
- it("accepts segment conditions using flat object nested paths", () => {
236
- expectSegmentSuccess({
237
- description: "Dark mode users",
238
- conditions: {
239
- attribute: "traits.favoriteTheme",
240
- operator: "equals",
241
- value: "dark",
242
- },
243
- });
244
- });
245
-
246
- it("rejects segment conditions when oneOf attribute value matches no branch", () => {
247
- expectSegmentFailure(
248
- {
249
- description: "Bad version",
250
- conditions: {
251
- attribute: "version",
252
- operator: "equals",
253
- value: true,
254
- },
255
- },
256
- 'Value does not match the schema of attribute "version"',
257
- );
258
- });
259
-
260
- it("rejects segment conditions when array item value violates enum constraints", () => {
261
- expectSegmentFailure(
262
- {
263
- description: "Bad permission",
264
- conditions: {
265
- attribute: "permissions",
266
- operator: "includes",
267
- value: "delete",
268
- },
269
- },
270
- 'Value does not match the item schema of attribute "permissions"',
271
- );
272
- });
273
- });
274
-
275
- describe("optional archived", () => {
276
- it("accepts segment with archived true", () => {
277
- expectSegmentSuccess({
278
- description: "Old segment",
279
- conditions: "*",
280
- archived: true,
281
- });
282
- });
283
-
284
- it("accepts segment with archived false", () => {
285
- expectSegmentSuccess({
286
- description: "Active segment",
287
- conditions: "*",
288
- archived: false,
289
- });
290
- });
291
-
292
- it("accepts segment without archived", () => {
293
- expectSegmentSuccess({
294
- description: "Segment",
295
- conditions: "*",
296
- });
297
- });
298
- });
299
-
300
- describe("strict: no extra keys", () => {
301
- it("rejects segment with extra key at root", () => {
302
- const result = parseSegment({
303
- description: "Segment",
304
- conditions: "*",
305
- extraKey: true,
306
- });
307
- expect(result.success).toBe(false);
308
- });
309
- });
310
-
311
- describe("description type", () => {
312
- it("rejects description that is not a string", () => {
313
- const result = parseSegment({
314
- description: 123,
315
- conditions: "*",
316
- });
317
- expect(result.success).toBe(false);
318
- });
319
- });
320
-
321
- describe("errors surface properly: intentional mistakes produce correct path and message", () => {
322
- it("missing description: error path includes description", () => {
323
- expectSegmentErrorSurfaces(
324
- { conditions: { attribute: "country", operator: "equals", value: "de" } },
325
- { pathContains: ["description"], messageContains: "Required" },
326
- );
327
- });
328
-
329
- it("conditions with unknown attribute: error path goes into conditions", () => {
330
- expectSegmentErrorSurfaces(
331
- {
332
- description: "Segment",
333
- conditions: {
334
- and: [
335
- { attribute: "country", operator: "equals", value: "de" },
336
- { attribute: "typoAttr", operator: "equals", value: "x" },
337
- ],
338
- },
339
- },
340
- { pathContains: ["conditions", "attribute"], messageContains: "Unknown attribute" },
341
- );
342
- });
343
-
344
- it("extra key at root: parse fails and message mentions unrecognized key", () => {
345
- const result = parseSegment({
346
- description: "Segment",
347
- conditions: "*",
348
- extraKey: true,
349
- });
350
- expect(result.success).toBe(false);
351
- const err = (result as z.ZodSafeParseError<unknown>).error;
352
- const messages = err.issues
353
- .map((i) => (typeof i.message === "string" ? i.message : ""))
354
- .join(" ");
355
- expect(messages).toMatch(/unrecognized|extraKey/i);
356
- });
357
- });
358
- });
@@ -1,18 +0,0 @@
1
- import { z } from "zod";
2
-
3
- import { ProjectConfig } from "../config";
4
-
5
- export function getSegmentZodSchema(projectConfig: ProjectConfig, conditionsZodSchema) {
6
- const segmentZodSchema = z
7
- .object({
8
- archived: z.boolean().optional(),
9
- promotable: z.boolean().optional(),
10
- description: z.string({
11
- error: (issue) => (issue.input === undefined ? "Required" : undefined),
12
- }),
13
- conditions: conditionsZodSchema,
14
- })
15
- .strict();
16
-
17
- return segmentZodSchema;
18
- }
@@ -1,159 +0,0 @@
1
- import type { ProjectConfig } from "../config";
2
- import { getTargetZodSchema } from "./targetSchema";
3
-
4
- function minimalProjectConfig(overrides: Partial<ProjectConfig> = {}): ProjectConfig {
5
- return {
6
- featuresDirectoryPath: "",
7
- segmentsDirectoryPath: "",
8
- attributesDirectoryPath: "",
9
- groupsDirectoryPath: "",
10
- schemasDirectoryPath: "",
11
- targetsDirectoryPath: "",
12
- testsDirectoryPath: "",
13
- stateDirectoryPath: "",
14
- datafilesDirectoryPath: "",
15
- datafileNamePattern: "",
16
- revisionFileName: "",
17
- catalogDirectoryPath: "",
18
- setsDirectoryPath: "",
19
- environments: ["staging", "production"],
20
- sets: false,
21
- namespaceCharacter: ".",
22
- tags: ["all", "web", "mobile"],
23
- adapter: {},
24
- plugins: [],
25
- defaultBucketBy: "userId",
26
- parser: "yml",
27
- prettyState: true,
28
- prettyDatafile: false,
29
- stringify: true,
30
- ...overrides,
31
- };
32
- }
33
-
34
- function parseTarget(input: unknown) {
35
- return getTargetZodSchema(minimalProjectConfig()).safeParse(input);
36
- }
37
-
38
- describe("targetSchema.ts :: getTargetZodSchema", () => {
39
- it("accepts a valid target", () => {
40
- const result = parseTarget({
41
- description: "Web target",
42
- tags: { and: ["all", "web"] },
43
- context: { device: "desktop" },
44
- promotable: true,
45
- });
46
-
47
- expect(result.success).toBe(true);
48
- });
49
-
50
- it("accepts a single tag", () => {
51
- const result = parseTarget({
52
- description: "Web target",
53
- tag: "web",
54
- });
55
-
56
- expect(result.success).toBe(true);
57
- });
58
-
59
- it("accepts feature patterns and the direct all-features wildcard", () => {
60
- expect(
61
- parseTarget({
62
- description: "Selected features",
63
- includeFeatures: ["checkout*", "account.profile"],
64
- excludeFeatures: ["checkout.internal*"],
65
- }).success,
66
- ).toBe(true);
67
-
68
- expect(
69
- parseTarget({
70
- description: "All features",
71
- includeFeatures: "*",
72
- }).success,
73
- ).toBe(true);
74
- });
75
-
76
- it("rejects non-wildcard scalar feature selectors and empty pattern arrays", () => {
77
- expect(
78
- parseTarget({ description: "Scalar feature", includeFeatures: "checkout" }).success,
79
- ).toBe(false);
80
- expect(parseTarget({ description: "Empty include", includeFeatures: [] }).success).toBe(false);
81
- expect(parseTarget({ description: "Empty exclude", excludeFeatures: [] }).success).toBe(false);
82
- });
83
-
84
- it("rejects empty tag selectors", () => {
85
- expect(parseTarget({ description: "Empty tags", tags: [] }).success).toBe(false);
86
- expect(parseTarget({ description: "Empty or", tags: { or: [] } }).success).toBe(false);
87
- expect(parseTarget({ description: "Empty and", tags: { and: [] } }).success).toBe(false);
88
- });
89
-
90
- it("rejects malformed feature glob patterns", () => {
91
- expect(parseTarget({ description: "Empty pattern", includeFeatures: [""] }).success).toBe(
92
- false,
93
- );
94
- expect(parseTarget({ description: "Spaces", includeFeatures: [" checkout*"] }).success).toBe(
95
- false,
96
- );
97
- expect(
98
- parseTarget({ description: "Double wildcard", includeFeatures: ["checkout**"] }).success,
99
- ).toBe(false);
100
- });
101
-
102
- it("rejects unknown single tag", () => {
103
- const result = parseTarget({ description: "Unknown tag", tag: "unknown" });
104
-
105
- expect(result.success).toBe(false);
106
- if (!result.success) {
107
- expect(result.error.issues.map((issue) => issue.message).join(" ")).toContain(
108
- 'Unknown tag "unknown"',
109
- );
110
- }
111
- });
112
-
113
- it("rejects tag and tags together", () => {
114
- const result = parseTarget({
115
- description: "Too many tag selectors",
116
- tag: "web",
117
- tags: ["mobile"],
118
- });
119
-
120
- expect(result.success).toBe(false);
121
- if (!result.success) {
122
- expect(result.error.issues.map((issue) => issue.message).join(" ")).toContain(
123
- 'Only one of "tag" or "tags" can be defined',
124
- );
125
- }
126
- });
127
-
128
- it("requires description", () => {
129
- const result = parseTarget({ tags: ["all"] });
130
-
131
- expect(result.success).toBe(false);
132
- if (!result.success) {
133
- expect(result.error.issues.map((issue) => issue.message).join(" ")).toContain("Required");
134
- }
135
- });
136
-
137
- it("rejects unknown tags", () => {
138
- const result = parseTarget({ description: "Unknown tag", tags: ["unknown"] });
139
-
140
- expect(result.success).toBe(false);
141
- if (!result.success) {
142
- expect(result.error.issues.map((issue) => issue.message).join(" ")).toContain(
143
- 'Unknown tag "unknown"',
144
- );
145
- }
146
- });
147
-
148
- it("rejects invalid tag shapes", () => {
149
- const result = parseTarget({ description: "Invalid tags", tags: { xor: ["web"] } });
150
-
151
- expect(result.success).toBe(false);
152
- });
153
-
154
- it("rejects non-boolean promotable", () => {
155
- const result = parseTarget({ description: "Bad promotable", promotable: "no" });
156
-
157
- expect(result.success).toBe(false);
158
- });
159
- });
@@ -1,46 +0,0 @@
1
- import { z } from "zod";
2
-
3
- import type { ProjectConfig } from "../config";
4
- import { refineWithMessage } from "./zodHelpers";
5
-
6
- const targetTagSchema = (projectConfig: ProjectConfig) =>
7
- refineWithMessage(
8
- z.string(),
9
- (value) => projectConfig.tags.includes(value),
10
- (value) => `Unknown tag "${value}"`,
11
- );
12
-
13
- export function getTargetZodSchema(projectConfig: ProjectConfig) {
14
- const tagSchema = targetTagSchema(projectConfig);
15
- const featurePatternSchema = z
16
- .string()
17
- .min(1)
18
- .refine((value) => value.trim() === value, "Feature patterns cannot have surrounding spaces")
19
- .refine((value) => !value.includes("**"), 'Use "*" for glob-like wildcard matching');
20
- const featurePatternsSchema = z.union([z.literal("*"), z.array(featurePatternSchema).min(1)]);
21
-
22
- return z
23
- .object({
24
- key: z.string().optional(),
25
- promotable: z.boolean().optional(),
26
- description: z.string({
27
- error: (issue) => (issue.input === undefined ? "Required" : undefined),
28
- }),
29
- tag: tagSchema.optional(),
30
- tags: z
31
- .union([
32
- z.array(tagSchema).min(1),
33
- z.object({ or: z.array(tagSchema).min(1) }).strict(),
34
- z.object({ and: z.array(tagSchema).min(1) }).strict(),
35
- ])
36
- .optional(),
37
- includeFeatures: featurePatternsSchema.optional(),
38
- excludeFeatures: featurePatternsSchema.optional(),
39
- context: z.record(z.string(), z.unknown()).optional(),
40
- })
41
- .strict()
42
- .refine((target) => !(target.tag && target.tags), {
43
- message: 'Only one of "tag" or "tags" can be defined',
44
- path: ["tags"],
45
- });
46
- }