Files
evidence-dsl/tests/DefinitionTests.js
T

289 lines
8.6 KiB
JavaScript
Raw Normal View History

import { describe, test } from 'node:test';
import assert from 'node:assert/strict';
import { DSLCompiler } from '../src/DSLCompiler.js';
function createMockArbiter() {
const relationConfigs = new Map();
return {
relationConfigs,
setRelationConfig(relation, config) {
relationConfigs.set(relation, config);
}
};
}
describe('Type Definitions', () => {
const arbiter = createMockArbiter();
const compiler = new DSLCompiler(arbiter);
test('Basic definitions', () => {
const testCases = [
{
input: `definition Employee { role: string }`,
description: 'Simple definition with one field'
},
{
input: `definition Employee {
role: string
isActive: boolean
}`,
description: 'Definition with multiple fields'
},
{
input: `definition Group {
name: string
description: string
created: timestamp
}`,
description: 'Definition with different field types'
}
];
testCases.forEach(({ input, description }) => {
const result = compiler.compile(input, `test-basic-def-${Date.now()}`);
assert.ok(result.success, `${description} should parse successfully`);
assert.ok(result.program.definitions.length > 0, 'Should have definitions');
});
});
test('Field types', () => {
const testCases = [
{ type: 'string', description: 'String field type' },
{ type: 'number', description: 'Number field type' },
{ type: 'boolean', description: 'Boolean field type' },
{ type: 'timestamp', description: 'Timestamp field type' },
{ type: 'User', description: 'Custom type field' },
{ type: 'Permission', description: 'Another custom type field' }
];
testCases.forEach(({ type, description }) => {
const dsl = `definition Permission { name: string }
definition Test { field: ${type} }`;
const result = compiler.compile(dsl, `test-field-type-${Date.now()}`);
assert.ok(result.success, `${description} should parse successfully`);
});
});
test('Array types', () => {
const testCases = [
{ type: 'string[]', description: 'String array' },
{ type: 'number[]', description: 'Number array' },
{ type: 'boolean[]', description: 'Boolean array' },
{ type: 'Permission[]', description: 'Custom type array' },
{ type: 'User[]', description: 'User array' }
];
testCases.forEach(({ type, description }) => {
const dsl = `definition Permission { name: string }
definition Test { items: ${type} }`;
const result = compiler.compile(dsl, `test-array-${Date.now()}`);
assert.ok(result.success, `${description} should parse successfully`);
});
});
test('Behaviors', () => {
const testCases = [
{
input: `definition Employee {
balance: number BEHAVES { decaying down hourly }
}`,
description: 'Decay behavior - down hourly'
},
{
input: `definition Employee {
reputation: number BEHAVES { decaying up daily }
}`,
description: 'Decay behavior - up daily'
},
{
input: `definition Employee {
score: number BEHAVES { decaying neutral weekly }
}`,
description: 'Decay behavior - neutral weekly'
},
{
input: `definition Employee {
stability: number BEHAVES { decaying stable monthly }
}`,
description: 'Decay behavior - stable monthly'
},
{
input: `definition Employee {
confidence: number BEHAVES { blurring fixed }
}`,
description: 'Blur behavior - fixed'
},
{
input: `definition Employee {
accuracy: number BEHAVES { blurring adaptive }
}`,
description: 'Blur behavior - adaptive'
},
{
input: `definition Employee {
precision: number BEHAVES { blurring confidence confidence_90 }
}`,
description: 'Blur behavior - confidence with level'
},
{
input: `definition Employee {
session: string BEHAVES { ttl 1h }
}`,
description: 'TTL behavior - hours'
},
{
input: `definition Employee {
token: string BEHAVES { ttl 24h }
}`,
description: 'TTL behavior - 24 hours'
},
{
input: `definition Employee {
cache: string BEHAVES { ttl 7d }
}`,
description: 'TTL behavior - days'
}
];
testCases.forEach(({ input, description }) => {
const result = compiler.compile(input, `test-behavior-${Date.now()}`);
assert.ok(result.success, `${description} should parse successfully`);
});
});
test('Caching', () => {
const testCases = [
{
input: `definition Employee {
role: string CACHE eager
}`,
description: 'Eager caching'
},
{
input: `definition Employee {
score: number CACHE lazy
}`,
description: 'Lazy caching'
},
{
input: `definition Employee {
balance: number BEHAVES { decaying down hourly } CACHE eager
}`,
description: 'Behavior with eager caching'
},
{
input: `definition Employee {
reputation: number BEHAVES { blurring adaptive } CACHE lazy
}`,
description: 'Behavior with lazy caching'
}
];
testCases.forEach(({ input, description }) => {
const result = compiler.compile(input, `test-cache-${Date.now()}`);
assert.ok(result.success, `${description} should parse successfully`);
});
});
test('Complex definitions', () => {
const testCases = [
{
input: `definition Employee {
role: string
isActive: boolean
lastActive: timestamp BEHAVES {
decaying down hourly
} CACHE lazy
isSuspended: boolean
balance: number BEHAVES {
decaying down hourly
} CACHE eager
score: number BEHAVES {
blurring adaptive confidence_95
} CACHE lazy
session: string BEHAVES {
ttl 24h
} CACHE eager
}`,
description: 'Complex definition with multiple behaviors and caching'
},
{
input: `definition Employee { role: string }
definition Permission { name: string }
definition Group {
name: string
permissions: Permission[]
members: Employee[]
created: timestamp BEHAVES {
decaying stable monthly
} CACHE lazy
isPublic: boolean CACHE eager
}`,
description: 'Definition with arrays and mixed behaviors'
},
{
input: `definition Employee { role: string }
definition Document {
level: string
owner: Employee
tags: string[]
content: string BEHAVES {
blurring fixed
} CACHE lazy
accessCount: number BEHAVES {
decaying up daily
} CACHE eager
expiresAt: timestamp BEHAVES {
ttl 30d
} CACHE eager
}`,
description: 'Definition with all behavior types'
}
];
testCases.forEach(({ input, description }) => {
const result = compiler.compile(input, `test-complex-def-${Date.now()}`);
assert.ok(result.success, `${description} should parse successfully`);
assert.ok(result.program.definitions.length > 0, 'Should have definitions');
});
});
test('Definition error handling', () => {
const testCases = [
{
input: `definition Employee { role: string`,
description: 'Missing closing brace should fail'
},
{
input: `definition Employee { role: }`,
description: 'Missing field type should fail'
},
{
input: `definition Employee { : string }`,
description: 'Missing field name should fail'
},
{
input: `definition Employee { role: string BEHAVES { }`,
description: 'Incomplete behavior should fail'
},
{
input: `definition Employee { role: string CACHE }`,
description: 'Incomplete cache directive should fail'
},
{
input: `definition Employee { role: string BEHAVES { invalid } }`,
description: 'Invalid behavior should fail'
}
];
testCases.forEach(({ input, description }) => {
try {
const result = compiler.compile(input, `test-def-error-${Date.now()}`);
assert.ok(!result.success, `${description} should fail to parse`);
} catch {
// Expected to fail
}
});
});
});