@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,165 +0,0 @@
1
- import type {
2
- GroupSegment,
3
- AndGroupSegment,
4
- OrGroupSegment,
5
- NotGroupSegment,
6
- Context,
7
- } from "@featurevisor/types";
8
- import { allSegmentsAreMatched } from "@featurevisor/sdk";
9
- import type { Featurevisor } from "@featurevisor/sdk";
10
-
11
- export function applyContextToSegments(
12
- featurevisor: Featurevisor,
13
- segments: GroupSegment | GroupSegment[],
14
- context: Context,
15
- removeSegments: string[] = [],
16
- ): GroupSegment | GroupSegment[] {
17
- const withContextApplied = applyContextToGroupSegments(
18
- featurevisor,
19
- segments,
20
- context,
21
- removeSegments,
22
- );
23
- const removed = removeRedundantGroupSegments(withContextApplied);
24
-
25
- return removed;
26
- }
27
-
28
- export function removeRedundantGroupSegments(
29
- groupSegments: GroupSegment | GroupSegment[],
30
- ): GroupSegment | GroupSegment[] {
31
- if (groupSegments === "*") {
32
- return groupSegments;
33
- }
34
-
35
- if (Array.isArray(groupSegments)) {
36
- // Recursively process each group segment
37
- const processed = groupSegments.map((gs) => removeRedundantGroupSegments(gs)) as GroupSegment[];
38
-
39
- // Filter out "*" values
40
- const filtered = processed.filter((gs) => gs !== "*");
41
-
42
- // If all were "*", return "*"
43
- if (filtered.length === 0) {
44
- return "*";
45
- }
46
-
47
- return filtered;
48
- }
49
-
50
- if (typeof groupSegments === "object") {
51
- if ("and" in groupSegments) {
52
- const processed = groupSegments.and.map((gs) =>
53
- removeRedundantGroupSegments(gs),
54
- ) as GroupSegment[];
55
- const filtered = processed.filter((gs) => gs !== "*");
56
-
57
- // If all were "*", return "*"
58
- if (filtered.length === 0) {
59
- return "*";
60
- }
61
-
62
- return {
63
- and: filtered,
64
- } as AndGroupSegment;
65
- }
66
-
67
- if ("or" in groupSegments) {
68
- const processed = groupSegments.or.map((gs) =>
69
- removeRedundantGroupSegments(gs),
70
- ) as GroupSegment[];
71
- const filtered = processed.filter((gs) => gs !== "*");
72
-
73
- // If all were "*", return "*"
74
- if (filtered.length === 0) {
75
- return "*";
76
- }
77
-
78
- return {
79
- or: filtered,
80
- } as OrGroupSegment;
81
- }
82
-
83
- if ("not" in groupSegments) {
84
- const processed = groupSegments.not.map((gs) =>
85
- removeRedundantGroupSegments(gs),
86
- ) as GroupSegment[];
87
- const filtered = processed.filter((gs) => gs !== "*");
88
-
89
- // `not` negates the implicit AND of its children. Matched children are true
90
- // and can be removed: not(true && X) becomes not(X). If every child was
91
- // true, keep not("*") so it remains an always-false expression.
92
- if (filtered.length === 0) {
93
- return {
94
- not: ["*"],
95
- } as NotGroupSegment;
96
- }
97
-
98
- return {
99
- not: filtered,
100
- } as NotGroupSegment;
101
- }
102
- }
103
-
104
- return groupSegments;
105
- }
106
-
107
- export function applyContextToGroupSegments(
108
- featurevisor: Featurevisor,
109
- groupSegments: GroupSegment | GroupSegment[],
110
- context: Context,
111
- removeSegments: string[] = [],
112
- ): GroupSegment | GroupSegment[] {
113
- if (groupSegments === "*") {
114
- return groupSegments;
115
- }
116
-
117
- if (Array.isArray(groupSegments)) {
118
- return groupSegments.map((gs) =>
119
- applyContextToGroupSegments(featurevisor, gs, context, removeSegments),
120
- ) as GroupSegment[];
121
- }
122
-
123
- if (typeof groupSegments === "string") {
124
- if (removeSegments.includes(groupSegments)) {
125
- return "*";
126
- }
127
-
128
- const matched = allSegmentsAreMatched(groupSegments, context, (segmentKey) =>
129
- featurevisor.getSegment(segmentKey),
130
- );
131
-
132
- if (matched) {
133
- return "*";
134
- }
135
- }
136
-
137
- if (typeof groupSegments === "object") {
138
- // AND, OR, NOT group segments
139
- if ("and" in groupSegments) {
140
- return {
141
- and: groupSegments.and.map((gs) =>
142
- applyContextToGroupSegments(featurevisor, gs, context, removeSegments),
143
- ) as GroupSegment[],
144
- } as AndGroupSegment;
145
- }
146
-
147
- if ("or" in groupSegments) {
148
- return {
149
- or: groupSegments.or.map((gs) =>
150
- applyContextToGroupSegments(featurevisor, gs, context, removeSegments),
151
- ) as GroupSegment[],
152
- } as OrGroupSegment;
153
- }
154
-
155
- if ("not" in groupSegments) {
156
- return {
157
- not: groupSegments.not.map((gs) =>
158
- applyContextToGroupSegments(featurevisor, gs, context, removeSegments),
159
- ) as GroupSegment[],
160
- } as NotGroupSegment;
161
- }
162
- }
163
-
164
- return groupSegments;
165
- }
@@ -1,445 +0,0 @@
1
- import * as fs from "fs";
2
- import * as os from "os";
3
- import * as path from "path";
4
-
5
- import type { DatafileContent, ExistingState, ParsedFeature, Segment } from "@featurevisor/types";
6
- import { parsers } from "@featurevisor/parsers";
7
-
8
- import type { ProjectConfig } from "../config";
9
- import { buildDatafile } from "./buildDatafile";
10
- import { buildTargetDatafile } from "./buildProject";
11
-
12
- function createProjectConfig(root: string, stringify = true): ProjectConfig {
13
- return {
14
- featuresDirectoryPath: path.join(root, "features"),
15
- segmentsDirectoryPath: path.join(root, "segments-missing"),
16
- attributesDirectoryPath: path.join(root, "attributes-missing"),
17
- groupsDirectoryPath: path.join(root, "groups-missing"),
18
- schemasDirectoryPath: path.join(root, "schemas-missing"),
19
- targetsDirectoryPath: path.join(root, "targets-missing"),
20
- testsDirectoryPath: path.join(root, "tests-missing"),
21
- stateDirectoryPath: path.join(root, ".featurevisor"),
22
- datafilesDirectoryPath: path.join(root, "datafiles"),
23
- datafileNamePattern: "featurevisor-%s.json",
24
- revisionFileName: "REVISION",
25
- catalogDirectoryPath: path.join(root, "catalog"),
26
- setsDirectoryPath: path.join(root, "sets"),
27
- environments: ["staging", "production"],
28
- sets: false,
29
- namespaceCharacter: ".",
30
- tags: ["all"],
31
- adapter: {},
32
- plugins: [],
33
- defaultBucketBy: "userId",
34
- parser: parsers.yml,
35
- prettyState: true,
36
- prettyDatafile: false,
37
- stringify,
38
- enforceCatchAllRule: false,
39
- };
40
- }
41
-
42
- function isParsedFeature(
43
- feature: ParsedFeature | Record<string, ParsedFeature>,
44
- ): feature is ParsedFeature {
45
- return typeof (feature as ParsedFeature).description === "string";
46
- }
47
-
48
- function createMockDatasource(
49
- feature: ParsedFeature | Record<string, ParsedFeature>,
50
- segmentsByKey: Record<string, Segment> = {},
51
- ) {
52
- let featuresByKey: Record<string, ParsedFeature>;
53
-
54
- if (isParsedFeature(feature)) {
55
- featuresByKey = { [feature.key]: feature };
56
- } else {
57
- featuresByKey = feature;
58
- }
59
-
60
- return {
61
- listSchemas: async () => [],
62
- readSchema: async () => {
63
- throw new Error("readSchema should not be called");
64
- },
65
- listFeatures: async () => Object.keys(featuresByKey),
66
- readFeature: async (key: string) => featuresByKey[key],
67
- listSegments: async () => Object.keys(segmentsByKey),
68
- readSegment: async (key: string) => segmentsByKey[key],
69
- listAttributes: async () => [],
70
- readAttribute: async () => {
71
- throw new Error("readAttribute should not be called");
72
- },
73
- } as any;
74
- }
75
-
76
- function createFeatureFixture(): ParsedFeature {
77
- return {
78
- key: "withRuleOverrides",
79
- description: "Rule variableOverrides fixture",
80
- tags: ["all"],
81
- bucketBy: "userId",
82
- variablesSchema: {
83
- config: {
84
- type: "object",
85
- defaultValue: {
86
- source: "default",
87
- nested: { value: 1 },
88
- list: ["d"],
89
- rows: [
90
- { id: 1, label: "one" },
91
- { id: 2, label: "two" },
92
- ],
93
- flag: false,
94
- },
95
- },
96
- fallbackConfig: {
97
- type: "object",
98
- defaultValue: {
99
- origin: "default",
100
- count: 1,
101
- },
102
- },
103
- },
104
- variations: [{ value: "control", weight: 100 }],
105
- rules: {
106
- staging: [
107
- {
108
- key: "rule1",
109
- segments: "*",
110
- percentage: 100,
111
- variables: {
112
- config: {
113
- source: "rule",
114
- nested: { value: 10 },
115
- list: ["base"],
116
- rows: [
117
- { id: 1, label: "one" },
118
- { id: 2, label: "two" },
119
- ],
120
- flag: true,
121
- },
122
- },
123
- variableOverrides: {
124
- config: [
125
- {
126
- segments: { and: ["mobile", "touch"] },
127
- value: {
128
- "nested.value": 20,
129
- "list:append": "m",
130
- "rows[id=1]:after": { id: 15, label: "one-half" },
131
- "rows[id=2]:remove": null,
132
- },
133
- },
134
- {
135
- conditions: [{ attribute: "country", operator: "equals", value: "de" }],
136
- value: {
137
- "nested.value": 30,
138
- "list:prepend": "de",
139
- "rows[id=1]:before": { id: 0, label: "zero" },
140
- },
141
- },
142
- {
143
- conditions: [{ attribute: "device", operator: "equals", value: "desktop" }],
144
- value: {
145
- source: "full",
146
- nested: { value: 999 },
147
- list: ["x"],
148
- rows: [{ id: 7, label: "seven" }],
149
- flag: false,
150
- },
151
- },
152
- ],
153
- fallbackConfig: [
154
- {
155
- conditions: [{ attribute: "country", operator: "equals", value: "nl" }],
156
- value: {
157
- count: 5,
158
- },
159
- },
160
- ],
161
- },
162
- },
163
- ],
164
- production: [
165
- {
166
- key: "everyone",
167
- segments: "*",
168
- percentage: 100,
169
- },
170
- ],
171
- },
172
- } as ParsedFeature;
173
- }
174
-
175
- describe("core: buildDatafile", function () {
176
- let root: string;
177
- let existingState: ExistingState;
178
-
179
- beforeEach(function () {
180
- root = fs.mkdtempSync(path.join(os.tmpdir(), "featurevisor-buildDatafile-"));
181
- fs.mkdirSync(path.join(root, "features"), { recursive: true });
182
- fs.mkdirSync(path.join(root, "segments-missing"), { recursive: true });
183
- existingState = { features: {} };
184
- });
185
-
186
- afterEach(function () {
187
- fs.rmSync(root, { recursive: true, force: true });
188
- });
189
-
190
- test("resolves rule variableOverrides mutations using rule variables as base and default fallback when missing", async function () {
191
- const config = createProjectConfig(root, true);
192
- const datasource = createMockDatasource(createFeatureFixture());
193
-
194
- const result = (await buildDatafile(
195
- config,
196
- datasource,
197
- {
198
- revision: "1",
199
- environment: "staging",
200
- },
201
- existingState,
202
- )) as DatafileContent;
203
-
204
- expect(result.schemaVersion).toBe("2");
205
-
206
- const trafficRule = result.features.withRuleOverrides.traffic.find((t) => t.key === "rule1");
207
-
208
- expect(trafficRule).toBeDefined();
209
- expect(trafficRule?.variableOverrides).toBeDefined();
210
-
211
- // Base comes from rule variables (source remains "rule"), not from schema default.
212
- expect(trafficRule?.variableOverrides?.config[0].value).toEqual({
213
- source: "rule",
214
- nested: { value: 20 },
215
- list: ["base", "m"],
216
- rows: [
217
- { id: 1, label: "one" },
218
- { id: 15, label: "one-half" },
219
- ],
220
- flag: true,
221
- });
222
-
223
- // Independent override still uses the same rule-level base and preserves order.
224
- expect(trafficRule?.variableOverrides?.config[1].value).toEqual({
225
- source: "rule",
226
- nested: { value: 30 },
227
- list: ["de", "base"],
228
- rows: [
229
- { id: 0, label: "zero" },
230
- { id: 1, label: "one" },
231
- { id: 2, label: "two" },
232
- ],
233
- flag: true,
234
- });
235
-
236
- // Full object override passes through unchanged.
237
- expect(trafficRule?.variableOverrides?.config[2].value).toEqual({
238
- source: "full",
239
- nested: { value: 999 },
240
- list: ["x"],
241
- rows: [{ id: 7, label: "seven" }],
242
- flag: false,
243
- });
244
-
245
- // No rule variable for fallbackConfig -> mutation base is variable schema defaultValue.
246
- expect(trafficRule?.variableOverrides?.fallbackConfig[0].value).toEqual({
247
- origin: "default",
248
- count: 5,
249
- });
250
-
251
- // Supports stringification for segments + conditions.
252
- expect(typeof trafficRule?.variableOverrides?.config[0].segments).toBe("string");
253
- expect(trafficRule?.variableOverrides?.config[0].segments).toEqual(
254
- JSON.stringify({ and: ["mobile", "touch"] }),
255
- );
256
- expect(typeof trafficRule?.variableOverrides?.config[1].conditions).toBe("string");
257
- expect(trafficRule?.variableOverrides?.config[1].conditions).toEqual(
258
- JSON.stringify([{ attribute: "country", operator: "equals", value: "de" }]),
259
- );
260
- });
261
-
262
- test("keeps segments and conditions non-stringified when projectConfig.stringify is false", async function () {
263
- const config = createProjectConfig(root, false);
264
- const datasource = createMockDatasource(createFeatureFixture());
265
-
266
- const result = (await buildDatafile(
267
- config,
268
- datasource,
269
- {
270
- revision: "1",
271
- environment: "staging",
272
- },
273
- existingState,
274
- )) as DatafileContent;
275
-
276
- const trafficRule = result.features.withRuleOverrides.traffic.find((t) => t.key === "rule1");
277
-
278
- expect(trafficRule?.variableOverrides?.config[0].segments).toEqual({
279
- and: ["mobile", "touch"],
280
- });
281
- expect(trafficRule?.variableOverrides?.config[1].conditions).toEqual([
282
- { attribute: "country", operator: "equals", value: "de" },
283
- ]);
284
- });
285
-
286
- test("includes features with missing tags when no tag filter is provided", async function () {
287
- const config = createProjectConfig(root, true);
288
- const feature = createFeatureFixture();
289
- delete feature.tags;
290
- const datasource = createMockDatasource(feature);
291
-
292
- const result = (await buildDatafile(
293
- config,
294
- datasource,
295
- {
296
- revision: "1",
297
- environment: "staging",
298
- },
299
- existingState,
300
- )) as DatafileContent;
301
-
302
- expect(result.features.withRuleOverrides).toBeDefined();
303
- });
304
-
305
- test("excludes features with missing tags when tag filter is provided", async function () {
306
- const config = createProjectConfig(root, true);
307
- const feature = createFeatureFixture();
308
- delete feature.tags;
309
- const datasource = createMockDatasource(feature);
310
-
311
- const result = (await buildDatafile(
312
- config,
313
- datasource,
314
- {
315
- revision: "1",
316
- environment: "staging",
317
- tag: "all",
318
- },
319
- existingState,
320
- )) as DatafileContent;
321
-
322
- expect(result.features.withRuleOverrides).toBeUndefined();
323
- });
324
-
325
- test("filters target features by include/exclude patterns and combines them with tags using AND", async function () {
326
- const config = createProjectConfig(root, true);
327
- const createFeature = (key: string, tags: string[]) => ({
328
- ...createFeatureFixture(),
329
- key,
330
- description: key,
331
- tags,
332
- });
333
- const datasource = createMockDatasource({
334
- "checkout.web": createFeature("checkout.web", ["all", "web"]),
335
- "checkout.mobile": createFeature("checkout.mobile", ["all", "mobile"]),
336
- "admin.web": createFeature("admin.web", ["all", "web"]),
337
- "admin.internal": createFeature("admin.internal", ["all", "web"]),
338
- });
339
-
340
- const filtered = await buildTargetDatafile({
341
- projectConfig: config,
342
- datasource,
343
- target: {
344
- description: "Selected web features",
345
- tag: "web",
346
- includeFeatures: ["checkout*", "admin*"],
347
- excludeFeatures: ["admin.internal"],
348
- },
349
- environment: "staging",
350
- existingState,
351
- revision: "1",
352
- });
353
-
354
- expect(Object.keys(filtered.features).sort()).toEqual(["admin.web", "checkout.web"]);
355
-
356
- const all = await buildTargetDatafile({
357
- projectConfig: config,
358
- datasource,
359
- target: {
360
- description: "All except admin features",
361
- includeFeatures: "*",
362
- excludeFeatures: ["admin*"],
363
- },
364
- environment: "staging",
365
- existingState,
366
- revision: "1",
367
- });
368
-
369
- expect(Object.keys(all.features).sort()).toEqual(["checkout.mobile", "checkout.web"]);
370
- });
371
-
372
- test("builds target datafiles without broadening NOT segment rules", async function () {
373
- const config = createProjectConfig(root, false);
374
- const feature: ParsedFeature = {
375
- key: "targeted",
376
- description: "Targeted feature",
377
- tags: ["all"],
378
- bucketBy: "userId",
379
- rules: {
380
- staging: [
381
- {
382
- key: "not-web",
383
- segments: {
384
- not: ["web"],
385
- },
386
- percentage: 100,
387
- },
388
- {
389
- key: "not-web-and-chrome",
390
- segments: {
391
- not: ["web", "chrome"],
392
- },
393
- percentage: 100,
394
- },
395
- ],
396
- },
397
- } as ParsedFeature;
398
- const datasource = createMockDatasource(feature, {
399
- web: {
400
- conditions: [{ attribute: "platform", operator: "equals", value: "web" }],
401
- },
402
- chrome: {
403
- conditions: [{ attribute: "browser", operator: "equals", value: "chrome" }],
404
- },
405
- });
406
-
407
- const webDatafile = await buildTargetDatafile({
408
- projectConfig: config,
409
- datasource,
410
- target: {
411
- description: "Web target",
412
- context: { platform: "web" },
413
- },
414
- environment: "staging",
415
- existingState,
416
- revision: "1",
417
- });
418
-
419
- expect(webDatafile.features.targeted.traffic[0].segments).toEqual({
420
- not: ["*"],
421
- });
422
- expect(webDatafile.features.targeted.traffic[1].segments).toEqual({
423
- not: ["chrome"],
424
- });
425
-
426
- const webChromeDatafile = await buildTargetDatafile({
427
- projectConfig: config,
428
- datasource,
429
- target: {
430
- description: "Web Chrome target",
431
- context: { platform: "web", browser: "chrome" },
432
- },
433
- environment: "staging",
434
- existingState,
435
- revision: "1",
436
- });
437
-
438
- expect(webChromeDatafile.features.targeted.traffic[0].segments).toEqual({
439
- not: ["*"],
440
- });
441
- expect(webChromeDatafile.features.targeted.traffic[1].segments).toEqual({
442
- not: ["*"],
443
- });
444
- });
445
- });