@featurevisor/catalog 0.0.1 → 3.0.1

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.
@@ -0,0 +1,236 @@
1
+ import * as fs from "fs";
2
+ import * as os from "os";
3
+ import * as path from "path";
4
+ import * as childProcess from "child_process";
5
+
6
+ import { exportCatalog, type CatalogRuntime } from ".";
7
+
8
+ function createProjectConfig(root: string, sets = false) {
9
+ return {
10
+ sets,
11
+ tags: ["all", "web", "mobile", "premium"],
12
+ environments: ["staging", "production"],
13
+ namespaceCharacter: ".",
14
+ catalogDirectoryPath: path.join(root, "catalog"),
15
+ featuresDirectoryPath: path.join(root, "features"),
16
+ segmentsDirectoryPath: path.join(root, "segments"),
17
+ attributesDirectoryPath: path.join(root, "attributes"),
18
+ targetsDirectoryPath: path.join(root, "targets"),
19
+ groupsDirectoryPath: path.join(root, "groups"),
20
+ schemasDirectoryPath: path.join(root, "schemas"),
21
+ testsDirectoryPath: path.join(root, "tests"),
22
+ setsDirectoryPath: path.join(root, "sets"),
23
+ };
24
+ }
25
+
26
+ function createDatasource(set = "") {
27
+ const featureKey = set ? `checkout.${set}` : "checkout";
28
+ const segmentKey = "premiumUsers";
29
+
30
+ return {
31
+ getExtension: () => "yml",
32
+ listSets: async () => ["dev", "production"],
33
+ forSet: (nextSet: string) => createDatasource(nextSet),
34
+ listHistoryEntries: async () => [
35
+ {
36
+ commit: `${set || "root"}123456789`,
37
+ author: "Test",
38
+ timestamp: "2026-01-01T00:00:00.000Z",
39
+ entities: [{ type: "feature", key: featureKey }],
40
+ },
41
+ ],
42
+ listFeatures: async () => [featureKey],
43
+ readFeature: async () => ({
44
+ key: featureKey,
45
+ description: "Checkout feature",
46
+ tags: ["web", "premium"],
47
+ bucketBy: "userId",
48
+ rules: {
49
+ staging: [
50
+ {
51
+ key: "rule",
52
+ segments: segmentKey,
53
+ conditions: [{ attribute: "country", operator: "equals", value: "nl" }],
54
+ percentage: 100,
55
+ enabled: true,
56
+ },
57
+ ],
58
+ production: [],
59
+ },
60
+ }),
61
+ listSegments: async () => [segmentKey],
62
+ readSegment: async () => ({
63
+ key: segmentKey,
64
+ description: "Premium users",
65
+ conditions: [{ attribute: "plan", operator: "equals", value: "premium" }],
66
+ }),
67
+ listAttributes: async () => ["plan", "country"],
68
+ readAttribute: async (key: string) => ({
69
+ key,
70
+ type: "string",
71
+ description: key === "plan" ? "Plan" : "Country",
72
+ }),
73
+ listTargets: async () => ["premiumWeb", "mobile"],
74
+ readTarget: async (key: string) =>
75
+ key === "premiumWeb"
76
+ ? {
77
+ key,
78
+ description: "Premium web",
79
+ tags: { and: ["web", "premium"] },
80
+ includeFeatures: ["checkout*"],
81
+ excludeFeatures: ["checkout.internal*"],
82
+ }
83
+ : { key, description: "Mobile", tag: "mobile", includeFeatures: "*" },
84
+ listGroups: async () => [],
85
+ readGroup: async () => undefined,
86
+ listSchemas: async () => [],
87
+ readSchema: async () => undefined,
88
+ listTests: async () => ["checkout"],
89
+ readTest: async () => ({ key: "checkout", feature: featureKey, assertions: [] }),
90
+ };
91
+ }
92
+
93
+ function createRuntime(): CatalogRuntime {
94
+ return {
95
+ getProjectSetExecutions: async (projectConfig, datasource) => {
96
+ if (!projectConfig.sets) {
97
+ return [{ set: "", projectConfig, datasource }];
98
+ }
99
+
100
+ return (await datasource.listSets()).map((set: string) => ({
101
+ set,
102
+ projectConfig: {
103
+ ...projectConfig,
104
+ featuresDirectoryPath: path.join(projectConfig.setsDirectoryPath, set, "features"),
105
+ segmentsDirectoryPath: path.join(projectConfig.setsDirectoryPath, set, "segments"),
106
+ attributesDirectoryPath: path.join(projectConfig.setsDirectoryPath, set, "attributes"),
107
+ targetsDirectoryPath: path.join(projectConfig.setsDirectoryPath, set, "targets"),
108
+ groupsDirectoryPath: path.join(projectConfig.setsDirectoryPath, set, "groups"),
109
+ schemasDirectoryPath: path.join(projectConfig.setsDirectoryPath, set, "schemas"),
110
+ testsDirectoryPath: path.join(projectConfig.setsDirectoryPath, set, "tests"),
111
+ },
112
+ datasource: datasource.forSet(set),
113
+ }));
114
+ },
115
+ };
116
+ }
117
+
118
+ function git(root: string, args: string[]) {
119
+ childProcess.execFileSync("git", ["-C", root, ...args], {
120
+ encoding: "utf8",
121
+ stdio: ["ignore", "pipe", "ignore"],
122
+ });
123
+ }
124
+
125
+ describe("catalog export", () => {
126
+ it("writes manifest, index, and feature detail for a root project", async () => {
127
+ const root = fs.mkdtempSync(path.join(os.tmpdir(), "featurevisor-catalog-"));
128
+
129
+ await exportCatalog(createRuntime(), root, createProjectConfig(root), createDatasource(), {
130
+ copyAssets: false,
131
+ });
132
+
133
+ const manifest = JSON.parse(
134
+ fs.readFileSync(path.join(root, "catalog", "data", "manifest.json"), "utf8"),
135
+ );
136
+ const index = JSON.parse(
137
+ fs.readFileSync(path.join(root, "catalog", "data", "root", "index.json"), "utf8"),
138
+ );
139
+ const detail = JSON.parse(
140
+ fs.readFileSync(
141
+ path.join(root, "catalog", "data", "root", "entities", "feature", "checkout.json"),
142
+ "utf8",
143
+ ),
144
+ );
145
+
146
+ expect(manifest.sets).toBe(false);
147
+ expect(index.counts.feature).toBe(1);
148
+ expect(index.entities.feature[0].targets).toEqual(["premiumWeb"]);
149
+ expect(index.entities.segment[0].targets).toEqual(["premiumWeb"]);
150
+ expect(index.entities.segment[0].usedInFeatureCount).toBe(1);
151
+ expect(
152
+ index.entities.attribute.find((entity: any) => entity.key === "country").targets,
153
+ ).toEqual(["premiumWeb"]);
154
+ expect(index.entities.attribute.find((entity: any) => entity.key === "plan").targets).toEqual([
155
+ "premiumWeb",
156
+ ]);
157
+ expect(
158
+ index.entities.attribute.find((entity: any) => entity.key === "plan").usedInSegmentCount,
159
+ ).toBe(1);
160
+ expect(detail.relationships).toMatchObject({
161
+ segments: ["premiumUsers"],
162
+ attributes: ["country"],
163
+ targets: ["premiumWeb"],
164
+ tests: ["checkout"],
165
+ });
166
+ });
167
+
168
+ it("writes per-set catalog files for sets-enabled projects", async () => {
169
+ const root = fs.mkdtempSync(path.join(os.tmpdir(), "featurevisor-catalog-"));
170
+
171
+ await exportCatalog(
172
+ createRuntime(),
173
+ root,
174
+ createProjectConfig(root, true),
175
+ createDatasource(),
176
+ {
177
+ copyAssets: false,
178
+ },
179
+ );
180
+
181
+ const manifest = JSON.parse(
182
+ fs.readFileSync(path.join(root, "catalog", "data", "manifest.json"), "utf8"),
183
+ );
184
+
185
+ expect(manifest.sets).toBe(true);
186
+ expect(manifest.setKeys).toEqual(["dev", "production"]);
187
+ expect(
188
+ fs.existsSync(
189
+ path.join(
190
+ root,
191
+ "catalog",
192
+ "data",
193
+ "sets",
194
+ "dev",
195
+ "entities",
196
+ "feature",
197
+ "checkout.dev.json",
198
+ ),
199
+ ),
200
+ ).toBe(true);
201
+ });
202
+
203
+ it("exports repository source links and dev editor links", async () => {
204
+ const root = fs.mkdtempSync(path.join(os.tmpdir(), "featurevisor-catalog-"));
205
+
206
+ git(root, ["init"]);
207
+ git(root, ["checkout", "-b", "catalog-test"]);
208
+ git(root, ["remote", "add", "origin", "git@github.com:featurevisor/featurevisor.git"]);
209
+
210
+ await exportCatalog(createRuntime(), root, createProjectConfig(root), createDatasource(), {
211
+ copyAssets: false,
212
+ dev: true,
213
+ devEditors: [{ id: "cursor", label: "Cursor", icon: "cursor" }],
214
+ });
215
+
216
+ const manifest = JSON.parse(
217
+ fs.readFileSync(path.join(root, "catalog", "data", "manifest.json"), "utf8"),
218
+ );
219
+ const detail = JSON.parse(
220
+ fs.readFileSync(
221
+ path.join(root, "catalog", "data", "root", "entities", "feature", "checkout.json"),
222
+ "utf8",
223
+ ),
224
+ );
225
+
226
+ expect(manifest.links).toEqual({
227
+ provider: "github",
228
+ repository: "https://github.com/featurevisor/featurevisor",
229
+ source: "https://github.com/featurevisor/featurevisor/blob/catalog-test/{{path}}",
230
+ commit: "https://github.com/featurevisor/featurevisor/commit/{{hash}}",
231
+ });
232
+ expect(manifest.dev.editors).toEqual([{ id: "cursor", label: "Cursor", icon: "cursor" }]);
233
+ expect(detail.sourcePath).toBe("features/checkout.yml");
234
+ expect(detail.editLinks.cursor).toMatch(/^cursor:\/\/file\/.+features\/checkout\.yml$/);
235
+ });
236
+ });