|
| 1 | +import { describe, it, expect, vi, beforeEach, afterEach } from "vitest" |
| 2 | +import type { PluginContext, Todo, LoopEvent } from "../types.js" |
| 3 | +import { createTaskContinuation } from "../index.ts" |
| 4 | + |
| 5 | +function createMockContext(): PluginContext { |
| 6 | + const mockSession = { |
| 7 | + id: "test-session", |
| 8 | + get: vi.fn(), |
| 9 | + messages: vi.fn(), |
| 10 | + prompt: vi.fn().mockResolvedValue(undefined), |
| 11 | + todo: vi.fn().mockResolvedValue([]), |
| 12 | + } |
| 13 | + |
| 14 | + const mockTui = { |
| 15 | + showToast: vi.fn().mockResolvedValue(undefined), |
| 16 | + } |
| 17 | + |
| 18 | + return { |
| 19 | + directory: "/test/directory", |
| 20 | + client: { |
| 21 | + session: mockSession as PluginContext["client"]["session"], |
| 22 | + tui: mockTui, |
| 23 | + }, |
| 24 | + on: vi.fn(), |
| 25 | + } as unknown as PluginContext |
| 26 | +} |
| 27 | + |
| 28 | +function createMockTodos(completed: number, pending: number): Todo[] { |
| 29 | + const todos: Todo[] = [] |
| 30 | + for (let i = 0; i < completed; i++) { |
| 31 | + todos.push({ |
| 32 | + id: `completed-${i}`, |
| 33 | + content: `Completed task ${i}`, |
| 34 | + status: "completed", |
| 35 | + priority: "high", |
| 36 | + }) |
| 37 | + } |
| 38 | + for (let i = 0; i < pending; i++) { |
| 39 | + todos.push({ |
| 40 | + id: `pending-${i}`, |
| 41 | + content: `Pending task ${i}`, |
| 42 | + status: "pending", |
| 43 | + priority: "high", |
| 44 | + }) |
| 45 | + } |
| 46 | + return todos |
| 47 | +} |
| 48 | + |
| 49 | +function createIdleEvent(sessionID: string): LoopEvent { |
| 50 | + return { |
| 51 | + type: "session.idle", |
| 52 | + properties: { sessionID }, |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +describe("CleanupTest", () => { |
| 57 | + beforeEach(() => { |
| 58 | + vi.useFakeTimers() |
| 59 | + }) |
| 60 | + |
| 61 | + afterEach(() => { |
| 62 | + vi.useRealTimers() |
| 63 | + vi.restoreAllMocks() |
| 64 | + }) |
| 65 | + |
| 66 | + it("should inject continuation when incomplete todos remain (simplified)", async () => { |
| 67 | + const ctx = createMockContext() |
| 68 | + const mockTodoFn = ctx.client.session.todo as unknown as { |
| 69 | + mockResolvedValue: (val: Todo[]) => void |
| 70 | + } |
| 71 | + mockTodoFn.mockResolvedValue(createMockTodos(1, 2)) |
| 72 | + |
| 73 | + const taskContinuation = createTaskContinuation(ctx) |
| 74 | + await taskContinuation.handler({ event: createIdleEvent("session-123") }) |
| 75 | + |
| 76 | + await vi.advanceTimersByTimeAsync(3000) |
| 77 | + expect(ctx.client.session.prompt).toHaveBeenCalled() |
| 78 | + }) |
| 79 | + |
| 80 | + it("should not inject continuation when all todos are completed", async () => { |
| 81 | + const ctx = createMockContext() |
| 82 | + const mockTodoFn = ctx.client.session.todo as unknown as { |
| 83 | + mockResolvedValue: (val: Todo[]) => void |
| 84 | + } |
| 85 | + mockTodoFn.mockResolvedValue(createMockTodos(5, 0)) |
| 86 | + |
| 87 | + const taskContinuation = createTaskContinuation(ctx) |
| 88 | + await taskContinuation.handler({ event: createIdleEvent("session-completed") }) |
| 89 | + |
| 90 | + await vi.advanceTimersByTimeAsync(3000) |
| 91 | + expect(ctx.client.session.prompt).not.toHaveBeenCalled() |
| 92 | + }) |
| 93 | +}) |
0 commit comments