mirror of
https://github.com/zenstackhq/zenstack
synced 2026-05-24 10:08:55 +00:00
43 lines
1.7 KiB
TypeScript
43 lines
1.7 KiB
TypeScript
import { invariant } from '@zenstackhq/common-helpers';
|
|
import { glob } from 'glob';
|
|
import fs from 'node:fs';
|
|
import os from 'node:os';
|
|
import path from 'node:path';
|
|
import { expect } from 'vitest';
|
|
import { loadDocument } from '../src';
|
|
|
|
export async function loadSchema(schema: string) {
|
|
// create a temp file
|
|
const tempFile = path.join(os.tmpdir(), `zenstack-schema-${crypto.randomUUID()}.zmodel`);
|
|
fs.writeFileSync(tempFile, schema);
|
|
const r = await loadDocument(tempFile, getPluginModels());
|
|
expect(r).toSatisfy(
|
|
(r) => r.success,
|
|
`Failed to load schema: ${(r as any).errors?.map((e) => e.toString()).join(', ')}`,
|
|
);
|
|
invariant(r.success);
|
|
return r.model;
|
|
}
|
|
|
|
export async function loadSchemaWithError(schema: string, error: string | RegExp) {
|
|
// create a temp file
|
|
const tempFile = path.join(os.tmpdir(), `zenstack-schema-${crypto.randomUUID()}.zmodel`);
|
|
fs.writeFileSync(tempFile, schema);
|
|
const r = await loadDocument(tempFile, getPluginModels());
|
|
expect(r.success).toBe(false);
|
|
invariant(!r.success);
|
|
if (typeof error === 'string') {
|
|
expect(r).toSatisfy(
|
|
(r) => r.errors.some((e) => e.toString().toLowerCase().includes(error.toLowerCase())),
|
|
`Expected error message to include "${error}" but got: ${r.errors.map((e) => e.toString()).join(', ')}`,
|
|
);
|
|
} else {
|
|
expect(r).toSatisfy(
|
|
(r) => r.errors.some((e) => error.test(e)),
|
|
`Expected error message to match "${error}" but got: ${r.errors.map((e) => e.toString()).join(', ')}`,
|
|
);
|
|
}
|
|
}
|
|
function getPluginModels() {
|
|
return glob.sync(path.resolve(__dirname, '../../runtime/src/plugins/**/plugin.zmodel'));
|
|
}
|