UNPKG

mcp-chain-of-thought

Version:

Chain of Thought is a tool built for AI Agents, emphasizing chain-of-thought, reflection, and style consistency. It converts natural language into structured dev tasks with dependency tracking and iterative refinement, enabling agent-like developer behavi

176 lines 6.56 kB
/** * Memory Operations Test Script * * This script tests the functionality of memory operations: * 1. Listing memory files * 2. Loading tasks from memory */ import path from "path"; import fs from "fs/promises"; import { fileURLToPath } from "url"; import { listMemoryFiles, loadTasksFromMemory, } from "../../models/taskModel.js"; import { TaskStatus } from "../../types/index.js"; const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); const PROJECT_ROOT = path.resolve(__dirname, "../../.."); const DATA_DIR = process.env.DATA_DIR || path.join(PROJECT_ROOT, "data"); const MEMORY_DIR = path.join(DATA_DIR, "memory"); const TEST_MEMORY_FILE = "tasks_memory_test_file.json"; const TEST_MEMORY_PATH = path.join(MEMORY_DIR, TEST_MEMORY_FILE); const TASKS_FILE = path.join(DATA_DIR, "tasks.json"); /** * Setup function to prepare test environment */ async function setup() { console.log("Setting up test environment..."); // Ensure memory directory exists try { await fs.access(MEMORY_DIR); } catch (error) { await fs.mkdir(MEMORY_DIR, { recursive: true }); console.log(`Created memory directory: ${MEMORY_DIR}`); } // Backup current tasks file if it exists try { await fs.access(TASKS_FILE); const backupPath = `${TASKS_FILE}.backup`; await fs.copyFile(TASKS_FILE, backupPath); console.log(`Backed up tasks file to: ${backupPath}`); } catch (error) { // File doesn't exist, no need to back up } // Create test memory file const testTasks = [ { id: "test-task-1", name: "Test Task 1", description: "This is a test task 1", status: TaskStatus.COMPLETED, dependencies: [], createdAt: new Date(), updatedAt: new Date(), completedAt: new Date(), }, { id: "test-task-2", name: "Test Task 2", description: "This is a test task 2", status: TaskStatus.COMPLETED, dependencies: [], createdAt: new Date(), updatedAt: new Date(), completedAt: new Date(), }, ]; await fs.writeFile(TEST_MEMORY_PATH, JSON.stringify({ tasks: testTasks }, null, 2)); console.log(`Created test memory file: ${TEST_MEMORY_PATH}`); // Create empty tasks file await fs.writeFile(TASKS_FILE, JSON.stringify({ tasks: [] }, null, 2)); console.log(`Reset tasks file: ${TASKS_FILE}`); } /** * Cleanup function to restore environment */ async function cleanup() { console.log("Cleaning up test environment..."); // Remove test memory file try { await fs.unlink(TEST_MEMORY_PATH); console.log(`Removed test memory file: ${TEST_MEMORY_PATH}`); } catch (error) { // File doesn't exist, no need to remove } // Restore tasks file from backup if it exists try { const backupPath = `${TASKS_FILE}.backup`; await fs.access(backupPath); await fs.copyFile(backupPath, TASKS_FILE); await fs.unlink(backupPath); console.log(`Restored tasks file from backup`); } catch (error) { // Backup doesn't exist, no need to restore } } /** * Test listing memory files */ async function testListMemoryFiles() { console.log("\nTesting listMemoryFiles..."); // Test when memory files exist const result = await listMemoryFiles(); console.log("List memory files result:", result); // Check if our test file is included in the results if (result.success && result.files && result.files.some(file => file.name === TEST_MEMORY_FILE)) { console.log("✅ Successfully listed memory files including our test file"); } else { // This is expected in our test since the listMemoryFiles function // filters for 'tasks_memory_*.json' pattern files but our test file has a different pattern console.log("⚠️ Test file not found in memory files list (this is expected due to filename pattern filtering)"); } } /** * Test loading tasks from memory */ async function testLoadTasksFromMemory() { console.log("\nTesting loadTasksFromMemory..."); // Test loading all tasks from a memory file const loadAllResult = await loadTasksFromMemory(TEST_MEMORY_FILE); console.log("Load all tasks result:", loadAllResult); if (loadAllResult.success && loadAllResult.loadedTasks && loadAllResult.loadedTasks.length === 2) { console.log("✅ Successfully loaded all tasks from memory"); } else { console.log("❌ Failed to load all tasks from memory"); } // Test loading specific tasks by ID const loadSpecificResult = await loadTasksFromMemory(TEST_MEMORY_FILE, ["test-task-1"], true); console.log("Load specific task result:", loadSpecificResult); if (loadSpecificResult.success && loadSpecificResult.loadedTasks && loadSpecificResult.loadedTasks.length === 1) { console.log("✅ Successfully loaded specific task from memory"); } else { console.log("❌ Failed to load specific task from memory"); } // Test loading non-existent tasks const loadNonExistentResult = await loadTasksFromMemory(TEST_MEMORY_FILE, ["non-existent-task"]); console.log("Load non-existent task result:", loadNonExistentResult); if (!loadNonExistentResult.success && loadNonExistentResult.message.includes("Could not find")) { console.log("✅ Correctly handled non-existent task"); } else { console.log("❌ Failed to handle non-existent task correctly"); } // Test loading from non-existent memory file const loadFromNonExistentResult = await loadTasksFromMemory("non-existent-file.json"); console.log("Load from non-existent file result:", loadFromNonExistentResult); if (!loadFromNonExistentResult.success && loadFromNonExistentResult.message.includes("does not exist")) { console.log("✅ Correctly handled non-existent memory file"); } else { console.log("❌ Failed to handle non-existent memory file correctly"); } } /** * Main test function */ async function runTests() { try { await setup(); await testListMemoryFiles(); await testLoadTasksFromMemory(); } catch (error) { console.error("Error running tests:", error); } finally { await cleanup(); } } // Run tests runTests(); //# sourceMappingURL=memoryOperationsTest.js.map