/**
* Comprehensive tests for CognitiveMCPServer to improve coverage
* Tests all tool handlers and private methods to meet 90% coverage threshold
*/
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { CognitiveMCPServer } from "../../server/CognitiveMCPServer.js";
import { ProcessingMode } from "../../types/core.js";
import type {
AnalyzeMemoryUsageArgs,
AnalyzeSystematicallyArgs,
DecomposeProblemArgs,
ForgettingAuditArgs,
ForgettingPolicyArgs,
OptimizeMemoryArgs,
RecoverMemoryArgs,
ThinkParallelArgs,
ThinkProbabilisticArgs,
} from "../../types/mcp.js";
import { TestCleanup } from "../utils/testCleanup.js";
describe("CognitiveMCPServer Comprehensive Coverage", () => {
let server: CognitiveMCPServer;
let originalEnv: NodeJS.ProcessEnv;
beforeEach(async () => {
// Store original environment
originalEnv = { ...process.env };
// Set up test environment
const testId = Math.random().toString(36).substring(7);
process.env.COGNITIVE_BRAIN_DIR = `./tmp/test-brain-${testId}`;
TestCleanup.initialize();
server = new CognitiveMCPServer();
await server.initialize(true); // Test mode
});
afterEach(async () => {
await server.shutdown();
TestCleanup.cleanup();
// Restore original environment
process.env = originalEnv;
});
describe("Advanced Tool Handlers", () => {
describe("handleAnalyzeSystematically", () => {
it("should analyze problems systematically", async () => {
const args: AnalyzeSystematicallyArgs = {
input: "How can we improve software development processes?",
mode: "auto",
verbosity: "standard",
include_executive_summary: true,
};
const result = await server.handleAnalyzeSystematically(args);
expect(result).toBeDefined();
expect(result.recommended_framework).toBeDefined();
expect(result.analysis_steps).toBeInstanceOf(Array);
expect(result.analysis_steps.length).toBeGreaterThan(0);
expect(result.confidence).toBeGreaterThanOrEqual(0);
expect(result.confidence).toBeLessThanOrEqual(1);
expect(result.processing_time_ms).toBeGreaterThanOrEqual(0);
});
it("should handle different systematic thinking modes", async () => {
const args: AnalyzeSystematicallyArgs = {
input: "Design a new mobile app",
mode: "manual",
verbosity: "detailed",
include_executive_summary: false,
};
const result = await server.handleAnalyzeSystematically(args);
expect(result).toBeDefined();
expect(result.recommended_framework).toBeDefined();
});
});
describe("handleThinkParallel", () => {
it("should process parallel reasoning streams", async () => {
const args: ThinkParallelArgs = {
input: "What are the implications of artificial intelligence?",
enable_coordination: true,
verbosity: "standard",
include_executive_summary: true,
};
const result = await server.handleThinkParallel(args);
expect(result).toBeDefined();
expect(result.stream_results).toBeInstanceOf(Array);
expect(result.stream_results.length).toBeGreaterThan(0);
expect(result.synthesized_conclusion).toBeDefined();
expect(result.coordination).toBeDefined();
expect(result.processing_time_ms).toBeGreaterThanOrEqual(0);
});
it("should handle parallel reasoning without coordination", async () => {
const args: ThinkParallelArgs = {
input: "Analyze climate change solutions",
enable_coordination: false,
verbosity: "summary",
include_executive_summary: false,
};
const result = await server.handleThinkParallel(args);
expect(result).toBeDefined();
expect(result.stream_results).toBeInstanceOf(Array);
});
});
describe("handleThinkProbabilistic", () => {
it("should handle probabilistic reasoning", async () => {
const args: ThinkProbabilisticArgs = {
input: "What is the likelihood of success for this startup idea?",
enable_bayesian_updating: true,
max_hypotheses: 3,
verbosity: "standard",
include_executive_summary: true,
};
const result = await server.handleThinkProbabilistic(args);
expect(result).toBeDefined();
expect(result.alternative_hypotheses).toBeInstanceOf(Array);
expect(result.alternative_hypotheses.length).toBeGreaterThan(0);
expect(result.evidence_integration).toBeDefined();
expect(result.uncertainty_assessment).toBeDefined();
expect(result.processing_time_ms).toBeGreaterThanOrEqual(0);
});
it("should handle probabilistic reasoning with different parameters", async () => {
const args: ThinkProbabilisticArgs = {
input: "Predict market trends for next year",
enable_bayesian_updating: false,
max_hypotheses: 5,
uncertainty_threshold: 0.2,
verbosity: "detailed",
include_executive_summary: false,
};
const result = await server.handleThinkProbabilistic(args);
expect(result).toBeDefined();
expect(result.alternative_hypotheses).toBeInstanceOf(Array);
});
});
describe("handleDecomposeProblem", () => {
it("should decompose complex problems", async () => {
const args: DecomposeProblemArgs = {
input: "Build a comprehensive e-commerce platform",
max_depth: 3,
strategies: ["functional", "temporal"],
verbosity: "standard",
include_executive_summary: true,
};
const result = await server.handleDecomposeProblem(args);
expect(result).toBeDefined();
expect(result.hierarchical_structure).toBeInstanceOf(Array);
expect(result.hierarchical_structure.length).toBeGreaterThan(0);
expect(result.dependencies).toBeInstanceOf(Array);
expect(result.processing_time_ms).toBeGreaterThanOrEqual(0);
});
it("should handle different decomposition strategies", async () => {
const args: DecomposeProblemArgs = {
input: "Organize a large conference",
max_depth: 2,
strategies: ["stakeholder", "risk"],
verbosity: "detailed",
include_executive_summary: false,
};
const result = await server.handleDecomposeProblem(args);
expect(result).toBeDefined();
expect(result.hierarchical_structure).toBeInstanceOf(Array);
});
});
describe("handleAnalyzeMemoryUsage", () => {
it("should analyze memory usage patterns", async () => {
// First store some memories
await server.handleRemember({
content: "Test memory for analysis",
type: "semantic",
importance: 0.8,
});
const args: AnalyzeMemoryUsageArgs = {
analysis_depth: "deep",
include_recommendations: true,
verbosity: "standard",
include_executive_summary: true,
};
const result = await server.handleAnalyzeMemoryUsage(args);
expect(result).toBeDefined();
expect(result.analysis).toBeDefined();
expect(result.analysis_time_ms).toBeGreaterThanOrEqual(0);
});
it("should handle different analysis depths", async () => {
const args: AnalyzeMemoryUsageArgs = {
analysis_depth: "shallow",
include_recommendations: false,
verbosity: "summary",
include_executive_summary: false,
};
const result = await server.handleAnalyzeMemoryUsage(args);
expect(result).toBeDefined();
expect(result.analysis).toBeDefined();
});
});
describe("handleOptimizeMemory", () => {
it("should optimize memory with conservative mode", async () => {
// First store some memories
await server.handleRemember({
content: "Memory to optimize",
type: "episodic",
importance: 0.3,
});
const args: OptimizeMemoryArgs = {
optimization_mode: "conservative",
target_memory_reduction: 0.1,
preserve_important_memories: true,
require_user_consent: false,
verbosity: "standard",
include_executive_summary: true,
};
const result = await server.handleOptimizeMemory(args);
expect(result).toBeDefined();
expect(result.success).toBe(true);
expect(result.optimization_summary).toBeDefined();
expect(
result.optimization_summary.memories_processed
).toBeGreaterThanOrEqual(0);
expect(
result.optimization_summary.total_space_freed_bytes
).toBeGreaterThanOrEqual(0);
expect(result.optimization_time_ms).toBeGreaterThanOrEqual(0);
});
it("should handle different optimization modes", async () => {
const args: OptimizeMemoryArgs = {
optimization_mode: "moderate",
target_memory_reduction: 0.2,
preserve_important_memories: true,
require_user_consent: true,
enable_gradual_degradation: true,
verbosity: "detailed",
include_executive_summary: false,
};
const result = await server.handleOptimizeMemory(args);
expect(result).toBeDefined();
expect(result.success).toBe(true);
expect(result.optimization_summary).toBeDefined();
});
});
describe("handleRecoverMemory", () => {
it("should recover forgotten memories", async () => {
const args: RecoverMemoryArgs = {
memory_id: "test_memory_123",
recovery_cues: [
{
type: "semantic",
value: "important information",
strength: 0.8,
},
{
type: "temporal",
value: "recent",
strength: 0.6,
},
],
max_recovery_attempts: 3,
verbosity: "standard",
include_executive_summary: true,
};
const result = await server.handleRecoverMemory(args);
expect(result).toBeDefined();
expect(result.success).toBe(true);
expect(result.recovery_attempts).toBeInstanceOf(Array);
expect(result.recovery_confidence).toBeGreaterThanOrEqual(0);
expect(result.recovery_confidence).toBeLessThanOrEqual(1);
expect(result.recovery_time_ms).toBeGreaterThanOrEqual(0);
});
it("should handle recovery with different strategies", async () => {
const args: RecoverMemoryArgs = {
memory_id: "another_test_memory",
recovery_cues: [
{
type: "associative",
value: "related concept",
strength: 0.7,
},
],
recovery_strategies: ["associative_recovery"],
confidence_threshold: 0.5,
verbosity: "detailed",
include_executive_summary: false,
};
const result = await server.handleRecoverMemory(args);
expect(result).toBeDefined();
expect(result.success).toBe(true);
expect(result.recovery_attempts).toBeInstanceOf(Array);
});
});
describe("handleForgettingAudit", () => {
it("should perform forgetting audit", async () => {
const args: ForgettingAuditArgs = {
verbosity: "standard",
include_executive_summary: true,
};
const result = await server.handleForgettingAudit(args);
expect(result).toBeDefined();
expect(result.success).toBe(true);
expect(result.audit_entries).toBeInstanceOf(Array);
expect(result.query_time_ms).toBeGreaterThanOrEqual(0);
});
it("should handle audit with query filters", async () => {
const args: ForgettingAuditArgs = {
query: {
limit: 10,
execution_status: ["executed"],
memory_ids: ["test_memory"],
},
verbosity: "detailed",
include_executive_summary: false,
};
const result = await server.handleForgettingAudit(args);
expect(result).toBeDefined();
expect(result.success).toBe(true);
expect(result.audit_entries).toBeInstanceOf(Array);
});
});
describe("handleForgettingPolicy", () => {
it("should list forgetting policies", async () => {
const args: ForgettingPolicyArgs = {
action: "list",
verbosity: "standard",
include_executive_summary: true,
};
const result = await server.handleForgettingPolicy(args);
expect(result).toBeDefined();
expect(result.success).toBe(true);
expect(result.policies).toBeInstanceOf(Array);
expect(result.processing_time_ms).toBeGreaterThanOrEqual(0);
});
it("should create new forgetting policy", async () => {
const args: ForgettingPolicyArgs = {
action: "create",
policy_data: {
policy_name: "test_policy",
description: "Test policy for unit tests",
active: true,
rules: [
{
rule_name: "test_rule",
conditions: [
{
condition_type: "importance_threshold",
operator: "less_than",
value: 0.3,
},
],
condition_logic: "AND",
action: "allow",
priority: 1,
},
],
},
verbosity: "standard",
include_executive_summary: true,
};
const result = await server.handleForgettingPolicy(args);
expect(result).toBeDefined();
expect(result.success).toBe(true);
expect(result.policy_id).toBeDefined();
});
});
});
describe("Server Lifecycle and Configuration", () => {
it("should handle multiple initialization calls gracefully", async () => {
// Server is already initialized in beforeEach
await expect(server.initialize(true)).resolves.not.toThrow();
expect(server.isInitialized()).toBe(true);
});
it("should provide server information", () => {
const info = server.getServerInfo();
expect(info).toBeDefined();
expect(info.name).toBe("thoughtmcp");
expect(info.version).toBeDefined();
expect(info.initialized).toBe(true);
});
it("should handle request method (placeholder)", async () => {
await expect(server.handleRequest("test_method", {})).rejects.toThrow(
"Request handling not yet implemented"
);
});
});
describe("Error Handling and Edge Cases", () => {
it("should handle validation errors for analyze systematically", async () => {
await expect(
server.handleAnalyzeSystematically({
input: "", // Empty input should fail validation
mode: "auto",
})
).rejects.toThrow();
});
it("should handle validation errors for think parallel", async () => {
await expect(
server.handleThinkParallel({
input: "", // Empty input should fail validation
})
).rejects.toThrow();
});
it("should handle validation errors for think probabilistic", async () => {
// Test that the method handles invalid input gracefully - it might not throw but should handle edge cases
const result = await server.handleThinkProbabilistic({
input: "test",
max_hypotheses: -1, // Invalid negative value - might be handled gracefully
});
// The method should still return a valid result even with edge case parameters
expect(result).toBeDefined();
expect(result.alternative_hypotheses).toBeDefined();
});
it("should handle validation errors for decompose problem", async () => {
await expect(
server.handleDecomposeProblem({
input: "", // Empty input should fail validation
})
).rejects.toThrow();
});
it("should handle validation errors for recover memory", async () => {
await expect(
server.handleRecoverMemory({
memory_id: "", // Empty memory_id should fail validation
recovery_cues: [],
})
).rejects.toThrow();
});
it("should handle shutdown when already shut down", async () => {
await server.shutdown();
await expect(server.shutdown()).resolves.not.toThrow();
});
});
describe("Private Method Coverage via Public Interface", () => {
it("should exercise complexity estimation through think", async () => {
// Test complex input to exercise estimateComplexity
const complexInput =
"How can we solve climate change while maintaining economic growth and ensuring social equity across multiple stakeholders including governments, corporations, and citizens?";
const result = await server.handleThink({
input: complexInput,
mode: ProcessingMode.ANALYTICAL,
});
expect(result).toBeDefined();
expect(result.content).toBeDefined();
});
it("should exercise uncertainty estimation through probabilistic thinking", async () => {
// Test uncertain input to exercise estimateUncertainty
const uncertainInput =
"What might happen if we maybe implement this possibly risky strategy that could potentially work?";
const result = await server.handleThinkProbabilistic({
input: uncertainInput,
enable_bayesian_updating: true,
});
expect(result).toBeDefined();
expect(result.alternative_hypotheses).toBeDefined();
});
it("should exercise domain identification through systematic analysis", async () => {
// Test domain-specific input to exercise identifyDomain
const techInput =
"How can we optimize our database performance and improve our software architecture?";
const result = await server.handleAnalyzeSystematically({
input: techInput,
mode: "auto",
});
expect(result).toBeDefined();
expect(result.recommended_framework).toBeDefined();
});
it("should exercise constraint extraction through problem decomposition", async () => {
// Test input with constraints to exercise extractConstraints
const constrainedInput =
"We need to complete this project within 3 months, under $50,000 budget, with only 2 developers";
const result = await server.handleDecomposeProblem({
input: constrainedInput,
max_depth: 2,
});
expect(result).toBeDefined();
expect(result.hierarchical_structure).toBeDefined();
});
it("should exercise stakeholder identification", async () => {
// Test input with stakeholders to exercise identifyStakeholders
const stakeholderInput =
"How should we communicate this change to customers, employees, and investors?";
const result = await server.handleThinkParallel({
input: stakeholderInput,
enable_coordination: true,
});
expect(result).toBeDefined();
expect(result.stream_results).toBeDefined();
});
it("should exercise time sensitivity assessment", async () => {
// Test urgent input to exercise assessTimeSensitivity
const urgentInput =
"URGENT: We need to fix this critical bug immediately before the deadline tomorrow!";
const result = await server.handleThink({
input: urgentInput,
mode: ProcessingMode.INTUITIVE,
});
expect(result).toBeDefined();
expect(result.content).toBeDefined();
});
it("should exercise resource requirement identification", async () => {
// Test input with resources to exercise identifyResourceRequirements
const resourceInput =
"We need funding, skilled developers, cloud infrastructure, and marketing budget for this project";
const result = await server.handleAnalyzeSystematically({
input: resourceInput,
mode: "manual",
});
expect(result).toBeDefined();
expect(result.recommended_framework).toBeDefined();
});
});
describe("Configuration and Context Creation", () => {
it("should create proper cognitive configuration", async () => {
const result = await server.handleThink({
input: "Test configuration creation",
mode: ProcessingMode.CREATIVE,
temperature: 1.0,
max_depth: 15,
enable_emotion: true,
enable_metacognition: true,
});
expect(result).toBeDefined();
expect(result.metadata.system_mode).toBe(ProcessingMode.CREATIVE);
expect(result.metadata.temperature).toBe(1.0);
});
it("should handle context creation with session information", async () => {
const result = await server.handleThink({
input: "Test context creation",
context: {
session_id: "test_session_123",
domain: "testing",
urgency: 0.8,
},
});
expect(result).toBeDefined();
expect(result.content).toBeDefined();
});
});
});