mirror of
https://github.com/gorules/zen.git
synced 2026-11-28 16:06:26 +00:00
240 lines
8.7 KiB
TypeScript
240 lines
8.7 KiB
TypeScript
import {
|
|
ZenEngine,
|
|
evaluateExpression,
|
|
evaluateUnaryExpression,
|
|
renderTemplate,
|
|
evaluateExpressionSync,
|
|
evaluateUnaryExpressionSync, renderTemplateSync, ZenDecisionContent,
|
|
} from '../index';
|
|
import fs from 'fs/promises';
|
|
import path from 'path';
|
|
import { describe, expect, it, jest } from '@jest/globals';
|
|
import assert from 'assert';
|
|
|
|
const testDataRoot = path.join(__dirname, '../../../', 'test-data');
|
|
|
|
const loader = async (key: string) => fs.readFile(path.join(testDataRoot, key));
|
|
|
|
jest.useRealTimers();
|
|
|
|
describe('ZenEngine', () => {
|
|
it('Evaluates decisions using loader', async () => {
|
|
const engine = new ZenEngine({
|
|
loader,
|
|
});
|
|
|
|
const r1 = await engine.evaluate('function.json', { input: 5 });
|
|
const r2 = await engine.evaluate('table.json', { input: 2 });
|
|
const r3 = await engine.evaluate('table.json', { input: 12 });
|
|
|
|
expect(r1.result.output).toEqual(10);
|
|
expect(r2.result.output).toEqual(0);
|
|
expect(r3.result.output).toEqual(10);
|
|
|
|
}, 10000);
|
|
|
|
it('Evaluates decisions using getDecision', async () => {
|
|
const engine = new ZenEngine({
|
|
loader,
|
|
});
|
|
|
|
const functionDecision = await engine.getDecision('function.json');
|
|
const tableDecision = await engine.getDecision('table.json');
|
|
|
|
const r1 = await functionDecision.evaluate({ input: 10 });
|
|
const r2 = await tableDecision.evaluate({ input: 5 });
|
|
const r3 = await tableDecision.evaluate({ input: 12 });
|
|
|
|
expect(r1.result.output).toEqual(20);
|
|
expect(r2.result.output).toEqual(0);
|
|
expect(r3.result.output).toEqual(10);
|
|
|
|
}, 10000);
|
|
|
|
it('Evaluates decisions returned as ZenDecisionContent from the loader', async () => {
|
|
const contents = new Map<string, ZenDecisionContent>();
|
|
const engine = new ZenEngine({
|
|
loader: async (key) => {
|
|
if (!contents.has(key)) {
|
|
contents.set(key, new ZenDecisionContent(await fs.readFile(path.join(testDataRoot, key))));
|
|
}
|
|
return contents.get(key)!;
|
|
},
|
|
});
|
|
|
|
const r1 = await engine.evaluate('table.json', { input: 12 });
|
|
const r2 = await engine.evaluate('function.json', { input: 5 });
|
|
const r3 = await new ZenEngine({ loader: async (key) => contents.get(key)! }).evaluate('table.json', { input: 2 });
|
|
|
|
expect(r1.result.output).toEqual(10);
|
|
expect(r2.result.output).toEqual(10);
|
|
expect(r3.result.output).toEqual(0);
|
|
}, 10000);
|
|
|
|
it('Creates a decision from contents', async () => {
|
|
const engine = new ZenEngine();
|
|
const functionContent = await fs.readFile(path.join(testDataRoot, 'function.json'));
|
|
const functionDecision = engine.createDecision(functionContent);
|
|
|
|
const r = await functionDecision.evaluate({ input: 15 });
|
|
expect(r.result.output).toEqual(30);
|
|
}, 10000);
|
|
|
|
it('Evaluate custom nodes with a handler', async () => {
|
|
const engine = new ZenEngine({
|
|
loader,
|
|
customHandler: async (request) => {
|
|
const prop1 = request.getField('prop1') as number;
|
|
const prop1Raw = request.getFieldRaw('prop1');
|
|
|
|
expect(prop1).toEqual(15);
|
|
expect(prop1Raw).toEqual('{{ a + 10 }}');
|
|
expect(request.node).toMatchObject({
|
|
id: '138b3b11-ff46-450f-9704-3f3c712067b2',
|
|
name: 'customNode1',
|
|
kind: 'sum',
|
|
config: {
|
|
prop1: '{{ a + 10 }}',
|
|
},
|
|
});
|
|
return { output: { data: prop1 + 10 } };
|
|
},
|
|
});
|
|
|
|
const r = await engine.evaluate('custom.json', { a: 5 });
|
|
expect(r.result.data).toEqual(25);
|
|
|
|
});
|
|
|
|
it('Parses ZenDecisionContent', async () => {
|
|
const decisionContent = new ZenDecisionContent(await loader('table.json'));
|
|
|
|
expect(decisionContent).toBeDefined();
|
|
expect(decisionContent.toBuffer()).toBeDefined();
|
|
});
|
|
|
|
it('Passes graph tests', async () => {
|
|
type TestCase = {
|
|
input: Record<string, unknown>;
|
|
output: Record<string, unknown>;
|
|
}
|
|
|
|
type Graph = {
|
|
tests: TestCase[];
|
|
}
|
|
|
|
const graphsRoot = path.join(testDataRoot, 'graphs');
|
|
const loader = async (key: string) => fs.readFile(path.join(graphsRoot, key));
|
|
|
|
const engine = new ZenEngine({ loader });
|
|
const now = '2025-08-19T16:55:02.078Z';
|
|
|
|
const entries = await fs.readdir(graphsRoot);
|
|
for (const entry of entries) {
|
|
if (!entry.endsWith('.json')) continue;
|
|
|
|
const fileContents = await fs.readFile(path.join(graphsRoot, entry));
|
|
const fileData: Graph = JSON.parse(fileContents.toString('utf8'));
|
|
|
|
for (const testCase of fileData.tests) {
|
|
const engineResponse = await engine.safeEvaluate(entry, testCase.input, { now });
|
|
|
|
const decision = await engine.safeGetDecision(entry);
|
|
assert.ok(decision.success, 'Decision must exist');
|
|
|
|
const decisionResponse = await decision.data.safeEvaluate(testCase.input, { now });
|
|
|
|
assert.ok(engineResponse.success, 'Engine response must be ok');
|
|
assert.ok(decisionResponse.success, 'Decision response must be ok');
|
|
|
|
expect(engineResponse.data.result).toMatchObject(testCase.output);
|
|
expect(decisionResponse.data.result).toMatchObject(testCase.output);
|
|
}
|
|
}
|
|
|
|
});
|
|
});
|
|
|
|
describe('Expressions', () => {
|
|
it('Evaluates standard expressions', async () => {
|
|
const expressions = [
|
|
{ expression: '1 + 1', result: 2 },
|
|
{ expression: 'a > b', context: { a: 5, b: 3 }, result: true },
|
|
{ expression: 'sum(a)', context: { a: [1, 2, 3, 4] }, result: 10 },
|
|
{ expression: 'contains("some", "none")', result: false },
|
|
{ expression: 'matches("test@email.com", "\\w+@\\w+\\.com")', result: true },
|
|
];
|
|
|
|
for (const { expression, result, context } of expressions) {
|
|
expect(await evaluateExpression(expression, context)).toEqual(result);
|
|
expect(evaluateExpressionSync(expression, context)).toEqual(result);
|
|
}
|
|
});
|
|
|
|
it('Evaluates unary expressions', async () => {
|
|
const expressions = [
|
|
{ expression: '>= 5', context: { $: 5 }, result: true },
|
|
{ expression: '< 5', context: { $: 5 }, result: false },
|
|
{ expression: '"FR", "ES"', context: { $: 'GB' }, result: false },
|
|
{ expression: 'contains($, "some")', context: { $: 'some-string' }, result: true },
|
|
];
|
|
|
|
for (const { expression, result, context } of expressions) {
|
|
expect(await evaluateUnaryExpression(expression, context)).toEqual(result);
|
|
expect(evaluateUnaryExpressionSync(expression, context)).toEqual(result);
|
|
}
|
|
});
|
|
|
|
it('Renders templates', async () => {
|
|
const templateCases = [
|
|
{ template: '{{ a + 10 }}', context: { a: 10 }, result: 20 },
|
|
{ template: '{{ a + 10 }}', context: { a: 15 }, result: 25 },
|
|
{ template: '{{ a + 10 }}', context: { a: 20 }, result: 30 },
|
|
{ template: '{{ a + 10 }}', context: { a: 25 }, result: 35 },
|
|
];
|
|
|
|
for (const { template, context, result } of templateCases) {
|
|
expect(await renderTemplate(template, context)).toEqual(result);
|
|
expect(renderTemplateSync(template, context)).toEqual(result);
|
|
}
|
|
});
|
|
|
|
it('Serialises traces in the string and reference modes on both engine versions', async () => {
|
|
const timeless = (value: any): any => {
|
|
if (Array.isArray(value)) return value.map(timeless);
|
|
if (value && typeof value === 'object') {
|
|
return Object.fromEntries(
|
|
Object.entries(value)
|
|
.filter(([key]) => key !== 'performance')
|
|
.map(([key, inner]) => [key, timeless(inner)]),
|
|
);
|
|
}
|
|
return value;
|
|
};
|
|
|
|
for (const version of [1, 2] as const) {
|
|
const engine = new ZenEngine({ loader, version });
|
|
const input = { input: 12 };
|
|
const plain: any = await engine.evaluate('table.json', input, { trace: 'legacy' });
|
|
const text: any = await engine.evaluate('table.json', input, { trace: 'string' });
|
|
const reference: any = await engine.evaluate('table.json', input, { trace: 'reference' });
|
|
const referenceText: any = await engine.evaluate('table.json', input, { trace: 'referenceString' });
|
|
|
|
expect(typeof plain.trace).toEqual('object');
|
|
expect(typeof text.trace).toEqual('string');
|
|
expect(timeless(JSON.parse(text.trace))).toEqual(timeless(plain.trace));
|
|
expect(typeof reference.trace).toEqual('object');
|
|
expect(typeof referenceText.trace).toEqual('string');
|
|
expect(Object.keys(JSON.parse(referenceText.trace)).sort()).toEqual(Object.keys(reference.trace).sort());
|
|
for (const response of [plain, text, reference, referenceText]) {
|
|
expect(response.result).toEqual(plain.result);
|
|
}
|
|
|
|
const decision = await engine.getDecision('table.json');
|
|
const direct: any = await decision.evaluate(input, { trace: 'string' });
|
|
expect(timeless(JSON.parse(direct.trace))).toEqual(timeless(plain.trace));
|
|
|
|
await expect((async () => engine.evaluate('table.json', input, { trace: 'nonsense' as any }))()).rejects.toThrow();
|
|
}
|
|
});
|
|
}); |