@zenstackhq/testtools 3.0.0-alpha.9 → 3.0.0-beta.10

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/dist/index.cjs CHANGED
@@ -31,17 +31,38 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
31
31
  // src/index.ts
32
32
  var src_exports = {};
33
33
  __export(src_exports, {
34
+ createPolicyTestClient: () => createPolicyTestClient,
35
+ createTestClient: () => createTestClient,
34
36
  createTestProject: () => createTestProject,
35
37
  generateTsSchema: () => generateTsSchema,
36
- generateTsSchemaFromFile: () => generateTsSchemaFromFile
38
+ generateTsSchemaFromFile: () => generateTsSchemaFromFile,
39
+ generateTsSchemaInPlace: () => generateTsSchemaInPlace,
40
+ getTestDbProvider: () => getTestDbProvider,
41
+ loadSchema: () => loadSchema,
42
+ loadSchemaWithError: () => loadSchemaWithError,
43
+ testLogger: () => testLogger
37
44
  });
38
45
  module.exports = __toCommonJS(src_exports);
39
46
 
47
+ // src/client.ts
48
+ var import_common_helpers2 = require("@zenstackhq/common-helpers");
49
+ var import_plugin_policy = require("@zenstackhq/plugin-policy");
50
+ var import_runtime4 = require("@zenstackhq/runtime");
51
+ var import_sdk2 = require("@zenstackhq/sdk");
52
+ var import_better_sqlite3 = __toESM(require("better-sqlite3"), 1);
53
+ var import_kysely = require("kysely");
54
+ var import_node_child_process2 = require("child_process");
55
+ var import_node_crypto2 = require("crypto");
56
+ var import_node_fs3 = __toESM(require("fs"), 1);
57
+ var import_node_path3 = __toESM(require("path"), 1);
58
+ var import_pg = require("pg");
59
+ var import_vitest2 = require("vitest");
60
+
40
61
  // src/project.ts
41
62
  var import_node_fs = __toESM(require("fs"), 1);
42
63
  var import_node_path = __toESM(require("path"), 1);
43
64
  var import_tmp = __toESM(require("tmp"), 1);
44
- function createTestProject() {
65
+ function createTestProject(zmodelContent) {
45
66
  const { name: workDir } = import_tmp.default.dirSync({
46
67
  unsafeCleanup: true
47
68
  });
@@ -81,56 +102,82 @@ function createTestProject() {
81
102
  "**/*.ts"
82
103
  ]
83
104
  }, null, 4));
105
+ if (zmodelContent) {
106
+ import_node_fs.default.writeFileSync(import_node_path.default.join(workDir, "schema.zmodel"), zmodelContent);
107
+ }
84
108
  return workDir;
85
109
  }
86
110
  __name(createTestProject, "createTestProject");
87
111
 
88
112
  // src/schema.ts
113
+ var import_common_helpers = require("@zenstackhq/common-helpers");
89
114
  var import_sdk = require("@zenstackhq/sdk");
90
- var import_glob = require("glob");
91
115
  var import_node_child_process = require("child_process");
116
+ var import_node_crypto = __toESM(require("crypto"), 1);
92
117
  var import_node_fs2 = __toESM(require("fs"), 1);
118
+ var import_node_os = __toESM(require("os"), 1);
93
119
  var import_node_path2 = __toESM(require("path"), 1);
94
120
  var import_ts_pattern = require("ts-pattern");
