apify-schema-tools 3.1.0 → 3.2.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.
- package/.node-version +1 -1
- package/CHANGELOG.md +7 -1
- package/biome.json +8 -2
- package/dist/apify-schema-tools.js +12 -9
- package/dist/apify-schema-tools.js.map +1 -1
- package/dist/apify.d.ts +1 -1
- package/dist/apify.d.ts.map +1 -1
- package/dist/apify.js +19 -5
- package/dist/apify.js.map +1 -1
- package/dist/cli/check.d.ts +5 -0
- package/dist/cli/check.d.ts.map +1 -0
- package/dist/cli/check.js +86 -0
- package/dist/cli/check.js.map +1 -0
- package/dist/cli/init.d.ts +5 -0
- package/dist/cli/init.d.ts.map +1 -0
- package/dist/cli/init.js +92 -0
- package/dist/cli/init.js.map +1 -0
- package/dist/cli/sync.d.ts +5 -0
- package/dist/cli/sync.d.ts.map +1 -0
- package/dist/cli/sync.js +112 -0
- package/dist/cli/sync.js.map +1 -0
- package/dist/configuration.d.ts +14 -5
- package/dist/configuration.d.ts.map +1 -1
- package/dist/configuration.js +9 -5
- package/dist/configuration.js.map +1 -1
- package/dist/main.d.ts +4 -0
- package/dist/main.d.ts.map +1 -0
- package/dist/main.js +19 -0
- package/dist/main.js.map +1 -0
- package/dist/middle-schema/compare-schemas.d.ts +3 -0
- package/dist/middle-schema/compare-schemas.d.ts.map +1 -0
- package/dist/middle-schema/compare-schemas.js +90 -0
- package/dist/middle-schema/compare-schemas.js.map +1 -0
- package/dist/middle-schema/generate-typescript.d.ts +7 -0
- package/dist/middle-schema/generate-typescript.d.ts.map +1 -0
- package/dist/middle-schema/generate-typescript.js +70 -0
- package/dist/middle-schema/generate-typescript.js.map +1 -0
- package/dist/middle-schema/parse-json-schema.d.ts +4 -0
- package/dist/middle-schema/parse-json-schema.d.ts.map +1 -0
- package/dist/middle-schema/parse-json-schema.js +65 -0
- package/dist/middle-schema/parse-json-schema.js.map +1 -0
- package/dist/middle-schema/parse-typescript.d.ts +4 -0
- package/dist/middle-schema/parse-typescript.d.ts.map +1 -0
- package/dist/middle-schema/parse-typescript.js +199 -0
- package/dist/middle-schema/parse-typescript.js.map +1 -0
- package/dist/middle-schema/schema-types.d.ts +24 -0
- package/dist/middle-schema/schema-types.d.ts.map +1 -0
- package/dist/middle-schema/schema-types.js +14 -0
- package/dist/middle-schema/schema-types.js.map +1 -0
- package/dist/middle-schema/schema.d.ts +24 -0
- package/dist/middle-schema/schema.d.ts.map +1 -0
- package/dist/middle-schema/schema.js +14 -0
- package/dist/middle-schema/schema.js.map +1 -0
- package/dist/schema/entities/abstract-entity.d.ts +5 -0
- package/dist/schema/entities/abstract-entity.d.ts.map +1 -0
- package/dist/schema/entities/abstract-entity.js +3 -0
- package/dist/schema/entities/abstract-entity.js.map +1 -0
- package/dist/schema/entities/primitive-union.d.ts +12 -0
- package/dist/schema/entities/primitive-union.d.ts.map +1 -0
- package/dist/schema/entities/primitive-union.js +74 -0
- package/dist/schema/entities/primitive-union.js.map +1 -0
- package/dist/schema/entities/primitive.d.ts +15 -0
- package/dist/schema/entities/primitive.d.ts.map +1 -0
- package/dist/schema/entities/primitive.js +54 -0
- package/dist/schema/entities/primitive.js.map +1 -0
- package/dist/schema/parsers/json-schema.d.ts +4 -0
- package/dist/schema/parsers/json-schema.d.ts.map +1 -0
- package/dist/schema/parsers/json-schema.js +12 -0
- package/dist/schema/parsers/json-schema.js.map +1 -0
- package/dist/schema/parsers/typescript.d.ts +3 -0
- package/dist/schema/parsers/typescript.d.ts.map +1 -0
- package/dist/schema/parsers/typescript.js +24 -0
- package/dist/schema/parsers/typescript.js.map +1 -0
- package/dist/schemas/input.d.ts +840 -0
- package/dist/schemas/input.d.ts.map +1 -0
- package/dist/schemas/input.js +349 -0
- package/dist/schemas/input.js.map +1 -0
- package/dist/utils/filesystem.d.ts +8 -0
- package/dist/utils/filesystem.d.ts.map +1 -0
- package/dist/utils/filesystem.js +16 -0
- package/dist/utils/filesystem.js.map +1 -0
- package/dist/utils/json-schemas-interactive-conflict.d.ts +16 -0
- package/dist/utils/json-schemas-interactive-conflict.d.ts.map +1 -0
- package/dist/utils/json-schemas-interactive-conflict.js +165 -0
- package/dist/utils/json-schemas-interactive-conflict.js.map +1 -0
- package/dist/utils/json-schemas.d.ts +42 -0
- package/dist/utils/json-schemas.d.ts.map +1 -0
- package/dist/utils/json-schemas.js +162 -0
- package/dist/utils/json-schemas.js.map +1 -0
- package/dist/zod/schemas/input.d.ts +840 -0
- package/dist/zod/schemas/input.d.ts.map +1 -0
- package/dist/zod/schemas/input.js +393 -0
- package/dist/zod/schemas/input.js.map +1 -0
- package/package.json +12 -12
- package/samples/all-defaults/.actor/input_schema.json +32 -3
- package/samples/all-defaults/src-schemas/input.json +2 -1
- package/samples/deep-merged-schemas/.actor/input_schema.json +36 -3
- package/samples/merged-schemas/.actor/input_schema.json +27 -3
- package/samples/package-json-config/.actor/input_schema.json +32 -3
- package/samples/package-json-config-merged/.actor/input_schema.json +36 -3
- package/src/apify.ts +21 -6
- package/src/cli/check.ts +114 -0
- package/src/cli/init.ts +125 -0
- package/src/cli/sync.ts +164 -0
- package/src/configuration.ts +17 -7
- package/src/main.ts +25 -0
- package/src/middle-schema/compare-schemas.ts +113 -0
- package/src/middle-schema/generate-typescript.ts +88 -0
- package/src/middle-schema/parse-json-schema.ts +104 -0
- package/src/middle-schema/parse-typescript.ts +239 -0
- package/src/middle-schema/schema-types.ts +40 -0
- package/test/apify.test.ts +410 -2
- package/test/cli/check.test.ts +1571 -0
- package/test/cli/init.test.ts +459 -0
- package/test/cli/sync.test.ts +341 -0
- package/test/common.ts +68 -0
- package/test/configuration.test.ts +8 -8
- package/test/middle-schema/compare-schemas.test.ts +585 -0
- package/test/middle-schema/generate-typescript.test.ts +191 -0
- package/test/middle-schema/parse-json-schema.test.ts +178 -0
- package/test/middle-schema/parse-typescript.test.ts +143 -0
- package/test/{json-schema-conflicts.test.ts → utils/json-schemas-interactive-conflict.test.ts} +2 -2
- package/test/{json-schemas.test.ts → utils/json-schemas.test.ts} +3 -3
- package/src/apify-schema-tools.ts +0 -420
- package/src/typescript.ts +0 -563
- package/test/apify-schema-tools.test.ts +0 -2216
- package/test/typescript.test.ts +0 -1079
- /package/src/{filesystem.ts → utils/filesystem.ts} +0 -0
- /package/src/{json-schema-conflicts.ts → utils/json-schemas-interactive-conflict.ts} +0 -0
- /package/src/{json-schemas.ts → utils/json-schemas.ts} +0 -0
|
@@ -0,0 +1,341 @@
|
|
|
1
|
+
import { execSync } from "node:child_process";
|
|
2
|
+
import { existsSync } from "node:fs";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { afterEach, beforeAll, describe, expect, it } from "vitest";
|
|
5
|
+
import { type ObjectSchema, readJsonSchema, writeJsonSchema } from "../../src/utils/json-schemas.js";
|
|
6
|
+
import {
|
|
7
|
+
cleanupTestDirectory,
|
|
8
|
+
getDatasetSchemaPath,
|
|
9
|
+
getInputSchemaPath,
|
|
10
|
+
getTestDir,
|
|
11
|
+
setupTestDirectory,
|
|
12
|
+
setupTestDirectoryFiles,
|
|
13
|
+
} from "../common.js";
|
|
14
|
+
|
|
15
|
+
const EXEC_CMD = `node ${import.meta.dirname}/../../dist/apify-schema-tools.js`;
|
|
16
|
+
|
|
17
|
+
const TEST_DIR = getTestDir("sync-command");
|
|
18
|
+
const INPUT_SCHEMA_PATH = getInputSchemaPath(TEST_DIR);
|
|
19
|
+
const DATASET_SCHEMA_PATH = getDatasetSchemaPath(TEST_DIR);
|
|
20
|
+
|
|
21
|
+
describe("sync command", () => {
|
|
22
|
+
beforeAll(() => {
|
|
23
|
+
execSync("npm run build", { stdio: "inherit" });
|
|
24
|
+
});
|
|
25
|
+
afterEach(() => {
|
|
26
|
+
cleanupTestDirectory(TEST_DIR);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it("should sync input schema to JSON Schema and TypeScript types, with default values", async () => {
|
|
30
|
+
setupTestDirectory(TEST_DIR);
|
|
31
|
+
const srcDir = join(TEST_DIR, "src-schemas");
|
|
32
|
+
const srcInputPath = join(srcDir, "input.json");
|
|
33
|
+
const srcDatasetPath = join(srcDir, "dataset-item.json");
|
|
34
|
+
const outputTsDir = join(TEST_DIR, "src", "generated");
|
|
35
|
+
const srcInput: ObjectSchema = {
|
|
36
|
+
title: "Input Schema",
|
|
37
|
+
type: "object",
|
|
38
|
+
properties: {
|
|
39
|
+
name: { type: "string" },
|
|
40
|
+
age: { type: "integer" },
|
|
41
|
+
},
|
|
42
|
+
required: ["name"],
|
|
43
|
+
};
|
|
44
|
+
const srcDataset: ObjectSchema = {
|
|
45
|
+
title: "Dataset Item Schema",
|
|
46
|
+
type: "object",
|
|
47
|
+
properties: {
|
|
48
|
+
id: { type: "string" },
|
|
49
|
+
value: { type: "number" },
|
|
50
|
+
},
|
|
51
|
+
required: ["id"],
|
|
52
|
+
};
|
|
53
|
+
setupTestDirectoryFiles({
|
|
54
|
+
testDirPath: TEST_DIR,
|
|
55
|
+
srcInputPath,
|
|
56
|
+
srcInput,
|
|
57
|
+
srcDatasetPath,
|
|
58
|
+
srcDataset,
|
|
59
|
+
});
|
|
60
|
+
execSync(
|
|
61
|
+
// Using the default options
|
|
62
|
+
`${EXEC_CMD} sync`,
|
|
63
|
+
{
|
|
64
|
+
cwd: TEST_DIR,
|
|
65
|
+
stdio: "inherit",
|
|
66
|
+
},
|
|
67
|
+
);
|
|
68
|
+
|
|
69
|
+
expect(existsSync(join(outputTsDir, "input.ts"))).toBe(true);
|
|
70
|
+
expect(existsSync(join(outputTsDir, "input-utils.ts"))).toBe(true);
|
|
71
|
+
expect(existsSync(INPUT_SCHEMA_PATH)).toBe(true);
|
|
72
|
+
|
|
73
|
+
const generatedInputSchema = readJsonSchema(INPUT_SCHEMA_PATH);
|
|
74
|
+
expect(generatedInputSchema).toEqual({
|
|
75
|
+
title: "Input Schema",
|
|
76
|
+
type: "object",
|
|
77
|
+
properties: {
|
|
78
|
+
name: { type: "string" },
|
|
79
|
+
age: { type: "integer" },
|
|
80
|
+
},
|
|
81
|
+
required: ["name"],
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
const generatedDatasetSchema = readJsonSchema(DATASET_SCHEMA_PATH);
|
|
85
|
+
expect(generatedDatasetSchema).toEqual({
|
|
86
|
+
title: "Dataset Schema",
|
|
87
|
+
fields: {
|
|
88
|
+
title: "Dataset Item Schema",
|
|
89
|
+
type: "object",
|
|
90
|
+
properties: {
|
|
91
|
+
id: { type: "string" },
|
|
92
|
+
value: { type: "number" },
|
|
93
|
+
},
|
|
94
|
+
required: ["id"],
|
|
95
|
+
},
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
it("should sync input schema to JSON Schema and TypeScript types, with custom options", async () => {
|
|
100
|
+
setupTestDirectory(TEST_DIR);
|
|
101
|
+
|
|
102
|
+
const srcDir = join(TEST_DIR, "custom-src");
|
|
103
|
+
const srcInputPath = join(srcDir, "custom-input.json");
|
|
104
|
+
const srcDatasetPath = join(srcDir, "custom-dataset-item.json");
|
|
105
|
+
const outputTsDir = join(TEST_DIR, "src", "custom-generated");
|
|
106
|
+
const addDir = join(TEST_DIR, "add-schemas");
|
|
107
|
+
const addInputPath = join(addDir, "input.json");
|
|
108
|
+
const addDatasetPath = join(addDir, "dataset-item.json");
|
|
109
|
+
const srcInput: ObjectSchema = {
|
|
110
|
+
title: "Input Schema",
|
|
111
|
+
type: "object",
|
|
112
|
+
properties: {
|
|
113
|
+
name: { type: "string" },
|
|
114
|
+
age: { type: "integer" },
|
|
115
|
+
},
|
|
116
|
+
required: ["name"],
|
|
117
|
+
};
|
|
118
|
+
const addInput: ObjectSchema = {
|
|
119
|
+
type: "object",
|
|
120
|
+
properties: {
|
|
121
|
+
email: { type: "string", format: "email" },
|
|
122
|
+
isActive: { type: "boolean" },
|
|
123
|
+
},
|
|
124
|
+
required: ["email"],
|
|
125
|
+
};
|
|
126
|
+
const srcDataset: ObjectSchema = {
|
|
127
|
+
title: "Dataset Item Schema",
|
|
128
|
+
type: "object",
|
|
129
|
+
properties: {
|
|
130
|
+
id: { type: "string" },
|
|
131
|
+
value: { type: "number" },
|
|
132
|
+
},
|
|
133
|
+
required: ["id"],
|
|
134
|
+
};
|
|
135
|
+
const addDataset: ObjectSchema = {
|
|
136
|
+
type: "object",
|
|
137
|
+
properties: {
|
|
138
|
+
description: { type: "string" },
|
|
139
|
+
timestamp: { type: "string", format: "date-time" },
|
|
140
|
+
},
|
|
141
|
+
required: ["description"],
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
setupTestDirectoryFiles({
|
|
145
|
+
testDirPath: TEST_DIR,
|
|
146
|
+
srcInputPath,
|
|
147
|
+
srcInput,
|
|
148
|
+
srcDatasetPath,
|
|
149
|
+
srcDataset,
|
|
150
|
+
addDir,
|
|
151
|
+
addInputPath,
|
|
152
|
+
addInput,
|
|
153
|
+
addDatasetPath,
|
|
154
|
+
addDataset,
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
execSync(
|
|
158
|
+
[
|
|
159
|
+
`${EXEC_CMD} sync`,
|
|
160
|
+
"-i input dataset",
|
|
161
|
+
"-o json-schemas ts-types",
|
|
162
|
+
`--src-input ${srcInputPath}`,
|
|
163
|
+
`--src-dataset ${srcDatasetPath}`,
|
|
164
|
+
`--output-ts-dir ${outputTsDir}`,
|
|
165
|
+
`--add-input ${addInputPath}`,
|
|
166
|
+
`--add-dataset ${addDatasetPath}`,
|
|
167
|
+
"--deep-merge",
|
|
168
|
+
"--include-input-utils false",
|
|
169
|
+
].join(" "),
|
|
170
|
+
{
|
|
171
|
+
cwd: TEST_DIR,
|
|
172
|
+
stdio: "inherit",
|
|
173
|
+
},
|
|
174
|
+
);
|
|
175
|
+
|
|
176
|
+
expect(existsSync(join(outputTsDir, "input.ts"))).toBe(true);
|
|
177
|
+
expect(existsSync(INPUT_SCHEMA_PATH)).toBe(true);
|
|
178
|
+
|
|
179
|
+
// Not generated because we set the option to false
|
|
180
|
+
expect(existsSync(join(outputTsDir, "input-utils.ts"))).toBe(false);
|
|
181
|
+
|
|
182
|
+
const generatedInputSchema = readJsonSchema(INPUT_SCHEMA_PATH);
|
|
183
|
+
expect(generatedInputSchema).toEqual({
|
|
184
|
+
title: "Input Schema",
|
|
185
|
+
type: "object",
|
|
186
|
+
properties: {
|
|
187
|
+
name: { type: "string" },
|
|
188
|
+
age: { type: "integer" },
|
|
189
|
+
email: { type: "string" }, // "format" was removed because it is an invalid property in Apify input schema
|
|
190
|
+
isActive: { type: "boolean" },
|
|
191
|
+
},
|
|
192
|
+
required: ["name", "email"],
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
const generatedDatasetSchema = readJsonSchema(DATASET_SCHEMA_PATH);
|
|
196
|
+
expect(generatedDatasetSchema).toEqual({
|
|
197
|
+
title: "Dataset Schema",
|
|
198
|
+
fields: {
|
|
199
|
+
title: "Dataset Item Schema",
|
|
200
|
+
type: "object",
|
|
201
|
+
properties: {
|
|
202
|
+
id: { type: "string" },
|
|
203
|
+
value: { type: "number" },
|
|
204
|
+
description: { type: "string" },
|
|
205
|
+
timestamp: { type: "string", format: "date-time" },
|
|
206
|
+
},
|
|
207
|
+
required: ["id", "description"],
|
|
208
|
+
},
|
|
209
|
+
});
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
it("throws an error when there is a conflict and the --fail-on-conflict option is used", () => {
|
|
213
|
+
setupTestDirectory(TEST_DIR);
|
|
214
|
+
const srcDir = join(TEST_DIR, "src-schemas");
|
|
215
|
+
const srcInputPath = join(srcDir, "input.json");
|
|
216
|
+
const srcDatasetPath = join(srcDir, "dataset-item.json");
|
|
217
|
+
|
|
218
|
+
// Create source schema with one title
|
|
219
|
+
const srcInput: ObjectSchema = {
|
|
220
|
+
title: "Source Input Schema", // This will conflict with existing schema
|
|
221
|
+
type: "object",
|
|
222
|
+
properties: {
|
|
223
|
+
name: { type: "string" },
|
|
224
|
+
},
|
|
225
|
+
required: ["name"],
|
|
226
|
+
};
|
|
227
|
+
const srcDataset: ObjectSchema = {
|
|
228
|
+
title: "Dataset Item Schema",
|
|
229
|
+
type: "object",
|
|
230
|
+
properties: {
|
|
231
|
+
id: { type: "string" },
|
|
232
|
+
},
|
|
233
|
+
required: ["id"],
|
|
234
|
+
};
|
|
235
|
+
|
|
236
|
+
setupTestDirectoryFiles({
|
|
237
|
+
testDirPath: TEST_DIR,
|
|
238
|
+
srcInputPath,
|
|
239
|
+
srcInput,
|
|
240
|
+
srcDatasetPath,
|
|
241
|
+
srcDataset,
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
// Create existing schema with different title to cause conflict
|
|
245
|
+
const existingInputSchema: ObjectSchema = {
|
|
246
|
+
title: "Existing Input Schema", // Different title causes conflict
|
|
247
|
+
type: "object",
|
|
248
|
+
properties: {
|
|
249
|
+
name: { type: "string" },
|
|
250
|
+
},
|
|
251
|
+
required: ["name"],
|
|
252
|
+
};
|
|
253
|
+
writeJsonSchema(INPUT_SCHEMA_PATH, existingInputSchema);
|
|
254
|
+
|
|
255
|
+
// Should throw an error due to --fail-on-conflict
|
|
256
|
+
expect(() =>
|
|
257
|
+
execSync(
|
|
258
|
+
[
|
|
259
|
+
`${EXEC_CMD} sync`,
|
|
260
|
+
"--fail-on-conflict",
|
|
261
|
+
"-i input",
|
|
262
|
+
"-o json-schemas",
|
|
263
|
+
`--src-input ${srcInputPath}`,
|
|
264
|
+
`--src-dataset ${srcDatasetPath}`,
|
|
265
|
+
].join(" "),
|
|
266
|
+
{
|
|
267
|
+
cwd: TEST_DIR,
|
|
268
|
+
stdio: "inherit",
|
|
269
|
+
},
|
|
270
|
+
),
|
|
271
|
+
).toThrow();
|
|
272
|
+
|
|
273
|
+
// Verify the existing schema was not overwritten
|
|
274
|
+
expect(readJsonSchema(INPUT_SCHEMA_PATH)).toEqual(existingInputSchema);
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
it("logs a warning when there is a conflict and the --force option is used", () => {
|
|
278
|
+
setupTestDirectory(TEST_DIR);
|
|
279
|
+
const srcDir = join(TEST_DIR, "src-schemas");
|
|
280
|
+
const srcInputPath = join(srcDir, "input.json");
|
|
281
|
+
const srcDatasetPath = join(srcDir, "dataset-item.json");
|
|
282
|
+
|
|
283
|
+
// Create source schema with one title
|
|
284
|
+
const srcInput: ObjectSchema = {
|
|
285
|
+
title: "Source Input Schema", // This will conflict with existing schema
|
|
286
|
+
type: "object",
|
|
287
|
+
properties: {
|
|
288
|
+
name: { type: "string" },
|
|
289
|
+
},
|
|
290
|
+
required: ["name"],
|
|
291
|
+
};
|
|
292
|
+
const srcDataset: ObjectSchema = {
|
|
293
|
+
title: "Dataset Item Schema",
|
|
294
|
+
type: "object",
|
|
295
|
+
properties: {
|
|
296
|
+
id: { type: "string" },
|
|
297
|
+
},
|
|
298
|
+
required: ["id"],
|
|
299
|
+
};
|
|
300
|
+
|
|
301
|
+
setupTestDirectoryFiles({
|
|
302
|
+
testDirPath: TEST_DIR,
|
|
303
|
+
srcInputPath,
|
|
304
|
+
srcInput,
|
|
305
|
+
srcDatasetPath,
|
|
306
|
+
srcDataset,
|
|
307
|
+
});
|
|
308
|
+
|
|
309
|
+
// Create existing schema with different title to cause conflict
|
|
310
|
+
const existingInputSchema: ObjectSchema = {
|
|
311
|
+
title: "Existing Input Schema", // Different title causes conflict
|
|
312
|
+
type: "object",
|
|
313
|
+
properties: {
|
|
314
|
+
name: { type: "string" },
|
|
315
|
+
},
|
|
316
|
+
required: ["name"],
|
|
317
|
+
};
|
|
318
|
+
writeJsonSchema(INPUT_SCHEMA_PATH, existingInputSchema);
|
|
319
|
+
|
|
320
|
+
// Should NOT throw an error due to --force, just log warning
|
|
321
|
+
expect(() =>
|
|
322
|
+
execSync(
|
|
323
|
+
[
|
|
324
|
+
`${EXEC_CMD} sync`,
|
|
325
|
+
"--force",
|
|
326
|
+
"-i input",
|
|
327
|
+
"-o json-schemas",
|
|
328
|
+
`--src-input ${srcInputPath}`,
|
|
329
|
+
`--src-dataset ${srcDatasetPath}`,
|
|
330
|
+
].join(" "),
|
|
331
|
+
{
|
|
332
|
+
cwd: TEST_DIR,
|
|
333
|
+
stdio: "inherit",
|
|
334
|
+
},
|
|
335
|
+
),
|
|
336
|
+
).not.toThrow();
|
|
337
|
+
|
|
338
|
+
// Verify the existing schema was overwritten
|
|
339
|
+
expect(readJsonSchema(INPUT_SCHEMA_PATH)).toEqual(srcInput);
|
|
340
|
+
});
|
|
341
|
+
});
|
package/test/common.ts
CHANGED
|
@@ -1,10 +1,25 @@
|
|
|
1
1
|
import { existsSync, mkdirSync, rmSync } from "node:fs";
|
|
2
2
|
import { join } from "node:path";
|
|
3
|
+
import type { JSONSchema4 } from "json-schema";
|
|
4
|
+
import { writeFile } from "../src/utils/filesystem.js";
|
|
5
|
+
import { type ObjectSchema, writeJsonSchema } from "../src/utils/json-schemas.js";
|
|
3
6
|
|
|
4
7
|
export function getTestDir(suffix: string): string {
|
|
5
8
|
return join(__dirname, `test-temp-${suffix}`);
|
|
6
9
|
}
|
|
7
10
|
|
|
11
|
+
export function getActorDir(testDirPath: string): string {
|
|
12
|
+
return join(testDirPath, ".actor");
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function getInputSchemaPath(testDirPath: string): string {
|
|
16
|
+
return join(getActorDir(testDirPath), "input_schema.json");
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function getDatasetSchemaPath(testDirPath: string): string {
|
|
20
|
+
return join(getActorDir(testDirPath), "dataset_schema.json");
|
|
21
|
+
}
|
|
22
|
+
|
|
8
23
|
export function setupTestDirectory(dirPath: string): void {
|
|
9
24
|
// Create directory if it doesn't exist, do nothing if it already exists
|
|
10
25
|
if (!existsSync(dirPath)) {
|
|
@@ -12,6 +27,59 @@ export function setupTestDirectory(dirPath: string): void {
|
|
|
12
27
|
}
|
|
13
28
|
}
|
|
14
29
|
|
|
30
|
+
type SetupTestDirectoryFilesOptions = {
|
|
31
|
+
testDirPath: string;
|
|
32
|
+
srcInputPath: string;
|
|
33
|
+
srcInput: ObjectSchema;
|
|
34
|
+
srcDatasetPath: string;
|
|
35
|
+
srcDataset: ObjectSchema;
|
|
36
|
+
addDir?: string;
|
|
37
|
+
addInputPath?: string;
|
|
38
|
+
addInput?: ObjectSchema;
|
|
39
|
+
addDatasetPath?: string;
|
|
40
|
+
addDataset?: ObjectSchema;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
export function setupTestDirectoryFiles({
|
|
44
|
+
testDirPath,
|
|
45
|
+
srcInputPath,
|
|
46
|
+
srcInput,
|
|
47
|
+
srcDatasetPath,
|
|
48
|
+
srcDataset,
|
|
49
|
+
addDir,
|
|
50
|
+
addInputPath,
|
|
51
|
+
addInput,
|
|
52
|
+
addDatasetPath,
|
|
53
|
+
addDataset,
|
|
54
|
+
}: SetupTestDirectoryFilesOptions): void {
|
|
55
|
+
const inputSchemaPath = getInputSchemaPath(testDirPath);
|
|
56
|
+
const datasetSchemaPath = getDatasetSchemaPath(testDirPath);
|
|
57
|
+
const baseInputSchema: JSONSchema4 = {
|
|
58
|
+
title: "Input Schema",
|
|
59
|
+
type: "object",
|
|
60
|
+
properties: {},
|
|
61
|
+
};
|
|
62
|
+
writeFile(inputSchemaPath, JSON.stringify(baseInputSchema, null, 4));
|
|
63
|
+
|
|
64
|
+
const datasetSchemaWrapper: JSONSchema4 = {
|
|
65
|
+
title: "Dataset Schema",
|
|
66
|
+
fields: {},
|
|
67
|
+
};
|
|
68
|
+
writeFile(datasetSchemaPath, JSON.stringify(datasetSchemaWrapper, null, 4));
|
|
69
|
+
|
|
70
|
+
writeJsonSchema(srcInputPath, srcInput);
|
|
71
|
+
writeJsonSchema(srcDatasetPath, srcDataset);
|
|
72
|
+
|
|
73
|
+
if (addDir) {
|
|
74
|
+
if (addInputPath && addInput) {
|
|
75
|
+
writeJsonSchema(addInputPath, addInput);
|
|
76
|
+
}
|
|
77
|
+
if (addDatasetPath && addDataset) {
|
|
78
|
+
writeJsonSchema(addDatasetPath, addDataset);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
15
83
|
export function cleanupTestDirectory(dirPath: string): void {
|
|
16
84
|
if (existsSync(dirPath)) {
|
|
17
85
|
rmSync(dirPath, { recursive: true, force: true });
|
|
@@ -3,8 +3,8 @@ import type { ArgumentParser } from "argparse";
|
|
|
3
3
|
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
4
4
|
import {
|
|
5
5
|
addCommonCliArgs,
|
|
6
|
+
CONFIGURATION_SCHEMA,
|
|
6
7
|
type CommonCliArgs,
|
|
7
|
-
Configuration,
|
|
8
8
|
parseConfigurationFromFileOrDefault,
|
|
9
9
|
writeConfigurationToPackageJson,
|
|
10
10
|
} from "../src/configuration.js";
|
|
@@ -16,14 +16,14 @@ vi.mock("node:fs", () => ({
|
|
|
16
16
|
rmSync: vi.fn(),
|
|
17
17
|
}));
|
|
18
18
|
|
|
19
|
-
vi.mock("../src/filesystem.js", () => ({
|
|
19
|
+
vi.mock("../src/utils/filesystem.js", () => ({
|
|
20
20
|
readFile: vi.fn(),
|
|
21
21
|
writeFile: vi.fn(),
|
|
22
22
|
}));
|
|
23
23
|
|
|
24
24
|
const mockExistsSync = vi.mocked(existsSync);
|
|
25
|
-
const mockReadFile = vi.mocked(await import("../src/filesystem.js")).readFile;
|
|
26
|
-
const mockWriteFile = vi.mocked(await import("../src/filesystem.js")).writeFile;
|
|
25
|
+
const mockReadFile = vi.mocked(await import("../src/utils/filesystem.js")).readFile;
|
|
26
|
+
const mockWriteFile = vi.mocked(await import("../src/utils/filesystem.js")).writeFile;
|
|
27
27
|
|
|
28
28
|
describe("parseConfigurationFromFileOrDefault", () => {
|
|
29
29
|
beforeEach(() => {
|
|
@@ -203,7 +203,7 @@ describe("addCommonCLIArgs", () => {
|
|
|
203
203
|
});
|
|
204
204
|
|
|
205
205
|
it("should add all CLI arguments with default configuration", () => {
|
|
206
|
-
const defaultConfig =
|
|
206
|
+
const defaultConfig = CONFIGURATION_SCHEMA.parse({});
|
|
207
207
|
|
|
208
208
|
addCommonCliArgs(parser, defaultConfig);
|
|
209
209
|
|
|
@@ -276,7 +276,7 @@ describe("addCommonCLIArgs", () => {
|
|
|
276
276
|
});
|
|
277
277
|
|
|
278
278
|
it("should add CLI arguments with custom configuration", () => {
|
|
279
|
-
const customConfig =
|
|
279
|
+
const customConfig = CONFIGURATION_SCHEMA.parse({
|
|
280
280
|
input: ["input"],
|
|
281
281
|
output: ["ts-types"],
|
|
282
282
|
srcInput: "custom-input.json",
|
|
@@ -331,7 +331,7 @@ describe("addCommonCLIArgs", () => {
|
|
|
331
331
|
});
|
|
332
332
|
|
|
333
333
|
it("should preserve argument order", () => {
|
|
334
|
-
const config =
|
|
334
|
+
const config = CONFIGURATION_SCHEMA.parse({});
|
|
335
335
|
|
|
336
336
|
addCommonCliArgs(parser, config);
|
|
337
337
|
|
|
@@ -349,7 +349,7 @@ describe("addCommonCLIArgs", () => {
|
|
|
349
349
|
});
|
|
350
350
|
|
|
351
351
|
it("should handle empty arrays in configuration", () => {
|
|
352
|
-
const configWithEmptyArrays =
|
|
352
|
+
const configWithEmptyArrays = CONFIGURATION_SCHEMA.parse({
|
|
353
353
|
input: [],
|
|
354
354
|
output: [],
|
|
355
355
|
});
|