cognitive-framework-mcp-server
Version:
MCP Server for Advanced Cognitive Framework - Provides sophisticated AI reasoning capabilities through standardized protocol
263 lines (226 loc) • 9.81 kB
text/typescript
/**
* Framework Parser Tests
* Test suite for the cognitive framework parser
*/
import { FrameworkParser } from '../src/parsers/framework-parser';
import { writeFileSync, unlinkSync } from 'fs';
import { join } from 'path';
describe('FrameworkParser', () => {
const testFrameworkPath = join(__dirname, 'test-framework.xml');
let parser: FrameworkParser;
const mockFrameworkXML = `
<AdvancedCognitiveFramework priority="ABSOLUTE_MAXIMUM">
<CognitiveIdentity>
<CoreMandate>Test cognitive framework for unit testing</CoreMandate>
<CognitiveTraits>
<Trait name="TestTrait">A test cognitive trait</Trait>
</CognitiveTraits>
<MetaCognitiveCapabilities>
<Capability name="TestCapability">A test meta-cognitive capability</Capability>
</MetaCognitiveCapabilities>
<CognitiveLoadManagement>
<Principle name="TestPrinciple">Test principle for load management</Principle>
<Metric name="TestMetric">Test metric for performance</Metric>
</CognitiveLoadManagement>
<LearningFromCognition>
<Capability name="TestLearning">Test learning capability</Capability>
<Repository name="TestRepo">Test repository</Repository>
</LearningFromCognition>
</CognitiveIdentity>
<ContextManagement>
<DynamicContextEngine>
<Principle name="TestContextPrinciple">Test context principle</Principle>
</DynamicContextEngine>
<ContextSources>
<Source name="TestSource" priority="high">Test context source</Source>
</ContextSources>
<SemanticContextClustering>
<Capability name="TestClustering">Test clustering capability</Capability>
<Structure name="TestStructure">Test structure</Structure>
<Algorithm name="TestAlgorithm">Test algorithm</Algorithm>
</SemanticContextClustering>
<PredictiveContexting>
<Capability name="TestPredictive">Test predictive capability</Capability>
<Predictor name="TestPredictor">Test predictor</Predictor>
</PredictiveContexting>
<ContextQualityAssurance>
<Metric name="TestQAMetric">Test QA metric</Metric>
<Validator name="TestValidator">Test validator</Validator>
<Optimizer name="TestOptimizer">Test optimizer</Optimizer>
</ContextQualityAssurance>
<AdaptiveIntelligence>
<Capability name="TestAdaptive">Test adaptive capability</Capability>
</AdaptiveIntelligence>
</ContextManagement>
<CognitivePrinciples>
<Principle name="TestPrinciple">
Test principle description
<Nuance>Test nuance</Nuance>
<Example>Test example</Example>
</Principle>
</CognitivePrinciples>
<CognitiveHeuristics>
<Heuristic name="TestHeuristic" facilitates="Testing" related-to="Unit tests">
Test heuristic description
</Heuristic>
</CognitiveHeuristics>
<CognitiveProtocols>
<Protocol name="TestProtocol">
<Purpose>Test protocol purpose</Purpose>
<Usage>Test protocol usage</Usage>
<Action>Test action</Action>
<OutputFormat>Test output format</OutputFormat>
</Protocol>
</CognitiveProtocols>
<PredefinedWorkflows>
<Workflow name="TestWorkflow" tags="test,unit">
<Purpose>Test workflow purpose</Purpose>
<Throughline>Test throughline</Throughline>
<AdaptiveCapabilities>
<Capability name="TestWorkflowCapability">Test workflow capability</Capability>
</AdaptiveCapabilities>
<Stage name="TestStage">
<Objective>Test stage objective</Objective>
<Guidance>Test stage guidance</Guidance>
<Step id="test1">Test step description</Step>
</Stage>
</Workflow>
</PredefinedWorkflows>
<OperationalLoop>
1. Test operational step one
2. Test operational step two
* Test detail one
* Test detail two
3. Test operational step three
</OperationalLoop>
</AdvancedCognitiveFramework>`;
beforeEach(() => {
// Create test framework file
writeFileSync(testFrameworkPath, mockFrameworkXML);
parser = new FrameworkParser(testFrameworkPath);
});
afterEach(() => {
// Clean up test file
try {
unlinkSync(testFrameworkPath);
} catch (error) {
// File might not exist, ignore error
}
});
describe('parseFramework', () => {
it('should parse framework successfully', async () => {
const framework = await parser.parseFramework();
expect(framework).toBeDefined();
expect(framework.identity).toBeDefined();
expect(framework.contextManagement).toBeDefined();
expect(framework.principles).toBeDefined();
expect(framework.heuristics).toBeDefined();
expect(framework.protocols).toBeDefined();
expect(framework.workflows).toBeDefined();
expect(framework.operationalLoop).toBeDefined();
});
it('should parse cognitive identity correctly', async () => {
const framework = await parser.parseFramework();
const identity = framework.identity;
expect(identity.coreMandate).toBe('Test cognitive framework for unit testing');
expect(identity.cognitiveTraits).toHaveLength(1);
expect(identity.cognitiveTraits[0].name).toBe('TestTrait');
expect(identity.metaCognitiveCapabilities).toHaveLength(1);
expect(identity.metaCognitiveCapabilities[0].name).toBe('TestCapability');
});
it('should parse context management correctly', async () => {
const framework = await parser.parseFramework();
const contextMgmt = framework.contextManagement;
expect(contextMgmt.dynamicContextEngine.principles).toHaveLength(1);
expect(contextMgmt.contextSources).toHaveLength(1);
expect(contextMgmt.contextSources[0].priority).toBe('high');
expect(contextMgmt.semanticContextClustering.capabilities).toHaveLength(1);
expect(contextMgmt.predictiveContexting.capabilities).toHaveLength(1);
expect(contextMgmt.contextQualityAssurance.metrics).toHaveLength(1);
expect(contextMgmt.adaptiveIntelligence.capabilities).toHaveLength(1);
});
it('should parse principles correctly', async () => {
const framework = await parser.parseFramework();
const principles = framework.principles.principles;
expect(principles).toHaveLength(1);
expect(principles[0].name).toBe('TestPrinciple');
expect(principles[0].nuances).toHaveLength(1);
expect(principles[0].examples).toHaveLength(1);
});
it('should parse heuristics correctly', async () => {
const framework = await parser.parseFramework();
const heuristics = framework.heuristics.heuristics;
expect(heuristics).toHaveLength(1);
expect(heuristics[0].name).toBe('TestHeuristic');
expect(heuristics[0].facilitates).toBe('Testing');
expect(heuristics[0].relatedTo).toBe('Unit tests');
});
it('should parse protocols correctly', async () => {
const framework = await parser.parseFramework();
const protocols = framework.protocols.protocols;
expect(protocols).toHaveLength(1);
expect(protocols[0].name).toBe('TestProtocol');
expect(protocols[0].purpose).toBe('Test protocol purpose');
expect(protocols[0].usage).toBe('Test protocol usage');
expect(protocols[0].actions).toHaveLength(1);
});
it('should parse workflows correctly', async () => {
const framework = await parser.parseFramework();
const workflows = framework.workflows.workflows;
expect(workflows).toHaveLength(1);
expect(workflows[0].name).toBe('TestWorkflow');
expect(workflows[0].tags).toEqual(['test', 'unit']);
expect(workflows[0].stages).toHaveLength(1);
expect(workflows[0].stages![0].steps).toHaveLength(1);
});
it('should parse operational loop correctly', async () => {
const framework = await parser.parseFramework();
const operationalLoop = framework.operationalLoop;
expect(operationalLoop.steps).toHaveLength(3);
expect(operationalLoop.steps[1].details).toHaveLength(2);
});
it('should throw error for invalid framework path', async () => {
const invalidParser = new FrameworkParser('/invalid/path');
await expect(invalidParser.parseFramework()).rejects.toThrow();
});
});
describe('validateFramework', () => {
it('should validate framework successfully', async () => {
await parser.parseFramework();
const isValid = parser.validateFramework();
expect(isValid).toBe(true);
});
it('should return false for uninitialized framework', () => {
const isValid = parser.validateFramework();
expect(isValid).toBe(false);
});
});
describe('getFrameworkStats', () => {
it('should return framework statistics', async () => {
await parser.parseFramework();
const stats = parser.getFrameworkStats();
expect(stats).toBeDefined();
expect(stats.cognitiveTraits).toBe(1);
expect(stats.metaCognitiveCapabilities).toBe(1);
expect(stats.principles).toBe(1);
expect(stats.heuristics).toBe(1);
expect(stats.protocols).toBe(1);
expect(stats.workflows).toBe(1);
expect(stats.operationalSteps).toBe(3);
});
it('should return empty stats for uninitialized framework', () => {
const stats = parser.getFrameworkStats();
expect(stats).toEqual({});
});
});
describe('getFramework', () => {
it('should return parsed framework', async () => {
const framework = await parser.parseFramework();
const retrievedFramework = parser.getFramework();
expect(retrievedFramework).toEqual(framework);
});
it('should throw error for uninitialized framework', () => {
expect(() => parser.getFramework()).toThrow('Framework not parsed yet');
});
});
});