95
- function makePrelude(provider, dbName) {
121
+ var import_vitest = require("vitest");
122
+
123
+ // src/utils.ts
124
+ var import_language = require("@zenstackhq/language");
125
+ function loadDocumentWithPlugins(filePath) {
126
+ const pluginModelFiles = [
127
+ require.resolve("@zenstackhq/plugin-policy/plugin.zmodel")
128
+ ];
129
+ return (0, import_language.loadDocument)(filePath, pluginModelFiles);
130
+ }
131
+ __name(loadDocumentWithPlugins, "loadDocumentWithPlugins");
132
+
133
+ // src/schema.ts
134
+ function makePrelude(provider, dbUrl) {
96
135
  return (0, import_ts_pattern.match)(provider).with("sqlite", () => {
97
136
  return `
98
137
  datasource db {
99
138
  provider = 'sqlite'
100
- url = '${dbName ?? ":memory:"}'
139
+ url = '${dbUrl ?? "file:./test.db"}'
101
140
  }
102
141
  `;
103
142
  }).with("postgresql", () => {
104
143
  return `
105
144
  datasource db {
106
145
  provider = 'postgresql'
107
- url = 'postgres://postgres:postgres@localhost:5432/${dbName}'
146
+ url = '${dbUrl ?? "postgres://postgres:postgres@localhost:5432/db"}'
108
147
  }
109
148
  `;
110
149
  }).exhaustive();
111
150
  }
112
151
  __name(makePrelude, "makePrelude");
113
- async function generateTsSchema(schemaText, provider = "sqlite", dbName, extraSourceFiles) {
152
+ async function generateTsSchema(schemaText, provider = "sqlite", dbUrl, extraSourceFiles) {
114
153
  const workDir = createTestProject();
115
- console.log(`Work directory: ${workDir}`);
116
154
  const zmodelPath = import_node_path2.default.join(workDir, "schema.zmodel");
117
155
  const noPrelude = schemaText.includes("datasource ");
118
- import_node_fs2.default.writeFileSync(zmodelPath, `${noPrelude ? "" : makePrelude(provider, dbName)}
156
+ import_node_fs2.default.writeFileSync(zmodelPath, `${noPrelude ? "" : makePrelude(provider, dbUrl)}
119
157
 
120
158
  ${schemaText}`);
121
- const pluginModelFiles = import_glob.glob.sync(import_node_path2.default.resolve(__dirname, "../../runtime/src/plugins/**/plugin.zmodel"));
159
+ const result = await loadDocumentWithPlugins(zmodelPath);
160
+ if (!result.success) {
161
+ throw new Error(`Failed to load schema from ${zmodelPath}: ${result.errors}`);
162
+ }
122
163
  const generator = new import_sdk.TsSchemaGenerator();
123
- const tsPath = import_node_path2.default.join(workDir, "schema.ts");
124
- await generator.generate(zmodelPath, pluginModelFiles, tsPath);
164
+ await generator.generate(result.model, workDir);
125
165
  if (extraSourceFiles) {
126
166
  for (const [fileName, content] of Object.entries(extraSourceFiles)) {
127
- const filePath = import_node_path2.default.resolve(workDir, `${fileName}.ts`);
167
+ const filePath = import_node_path2.default.resolve(workDir, !fileName.endsWith(".ts") ? `${fileName}.ts` : fileName);
128
168
  import_node_fs2.default.mkdirSync(import_node_path2.default.dirname(filePath), {
129
169
  recursive: true
130
170
  });
131
171
  import_node_fs2.default.writeFileSync(filePath, content);
132
172
  }
133
173
  }
174
+ return {
175
+ ...await compileAndLoad(workDir),
176
+ model: result.model
177
+ };
178
+ }
179
+ __name(generateTsSchema, "generateTsSchema");
180
+ async function compileAndLoad(workDir) {
134
181
  (0, import_node_child_process.execSync)("npx tsc", {
135
182
  cwd: workDir,
136
183
  stdio: "inherit"
@@ -141,16 +188,360 @@ ${schemaText}`);
141
188
  schema: module2.schema
142
189
  };
143
190
  }
144
- __name(generateTsSchema, "generateTsSchema");
191
+ __name(compileAndLoad, "compileAndLoad");
145
192
  function generateTsSchemaFromFile(filePath) {
146
193
  const schemaText = import_node_fs2.default.readFileSync(filePath, "utf8");
147
194
  return generateTsSchema(schemaText);
148
195
  }
149
196
  __name(generateTsSchemaFromFile, "generateTsSchemaFromFile");
197
+ async function generateTsSchemaInPlace(schemaPath) {
198
+ const workDir = import_node_path2.default.dirname(schemaPath);
199
+ const result = await loadDocumentWithPlugins(schemaPath);
200
+ if (!result.success) {
201
+ throw new Error(`Failed to load schema from ${schemaPath}: ${result.errors}`);
202
+ }
203
+ const generator = new import_sdk.TsSchemaGenerator();
204
+ await generator.generate(result.model, workDir);
205
+ return compileAndLoad(workDir);
206
+ }
207
+ __name(generateTsSchemaInPlace, "generateTsSchemaInPlace");
208
+ async function loadSchema(schema, additionalSchemas) {
209
+ if (!schema.includes("datasource ")) {
210
+ schema = `${makePrelude("sqlite")}
211
+
212
+ ${schema}`;
213
+ }
214
+ const tempDir = import_node_fs2.default.mkdtempSync(import_node_path2.default.join(import_node_os.default.tmpdir(), "zenstack-schema"));
215
+ const tempFile = import_node_path2.default.join(tempDir, `schema.zmodel`);
216
+ import_node_fs2.default.writeFileSync(tempFile, schema);
217
+ if (additionalSchemas) {
218
+ for (const [fileName, content] of Object.entries(additionalSchemas)) {
219
+ let name = fileName;
220
+ if (!name.endsWith(".zmodel")) {
221
+ name += ".zmodel";
222
+ }
223
+ const filePath = import_node_path2.default.join(tempDir, name);
224
+ import_node_fs2.default.writeFileSync(filePath, content);
225
+ }
226
+ }
227
+ const r = await loadDocumentWithPlugins(tempFile);
228
+ (0, import_vitest.expect)(r).toSatisfy((r2) => r2.success, `Failed to load schema: ${r.errors?.map((e) => e.toString()).join(", ")}`);
229
+ (0, import_common_helpers.invariant)(r.success);
230
+ return r.model;
231
+ }
232
+ __name(loadSchema, "loadSchema");
233
+ async function loadSchemaWithError(schema, error) {
234
+ if (!schema.includes("datasource ")) {
235
+ schema = `${makePrelude("sqlite")}
236
+
237
+ ${schema}`;
238
+ }
239
+ const tempFile = import_node_path2.default.join(import_node_os.default.tmpdir(), `zenstack-schema-${import_node_crypto.default.randomUUID()}.zmodel`);
240
+ import_node_fs2.default.writeFileSync(tempFile, schema);
241
+ const r = await loadDocumentWithPlugins(tempFile);
242
+ (0, import_vitest.expect)(r.success).toBe(false);
243
+ (0, import_common_helpers.invariant)(!r.success);
244
+ if (typeof error === "string") {
245
+ (0, import_vitest.expect)(r).toSatisfy((r2) => r2.errors.some((e) => e.toString().toLowerCase().includes(error.toLowerCase())), `Expected error message to include "${error}" but got: ${r.errors.map((e) => e.toString()).join(", ")}`);
246
+ } else {
247
+ (0, import_vitest.expect)(r).toSatisfy((r2) => r2.errors.some((e) => error.test(e)), `Expected error message to match "${error}" but got: ${r.errors.map((e) => e.toString()).join(", ")}`);
248
+ }
249
+ }
250
+ __name(loadSchemaWithError, "loadSchemaWithError");
251
+
252
+ // src/client.ts
253
+ function getTestDbProvider() {
254
+ const val = process.env["TEST_DB_PROVIDER"] ?? "sqlite";
255
+ if (![
256
+ "sqlite",
257
+ "postgresql"
258
+ ].includes(val)) {
259
+ throw new Error(`Invalid TEST_DB_PROVIDER value: ${val}`);
260
+ }
261
+ return val;
262
+ }
263
+ __name(getTestDbProvider, "getTestDbProvider");
264
+ var TEST_PG_CONFIG = {
265
+ host: process.env["TEST_PG_HOST"] ?? "localhost",
266
+ port: process.env["TEST_PG_PORT"] ? parseInt(process.env["TEST_PG_PORT"]) : 5432,
267
+ user: process.env["TEST_PG_USER"] ?? "postgres",
268
+ password: process.env["TEST_PG_PASSWORD"] ?? "postgres"
269
+ };
270
+ async function createTestClient(schema, options, schemaFile) {
271
+ let workDir = options?.workDir;
272
+ let _schema;
273
+ const provider = options?.provider ?? getTestDbProvider() ?? "sqlite";
274
+ const dbName = options?.dbName ?? getTestDbName(provider);
275
+ const dbUrl = provider === "sqlite" ? `file:${dbName}` : `postgres://${TEST_PG_CONFIG.user}:${TEST_PG_CONFIG.password}@${TEST_PG_CONFIG.host}:${TEST_PG_CONFIG.port}/${dbName}`;
276
+ let model;
277
+ if (typeof schema === "string") {
278
+ const generated = await generateTsSchema(schema, provider, dbUrl, options?.extraSourceFiles);
279
+ workDir = generated.workDir;
280
+ model = generated.model;
281
+ _schema = {
282
+ ...generated.schema,
283
+ provider: {
284
+ type: provider
285
+ }
286
+ };
287
+ } else {
288
+ _schema = {
289
+ ...schema,
290
+ provider: {
291
+ type: provider
292
+ }
293
+ };
294
+ workDir ??= createTestProject();
295
+ if (schemaFile) {
296
+ let schemaContent = import_node_fs3.default.readFileSync(schemaFile, "utf-8");
297
+ if (dbUrl) {
298
+ schemaContent = schemaContent.replace(/datasource\s+db\s*{[^}]*}/m, `datasource db {
299
+ provider = '${provider}'
300
+ url = '${dbUrl}'
301
+ }`);
302
+ }
303
+ import_node_fs3.default.writeFileSync(import_node_path3.default.join(workDir, "schema.zmodel"), schemaContent);
304
+ }
305
+ }
306
+ (0, import_common_helpers2.invariant)(workDir);
307
+ if (options?.debug) {
308
+ console.log(`Work directory: ${workDir}`);
309
+ }
310
+ const { plugins, ...rest } = options ?? {};
311
+ const _options = {
312
+ ...rest
313
+ };
314
+ if (options?.usePrismaPush) {
315
+ (0, import_common_helpers2.invariant)(typeof schema === "string" || schemaFile, "a schema file must be provided when using prisma db push");
316
+ if (!model) {
317
+ const r = await loadDocumentWithPlugins(import_node_path3.default.join(workDir, "schema.zmodel"));
318
+ if (!r.success) {
319
+ throw new Error(r.errors.join("\n"));
320
+ }
321
+ model = r.model;
322
+ }
323
+ const prismaSchema = new import_sdk2.PrismaSchemaGenerator(model);
324
+ const prismaSchemaText = await prismaSchema.generate();
325
+ import_node_fs3.default.writeFileSync(import_node_path3.default.resolve(workDir, "schema.prisma"), prismaSchemaText);
326
+ (0, import_node_child_process2.execSync)("npx prisma db push --schema ./schema.prisma --skip-generate --force-reset", {
327
+ cwd: workDir,
328
+ stdio: "ignore"
329
+ });
330
+ } else {
331
+ if (provider === "postgresql") {
332
+ (0, import_common_helpers2.invariant)(dbName, "dbName is required");
333
+ const pgClient = new import_pg.Client(TEST_PG_CONFIG);
334
+ await pgClient.connect();
335
+ await pgClient.query(`DROP DATABASE IF EXISTS "${dbName}"`);
336
+ await pgClient.query(`CREATE DATABASE "${dbName}"`);
337
+ await pgClient.end();
338
+ }
339
+ }
340
+ if (provider === "postgresql") {
341
+ _options.dialect = new import_kysely.PostgresDialect({
342
+ pool: new import_pg.Pool({
343
+ ...TEST_PG_CONFIG,
344
+ database: dbName
345
+ })
346
+ });
347
+ } else {
348
+ _options.dialect = new import_kysely.SqliteDialect({
349
+ database: new import_better_sqlite3.default(import_node_path3.default.join(workDir, dbName))
350
+ });
351
+ }
352
+ let client = new import_runtime4.ZenStackClient(_schema, _options);
353
+ if (!options?.usePrismaPush) {
354
+ await client.$pushSchema();
355
+ }
356
+ if (plugins) {
357
+ for (const plugin of plugins) {
358
+ client = client.$use(plugin);
359
+ }
360
+ }
361
+ return client;
362
+ }
363
+ __name(createTestClient, "createTestClient");
364
+ async function createPolicyTestClient(schema, options) {
365
+ return createTestClient(schema, {
366
+ ...options,
367
+ plugins: [
368
+ ...options?.plugins ?? [],
369
+ new import_plugin_policy.PolicyPlugin()
370
+ ]
371
+ });
372
+ }
373
+ __name(createPolicyTestClient, "createPolicyTestClient");
374
+ function testLogger(e) {
375
+ console.log(e.query.sql, e.query.parameters);
376
+ }
377
+ __name(testLogger, "testLogger");
378
+ function getTestDbName(provider) {
379
+ if (provider === "sqlite") {
380
+ return "./test.db";
381
+ }
382
+ const testName = import_vitest2.expect.getState().currentTestName;
383
+ const testPath = import_vitest2.expect.getState().testPath ?? "";
384
+ (0, import_common_helpers2.invariant)(testName);
385
+ const digest = (0, import_node_crypto2.createHash)("md5").update(testName + testPath).digest("hex");
386
+ return "test_" + testName.toLowerCase().replace(/[^a-z0-9_]/g, "_").replace(/_+/g, "_").substring(0, 30) + digest.slice(0, 6);
387
+ }
388
+ __name(getTestDbName, "getTestDbName");
389
+
390
+ // src/vitest-ext.ts
391
+ var import_runtime6 = require("@zenstackhq/runtime");
392
+ var import_vitest3 = require("vitest");
393
+ function isPromise(value) {
394
+ return typeof value.then === "function" && typeof value.catch === "function";
395
+ }
396
+ __name(isPromise, "isPromise");
397
+ function expectError(err, errorType) {
398
+ if (err instanceof errorType) {
399
+ return {
400
+ message: /* @__PURE__ */ __name(() => "", "message"),
401
+ pass: true
402
+ };
403
+ } else {
404
+ return {
405
+ message: /* @__PURE__ */ __name(() => `expected ${errorType}, got ${err}`, "message"),
406
+ pass: false
407
+ };
408
+ }
409
+ }
410
+ __name(expectError, "expectError");
411
+ function expectErrorMessages(expectedMessages, message) {
412
+ for (const m of expectedMessages) {
413
+ if (!message.includes(m)) {
414
+ return {
415
+ message: /* @__PURE__ */ __name(() => `expected message not found in error: ${m}, got message: ${message}`, "message"),
416
+ pass: false
417
+ };
418
+ }
419
+ }
420
+ return void 0;
421
+ }
422
+ __name(expectErrorMessages, "expectErrorMessages");
423
+ import_vitest3.expect.extend({
424
+ async toResolveTruthy(received) {
425
+ if (!isPromise(received)) {
426
+ return {
427
+ message: /* @__PURE__ */ __name(() => "a promise is expected", "message"),
428
+ pass: false
429
+ };
430
+ }
431
+ const r = await received;
432
+ return {
433
+ pass: !!r,
434
+ message: /* @__PURE__ */ __name(() => `Expected promise to resolve to a truthy value, but got ${r}`, "message")
435
+ };
436
+ },
437
+ async toResolveFalsy(received) {
438
+ if (!isPromise(received)) {
439
+ return {
440
+ message: /* @__PURE__ */ __name(() => "a promise is expected", "message"),
441
+ pass: false
442
+ };
443
+ }
444
+ const r = await received;
445
+ return {
446
+ pass: !r,
447
+ message: /* @__PURE__ */ __name(() => `Expected promise to resolve to a falsy value, but got ${r}`, "message")
448
+ };
449
+ },
450
+ async toResolveNull(received) {
451
+ if (!isPromise(received)) {
452
+ return {
453
+ message: /* @__PURE__ */ __name(() => "a promise is expected", "message"),
454
+ pass: false
455
+ };
456
+ }
457
+ const r = await received;
458
+ return {
459
+ pass: r === null,
460
+ message: /* @__PURE__ */ __name(() => `Expected promise to resolve to a null value, but got ${r}`, "message")
461
+ };
462
+ },
463
+ async toResolveWithLength(received, length) {
464
+ const r = await received;
465
+ return {
466
+ pass: Array.isArray(r) && r.length === length,
467
+ message: /* @__PURE__ */ __name(() => `Expected promise to resolve with an array with length ${length}, but got ${r}`, "message")
468
+ };
469
+ },
470
+ async toBeRejectedNotFound(received) {
471
+ if (!isPromise(received)) {
472
+ return {
473
+ message: /* @__PURE__ */ __name(() => "a promise is expected", "message"),
474
+ pass: false
475
+ };
476
+ }
477
+ try {
478
+ await received;
479
+ } catch (err) {
480
+ return expectError(err, import_runtime6.NotFoundError);
481
+ }
482
+ return {
483
+ message: /* @__PURE__ */ __name(() => `expected NotFoundError, got no error`, "message"),
484
+ pass: false
485
+ };
486
+ },
487
+ async toBeRejectedByPolicy(received, expectedMessages) {
488
+ if (!isPromise(received)) {
489
+ return {
490
+ message: /* @__PURE__ */ __name(() => "a promise is expected", "message"),
491
+ pass: false
492
+ };
493
+ }
494
+ try {
495
+ await received;
496
+ } catch (err) {
497
+ if (expectedMessages && err instanceof import_runtime6.RejectedByPolicyError) {
498
+ const r = expectErrorMessages(expectedMessages, err.message || "");
499
+ if (r) {
500
+ return r;
501
+ }
502
+ }
503
+ return expectError(err, import_runtime6.RejectedByPolicyError);
504
+ }
505
+ return {
506
+ message: /* @__PURE__ */ __name(() => `expected PolicyError, got no error`, "message"),
507
+ pass: false
508
+ };
509
+ },
510
+ async toBeRejectedByValidation(received, expectedMessages) {
511
+ if (!isPromise(received)) {
512
+ return {
513
+ message: /* @__PURE__ */ __name(() => "a promise is expected", "message"),
514
+ pass: false
515
+ };
516
+ }
517
+ try {
518
+ await received;
519
+ } catch (err) {
520
+ if (expectedMessages && err instanceof import_runtime6.InputValidationError) {
521
+ const r = expectErrorMessages(expectedMessages, err.message || "");
522
+ if (r) {
523
+ return r;
524
+ }
525
+ }
526
+ return expectError(err, import_runtime6.InputValidationError);
527
+ }
528
+ return {
529
+ message: /* @__PURE__ */ __name(() => `expected InputValidationError, got no error`, "message"),
530
+ pass: false
531
+ };
532
+ }
533
+ });
150
534
  // Annotate the CommonJS export names for ESM import in node:
