zenstack/packages/language/test/utils.ts
Yiming Cao 59dfa73f01
refactor: move policy attributes to its own zmodel (#307)
* refactor: move policy attributes to its own zmodel

* update

* update

* fix tests
2025-10-16 21:52:22 -07:00

42 lines
1.6 KiB
TypeScript

import { invariant } from '@zenstackhq/common-helpers';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { expect } from 'vitest';
import { loadDocument } from '../src';
const pluginDocs = [path.resolve(__dirname, '../../plugins/policy/plugin.zmodel')];
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, pluginDocs);
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, pluginDocs);
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(', ')}`,
);
}
}