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('Integration Tests', () => { const arbiter = createMockArbiter(); const compiler = new DSLCompiler(arbiter); test('Complete authorization system', () => { const completeSystem = ` // Type definitions with complex behaviors definition Employee { role: Role group: Group 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 clearance: string BEHAVES { blurring fixed } CACHE eager reputation: number BEHAVES { decaying up daily } CACHE lazy activityScore: number verificationLevel: number } definition Role { permissions: Permission[] clearance: string } definition Group { name: string permissions: Permission[] level: string clearance: string isPublic: boolean CACHE eager created: timestamp BEHAVES { decaying stable monthly } CACHE lazy } definition Permission { name: string level: 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 isPublic: boolean CACHE eager } definition Resource { level: string owner: Employee permissions: Permission[] isPublic: boolean CACHE eager accessCount: number BEHAVES { decaying up daily } CACHE eager } // Facts with various properties and caching fact hasRole(user: Employee, role: string) CACHE eager fact isMember(user: any, group: any) transitive CACHE lazy limit 10 fact isFriend(user: any, friend: any) symmetrical CACHE eager limit 100 fact owns(user: Employee, doc: Document) CACHE eager fact isSuspended(user: Employee) CACHE lazy fact hasPermission(user: Employee, resource: Resource, action: string) CACHE eager fact isAdmin(user: Employee) CACHE eager fact isOwner(user: Employee, resource: Resource) CACHE eager fact hasAccess(user: Employee, resource: Resource, level: string) CACHE lazy fact isColleague(user: any, colleague: any) symmetrical CACHE lazy limit 50 fact isParentOf(parent: Employee, child: Employee) transitive CACHE eager limit 3 fact hasClearance(user: Employee, level: string) CACHE eager fact parentOf(user: any, parent: any) CACHE eager fact similar(a: any, b: any) CACHE lazy fact isActive(user: Employee) CACHE eager fact isTrusted(user: Employee) CACHE eager fact isBlacklisted(user: Employee) CACHE lazy fact hasRecentActivity(user: Employee) CACHE lazy fact recentlyActive(user: Employee) CACHE lazy // Evidence rules with complex logic evidence canRead(user: Employee, doc: Document) { owns(user, doc) isMember(user, *group) { canRead(group, doc) } limit 5 parentOf(user, *parent) { canRead(parent, doc) } limit 3 similar(doc, *similar) |similarity| { canRead(user, similar) } limit 5 with similarity > 0.7 WHEN hasRole(user, 'admin') UNLESS isSuspended(user) } evidence canWrite(user: Employee, doc: Document) { owns(user, doc) isMember(user, *group) { canWrite(group, doc) } limit 3 WHEN hasRole(user, 'admin') UNLESS isSuspended(user) REQUIRES isActive(user) } evidence canDelete(user: Employee, doc: Document) { owns(user, doc) ALWAYS isActive(user) WHEN hasRole(user, 'admin') UNLESS isSuspended(user) REQUIRES isActive(user) } evidence canAccessCritical(user: Employee, resource: Resource) { fusion min { hasClearance(user, resource.level), isActive(user), NOT isBlacklisted(user) } fusion max { hasRole(user, 'admin') } fusion majority { hasClearance(user, 'secret'), isTrusted(user), recentlyActive(user) } } evidence canAccessSensitive(user: Employee, doc: Document) { ALWAYS isActive(user) WHEN hasRole(user, 'admin') UNLESS isSuspended(user) REQUIRES hasClearance(user, doc.level) fusion majority { isTrusted(user), hasRecentActivity(user) } } // Measures for computed values measure userRole(user: Employee) { user.role } PROVIDES string measure userPermissions(user: Employee) { fusion max { user.role.permissions, user.group.permissions } } PROVIDES Permission measure effectiveClearance(user: Employee) { fusion majority { user.clearance, user.role.clearance, user.group.clearance } } PROVIDES string measure userTrustScore(user: Employee) { fusion average { user.reputation, user.activityScore, user.verificationLevel } } PROVIDES number measure userBalance(user: Employee) { user.balance } PROVIDES number measure userScore(user: Employee) { user.score } PROVIDES number `; const result = compiler.compile(completeSystem, 'test-complete-system'); assert.ok(result.success, 'Complete authorization system should compile successfully'); assert.ok(result.program.definitions.length >= 4, 'Should have multiple definitions'); assert.ok(result.program.facts.length >= 10, 'Should have multiple facts'); assert.ok(result.program.evidence.length >= 5, 'Should have multiple evidence rules'); assert.ok(result.program.measures.length >= 6, 'Should have multiple measures'); }); test('Multi-domain system', () => { const multiDomain = ` // Authentication domain definition Employee { role: string isActive: boolean lastActive: timestamp BEHAVES { decaying down hourly } CACHE lazy session: string BEHAVES { ttl 24h } CACHE eager } fact hasRole(user: Employee, role: string) CACHE eager fact isActive(user: Employee) CACHE eager fact recentlyActive(user: any) CACHE lazy fact isPublic(doc: any) CACHE eager evidence canAuthenticate(user: Employee) { isActive(user) recentlyActive(user) } // Authorization domain definition Resource { level: string owner: Employee permissions: Permission[] } definition Permission { name: string level: string } fact owns(user: Employee, resource: Resource) CACHE eager fact hasPermission(user: Employee, resource: Resource, action: string) CACHE eager evidence canAccess(user: Employee, resource: Resource) { owns(user, resource) hasPermission(user, resource, 'read') } // Finance domain definition Tenant { balance: number BEHAVES { decaying down hourly } CACHE eager owner: Employee isActive: boolean CACHE eager } fact hasAccount(user: Employee, account: Tenant) CACHE eager fact hasBalance(user: Employee, amount: number) CACHE eager evidence canWithdraw(user: Employee, amount: number) { hasBalance(user, amount) isActive(user) } // Social domain definition Group { name: string members: Employee[] isPublic: boolean CACHE eager } fact isMember(user: any, group: any) transitive CACHE lazy limit 10 fact isFriend(user: any, friend: any) symmetrical CACHE eager limit 100 evidence canAccessGroup(user: Employee, group: Group) { isMember(user, group) isPublic(group) } `; const result = compiler.compile(multiDomain, 'test-multi-domain'); assert.ok(result.success, 'Multi-domain system should compile successfully'); assert.ok(result.program.definitions.length >= 4, 'Should have multiple domain definitions'); assert.ok(result.program.facts.length >= 8, 'Should have multiple domain facts'); assert.ok(result.program.evidence.length >= 4, 'Should have multiple domain evidence rules'); }); test('Hierarchical access', () => { const hierarchicalSystem = ` definition Employee { role: string level: string isActive: boolean clearance: string } definition Organization { name: string level: string parent: Organization } definition Resource { level: string } fact isMember(user: any, org: any) transitive CACHE lazy limit 5 fact isParentOf(parent: Organization, child: Organization) transitive CACHE eager limit 3 fact hasRole(user: Employee, role: string) CACHE eager fact hasClearance(user: Employee, level: string) CACHE eager fact isSuspended(user: any) CACHE lazy fact parentOf(user: any, parent: any) CACHE eager evidence canAccessOrg(user: Employee, org: Organization) { isMember(user, org) isParentOf(org, *parentOrg) { canAccessOrg(user, parentOrg) } limit 3 WHEN hasRole(user, 'admin') UNLESS isSuspended(user) } evidence canAccessResource(user: Employee, resource: Resource) { isMember(user, *org) { canAccessResource(org, resource) } limit 5 parentOf(user, *parent) { canAccessResource(parent, resource) } limit 2 } `; const result = compiler.compile(hierarchicalSystem, 'test-hierarchical'); assert.ok(result.success, 'Hierarchical access system should compile successfully'); }); test('Similarity-based access', () => { const similaritySystem = ` definition Employee { profile: string interests: string[] isActive: boolean } definition Document { content: string tags: string[] isPublic: boolean owner: Employee } fact isFriend(user: any, friend: any) symmetrical CACHE eager limit 100 fact hasInterest(user: any, interest: string) CACHE lazy fact hasTag(doc: any, tag: string) CACHE lazy fact owns(user: any, doc: any) CACHE eager fact similar(a: any, b: any) CACHE lazy fact isPublic(doc: any) CACHE eager fact hasInterests(user: any) CACHE lazy fact hasTags(doc: any) CACHE lazy fact hasProfile(user: any) CACHE lazy fact hasContent(doc: any) CACHE lazy evidence canRead(user: Employee, doc: Document) { owns(user, doc) similar(doc, *similar) |similarity| { canRead(user, similar) isPublic(similar) } limit 10 with similarity > 0.7 isFriend(user, *friend) { canRead(friend, doc) } limit 5 fusion majority { hasInterests(user), hasTags(doc) } } evidence canRecommend(user: Employee, doc: Document) { similar(user, *similarUser) |similarity| { canRead(similarUser, doc) } limit 20 with similarity > 0.8 fusion average { hasProfile(user), hasContent(doc) } } `; const result = compiler.compile(similaritySystem, 'test-similarity'); assert.ok(result.success, 'Similarity-based access system should compile successfully'); }); test('Temporal access', () => { const temporalSystem = ` definition Employee { lastActive: timestamp BEHAVES { decaying down hourly } CACHE lazy session: string BEHAVES { ttl 24h } CACHE eager isActive: boolean } definition Event { startTime: timestamp endTime: timestamp isPublic: boolean } fact hasAccess(user: Employee, event: Event) CACHE lazy fact isParticipant(user: Employee, event: Event) CACHE eager fact recentlyActive(user: any) CACHE lazy fact sessionFresh(user: any) CACHE lazy fact isPublic(doc: any) CACHE eager fact isSuspended(user: any) CACHE lazy fact isActive(user: any) CACHE eager evidence canAccessEvent(user: Employee, event: Event) { recentlyActive(user) isParticipant(user, event) WHEN isPublic(event) UNLESS isSuspended(user) fusion min { sessionFresh(user), isActive(user) } } evidence canAccessHistorical(user: Employee, event: Event) { recentlyActive(user) fusion majority { isActive(user), sessionFresh(user), isPublic(event) } } `; const result = compiler.compile(temporalSystem, 'test-temporal'); assert.ok(result.success, 'Temporal access system should compile successfully'); }); test('Complex behaviors', () => { const behaviorSystem = ` definition Employee { balance: number BEHAVES { decaying down hourly } CACHE eager score: number BEHAVES { blurring adaptive confidence_95 } CACHE lazy session: string BEHAVES { ttl 24h } CACHE eager reputation: number BEHAVES { decaying up daily } CACHE lazy clearance: string BEHAVES { blurring fixed } CACHE eager lastActive: timestamp BEHAVES { decaying down hourly } CACHE lazy } definition Document { content: string BEHAVES { blurring fixed } CACHE lazy accessCount: number BEHAVES { decaying up daily } CACHE eager expiresAt: timestamp BEHAVES { ttl 30d } CACHE eager isPublic: boolean CACHE eager } fact hasBalance(user: Employee, amount: number) CACHE eager fact hasScore(user: Employee, score: number) CACHE lazy fact hasReputation(user: Employee, reputation: number) CACHE lazy fact hasPositiveBalance(user: any) CACHE eager fact hasHighScore(user: any) CACHE eager fact hasGoodReputation(user: any) CACHE eager fact isNotOverused(doc: any) CACHE eager fact isActive(user: any) CACHE eager fact recentlyActive(user: any) CACHE lazy fact isPublic(doc: any) CACHE eager evidence canAccessDocument(user: Employee, doc: Document) { hasPositiveBalance(user) hasHighScore(user) hasGoodReputation(user) isNotOverused(doc) fusion majority { isActive(user), recentlyActive(user), isPublic(doc) } } measure userEffectiveScore(user: Employee) { fusion average { user.score, user.reputation, user.balance } } PROVIDES number measure documentPopularity(doc: Document) { doc.accessCount } PROVIDES number `; const result = compiler.compile(behaviorSystem, 'test-behaviors'); assert.ok(result.success, 'Complex behaviors system should compile successfully'); }); test('Performance scenarios', () => { const performanceSystem = ` definition Employee { role: string isActive: boolean permissions: Permission[] CACHE eager } definition Resource { level: string owner: Employee permissions: Permission[] CACHE eager } definition Permission { name: string level: string } // High-frequency facts with limits fact isMember(user: any, group: any) transitive CACHE lazy limit 5 fact isFriend(user: any, friend: any) symmetrical CACHE eager limit 50 fact hasPermission(user: Employee, resource: Resource, action: string) CACHE eager fact owns(user: Employee, resource: Resource) CACHE eager // Optimized evidence rules evidence canAccess(user: Employee, resource: Resource) { owns(user, resource) isMember(user, *group) { canAccess(group, resource) } limit 3 WHEN hasPermission(user, resource, 'read') } evidence canModify(user: Employee, resource: Resource) { owns(user, resource) isMember(user, *group) { canModify(group, resource) } limit 2 WHEN hasPermission(user, resource, 'write') } // Efficient measures measure userEffectivePermissions(user: Employee) { user.permissions } PROVIDES Permission measure resourceAccessLevel(resource: Resource) { resource.level } PROVIDES string `; const result = compiler.compile(performanceSystem, 'test-performance'); assert.ok(result.success, 'Performance scenarios should compile successfully'); }); });