151
535
  0 && (module.exports = {
536
+ createPolicyTestClient,
537
+ createTestClient,
152
538
  createTestProject,
153
539
  generateTsSchema,
154
- generateTsSchemaFromFile
540
+ generateTsSchemaFromFile,
541
+ generateTsSchemaInPlace,
542
+ getTestDbProvider,
543
+ loadSchema,
544
+ loadSchemaWithError,
545
+ testLogger
155
546
  });
156
547
  //# sourceMappingURL=index.cjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts","../src/project.ts","../src/schema.ts"],"sourcesContent":["export * from './project';\nexport * from './schema';\n","import fs from 'node:fs';\nimport path from 'node:path';\nimport tmp from 'tmp';\n\nexport function createTestProject() {\n const { name: workDir } = tmp.dirSync({ unsafeCleanup: true });\n\n fs.mkdirSync(path.join(workDir, 'node_modules'));\n\n // symlink all entries from \"node_modules\"\n const nodeModules = fs.readdirSync(path.join(__dirname, '../node_modules'));\n for (const entry of nodeModules) {\n if (entry.startsWith('@zenstackhq')) {\n continue;\n }\n fs.symlinkSync(\n path.join(__dirname, '../node_modules', entry),\n path.join(workDir, 'node_modules', entry),\n 'dir',\n );\n }\n\n // in addition, symlink zenstack packages\n const zenstackPackages = ['language', 'sdk', 'runtime', 'cli'];\n fs.mkdirSync(path.join(workDir, 'node_modules/@zenstackhq'));\n for (const pkg of zenstackPackages) {\n fs.symlinkSync(\n path.join(__dirname, `../../${pkg}`),\n path.join(workDir, `node_modules/@zenstackhq/${pkg}`),\n 'dir',\n );\n }\n\n fs.writeFileSync(\n path.join(workDir, 'package.json'),\n JSON.stringify(\n {\n name: 'test',\n version: '1.0.0',\n type: 'module',\n },\n null,\n 4,\n ),\n );\n\n fs.writeFileSync(\n path.join(workDir, 'tsconfig.json'),\n JSON.stringify(\n {\n compilerOptions: {\n module: 'ESNext',\n target: 'ESNext',\n moduleResolution: 'Bundler',\n esModuleInterop: true,\n skipLibCheck: true,\n strict: true,\n },\n include: ['**/*.ts'],\n },\n null,\n 4,\n ),\n );\n\n return workDir;\n}\n","import { TsSchemaGenerator } from '@zenstackhq/sdk';\nimport type { SchemaDef } from '@zenstackhq/sdk/schema';\nimport { glob } from 'glob';\nimport { execSync } from 'node:child_process';\nimport fs from 'node:fs';\nimport path from 'node:path';\nimport { match } from 'ts-pattern';\nimport { createTestProject } from './project';\n\nfunction makePrelude(provider: 'sqlite' | 'postgresql', dbName?: string) {\n return match(provider)\n .with('sqlite', () => {\n return `\ndatasource db {\n provider = 'sqlite'\n url = '${dbName ?? ':memory:'}'\n}\n`;\n })\n .with('postgresql', () => {\n return `\ndatasource db {\n provider = 'postgresql'\n url = 'postgres://postgres:postgres@localhost:5432/${dbName}'\n}\n`;\n })\n .exhaustive();\n}\n\nexport async function generateTsSchema(\n schemaText: string,\n provider: 'sqlite' | 'postgresql' = 'sqlite',\n dbName?: string,\n extraSourceFiles?: Record<string, string>,\n) {\n const workDir = createTestProject();\n console.log(`Work directory: ${workDir}`);\n\n const zmodelPath = path.join(workDir, 'schema.zmodel');\n const noPrelude = schemaText.includes('datasource ');\n fs.writeFileSync(zmodelPath, `${noPrelude ? '' : makePrelude(provider, dbName)}\\n\\n${schemaText}`);\n\n const pluginModelFiles = glob.sync(path.resolve(__dirname, '../../runtime/src/plugins/**/plugin.zmodel'));\n\n const generator = new TsSchemaGenerator();\n const tsPath = path.join(workDir, 'schema.ts');\n await generator.generate(zmodelPath, pluginModelFiles, tsPath);\n\n if (extraSourceFiles) {\n for (const [fileName, content] of Object.entries(extraSourceFiles)) {\n const filePath = path.resolve(workDir, `${fileName}.ts`);\n fs.mkdirSync(path.dirname(filePath), { recursive: true });\n fs.writeFileSync(filePath, content);\n }\n }\n\n // compile the generated TS schema\n execSync('npx tsc', {\n cwd: workDir,\n stdio: 'inherit',\n });\n\n // load the schema module\n const module = await import(path.join(workDir, 'schema.js'));\n return { workDir, schema: module.schema as SchemaDef };\n}\n\nexport function generateTsSchemaFromFile(filePath: string) {\n const schemaText = fs.readFileSync(filePath, 'utf8');\n return generateTsSchema(schemaText);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;;;ACAA,qBAAe;AACf,uBAAiB;AACjB,iBAAgB;AAET,SAASA,oBAAAA;AACZ,QAAM,EAAEC,MAAMC,QAAO,IAAKC,WAAAA,QAAIC,QAAQ;IAAEC,eAAe;EAAK,CAAA;AAE5DC,iBAAAA,QAAGC,UAAUC,iBAAAA,QAAKC,KAAKP,SAAS,cAAA,CAAA;AAGhC,QAAMQ,cAAcJ,eAAAA,QAAGK,YAAYH,iBAAAA,QAAKC,KAAKG,WAAW,iBAAA,CAAA;AACxD,aAAWC,SAASH,aAAa;AAC7B,QAAIG,MAAMC,WAAW,aAAA,GAAgB;AACjC;IACJ;AACAR,mBAAAA,QAAGS,YACCP,iBAAAA,QAAKC,KAAKG,WAAW,mBAAmBC,KAAAA,GACxCL,iBAAAA,QAAKC,KAAKP,SAAS,gBAAgBW,KAAAA,GACnC,KAAA;EAER;AAGA,QAAMG,mBAAmB;IAAC;IAAY;IAAO;IAAW;;AACxDV,iBAAAA,QAAGC,UAAUC,iBAAAA,QAAKC,KAAKP,SAAS,0BAAA,CAAA;AAChC,aAAWe,OAAOD,kBAAkB;AAChCV,mBAAAA,QAAGS,YACCP,iBAAAA,QAAKC,KAAKG,WAAW,SAASK,GAAAA,EAAK,GACnCT,iBAAAA,QAAKC,KAAKP,SAAS,4BAA4Be,GAAAA,EAAK,GACpD,KAAA;EAER;AAEAX,iBAAAA,QAAGY,cACCV,iBAAAA,QAAKC,KAAKP,SAAS,cAAA,GACnBiB,KAAKC,UACD;IACInB,MAAM;IACNoB,SAAS;IACTC,MAAM;EACV,GACA,MACA,CAAA,CAAA;AAIRhB,iBAAAA,QAAGY,cACCV,iBA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yB,WAAWnB,kBAAAA,QAAKS,QAAQd,SAAS,GAAGoB,QAAAA,KAAa;AACvDX,sBAAAA,QAAGgB,UAAUpB,kBAAAA,QAAKqB,QAAQF,QAAAA,GAAW;QAAEG,WAAW;MAAK,CAAA;AACvDlB,sBAAAA,QAAGC,cAAcc,UAAUH,OAAAA;IAC/B;EACJ;AAGAO,0CAAS,WAAW;IAChBC,KAAK7B;IACL8B,OAAO;EACX,CAAA;AAGA,QAAMC,UAAS,MAAM,OAAO1B,kBAAAA,QAAKC,KAAKN,SAAS,WAAA;AAC/C,SAAO;IAAEA;IAASgC,QAAQD,QAAOC;EAAoB;AACzD;AApCsBnC;AAsCf,SAASoC,yBAAyBT,UAAgB;AACrD,QAAM1B,aAAaW,gBAAAA,QAAGyB,aAAaV,UAAU,MAAA;AAC7C,SAAO3B,iBAAiBC,UAAAA;AAC5B;AAHgBmC;","names":["createTestProject","name","workDir","tmp","dirSync","unsafeCleanup","fs","mkdirSync","path","join","nodeModules","readdirSync","__dirname","entry","startsWith","symlinkSync","zenstackPackages","pkg","writeFileSync","JSON","stringify","version","type","compilerOptions","module","target","moduleResolution","esModuleInterop","skipLibCheck","strict","include","import_node_fs","import_node_path","makePrelude","provider","dbName","match","with","exhaustive","generateTsSchema","schemaText","extraSourceFiles","workDir","createTestProject","console","log","zmodelPath","path","join","noPrelude","includes","fs","writeFileSync","pluginModelFiles","glob","sync","resolve","__dirname","generator","TsSchemaGenerator","tsPath","generate","fileName","content","Object","entries","filePath","mkdirSync","dirname","recursive","execSync","cwd","stdio","module","schema","generateTsSchemaFromFile","readFileSync"]}
