@featurevisor/core 3.1.0 → 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 -2246
  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
package/src/sets.ts DELETED
@@ -1,76 +0,0 @@
1
- import * as path from "path";
2
-
3
- import type { ProjectConfig } from "./config";
4
- import type { Datasource } from "./datasource";
5
- import { CLI_FORMAT_BOLD } from "./tester/cliFormat";
6
-
7
- export interface ProjectSetExecution {
8
- set: string;
9
- projectConfig: ProjectConfig;
10
- datasource: Datasource;
11
- }
12
-
13
- export async function getProjectSetExecutions(
14
- projectConfig: ProjectConfig,
15
- datasource: Datasource,
16
- selectedSet?: string,
17
- ): Promise<ProjectSetExecution[]> {
18
- if (!projectConfig.sets) {
19
- return [{ set: "", projectConfig, datasource }];
20
- }
21
-
22
- const availableSetKeys = await datasource.listSets();
23
-
24
- if (selectedSet && !availableSetKeys.includes(selectedSet)) {
25
- throw new Error(
26
- `Unknown set "${selectedSet}". Available sets: ${availableSetKeys.join(", ") || "none"}.`,
27
- );
28
- }
29
-
30
- const setKeys = selectedSet ? [selectedSet] : availableSetKeys;
31
-
32
- if (setKeys.length === 0) {
33
- throw new Error(`No sets found in ${projectConfig.setsDirectoryPath}.`);
34
- }
35
-
36
- return setKeys.map((set) => {
37
- const setDatasource = datasource.forSet(set);
38
-
39
- return {
40
- set,
41
- projectConfig: setDatasource.getConfig(),
42
- datasource: setDatasource,
43
- };
44
- });
45
- }
46
-
47
- export function assertProjectSetJsonSelection(
48
- projectConfig: ProjectConfig,
49
- selectedSet: string | undefined,
50
- json: boolean | undefined,
51
- ) {
52
- if (projectConfig.sets && json && !selectedSet) {
53
- throw new Error("Pass --set=<set> when using --json in a project with sets enabled.");
54
- }
55
- }
56
-
57
- export function getProjectSetRelativeFilePath(
58
- projectConfig: ProjectConfig,
59
- set: string,
60
- filePath: string,
61
- ) {
62
- const setDirectoryPath = path.join(projectConfig.setsDirectoryPath, set);
63
-
64
- if (filePath === setDirectoryPath || filePath.startsWith(`${setDirectoryPath}${path.sep}`)) {
65
- return filePath;
66
- }
67
-
68
- return path.join(setDirectoryPath, filePath);
69
- }
70
-
71
- export function printSetHeader(projectConfig: ProjectConfig, set: string, silent = false) {
72
- if (projectConfig.sets && !silent) {
73
- console.log("");
74
- console.log(CLI_FORMAT_BOLD, `Set "${set}"`);
75
- }
76
- }
@@ -1,74 +0,0 @@
1
- import type { ParsedFeature, Target } from "@featurevisor/types";
2
-
3
- import { getTargetFeatureKeys, resolveTargets, targetIncludesFeature } from "./targeting";
4
-
5
- const features: Record<string, ParsedFeature> = {
6
- webCheckout: {
7
- key: "webCheckout",
8
- description: "Web checkout",
9
- tags: ["web", "checkout"],
10
- bucketBy: "userId",
11
- rules: [],
12
- },
13
- webInternal: {
14
- key: "webInternal",
15
- description: "Internal web",
16
- tags: ["web", "internal"],
17
- bucketBy: "userId",
18
- rules: [],
19
- },
20
- mobileCheckout: {
21
- key: "mobileCheckout",
22
- description: "Mobile checkout",
23
- tags: ["mobile", "checkout"],
24
- bucketBy: "userId",
25
- rules: [],
26
- },
27
- };
28
-
29
- const targets: Record<string, Target> = {
30
- web: {
31
- description: "Web",
32
- tags: { and: ["web", "checkout"] },
33
- includeFeatures: ["web*"],
34
- excludeFeatures: ["webInternal*"],
35
- },
36
- mobile: { description: "Mobile", tag: "mobile" },
37
- };
38
-
39
- function createDatasource() {
40
- return {
41
- listTargets: async () => Object.keys(targets),
42
- readTarget: async (key: string) => targets[key],
43
- listFeatures: async () => Object.keys(features),
44
- readFeature: async (key: string) => features[key],
45
- } as any;
46
- }
47
-
48
- describe("targeting", () => {
49
- it("matches full target selectors", () => {
50
- expect(targetIncludesFeature(targets.web, "webCheckout", features.webCheckout)).toEqual(true);
51
- expect(targetIncludesFeature(targets.web, "webInternal", features.webInternal)).toEqual(false);
52
- expect(targetIncludesFeature(targets.web, "mobileCheckout", features.mobileCheckout)).toEqual(
53
- false,
54
- );
55
- });
56
-
57
- it("resolves repeated targets once and returns their feature union", async () => {
58
- const datasource = createDatasource();
59
- const resolved = await resolveTargets(datasource, ["web", "mobile", "web"]);
60
- const featureKeys = await getTargetFeatureKeys(datasource, resolved);
61
-
62
- expect(resolved.map((target) => target.key)).toEqual(["web", "mobile"]);
63
- expect(Array.from(featureKeys).sort()).toEqual(["mobileCheckout", "webCheckout"]);
64
- });
65
-
66
- it("validates unknown and missing targets", async () => {
67
- await expect(resolveTargets(createDatasource(), "missing")).rejects.toThrow(
68
- 'Unknown target "missing"',
69
- );
70
- await expect(resolveTargets({ listTargets: async () => [] } as any, undefined)).rejects.toThrow(
71
- "No targets found",
72
- );
73
- });
74
- });
package/src/targeting.ts DELETED
@@ -1,106 +0,0 @@
1
- import type { ParsedFeature, Target } from "@featurevisor/types";
2
-
3
- import type { Datasource } from "./datasource";
4
-
5
- export type ResolvedTarget = Target & { key: string };
6
-
7
- export function normalizeOptionValues(value: string | string[] | undefined): string[] {
8
- if (typeof value === "undefined") return [];
9
- return Array.isArray(value) ? value : [value];
10
- }
11
-
12
- export function matchesFeaturePatterns(
13
- featureKey: string,
14
- patterns: Target["includeFeatures"],
15
- ): boolean {
16
- if (!patterns) return false;
17
- return normalizeOptionValues(patterns).some((pattern) => {
18
- const escaped = pattern.replace(/[.+?^${}()|[\]\\]/g, "\\$&").replace(/\*/g, ".*");
19
- return new RegExp(`^${escaped}$`).test(featureKey);
20
- });
21
- }
22
-
23
- export function matchesTargetTags(candidateTags: string[], selector: Target["tags"]): boolean {
24
- if (!selector) return true;
25
- if (Array.isArray(selector)) {
26
- return selector.some((tag) => candidateTags.includes(tag));
27
- }
28
- if ("or" in selector) {
29
- return selector.or.some((tag) => candidateTags.includes(tag));
30
- }
31
- return selector.and.every((tag) => candidateTags.includes(tag));
32
- }
33
-
34
- export function targetIncludesFeature(
35
- target: Target,
36
- featureKey: string,
37
- feature: ParsedFeature,
38
- ): boolean {
39
- if (feature.archived === true) return false;
40
-
41
- const featureTags = feature.tags || [];
42
- if (target.tag && !featureTags.includes(target.tag)) return false;
43
- if (target.tags && !matchesTargetTags(featureTags, target.tags)) return false;
44
- if (target.includeFeatures && !matchesFeaturePatterns(featureKey, target.includeFeatures)) {
45
- return false;
46
- }
47
- if (target.excludeFeatures && matchesFeaturePatterns(featureKey, target.excludeFeatures)) {
48
- return false;
49
- }
50
-
51
- return true;
52
- }
53
-
54
- export async function resolveTargets(
55
- datasource: Datasource,
56
- requestedTargets?: string | string[],
57
- options: { defaultToAll?: boolean; requireTargets?: boolean } = {},
58
- ): Promise<ResolvedTarget[]> {
59
- const targetKeys = await datasource.listTargets();
60
- const requestedTargetKeys = normalizeOptionValues(requestedTargets);
61
- const selectedKeys =
62
- requestedTargetKeys.length > 0
63
- ? requestedTargetKeys
64
- : options.defaultToAll === false
65
- ? []
66
- : targetKeys;
67
-
68
- if (options.requireTargets !== false && targetKeys.length === 0) {
69
- throw new Error(
70
- 'No targets found. Add at least one target definition, for example "targets/all.yml".',
71
- );
72
- }
73
-
74
- const uniqueKeys = Array.from(new Set(selectedKeys));
75
- for (const targetKey of uniqueKeys) {
76
- if (!targetKeys.includes(targetKey)) {
77
- throw new Error(
78
- `Unknown target "${targetKey}". Available targets: ${targetKeys.join(", ") || "none"}.`,
79
- );
80
- }
81
- }
82
-
83
- return Promise.all(
84
- uniqueKeys.map(async (targetKey) => ({
85
- ...(await datasource.readTarget(targetKey)),
86
- key: targetKey,
87
- })),
88
- );
89
- }
90
-
91
- export async function getTargetFeatureKeys(
92
- datasource: Datasource,
93
- targets: ResolvedTarget[],
94
- ): Promise<Set<string>> {
95
- const result = new Set<string>();
96
- const featureKeys = await datasource.listFeatures();
97
-
98
- for (const featureKey of featureKeys) {
99
- const feature = await datasource.readFeature(featureKey);
100
- if (targets.some((target) => targetIncludesFeature(target, featureKey, feature))) {
101
- result.add(featureKey);
102
- }
103
- }
104
-
105
- return result;
106
- }
@@ -1,16 +0,0 @@
1
- export const CLI_FORMAT_RED = "\x1b[31m%s\x1b[0m";
2
- export const CLI_FORMAT_GREEN = "\x1b[32m%s\x1b[0m";
3
- export const CLI_FORMAT_YELLOW = "\x1b[33m%s\x1b[0m";
4
- export const CLI_FORMAT_DIM = "\x1b[2m%s\x1b[0m";
5
-
6
- export const CLI_COLOR_GREEN = 32;
7
- export const CLI_COLOR_YELLOW = 33;
8
- export const CLI_COLOR_CYAN = 36;
9
- export const CLI_COLOR_DIM = 2;
10
-
11
- export const CLI_FORMAT_BOLD = "\x1b[1m%s\x1b[0m";
12
- export const CLI_FORMAT_BOLD_UNDERLINE = "\x1b[1m\x1b[4m%s\x1b[0m";
13
-
14
- export function colorize(value: string | number | boolean, colorCode: number) {
15
- return `\x1b[${colorCode}m${value}\x1b[0m`;
16
- }
@@ -1,149 +0,0 @@
1
- import { checkIfObjectsAreEqual, checkIfArraysAreEqual } from "./helpers";
2
-
3
- describe("helpers", function () {
4
- describe("checkIfObjectsAreEqual", function () {
5
- it("should return true for two empty objects", () => {
6
- expect(checkIfObjectsAreEqual({}, {})).toBe(true);
7
- });
8
-
9
- it("should return true for objects with same keys and values", () => {
10
- expect(checkIfObjectsAreEqual({ a: 1, b: 2 }, { a: 1, b: 2 })).toBe(true);
11
- });
12
-
13
- it("should return false for objects with different keys", () => {
14
- expect(checkIfObjectsAreEqual({ a: 1 }, { b: 1 })).toBe(false);
15
- });
16
-
17
- it("should return false for objects with same keys but different values", () => {
18
- expect(checkIfObjectsAreEqual({ a: 1 }, { a: 2 })).toBe(false);
19
- });
20
-
21
- it("should return false if one object has extra keys", () => {
22
- expect(checkIfObjectsAreEqual({ a: 1, b: 2 }, { a: 1 })).toBe(false);
23
- expect(checkIfObjectsAreEqual({ a: 1 }, { a: 1, b: 2 })).toBe(false);
24
- });
25
-
26
- it("should return true for nested objects that are deeply equal", () => {
27
- expect(checkIfObjectsAreEqual({ a: { b: 2 }, c: 3 }, { a: { b: 2 }, c: 3 })).toBe(true);
28
- });
29
-
30
- it("should return false for nested objects that are not deeply equal", () => {
31
- expect(checkIfObjectsAreEqual({ a: { b: 2 }, c: 3 }, { a: { b: 3 }, c: 3 })).toBe(false);
32
- });
33
-
34
- // it("should return false if types differ", () => {
35
- // expect(checkIfObjectsAreEqual({ a: 1 }, null as any)).toBe(false);
36
- // expect(checkIfObjectsAreEqual(null as any, { a: 1 })).toBe(false);
37
- // expect(checkIfObjectsAreEqual([], {})).toBe(false);
38
- // });
39
-
40
- it("should return true for objects with array values that are equal", () => {
41
- expect(checkIfObjectsAreEqual({ a: [1, 2, 3] }, { a: [1, 2, 3] })).toBe(true);
42
- });
43
-
44
- it("should return false for objects with array values that are not equal", () => {
45
- expect(checkIfObjectsAreEqual({ a: [1, 2, 3] }, { a: [1, 2, 4] })).toBe(false);
46
- });
47
-
48
- it("should return true for objects with different key order", () => {
49
- expect(checkIfObjectsAreEqual({ a: 1, b: 2 }, { b: 2, a: 1 })).toBe(true);
50
- });
51
-
52
- it("should return true for objects with undefined values if both have them", () => {
53
- expect(checkIfObjectsAreEqual({ a: undefined }, { a: undefined })).toBe(true);
54
- });
55
-
56
- it("should return false for objects with one undefined and one missing key", () => {
57
- expect(checkIfObjectsAreEqual({ a: undefined }, {})).toBe(false);
58
- });
59
- });
60
-
61
- describe("checkIfArraysAreEqual", function () {
62
- it("should return true for two empty arrays", () => {
63
- expect(checkIfArraysAreEqual([], [])).toBe(true);
64
- });
65
-
66
- it("should return true for arrays with same elements in same order", () => {
67
- expect(checkIfArraysAreEqual([1, 2, 3], [1, 2, 3])).toBe(true);
68
- });
69
-
70
- it("should return false for arrays with same elements in different order", () => {
71
- expect(checkIfArraysAreEqual([1, 2, 3], [3, 2, 1])).toBe(false);
72
- });
73
-
74
- it("should return false for arrays of different lengths", () => {
75
- expect(checkIfArraysAreEqual([1, 2], [1, 2, 3])).toBe(false);
76
- expect(checkIfArraysAreEqual([1, 2, 3], [1, 2])).toBe(false);
77
- });
78
-
79
- it("should return false for arrays with different elements", () => {
80
- expect(checkIfArraysAreEqual([1, 2, 3], [1, 2, 4])).toBe(false);
81
- });
82
-
83
- it("should return true for nested arrays that are deeply equal", () => {
84
- expect(checkIfArraysAreEqual([[1, 2], [3]], [[1, 2], [3]])).toBe(true);
85
- });
86
-
87
- it("should return false for nested arrays that are not deeply equal", () => {
88
- expect(checkIfArraysAreEqual([[1, 2], [3]], [[1, 2], [4]])).toBe(false);
89
- });
90
-
91
- it("should return true for arrays with objects that are deeply equal", () => {
92
- expect(checkIfArraysAreEqual([{ a: 1 }, { b: 2 }], [{ a: 1 }, { b: 2 }])).toBe(true);
93
- });
94
-
95
- it("should return false for arrays with objects that are not deeply equal", () => {
96
- expect(checkIfArraysAreEqual([{ a: 1 }, { b: 2 }], [{ a: 1 }, { b: 3 }])).toBe(false);
97
- });
98
-
99
- it("should return false if types differ", () => {
100
- expect(checkIfArraysAreEqual([1, 2, 3], null as any)).toBe(false);
101
- expect(checkIfArraysAreEqual(null as any, [1, 2, 3])).toBe(false);
102
- expect(checkIfArraysAreEqual({} as any, [])).toBe(false);
103
- });
104
-
105
- it("should return true for arrays with undefined values if both have them in same positions", () => {
106
- expect(checkIfArraysAreEqual([1, undefined, 3], [1, undefined, 3])).toBe(true);
107
- });
108
-
109
- it("should return false for arrays with undefined values in different positions", () => {
110
- expect(checkIfArraysAreEqual([1, undefined, 3], [1, 3, undefined])).toBe(false);
111
- });
112
-
113
- it("should return true for arrays with different reference but same primitive values", () => {
114
- expect(checkIfArraysAreEqual([1, 2, 3], [1, 2, 3])).toBe(true);
115
- });
116
-
117
- it("should return true for arrays with nested objects and arrays that are deeply equal", () => {
118
- expect(
119
- checkIfArraysAreEqual([{ a: [1, 2], b: { c: 3 } }], [{ a: [1, 2], b: { c: 3 } }]),
120
- ).toBe(true);
121
- });
122
-
123
- it("should return false for arrays with nested objects and arrays that are not deeply equal", () => {
124
- expect(
125
- checkIfArraysAreEqual([{ a: [1, 2], b: { c: 3 } }], [{ a: [1, 2], b: { c: 4 } }]),
126
- ).toBe(false);
127
- });
128
-
129
- it("should return false for arrays with extra undefined at the end", () => {
130
- expect(checkIfArraysAreEqual([1, 2], [1, 2, undefined])).toBe(false);
131
- });
132
-
133
- // it("should return true for arrays with NaN values in same positions", () => {
134
- // expect(checkIfArraysAreEqual([NaN, 2], [NaN, 2])).toBe(true);
135
- // });
136
-
137
- // it("should return false for arrays with NaN values in different positions", () => {
138
- // expect(checkIfArraysAreEqual([2, NaN], [NaN, 2])).toBe(false);
139
- // });
140
-
141
- it("should return true for arrays with null values in same positions", () => {
142
- expect(checkIfArraysAreEqual([null, 2], [null, 2])).toBe(true);
143
- });
144
-
145
- it("should return false for arrays with null values in different positions", () => {
146
- expect(checkIfArraysAreEqual([2, null], [null, 2])).toBe(false);
147
- });
148
- });
149
- });
@@ -1,76 +0,0 @@
1
- export function checkIfObjectsAreEqual(obj1: any, obj2: any): boolean {
2
- if (obj1 === obj2) {
3
- return true;
4
- }
5
-
6
- if (typeof obj1 !== "object" || obj1 === null || typeof obj2 !== "object" || obj2 === null) {
7
- return false;
8
- }
9
-
10
- const keys1 = Object.keys(obj1);
11
- const keys2 = Object.keys(obj2);
12
-
13
- if (keys1.length !== keys2.length) {
14
- return false;
15
- }
16
-
17
- for (const key of keys1) {
18
- if (!keys2.includes(key)) {
19
- return false;
20
- }
21
-
22
- const val1 = obj1[key];
23
- const val2 = obj2[key];
24
-
25
- if (Array.isArray(val1) && Array.isArray(val2)) {
26
- if (!checkIfArraysAreEqual(val1, val2)) {
27
- return false;
28
- }
29
- } else if (typeof val1 === "object" && typeof val2 === "object") {
30
- if (!checkIfObjectsAreEqual(val1, val2)) {
31
- return false;
32
- }
33
- } else if (val1 !== val2) {
34
- return false;
35
- }
36
- }
37
- return true;
38
- }
39
-
40
- export function checkIfArraysAreEqual(arr1: any[], arr2: any[]): boolean {
41
- if (arr1 === arr2) {
42
- return true;
43
- }
44
-
45
- if (!Array.isArray(arr1) || !Array.isArray(arr2)) {
46
- return false;
47
- }
48
-
49
- if (arr1.length !== arr2.length) {
50
- return false;
51
- }
52
-
53
- for (let i = 0; i < arr1.length; i++) {
54
- const val1 = arr1[i];
55
- const val2 = arr2[i];
56
-
57
- if (Array.isArray(val1) && Array.isArray(val2)) {
58
- if (!checkIfArraysAreEqual(val1, val2)) {
59
- return false;
60
- }
61
- } else if (
62
- typeof val1 === "object" &&
63
- typeof val2 === "object" &&
64
- val1 !== null &&
65
- val2 !== null
66
- ) {
67
- if (!checkIfObjectsAreEqual(val1, val2)) {
68
- return false;
69
- }
70
- } else if (val1 !== val2) {
71
- return false;
72
- }
73
- }
74
-
75
- return true;
76
- }
@@ -1,2 +0,0 @@
1
- export * from "./testProject";
2
- export * from "./testFeature";
@@ -1,34 +0,0 @@
1
- export function prettyDuration(diffInMs) {
2
- let diff = Math.abs(diffInMs);
3
-
4
- if (diff === 0) {
5
- return `0ms`;
6
- }
7
-
8
- const ms = diff % 1000;
9
- diff = (diff - ms) / 1000;
10
- const secs = diff % 60;
11
- diff = (diff - secs) / 60;
12
- const mins = diff % 60;
13
- const hrs = (diff - mins) / 60;
14
-
15
- let result = "";
16
-
17
- if (hrs) {
18
- result += ` ${hrs}h`;
19
- }
20
-
21
- if (mins) {
22
- result += ` ${mins}m`;
23
- }
24
-
25
- if (secs) {
26
- result += ` ${secs}s`;
27
- }
28
-
29
- if (ms) {
30
- result += ` ${ms}ms`;
31
- }
32
-
33
- return result.trim();
34
- }
@@ -1,84 +0,0 @@
1
- import type { TestResult } from "@featurevisor/types";
2
-
3
- import {
4
- CLI_COLOR_CYAN,
5
- CLI_FORMAT_BOLD,
6
- CLI_FORMAT_GREEN,
7
- CLI_FORMAT_RED,
8
- colorize,
9
- } from "./cliFormat";
10
- import { prettyDuration } from "./prettyDuration";
11
-
12
- export function printTestResult(testResult: TestResult, relativeTestFilePath, rootDirectoryPath) {
13
- console.log("");
14
-
15
- const title = `Testing: ${relativeTestFilePath.replace(rootDirectoryPath, "")} (${prettyDuration(
16
- testResult.duration,
17
- )})`;
18
- console.log(colorize(title, CLI_COLOR_CYAN));
19
-
20
- if (testResult.notFound) {
21
- console.log(CLI_FORMAT_RED, ` => ${testResult.type} ${testResult.key} not found`);
22
-
23
- return;
24
- }
25
-
26
- console.log(CLI_FORMAT_BOLD, ` ${testResult.type} "${testResult.key}":`);
27
-
28
- testResult.assertions.forEach(function (assertion) {
29
- if (assertion.passed) {
30
- console.log(
31
- CLI_FORMAT_GREEN,
32
- ` ✔ ${assertion.description} (${prettyDuration(assertion.duration)})`,
33
- );
34
- } else {
35
- console.log(
36
- CLI_FORMAT_RED,
37
- ` ✘ ${assertion.description} (${prettyDuration(assertion.duration)})`,
38
- );
39
-
40
- assertion.errors?.forEach(function (error) {
41
- if (error.message) {
42
- console.log(CLI_FORMAT_RED, ` => ${error.message}`);
43
-
44
- return;
45
- }
46
-
47
- let section: string = error.type;
48
-
49
- if (error.type === "flag") {
50
- section = "expectedToBeEnabled";
51
- } else if (error.type === "variation") {
52
- section = "expectedVariation";
53
- } else if (error.type === "variable") {
54
- section = "expectedVariables";
55
- }
56
-
57
- if (error.details && error.details.childIndex !== undefined) {
58
- section = `children[${error.details.childIndex}].${section}`;
59
- }
60
-
61
- if (error.type === "variable") {
62
- const variableKey = (error.details as any).variableKey;
63
-
64
- console.log(CLI_FORMAT_RED, ` => ${section}.${variableKey}:`);
65
- console.log(CLI_FORMAT_RED, ` => expected: ${error.expected}`);
66
- console.log(CLI_FORMAT_RED, ` => received: ${error.actual}`);
67
- } else {
68
- if (error.type === "evaluation") {
69
- if (error.details && error.details.variableKey) {
70
- section = `${section}.variables.${error.details.variableKey}.${error.details.evaluationKey}`;
71
- } else if (error.details && error.details.evaluationType) {
72
- section = `${section}.${error.details.evaluationType}.${error.details.evaluationKey}`;
73
- }
74
- }
75
-
76
- console.log(
77
- CLI_FORMAT_RED,
78
- ` => ${section}: expected "${error.expected}", received "${error.actual}"`,
79
- );
80
- }
81
- });
82
- }
83
- });
84
- }