/**
* Problem Routes Unit Tests
*
* Tests for the problem decomposition and framework selection routes.
* Requirements: 6.1, 6.2, 6.3
*/
import { beforeEach, describe, expect, it, vi } from "vitest";
import type { CognitiveCore } from "../../../../server/cognitive-core.js";
import { createProblemRoutes } from "../../../../server/routes/problem.js";
// Mock framework selection result
const mockFrameworkSelection = {
primaryFramework: {
id: "scientific-method",
name: "Scientific Method",
description: "Systematic approach using hypothesis testing",
bestSuitedFor: [],
steps: [],
execute: vi.fn(),
adapt: vi.fn(),
},
alternatives: [
{
framework: {
id: "design-thinking",
name: "Design Thinking",
description: "Human-centered approach to problem solving",
bestSuitedFor: [],
steps: [],
execute: vi.fn(),
adapt: vi.fn(),
},
confidence: 0.75,
reason: "Good for user-centered problems",
},
{
framework: {
id: "systems-thinking",
name: "Systems Thinking",
description: "Holistic approach considering interconnections",
bestSuitedFor: [],
steps: [],
execute: vi.fn(),
adapt: vi.fn(),
},
confidence: 0.65,
reason: "Good for complex interdependencies",
},
],
confidence: 0.85,
reason: "Selected Scientific Method: ideal for simple problems, ideal for low uncertainty",
isHybrid: false,
timestamp: new Date(),
};
// Mock cognitive core with problem decomposer and framework selector
const createMockCognitiveCore = (): CognitiveCore => {
return {
memoryRepository: {} as CognitiveCore["memoryRepository"],
reasoningOrchestrator: {} as CognitiveCore["reasoningOrchestrator"],
frameworkSelector: {
selectFramework: vi.fn().mockReturnValue(mockFrameworkSelection),
} as unknown as CognitiveCore["frameworkSelector"],
confidenceAssessor: {} as CognitiveCore["confidenceAssessor"],
biasDetector: {} as CognitiveCore["biasDetector"],
emotionAnalyzer: {} as CognitiveCore["emotionAnalyzer"],
problemDecomposer: {
decompose: vi.fn().mockReturnValue({
subProblems: [
{
id: "root",
name: "Build E-commerce Platform",
description: "Build a scalable e-commerce platform",
depth: 1,
domain: "ecommerce",
},
{
id: "root-0",
name: "Design User Authentication",
description: "Define and implement the user authentication component",
depth: 2,
parent: "root",
domain: "ecommerce",
},
{
id: "root-1",
name: "Design Product Catalog",
description: "Define and implement the product catalog component",
depth: 2,
parent: "root",
domain: "ecommerce",
},
],
dependencies: [
{
from: "root",
to: "root-0",
type: "foundation",
description: '"User Authentication" provides the foundation for "the main problem"',
},
{
from: "root",
to: "root-1",
type: "hierarchical",
description: '"Product Catalog" is a component of "the main problem"',
},
],
}),
} as unknown as CognitiveCore["problemDecomposer"],
memoryAugmentedReasoning: {} as CognitiveCore["memoryAugmentedReasoning"],
};
};
describe("Problem Routes", () => {
let mockCore: CognitiveCore;
beforeEach(() => {
mockCore = createMockCognitiveCore();
});
describe("createProblemRoutes", () => {
it("should create a router with routes", () => {
const router = createProblemRoutes(mockCore);
expect(router).toBeDefined();
expect(router.stack).toBeDefined();
expect(router.stack.length).toBeGreaterThan(0);
});
it("should have POST route for decompose endpoint", () => {
const router = createProblemRoutes(mockCore);
expect(router.stack.length).toBeGreaterThan(0);
const firstLayer = router.stack[0];
expect(firstLayer).toBeDefined();
});
});
describe("Problem Decomposer Integration", () => {
it("should have problemDecomposer.decompose mock configured", () => {
createProblemRoutes(mockCore);
expect(mockCore.problemDecomposer.decompose).toBeDefined();
});
it("should return decomposition result with correct structure", () => {
const result = mockCore.problemDecomposer.decompose("test problem", 3);
expect(result).toBeDefined();
expect(result.subProblems).toBeDefined();
expect(result.dependencies).toBeDefined();
expect(result.subProblems.length).toBeGreaterThan(0);
});
});
describe("Decomposition Strategy Support", () => {
it("should support all valid strategies", () => {
const validStrategies = ["functional", "temporal", "stakeholder", "component"];
expect(validStrategies).toHaveLength(4);
});
});
describe("Max Depth Validation", () => {
it("should accept maxDepth between 1 and 5", () => {
const validDepths = [1, 2, 3, 4, 5];
expect(validDepths).toHaveLength(5);
});
});
describe("Framework Select Endpoint", () => {
it("should have POST route for framework/select endpoint", () => {
const router = createProblemRoutes(mockCore);
// Router should have at least 2 routes (decompose and framework/select)
expect(router.stack.length).toBeGreaterThanOrEqual(2);
});
it("should have frameworkSelector.selectFramework mock configured", () => {
createProblemRoutes(mockCore);
expect(mockCore.frameworkSelector.selectFramework).toBeDefined();
});
it("should return framework selection result with correct structure", () => {
const problemObj = {
id: "test-problem-1",
description: "How to optimize database queries?",
goals: [],
constraints: [],
context: "",
};
const contextObj = {
problem: problemObj,
evidence: [],
constraints: [],
goals: [],
};
const result = mockCore.frameworkSelector.selectFramework(problemObj, contextObj);
expect(result).toBeDefined();
expect(result.primaryFramework).toBeDefined();
expect(result.primaryFramework.id).toBe("scientific-method");
expect(result.primaryFramework.name).toBe("Scientific Method");
expect(result.alternatives).toBeDefined();
expect(result.alternatives.length).toBeGreaterThan(0);
expect(result.confidence).toBeGreaterThanOrEqual(0);
expect(result.confidence).toBeLessThanOrEqual(1);
expect(result.reason).toBeDefined();
expect(typeof result.isHybrid).toBe("boolean");
});
it("should return alternatives with confidence and reason", () => {
const problemObj = {
id: "test-problem-2",
description: "Design a user interface",
goals: [],
constraints: [],
context: "",
};
const contextObj = {
problem: problemObj,
evidence: [],
constraints: [],
goals: [],
};
const result = mockCore.frameworkSelector.selectFramework(problemObj, contextObj);
expect(result.alternatives).toBeDefined();
for (const alt of result.alternatives) {
expect(alt.framework).toBeDefined();
expect(alt.framework.id).toBeDefined();
expect(alt.framework.name).toBeDefined();
expect(alt.confidence).toBeGreaterThanOrEqual(0);
expect(alt.confidence).toBeLessThanOrEqual(1);
expect(alt.reason).toBeDefined();
}
});
});
});