|
| 1 | +import { test, expect, describe, beforeEach, afterEach } from "bun:test"; |
| 2 | +import { mkdtemp, rm, mkdir, writeFile } from "node:fs/promises"; |
| 3 | +import { tmpdir } from "node:os"; |
| 4 | +import { join } from "node:path"; |
| 5 | +import { createMockAgent } from "./agent"; |
| 6 | +import { DexMock } from "./testing/dex-mock"; |
| 7 | +import type { DexTask } from "./dex"; |
| 8 | + |
| 9 | +/** |
| 10 | + * Helper to create a minimal DexTask for testing |
| 11 | + */ |
| 12 | +function createTask(overrides: Partial<DexTask> = {}): DexTask { |
| 13 | + return { |
| 14 | + id: "task-1", |
| 15 | + parent_id: null, |
| 16 | + name: "Test task", |
| 17 | + description: null, |
| 18 | + priority: 1, |
| 19 | + completed: false, |
| 20 | + result: null, |
| 21 | + metadata: null, |
| 22 | + created_at: "2024-01-01T00:00:00Z", |
| 23 | + updated_at: "2024-01-01T00:00:00Z", |
| 24 | + started_at: null, |
| 25 | + completed_at: null, |
| 26 | + blockedBy: [], |
| 27 | + blocks: [], |
| 28 | + children: [], |
| 29 | + ...overrides, |
| 30 | + }; |
| 31 | +} |
| 32 | + |
| 33 | +describe("Integration: Happy path with full mock stack", () => { |
| 34 | + let testDir: string; |
| 35 | + let originalCwd: string; |
| 36 | + let dexMock: DexMock; |
| 37 | + |
| 38 | + beforeEach(async () => { |
| 39 | + dexMock = new DexMock(); |
| 40 | + |
| 41 | + // Create temp directory for filesystem requirements |
| 42 | + testDir = await mkdtemp(join(tmpdir(), "math-integration-test-")); |
| 43 | + originalCwd = process.cwd(); |
| 44 | + process.chdir(testDir); |
| 45 | + |
| 46 | + // Create required .math/todo directory with PROMPT.md |
| 47 | + const todoDir = join(testDir, ".math", "todo"); |
| 48 | + await mkdir(todoDir, { recursive: true }); |
| 49 | + await writeFile(join(todoDir, "PROMPT.md"), "# Test Prompt\n\nTest instructions."); |
| 50 | + |
| 51 | + // Create .dex directory (required by loop) |
| 52 | + await mkdir(join(testDir, ".dex"), { recursive: true }); |
| 53 | + }); |
| 54 | + |
| 55 | + afterEach(async () => { |
| 56 | + process.chdir(originalCwd); |
| 57 | + await rm(testDir, { recursive: true, force: true }); |
| 58 | + }); |
| 59 | + |
| 60 | + test("completes 3 dependent tasks in order using MockAgent and DexMock", async () => { |
| 61 | + const { runLoop } = await import("./loop"); |
| 62 | + |
| 63 | + // Set up DexMock with 3 tasks: task-1 -> task-2 -> task-3 (dependency chain) |
| 64 | + dexMock.setTasks([ |
| 65 | + createTask({ id: "task-1", name: "First task" }), |
| 66 | + createTask({ id: "task-2", name: "Second task", blockedBy: ["task-1"] }), |
| 67 | + createTask({ id: "task-3", name: "Third task", blockedBy: ["task-2"] }), |
| 68 | + ]); |
| 69 | + |
| 70 | + // Create MockAgent that completes tasks via DexMock |
| 71 | + const mockAgent = createMockAgent({ |
| 72 | + dexMock, |
| 73 | + completeTask: true, |
| 74 | + exitCode: 0, |
| 75 | + logs: [ |
| 76 | + { category: "info", message: "Agent processing task" }, |
| 77 | + { category: "success", message: "Task completed" }, |
| 78 | + ], |
| 79 | + output: ["Task completed successfully\n"], |
| 80 | + }); |
| 81 | + |
| 82 | + // Suppress console output during test |
| 83 | + const originalLog = console.log; |
| 84 | + const originalStdoutWrite = process.stdout.write.bind(process.stdout); |
| 85 | + console.log = () => {}; |
| 86 | + process.stdout.write = () => true; |
| 87 | + |
| 88 | + try { |
| 89 | + // Run the loop with maxIterations: 5 (we need 3 iterations for 3 tasks) |
| 90 | + // Note: pauseSeconds must be non-zero to avoid falsy default (0 || 3 = 3) |
| 91 | + await runLoop({ |
| 92 | + dexClient: dexMock, |
| 93 | + agent: mockAgent, |
| 94 | + maxIterations: 5, |
| 95 | + pauseSeconds: 0.001, |
| 96 | + ui: false, |
| 97 | + }); |
| 98 | + |
| 99 | + // Assert: All 3 tasks completed |
| 100 | + const finalStatus = await dexMock.status(); |
| 101 | + expect(finalStatus.stats.completed).toBe(3); |
| 102 | + expect(finalStatus.stats.pending).toBe(0); |
| 103 | + expect(finalStatus.stats.inProgress).toBe(0); |
| 104 | + |
| 105 | + // Assert: DexMock.getCalls() shows correct sequence |
| 106 | + const calls = dexMock.getCalls(); |
| 107 | + const methodSequence = calls.map((c) => c.method); |
| 108 | + |
| 109 | + // Verify we have start/complete pairs for each task |
| 110 | + const startCalls = calls.filter((c) => c.method === "start"); |
| 111 | + const completeCalls = calls.filter((c) => c.method === "complete"); |
| 112 | + |
| 113 | + expect(startCalls.length).toBe(3); |
| 114 | + expect(completeCalls.length).toBe(3); |
| 115 | + |
| 116 | + // Verify tasks were completed in order: task-1, task-2, task-3 |
| 117 | + expect(startCalls[0]?.args[0]).toBe("task-1"); |
| 118 | + expect(startCalls[1]?.args[0]).toBe("task-2"); |
| 119 | + expect(startCalls[2]?.args[0]).toBe("task-3"); |
| 120 | + |
| 121 | + expect(completeCalls[0]?.args[0]).toBe("task-1"); |
| 122 | + expect(completeCalls[1]?.args[0]).toBe("task-2"); |
| 123 | + expect(completeCalls[2]?.args[0]).toBe("task-3"); |
| 124 | + |
| 125 | + // Verify each start is followed by its corresponding complete |
| 126 | + for (let i = 0; i < 3; i++) { |
| 127 | + const taskId = `task-${i + 1}`; |
| 128 | + const startIdx = methodSequence.indexOf("start", calls.findIndex((c) => c.method === "start" && c.args[0] === taskId)); |
| 129 | + const completeIdx = calls.findIndex((c) => c.method === "complete" && c.args[0] === taskId); |
| 130 | + expect(startIdx).toBeLessThan(completeIdx); |
| 131 | + } |
| 132 | + } finally { |
| 133 | + console.log = originalLog; |
| 134 | + process.stdout.write = originalStdoutWrite; |
| 135 | + } |
| 136 | + |
| 137 | + // Loop exited successfully (no max iterations exceeded error thrown) |
| 138 | + }); |
| 139 | +}); |
0 commit comments