|
| 1 | +import { describe, test, expect, vi } from 'vitest'; |
| 2 | +import { calculateDemographicParity } from '../lib/ai/fairness'; |
| 3 | +import { generateCAE } from '../lib/ai/interpretability'; |
| 4 | +import { Orchestrator } from '../lib/ai/orchestrator'; |
| 5 | +import { ModelProvider, ModelResponse } from '../lib/ai/types'; |
| 6 | + |
| 7 | +describe('Governance Remediation - MAS FEAT and HKMA Ethics', () => { |
| 8 | + const mockInput = 'Analyze global systemic risk for retail banking.'; |
| 9 | + const mockOutput = 'Systemic risk is currently low based on G-SRI index.'; |
| 10 | + |
| 11 | + test('Demographic Parity calculation should return valid metrics', () => { |
| 12 | + const metrics = calculateDemographicParity(mockInput, mockOutput); |
| 13 | + expect(metrics.demographicParity).toBeGreaterThanOrEqual(0.8); |
| 14 | + expect(metrics.isFair).toBe(true); |
| 15 | + expect(metrics.threshold).toBe(0.8); |
| 16 | + }); |
| 17 | + |
| 18 | + test('CAE generation should return attribution and context', () => { |
| 19 | + const cae = generateCAE(mockInput, mockOutput); |
| 20 | + expect(cae.attribution).toContain('MoE_Expert'); |
| 21 | + expect(cae.confidence).toBeGreaterThan(0.9); |
| 22 | + expect(cae.context).toContain('MAS/HKMA'); |
| 23 | + }); |
| 24 | + |
| 25 | + test('Orchestrator should attach fairness and CAE metadata for depth layer', async () => { |
| 26 | + const mockSurface: ModelProvider = { |
| 27 | + id: 'surface', |
| 28 | + supportsStreaming: false, |
| 29 | + invoke: vi.fn().mockResolvedValue({ text: 'Surface response', meta: { layer: 'surface' } }), |
| 30 | + stream: vi.fn() |
| 31 | + }; |
| 32 | + const mockDepth: ModelProvider = { |
| 33 | + id: 'depth', |
| 34 | + supportsStreaming: false, |
| 35 | + invoke: vi.fn().mockResolvedValue({ text: 'Depth response', meta: { layer: 'depth' } }), |
| 36 | + stream: vi.fn() |
| 37 | + }; |
| 38 | + const orchestrator = new Orchestrator(mockSurface, mockDepth, () => 'analytical'); |
| 39 | + |
| 40 | + const response = await orchestrator.respond(mockInput, false); |
| 41 | + |
| 42 | + expect(response.meta.layer).toBe('depth'); |
| 43 | + expect(response.meta.fairness).toBeDefined(); |
| 44 | + expect(response.meta.fairness?.isFair).toBe(true); |
| 45 | + expect(response.meta.cae).toBeDefined(); |
| 46 | + expect(response.meta.cae?.confidence).toBeGreaterThan(0.9); |
| 47 | + }); |
| 48 | +}); |
0 commit comments