1
+ {"version":3,"sources":["../src/index.ts","../src/client.ts","../src/project.ts","../src/schema.ts","../src/utils.ts","../src/vitest-ext.ts"],"sourcesContent":["export * from './client';\nexport * from './project';\nexport * from './schema';\nexport * from './vitest-ext';\n","import { invariant } from '@zenstackhq/common-helpers';\nimport type { Model } from '@zenstackhq/language/ast';\nimport { PolicyPlugin } from '@zenstackhq/plugin-policy';\nimport { ZenStackClient, type ClientContract, type ClientOptions } from '@zenstackhq/runtime';\nimport type { SchemaDef } from '@zenstackhq/runtime/schema';\nimport { PrismaSchemaGenerator } from '@zenstackhq/sdk';\nimport SQLite from 'better-sqlite3';\nimport { PostgresDialect, SqliteDialect, type LogEvent } from 'kysely';\nimport { execSync } from 'node:child_process';\nimport { createHash } from 'node:crypto';\nimport fs from 'node:fs';\nimport path from 'node:path';\nimport { Client as PGClient, Pool } from 'pg';\nimport { expect } from 'vitest';\nimport { createTestProject } from './project';\nimport { generateTsSchema } from './schema';\nimport { loadDocumentWithPlugins } from './utils';\n\nexport function getTestDbProvider() {\n const val = process.env['TEST_DB_PROVIDER'] ?? 'sqlite';\n if (!['sqlite', 'postgresql'].includes(val!)) {\n throw new Error(`Invalid TEST_DB_PROVIDER value: ${val}`);\n }\n return val as 'sqlite' | 'postgresql';\n}\n\nconst TEST_PG_CONFIG = {\n host: process.env['TEST_PG_HOST'] ?? 'localhost',\n port: process.env['TEST_PG_PORT'] ? parseInt(process.env['TEST_PG_PORT']) : 5432,\n user: process.env['TEST_PG_USER'] ?? 'postgres',\n password: process.env['TEST_PG_PASSWORD'] ?? 'postgres',\n};\n\nexport type CreateTestClientOptions<Schema extends SchemaDef> = Omit<ClientOptions<Schema>, 'dialect'> & {\n provider?: 'sqlite' | 'postgresql';\n dbName?: string;\n usePrismaPush?: boolean;\n extraSourceFiles?: Record<string, string>;\n workDir?: string;\n debug?: boolean;\n};\n\nexport async function createTestClient<Schema extends SchemaDef>(\n schema: Schema,\n options?: CreateTestClientOptions<Schema>,\n schemaFile?: string,\n): Promise<ClientContract<Schema>>;\nexport async function createTestClient<Schema extends SchemaDef>(\n schema: string,\n options?: CreateTestClientOptions<Schema>,\n): Promise<any>;\nexport async function createTestClient<Schema extends SchemaDef>(\n schema: Schema | string,\n options?: CreateTestClientOptions<Schema>,\n schemaFile?: string,\n): Promise<any> {\n let workDir = options?.workDir;\n let _schema: Schema;\n const provider = options?.provider ?? getTestDbProvider() ?? 'sqlite';\n\n const dbName = options?.dbName ?? getTestDbName(provider);\n\n const dbUrl =\n provider === 'sqlite'\n ? `file:${dbName}`\n : `postgres://${TEST_PG_CONFIG.user}:${TEST_PG_CONFIG.password}@${TEST_PG_CONFIG.host}:${TEST_PG_CONFIG.port}/${dbName}`;\n\n let model: Model | undefined;\n\n if (typeof schema === 'string') {\n const generated = await generateTsSchema(schema, provider, dbUrl, options?.extraSourceFiles);\n workDir = generated.workDir;\n model = generated.model;\n // replace schema's provider\n _schema = {\n ...generated.schema,\n provider: {\n type: provider,\n },\n } as Schema;\n } else {\n // replace schema's provider\n _schema = {\n ...schema,\n provider: {\n type: provider,\n },\n };\n workDir ??= createTestProject();\n if (schemaFile) {\n let schemaContent = fs.readFileSync(schemaFile, 'utf-8');\n if (dbUrl) {\n // replace `datasource db { }` section\n schemaContent = schemaContent.replace(\n /datasource\\s+db\\s*{[^}]*}/m,\n `datasource db {\n provider = '${provider}'\n url = '${dbUrl}'\n}`,\n );\n }\n fs.writeFileSync(path.join(workDir, 'schema.zmodel'), schemaContent);\n }\n }\n\n invariant(workDir);\n if (options?.debug) {\n console.log(`Work directory: ${workDir}`);\n }\n\n const { plugins, ...rest } = options ?? {};\n const _options: ClientOptions<Schema> = {\n ...rest,\n } as ClientOptions<Schema>;\n\n if (options?.usePrismaPush) {\n invariant(typeof schema === 'string' || schemaFile, 'a schema file must be provided when using prisma db push');\n if (!model) {\n const r = await loadDocumentWithPlugins(path.join(workDir, 'schema.zmodel'));\n if (!r.success) {\n throw new Error(r.errors.join('\\n'));\n }\n model = r.model;\n }\n const prismaSchema = new PrismaSchemaGenerator(model);\n const prismaSchemaText = await prismaSchema.generate();\n fs.writeFileSync(path.resolve(workDir!, 'schema.prisma'), prismaSchemaText);\n execSync('npx prisma db push --schema ./schema.prisma --skip-generate --force-reset', {\n cwd: workDir,\n stdio: 'ignore',\n });\n } else {\n if (provider === 'postgresql') {\n invariant(dbName, 'dbName is required');\n const pgClient = new PGClient(TEST_PG_CONFIG);\n await pgClient.connect();\n await pgClient.query(`DROP DATABASE IF EXISTS \"${dbName}\"`);\n await pgClient.query(`CREATE DATABASE \"${dbName}\"`);\n await pgClient.end();\n }\n }\n\n if (provider === 'postgresql') {\n _options.dialect = new PostgresDialect({\n pool: new Pool({\n ...TEST_PG_CONFIG,\n database: dbName,\n }),\n });\n } else {\n _options.dialect = new SqliteDialect({\n database: new SQLite(path.join(workDir!, dbName)),\n });\n }\n\n let client = new ZenStackClient(_schema, _options);\n\n if (!options?.usePrismaPush) {\n await client.$pushSchema();\n }\n\n if (plugins) {\n for (const plugin of plugins) {\n client = client.$use(plugin);\n }\n }\n\n return client;\n}\n\nexport async function createPolicyTestClient<Schema extends SchemaDef>(\n schema: Schema,\n options?: CreateTestClientOptions<Schema>,\n): Promise<ClientContract<Schema>>;\nexport async function createPolicyTestClient<Schema extends SchemaDef>(\n schema: string,\n options?: CreateTestClientOptions<Schema>,\n): Promise<any>;\nexport async function createPolicyTestClient<Schema extends SchemaDef>(\n schema: Schema | string,\n options?: CreateTestClientOptions<Schema>,\n): Promise<any> {\n return createTestClient(\n schema as any,\n {\n ...options,\n plugins: [...(options?.plugins ?? []), new PolicyPlugin()],\n } as any,\n );\n}\n\nexport function testLogger(e: LogEvent) {\n console.log(e.query.sql, e.query.parameters);\n}\n\nfunction getTestDbName(provider: string) {\n if (provider === 'sqlite') {\n return './test.db';\n }\n const testName = expect.getState().currentTestName;\n const testPath = expect.getState().testPath ?? '';\n invariant(testName);\n // digest test name\n const digest = createHash('md5')\n .update(testName + testPath)\n .digest('hex');\n // compute a database name based on test name\n return (\n 'test_' +\n testName\n .toLowerCase()\n .replace(/[^a-z0-9_]/g, '_')\n .replace(/_+/g, '_')\n .substring(0, 30) +\n digest.slice(0, 6)\n );\n}\n","import fs from 'node:fs';\nimport path from 'node:path';\nimport tmp from 'tmp';\n\nexport function createTestProject(zmodelContent?: string) {\n const { name: workDir } = tmp.dirSync({ unsafeCleanup: true });\n\n fs.mkdirSync(path.join(workDir, 'node_modules'));\n\n // symlink all entries from \"node_modules\"\n const nodeModules = fs.readdirSync(path.join(__dirname, '../node_modules'));\n for (const entry of nodeModules) {\n if (entry.startsWith('@zenstackhq')) {\n continue;\n }\n fs.symlinkSync(\n path.join(__dirname, '../node_modules', entry),\n path.join(workDir, 'node_modules', entry),\n 'dir',\n );\n }\n\n // in addition, symlink zenstack packages\n const zenstackPackages = ['language', 'sdk', 'runtime', 'cli'];\n fs.mkdirSync(path.join(workDir, 'node_modules/@zenstackhq'));\n for (const pkg of zenstackPackages) {\n fs.symlinkSync(\n path.join(__dirname, `../../${pkg}`),\n path.join(workDir, `node_modules/@zenstackhq/${pkg}`),\n 'dir',\n );\n }\n\n fs.writeFileSync(\n path.join(workDir, 'package.json'),\n JSON.stringify(\n {\n name: 'test',\n version: '1.0.0',\n type: 'module',\n },\n null,\n 4,\n ),\n );\n\n fs.writeFileSync(\n path.join(workDir, 'tsconfig.json'),\n JSON.stringify(\n {\n compilerOptions: {\n module: 'ESNext',\n target: 'ESNext',\n moduleResolution: 'Bundler',\n esModuleInterop: true,\n skipLibCheck: true,\n strict: true,\n },\n include: ['**/*.ts'],\n },\n null,\n 4,\n ),\n );\n\n if (zmodelContent) {\n fs.writeFileSync(path.join(workDir, 'schema.zmodel'), zmodelContent);\n }\n\n return workDir;\n}\n","import { invariant } from '@zenstackhq/common-helpers';\nimport { TsSchemaGenerator } from '@zenstackhq/sdk';\nimport type { SchemaDef } from '@zenstackhq/sdk/schema';\nimport { execSync } from 'node:child_process';\nimport crypto from 'node:crypto';\nimport fs from 'node:fs';\nimport os from 'node:os';\nimport path from 'node:path';\nimport { match } from 'ts-pattern';\nimport { expect } from 'vitest';\nimport { createTestProject } from './project';\nimport { loadDocumentWithPlugins } from './utils';\n\nfunction makePrelude(provider: 'sqlite' | 'postgresql', dbUrl?: string) {\n return match(provider)\n .with('sqlite', () => {\n return `\ndatasource db {\n provider = 'sqlite'\n url = '${dbUrl ?? 'file:./test.db'}'\n}\n`;\n })\n .with('postgresql', () => {\n return `\ndatasource db {\n provider = 'postgresql'\n url = '${dbUrl ?? 'postgres://postgres:postgres@localhost:5432/db'}'\n}\n`;\n })\n .exhaustive();\n}\n\nexport async function generateTsSchema(\n schemaText: string,\n provider: 'sqlite' | 'postgresql' = 'sqlite',\n dbUrl?: string,\n extraSourceFiles?: Record<string, string>,\n) {\n const workDir = createTestProject();\n\n const zmodelPath = path.join(workDir, 'schema.zmodel');\n const noPrelude = schemaText.includes('datasource ');\n fs.writeFileSync(zmodelPath, `${noPrelude ? '' : makePrelude(provider, dbUrl)}\\n\\n${schemaText}`);\n\n const result = await loadDocumentWithPlugins(zmodelPath);\n if (!result.success) {\n throw new Error(`Failed to load schema from ${zmodelPath}: ${result.errors}`);\n }\n\n const generator = new TsSchemaGenerator();\n await generator.generate(result.model, workDir);\n\n if (extraSourceFiles) {\n for (const [fileName, content] of Object.entries(extraSourceFiles)) {\n const filePath = path.resolve(workDir, !fileName.endsWith('.ts') ? `${fileName}.ts` : fileName);\n fs.mkdirSync(path.dirname(filePath), { recursive: true });\n fs.writeFileSync(filePath, content);\n }\n }\n\n // compile the generated TS schema\n return { ...(await compileAndLoad(workDir)), model: result.model };\n}\n\nasync function compileAndLoad(workDir: string) {\n execSync('npx tsc', {\n cwd: workDir,\n stdio: 'inherit',\n });\n\n // load the schema module\n const module = await import(path.join(workDir, 'schema.js'));\n return { workDir, schema: module.schema as SchemaDef };\n}\n\nexport function generateTsSchemaFromFile(filePath: string) {\n const schemaText = fs.readFileSync(filePath, 'utf8');\n return generateTsSchema(schemaText);\n}\n\nexport async function generateTsSchemaInPlace(schemaPath: string) {\n const workDir = path.dirname(schemaPath);\n const result = await loadDocumentWithPlugins(schemaPath);\n if (!result.success) {\n throw new Error(`Failed to load schema from ${schemaPath}: ${result.errors}`);\n }\n const generator = new TsSchemaGenerator();\n await generator.generate(result.model, workDir);\n return compileAndLoad(workDir);\n}\n\nexport async function loadSchema(schema: string, additionalSchemas?: Record<string, string>) {\n if (!schema.includes('datasource ')) {\n schema = `${makePrelude('sqlite')}\\n\\n${schema}`;\n }\n\n // create a temp folder\n const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'zenstack-schema'));\n\n // create a temp file\n const tempFile = path.join(tempDir, `schema.zmodel`);\n fs.writeFileSync(tempFile, schema);\n\n if (additionalSchemas) {\n for (const [fileName, content] of Object.entries(additionalSchemas)) {\n let name = fileName;\n if (!name.endsWith('.zmodel')) {\n name += '.zmodel';\n }\n const filePath = path.join(tempDir, name);\n fs.writeFileSync(filePath, content);\n }\n }\n\n const r = await loadDocumentWithPlugins(tempFile);\n expect(r).toSatisfy(\n (r) => r.success,\n `Failed to load schema: ${(r as any).errors?.map((e: any) => e.toString()).join(', ')}`,\n );\n invariant(r.success);\n return r.model;\n}\n\nexport async function loadSchemaWithError(schema: string, error: string | RegExp) {\n if (!schema.includes('datasource ')) {\n schema = `${makePrelude('sqlite')}\\n\\n${schema}`;\n }\n\n // create a temp file\n const tempFile = path.join(os.tmpdir(), `zenstack-schema-${crypto.randomUUID()}.zmodel`);\n fs.writeFileSync(tempFile, schema);\n const r = await loadDocumentWithPlugins(tempFile);\n expect(r.success).toBe(false);\n invariant(!r.success);\n if (typeof error === 'string') {\n expect(r).toSatisfy(\n (r) => r.errors.some((e: any) => e.toString().toLowerCase().includes(error.toLowerCase())),\n `Expected error message to include \"${error}\" but got: ${r.errors.map((e: any) => e.toString()).join(', ')}`,\n );\n } else {\n expect(r).toSatisfy(\n (r) => r.errors.some((e: any) => error.test(e)),\n `Expected error message to match \"${error}\" but got: ${r.errors.map((e: any) => e.toString()).join(', ')}`,\n );\n }\n}\n","import { loadDocument } from '@zenstackhq/language';\n\nexport function loadDocumentWithPlugins(filePath: string) {\n const pluginModelFiles = [require.resolve('@zenstackhq/plugin-policy/plugin.zmodel')];\n return loadDocument(filePath, pluginModelFiles);\n}\n","import { InputValidationError, NotFoundError, RejectedByPolicyError } from '@zenstackhq/runtime';\nimport { expect } from 'vitest';\n\nfunction isPromise(value: any) {\n return typeof value.then === 'function' && typeof value.catch === 'function';\n}\n\nfunction expectError(err: any, errorType: any) {\n if (err instanceof errorType) {\n return {\n message: () => '',\n pass: true,\n };\n } else {\n return {\n message: () => `expected ${errorType}, got ${err}`,\n pass: false,\n };\n }\n}\n\nfunction expectErrorMessages(expectedMessages: string[], message: string) {\n for (const m of expectedMessages) {\n if (!message.includes(m)) {\n return {\n message: () => `expected message not found in error: ${m}, got message: ${message}`,\n pass: false,\n };\n }\n }\n return undefined;\n}\n\nexpect.extend({\n async toResolveTruthy(received: Promise<unknown>) {\n if (!isPromise(received)) {\n return { message: () => 'a promise is expected', pass: false };\n }\n const r = await received;\n return {\n pass: !!r,\n message: () => `Expected promise to resolve to a truthy value, but got ${r}`,\n };\n },\n\n async toResolveFalsy(received: Promise<unknown>) {\n if (!isPromise(received)) {\n return { message: () => 'a promise is expected', pass: false };\n }\n const r = await received;\n return {\n pass: !r,\n message: () => `Expected promise to resolve to a falsy value, but got ${r}`,\n };\n },\n\n async toResolveNull(received: Promise<unknown>) {\n if (!isPromise(received)) {\n return { message: () => 'a promise is expected', pass: false };\n }\n const r = await received;\n return {\n pass: r === null,\n message: () => `Expected promise to resolve to a null value, but got ${r}`,\n };\n },\n\n async toResolveWithLength(received: Promise<unknown>, length: number) {\n const r = await received;\n return {\n pass: Array.isArray(r) && r.length === length,\n message: () => `Expected promise to resolve with an array with length ${length}, but got ${r}`,\n };\n },\n\n async toBeRejectedNotFound(received: Promise<unknown>) {\n if (!isPromise(received)) {\n return { message: () => 'a promise is expected', pass: false };\n }\n try {\n await received;\n } catch (err) {\n return expectError(err, NotFoundError);\n }\n return {\n message: () => `expected NotFoundError, got no error`,\n pass: false,\n };\n },\n\n async toBeRejectedByPolicy(received: Promise<unknown>, expectedMessages?: string[]) {\n if (!isPromise(received)) {\n return { message: () => 'a promise is expected', pass: false };\n }\n try {\n await received;\n } catch (err) {\n if (expectedMessages && err instanceof RejectedByPolicyError) {\n const r = expectErrorMessages(expectedMessages, err.message || '');\n if (r) {\n return r;\n }\n }\n return expectError(err, RejectedByPolicyError);\n }\n return {\n message: () => `expected PolicyError, got no error`,\n pass: false,\n };\n },\n\n async toBeRejectedByValidation(received: Promise<unknown>, expectedMessages?: string[]) {\n if (!isPromise(received)) {\n return { message: () => 'a promise is expected', pass: false };\n }\n try {\n await received;\n } catch (err) {\n if (expectedMessages && err instanceof InputValidationError) {\n const r = expectErrorMessages(expectedMessages, err.message || '');\n if (r) {\n return r;\n }\n }\n return expectError(err, InputValidationError);\n }\n return {\n message: () => `expected InputValidationError, got no error`,\n pass: false,\n };\n },\n});\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;;;;;;;;;;ACAA,IAAAA,yBAA0B;AAE1B,2BAA6B;AAC7B,IAAAC,kBAAwE;AAExE,IAAAC,cAAsC;AACtC,4BAAmB;AACnB,oBAA8D;AAC9D,IAAAC,6BAAyB;AACzB,IAAAC,sBAA2B;AAC3B,IAAAC,kBAAe;AACf,IAAAC,oBAAiB;AACjB,gBAAyC;AACzC,IAAAC,iBAAuB;;;ACbvB,qBAAe;AACf,uBAAiB;AACjB,iBAAgB;AAET,SAASC,kBAAkBC,eAAsB;AACpD,QAAM,EAAEC,MAAMC,QAAO,IAAKC,WAAAA,QAAIC,QAAQ;IAAEC,eAAe;EAAK,CAAA;AAE5DC,iBAAAA,QAAGC,UAAUC,iBAAAA,QAAKC,KAAKP,SAAS,cAAA,CAAA;AAGhC,QAAMQ,cAAcJ,eAAAA,QAAGK,YAAYH,iBAAAA,QAAKC,KAAKG,WAAW,iBAAA,CAAA;AACxD,aAAWC,SAASH,aAAa;AAC7B,QAAIG,MAAMC,WAAW,aAAA,GAAgB;AACjC;IACJ;AACAR,mBAAAA,QAAGS,YACCP,iBAAAA,QAAKC,KAAKG,WAAW,mBAAmBC,KAAAA,GACxCL,iBAAAA,QAAKC,KAAKP,SAAS,gBAAgBW,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A4BP,kBAA2B;AAClF,QAAI,CAACV,UAAUiB,QAAAA,GAAW;AACtB,aAAO;QAAEV,SAAS,6BAAM,yBAAN;QAA+BC,MAAM;MAAM;IACjE;AACA,QAAI;AACA,YAAMS;IACV,SAASZ,KAAK;AACV,UAAIK,oBAAoBL,eAAeyB,sCAAsB;AACzD,cAAMZ,IAAIT,oBAAoBC,kBAAkBL,IAAIE,WAAW,EAAA;AAC/D,YAAIW,GAAG;AACH,iBAAOA;QACX;MACJ;AACA,aAAOd,YAAYC,KAAKyB,oCAAAA;IAC5B;AACA,WAAO;MACHvB,SAAS,6BAAM,+CAAN;MACTC,MAAM;IACV;EACJ;AACJ,CAAA;","names":["import_common_helpers","import_runtime","import_sdk","import_node_child_process","import_node_crypto","import_node_fs","import_node_path","import_vitest","createTestProject","zmodelContent","name","workDir","tmp","dirSync","unsafeCleanup","fs","mkdirSync","path","join","nodeModules","readdirSync","__dirname","entry","startsWith","symlinkSync","zenstackPackages","pkg","writeFileSync","JSON","stringify","version","type","compilerOptions","module","target","moduleResolution","esModuleInterop","skipLibCheck","strict","include","import_node_fs","import_node_path","loadDocumentWithPlugins","filePath","pluginModelFiles","require","loadDocument","makePrelude","provider","dbUrl","match","with","exhaustive","generateTsSchema","schemaText","extraSourceFiles","workDir","createTestProject","zmodelPath","path","join","noPrelude","includes","fs","writeFileSync","result","loadDocumentWithPlugins","success","Error","errors","generator","TsSchemaGenerator","generate","model","fileName","content","Object","entries","filePath","resolve","endsWith","mkdirSync","dirname","recursive","compileAndLoad","execSync","cwd","stdio","module","schema","generateTsSchemaFromFile","readFileSync","generateTsSchemaInPlace","schemaPath","loadSchema","additionalSchemas","tempDir","mkdtempSync","os","tmpdir","tempFile","name","r","expect","toSatisfy","map","e","toString","invariant","loadSchemaWithError","error","crypto","randomUUID","toBe","some","toLowerCase","test","getTestDbProvider","val","process","env","includes","Error","TEST_PG_CONFIG","host","port","parseInt","user","password","createTestClient","schema","options","schemaFile","workDir","_schema","provider","dbName","getTestDbName","dbUrl","model","generated","generateTsSchema","extraSourceFiles","type","createTestProject","schemaContent","fs","readFileSync","replace","writeFileSync","path","join","invariant","debug","console","log","plugins","rest","_options","usePrismaPush","r","loadDocumentWithPlugins","success","errors","prismaSchema","PrismaSchemaGenerator","prismaSchemaText","generate","resolve","execSync","cwd","stdio","pgClient","PGClient","connect","query","end","dialect","PostgresDialect","pool","Pool","database","SqliteDialect","SQLite","client","ZenStackClient","$pushSchema","plugin","$use","createPolicyTestClient","PolicyPlugin","testLogger","e","sql","parameters","testName","expect","getState","currentTestName","testPath","digest","createHash","update","toLowerCase","substring","slice","import_runtime","import_vitest","isPromise","value","then","catch","expectError","err","errorType","message","pass","expectErrorMessages","expectedMessages","m","includes","undefined","expect","extend","toResolveTruthy","received","r","toResolveFalsy","toResolveNull","toResolveWithLength","length","Array","isArray","toBeRejectedNotFound","NotFoundError","toBeRejectedByPolicy","RejectedByPolicyError","toBeRejectedByValidation","InputValidationError"]}
package/dist/index.d.cts CHANGED
@@ -1,14 +1,41 @@
1
- import { SchemaDef } from '@zenstackhq/sdk/schema';
1
+ import { ClientOptions, ClientContract } from '@zenstackhq/runtime';
2
+ import { SchemaDef } from '@zenstackhq/runtime/schema';
3
+ import { LogEvent } from 'kysely';
4
+ import * as _zenstackhq_language_ast from '@zenstackhq/language/ast';
5
+ import { SchemaDef as SchemaDef$1 } from '@zenstackhq/sdk/schema';
2
6
 
3
- declare function createTestProject(): string;
7
+ declare function getTestDbProvider(): "sqlite" | "postgresql";
8
+ type CreateTestClientOptions<Schema extends SchemaDef> = Omit<ClientOptions<Schema>, 'dialect'> & {
9
+ provider?: 'sqlite' | 'postgresql';
10
+ dbName?: string;
11
+ usePrismaPush?: boolean;
12
+ extraSourceFiles?: Record<string, string>;
13
+ workDir?: string;
14
+ debug?: boolean;
15
+ };
16
+ declare function createTestClient<Schema extends SchemaDef>(schema: Schema, options?: CreateTestClientOptions<Schema>, schemaFile?: string): Promise<ClientContract<Schema>>;
17
+ declare function createTestClient<Schema extends SchemaDef>(schema: string, options?: CreateTestClientOptions<Schema>): Promise<any>;
18
+ declare function createPolicyTestClient<Schema extends SchemaDef>(schema: Schema, options?: CreateTestClientOptions<Schema>): Promise<ClientContract<Schema>>;
19
+ declare function createPolicyTestClient<Schema extends SchemaDef>(schema: string, options?: CreateTestClientOptions<Schema>): Promise<any>;
20
+ declare function testLogger(e: LogEvent): void;
4
21
 
5
- declare function generateTsSchema(schemaText: string, provider?: 'sqlite' | 'postgresql', dbName?: string, extraSourceFiles?: Record<string, string>): Promise<{
22
+ declare function createTestProject(zmodelContent?: string): string;
23
+
24
+ declare function generateTsSchema(schemaText: string, provider?: 'sqlite' | 'postgresql', dbUrl?: string, extraSourceFiles?: Record<string, string>): Promise<{
25
+ model: _zenstackhq_language_ast.Model;
6
26
  workDir: string;
7
- schema: SchemaDef;
27
+ schema: SchemaDef$1;
8
28
  }>;
9
29
  declare function generateTsSchemaFromFile(filePath: string): Promise<{
30
+ model: _zenstackhq_language_ast.Model;
31
+ workDir: string;
32
+ schema: SchemaDef$1;
33
+ }>;
34
+ declare function generateTsSchemaInPlace(schemaPath: string): Promise<{
10
35
  workDir: string;
11
- schema: SchemaDef;
36
+ schema: SchemaDef$1;
12
37
  }>;
38
+ declare function loadSchema(schema: string, additionalSchemas?: Record<string, string>): Promise<_zenstackhq_language_ast.Model>;
39
+ declare function loadSchemaWithError(schema: string, error: string | RegExp): Promise<void>;
13
40
 
14
- export { createTestProject, generateTsSchema, generateTsSchemaFromFile };
41
+ export { type CreateTestClientOptions, createPolicyTestClient, createTestClient, createTestProject, generateTsSchema, generateTsSchemaFromFile, generateTsSchemaInPlace, getTestDbProvider, loadSchema, loadSchemaWithError, testLogger };