-
-
Notifications
You must be signed in to change notification settings - Fork 149
Expand file tree
/
Copy pathgenerate.test.ts
More file actions
372 lines (330 loc) · 14.5 KB
/
Copy pathgenerate.test.ts
File metadata and controls
372 lines (330 loc) · 14.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
import { formatDocument } from '@zenstackhq/language';
import fs from 'node:fs';
import path from 'node:path';
import { describe, expect, it } from 'vitest';
import { createProject, getDefaultPrelude, runCli } from './utils';
const model = `
model User {
id String @id @default(cuid())
}
`;
describe('CLI generate command test', () => {
it('should generate a TypeScript schema', async () => {
const { workDir } = await createProject(model);
runCli('generate', workDir);
expect(fs.existsSync(path.join(workDir, 'zenstack/schema.ts'))).toBe(true);
expect(fs.existsSync(path.join(workDir, 'zenstack/schema.prisma'))).toBe(false);
});
it('should respect custom output directory', async () => {
const { workDir } = await createProject(model);
runCli('generate --output ./zen', workDir);
expect(fs.existsSync(path.join(workDir, 'zen/schema.ts'))).toBe(true);
});
it('should respect custom schema location', async () => {
const { workDir } = await createProject(model);
fs.renameSync(path.join(workDir, 'zenstack/schema.zmodel'), path.join(workDir, 'zenstack/foo.zmodel'));
runCli('generate --schema ./zenstack/foo.zmodel', workDir);
expect(fs.existsSync(path.join(workDir, 'zenstack/schema.ts'))).toBe(true);
});
it('should respect package.json config', async () => {
const { workDir } = await createProject(model);
fs.mkdirSync(path.join(workDir, 'foo'));
fs.renameSync(path.join(workDir, 'zenstack/schema.zmodel'), path.join(workDir, 'foo/schema.zmodel'));
fs.rmdirSync(path.join(workDir, 'zenstack'));
const pkgJson = JSON.parse(fs.readFileSync(path.join(workDir, 'package.json'), 'utf8'));
pkgJson.zenstack = {
schema: './foo/schema.zmodel',
output: './bar',
};
fs.writeFileSync(path.join(workDir, 'package.json'), JSON.stringify(pkgJson, null, 2));
runCli('generate', workDir);
expect(fs.existsSync(path.join(workDir, 'bar/schema.ts'))).toBe(true);
});
it('should respect package.json schema dir config', async () => {
const { workDir } = await createProject(model);
fs.mkdirSync(path.join(workDir, 'foo'));
fs.renameSync(path.join(workDir, 'zenstack/schema.zmodel'), path.join(workDir, 'foo/schema.zmodel'));
fs.rmdirSync(path.join(workDir, 'zenstack'));
const pkgJson = JSON.parse(fs.readFileSync(path.join(workDir, 'package.json'), 'utf8'));
pkgJson.zenstack = {
schema: './foo',
output: './bar',
};
fs.writeFileSync(path.join(workDir, 'package.json'), JSON.stringify(pkgJson, null, 2));
runCli('generate', workDir);
expect(fs.existsSync(path.join(workDir, 'bar/schema.ts'))).toBe(true);
});
it('should respect plugin lite options', async () => {
const modelWithPlugin = `
plugin typescript {
provider = "@core/typescript"
lite = true
}
model User {
id String @id @default(cuid())
}
`;
const { workDir } = await createProject(modelWithPlugin);
runCli('generate', workDir);
expect(fs.existsSync(path.join(workDir, 'zenstack/schema.ts'))).toBe(true);
expect(fs.existsSync(path.join(workDir, 'zenstack/schema-lite.ts'))).toBe(true);
});
it('should respect plugin lite-only options', async () => {
const modelWithPlugin = `
plugin typescript {
provider = "@core/typescript"
liteOnly = true
}
model User {
id String @id @default(cuid())
}
`;
const { workDir } = await createProject(modelWithPlugin);
runCli('generate', workDir);
expect(fs.existsSync(path.join(workDir, 'zenstack/schema.ts'))).toBe(false);
expect(fs.existsSync(path.join(workDir, 'zenstack/schema-lite.ts'))).toBe(true);
});
it('should respect lite option', async () => {
const { workDir } = await createProject(model);
runCli('generate --lite', workDir);
expect(fs.existsSync(path.join(workDir, 'zenstack/schema.ts'))).toBe(true);
expect(fs.existsSync(path.join(workDir, 'zenstack/schema-lite.ts'))).toBe(true);
});
it('should respect liteOnly option', async () => {
const { workDir } = await createProject(model);
runCli('generate --lite-only', workDir);
expect(fs.existsSync(path.join(workDir, 'zenstack/schema.ts'))).toBe(false);
expect(fs.existsSync(path.join(workDir, 'zenstack/schema-lite.ts'))).toBe(true);
});
it('should respect explicit liteOnly true option', async () => {
const { workDir } = await createProject(model);
runCli('generate --lite-only=true', workDir);
expect(fs.existsSync(path.join(workDir, 'zenstack/schema.ts'))).toBe(false);
expect(fs.existsSync(path.join(workDir, 'zenstack/schema-lite.ts'))).toBe(true);
});
it('should respect explicit liteOnly false option', async () => {
const { workDir } = await createProject(model);
runCli('generate --lite-only=false', workDir);
expect(fs.existsSync(path.join(workDir, 'zenstack/schema.ts'))).toBe(true);
expect(fs.existsSync(path.join(workDir, 'zenstack/schema-lite.ts'))).toBe(false);
});
it('should prefer CLI options over @core/typescript plugin settings for lite and liteOnly', async () => {
const modelWithPlugin = `
plugin typescript {
provider = "@core/typescript"
lite = true
liteOnly = true
}
model User {
id String @id @default(cuid())
}
`;
const { workDir } = await createProject(modelWithPlugin);
runCli('generate --lite=false --lite-only=false', workDir);
expect(fs.existsSync(path.join(workDir, 'zenstack/schema.ts'))).toBe(true);
expect(fs.existsSync(path.join(workDir, 'zenstack/schema-lite.ts'))).toBe(false);
});
it('should generate models.ts and input.ts by default', async () => {
const { workDir } = await createProject(model);
runCli('generate', workDir);
expect(fs.existsSync(path.join(workDir, 'zenstack/schema.ts'))).toBe(true);
expect(fs.existsSync(path.join(workDir, 'zenstack/models.ts'))).toBe(true);
expect(fs.existsSync(path.join(workDir, 'zenstack/input.ts'))).toBe(true);
});
it('should respect plugin options for generateModels and generateInput by default', async () => {
const modelWithPlugin = `
plugin typescript {
provider = "@core/typescript"
generateModels = false
generateInput = false
}
model User {
id String @id @default(cuid())
}
`;
const { workDir } = await createProject(modelWithPlugin);
runCli('generate', workDir);
expect(fs.existsSync(path.join(workDir, 'zenstack/schema.ts'))).toBe(true);
expect(fs.existsSync(path.join(workDir, 'zenstack/models.ts'))).toBe(false);
expect(fs.existsSync(path.join(workDir, 'zenstack/input.ts'))).toBe(false);
});
it('should generate models.ts when --generate-models=true is passed', async () => {
const { workDir } = await createProject(model);
runCli('generate --generate-models=true', workDir);
expect(fs.existsSync(path.join(workDir, 'zenstack/schema.ts'))).toBe(true);
expect(fs.existsSync(path.join(workDir, 'zenstack/models.ts'))).toBe(true);
expect(fs.existsSync(path.join(workDir, 'zenstack/input.ts'))).toBe(true);
});
it('should not generate models.ts when --generate-models=false is passed', async () => {
const { workDir } = await createProject(model);
runCli('generate --generate-models=false', workDir);
expect(fs.existsSync(path.join(workDir, 'zenstack/schema.ts'))).toBe(true);
expect(fs.existsSync(path.join(workDir, 'zenstack/models.ts'))).toBe(false);
expect(fs.existsSync(path.join(workDir, 'zenstack/input.ts'))).toBe(true);
});
it('should generate input.ts when --generate-input=true is passed', async () => {
const { workDir } = await createProject(model);
runCli('generate --generate-input=true', workDir);
expect(fs.existsSync(path.join(workDir, 'zenstack/schema.ts'))).toBe(true);
expect(fs.existsSync(path.join(workDir, 'zenstack/models.ts'))).toBe(true);
expect(fs.existsSync(path.join(workDir, 'zenstack/input.ts'))).toBe(true);
});
it('should not generate input.ts when --generate-input=false is passed', async () => {
const { workDir } = await createProject(model);
runCli('generate --generate-input=false', workDir);
expect(fs.existsSync(path.join(workDir, 'zenstack/schema.ts'))).toBe(true);
expect(fs.existsSync(path.join(workDir, 'zenstack/models.ts'))).toBe(true);
expect(fs.existsSync(path.join(workDir, 'zenstack/input.ts'))).toBe(false);
});
it('should report error for unresolvable plugin module', async () => {
const modelWithMissingPlugin = `
plugin foo {
provider = '@zenstackhq/nonexistent-plugin'
}
model User {
id String @id @default(cuid())
}
`;
const { workDir } = await createProject(modelWithMissingPlugin);
expect(() => runCli('generate', workDir)).toThrow(/Cannot find plugin module/);
});
it('should succeed when plugin module exists but has no CLI generator', async () => {
const modelWithNoGeneratorPlugin = `
plugin foo {
provider = './my-plugin.mjs'
}
model User {
id String @id @default(cuid())
}
`;
const { workDir } = await createProject(modelWithNoGeneratorPlugin);
// Create a plugin module that doesn't export a default CLI generator
fs.writeFileSync(path.join(workDir, 'zenstack/my-plugin.mjs'), 'export const name = "no-generator";');
runCli('generate', workDir);
// Should succeed without error, generating the default typescript output
expect(fs.existsSync(path.join(workDir, 'zenstack/schema.ts'))).toBe(true);
});
it('should succeed when plugin only provides a plugin.zmodel for custom attributes', async () => {
const modelWithZmodelOnlyPlugin = `
plugin myPlugin {
provider = './my-plugin'
}
model User {
id String @id @default(cuid())
@@custom
}
`;
const { workDir } = await createProject(modelWithZmodelOnlyPlugin);
// Create a plugin directory with index.mjs (no default export) and a plugin.zmodel defining a custom attribute
const pluginDir = path.join(workDir, 'zenstack/my-plugin');
fs.mkdirSync(pluginDir, { recursive: true });
fs.writeFileSync(path.join(pluginDir, 'index.mjs'), 'export const name = "my-plugin";');
fs.writeFileSync(path.join(pluginDir, 'plugin.zmodel'), 'attribute @@custom()');
runCli('generate', workDir);
// Should succeed without error, generating the default typescript output
expect(fs.existsSync(path.join(workDir, 'zenstack/schema.ts'))).toBe(true);
});
it('should succeed when plugin provider is a .zmodel file', async () => {
const modelWithZmodelProvider = `
plugin myPlugin {
provider = './custom-attrs/plugin.zmodel'
}
model User {
id String @id @default(cuid())
@@custom
}
`;
const { workDir } = await createProject(modelWithZmodelProvider);
const pluginDir = path.join(workDir, 'zenstack/custom-attrs');
fs.mkdirSync(pluginDir, { recursive: true });
fs.writeFileSync(path.join(pluginDir, 'plugin.zmodel'), 'attribute @@custom()');
runCli('generate', workDir);
expect(fs.existsSync(path.join(workDir, 'zenstack/schema.ts'))).toBe(true);
});
it('should load plugin from a bare package specifier via jiti', async () => {
const modelWithBarePlugin = `
plugin foo {
provider = 'my-test-plugin'
}
model User {
id String @id @default(cuid())
}
`;
const { workDir } = await createProject(modelWithBarePlugin);
// Create a fake node_modules package with a TS entry point
// This can only be resolved by jiti, not by native import() or fs.existsSync checks
const pkgDir = path.join(workDir, 'node_modules/my-test-plugin');
fs.mkdirSync(pkgDir, { recursive: true });
fs.writeFileSync(
path.join(pkgDir, 'package.json'),
JSON.stringify({ name: 'my-test-plugin', main: './index.ts' }),
);
fs.writeFileSync(
path.join(pkgDir, 'index.ts'),
`
const plugin = {
name: 'test-bare-plugin',
statusText: 'Testing bare plugin',
async generate() {},
};
export default plugin;
`,
);
runCli('generate', workDir);
expect(fs.existsSync(path.join(workDir, 'zenstack/schema.ts'))).toBe(true);
});
it('should resolve plugin paths relative to the schema file where the plugin is declared', async () => {
// Entry schema imports a sub-schema that declares a plugin with a relative path.
// The plugin path should resolve relative to the sub-schema, not the entry schema.
const { workDir } = await createProject(
`import './core/core'
${getDefaultPrelude()}
model User {
id String @id @default(cuid())
}
`,
{ customPrelude: true },
);
// Create core/ subdirectory with its own schema and plugin
const coreDir = path.join(workDir, 'zenstack/core');
fs.mkdirSync(coreDir, { recursive: true });
const coreSchema = await formatDocument(`
plugin foo {
provider = './my-core-plugin.ts'
}
`);
fs.writeFileSync(path.join(coreDir, 'core.zmodel'), coreSchema);
// Plugin lives next to the core schema, NOT next to the entry schema
fs.writeFileSync(
path.join(coreDir, 'my-core-plugin.ts'),
`
const plugin = {
name: 'core-plugin',
statusText: 'Testing core plugin',
async generate() {},
};
export default plugin;
`,
);
// This would fail if the plugin path was resolved relative to the entry schema
runCli('generate', workDir);
expect(fs.existsSync(path.join(workDir, 'zenstack/schema.ts'))).toBe(true);
});
it('should prefer CLI options over @core/typescript plugin settings for generateModels and generateInput', async () => {
const modelWithPlugin = `
plugin typescript {
provider = "@core/typescript"
generateModels = false
generateInput = false
}
model User {
id String @id @default(cuid())
}
`;
const { workDir } = await createProject(modelWithPlugin);
runCli('generate --generate-models --generate-input', workDir);
expect(fs.existsSync(path.join(workDir, 'zenstack/schema.ts'))).toBe(true);
expect(fs.existsSync(path.join(workDir, 'zenstack/models.ts'))).toBe(true);
expect(fs.existsSync(path.join(workDir, 'zenstack/input.ts'))).toBe(true);
});
});