|
| 1 | +import { |
| 2 | + ApprovalGate, |
| 3 | + EventBus, |
| 4 | + SessionState, |
| 5 | + ToolRegistry, |
| 6 | + type DevAgentConfig, |
| 7 | + type LLMProvider, |
| 8 | + type StreamChunk, |
| 9 | +} from "@devagent/runtime"; |
| 10 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 11 | + |
| 12 | +import type { RunSingleQueryOptions } from "./main-types.js"; |
| 13 | + |
| 14 | +function createTestConfig(): DevAgentConfig { |
| 15 | + return { |
| 16 | + provider: "openai", |
| 17 | + model: "gpt-5", |
| 18 | + approval: { mode: "default" }, |
| 19 | + budget: { |
| 20 | + maxIterations: 10, |
| 21 | + maxContextTokens: 100_000, |
| 22 | + responseHeadroom: 2_000, |
| 23 | + costWarningThreshold: 1.0, |
| 24 | + enableCostTracking: true, |
| 25 | + }, |
| 26 | + context: { turnIsolation: false }, |
| 27 | + logging: { enabled: false }, |
| 28 | + } as unknown as DevAgentConfig; |
| 29 | +} |
| 30 | + |
| 31 | +function deferred(): { |
| 32 | + readonly promise: Promise<void>; |
| 33 | + readonly resolve: () => void; |
| 34 | +} { |
| 35 | + let resolve!: () => void; |
| 36 | + const promise = new Promise<void>((res) => { |
| 37 | + resolve = res; |
| 38 | + }); |
| 39 | + return { promise, resolve }; |
| 40 | +} |
| 41 | + |
| 42 | +function createRunOptions(provider: LLMProvider): RunSingleQueryOptions { |
| 43 | + const config = createTestConfig(); |
| 44 | + const bus = new EventBus(); |
| 45 | + return { |
| 46 | + query: "inspect", |
| 47 | + provider, |
| 48 | + toolRegistry: new ToolRegistry(), |
| 49 | + bus, |
| 50 | + gate: new ApprovalGate(config.approval, bus), |
| 51 | + config, |
| 52 | + repoRoot: "/tmp", |
| 53 | + mode: "act", |
| 54 | + skills: { list: () => [] } as RunSingleQueryOptions["skills"], |
| 55 | + contextManager: { setSummarizeCallback: () => {} } as RunSingleQueryOptions["contextManager"], |
| 56 | + doubleCheck: {} as RunSingleQueryOptions["doubleCheck"], |
| 57 | + initialMessages: undefined, |
| 58 | + verbosity: "quiet", |
| 59 | + verbosityConfig: { base: "quiet", categories: new Set() }, |
| 60 | + sessionState: new SessionState(), |
| 61 | + }; |
| 62 | +} |
| 63 | + |
| 64 | +function createProvider(): { readonly provider: LLMProvider; readonly chat: ReturnType<typeof vi.fn>; readonly abort: ReturnType<typeof vi.fn> } { |
| 65 | + const chat = vi.fn(async function* (): AsyncIterable<StreamChunk> { |
| 66 | + yield { type: "text", content: "done" }; |
| 67 | + yield { type: "done", content: "" }; |
| 68 | + }); |
| 69 | + const abort = vi.fn(); |
| 70 | + return { |
| 71 | + chat, |
| 72 | + abort, |
| 73 | + provider: { |
| 74 | + id: "test-provider", |
| 75 | + chat, |
| 76 | + abort, |
| 77 | + }, |
| 78 | + }; |
| 79 | +} |
| 80 | + |
| 81 | +describe("runTuiQuery cancellation", () => { |
| 82 | + afterEach(() => { |
| 83 | + vi.doUnmock("./prompt-commands.js"); |
| 84 | + vi.resetModules(); |
| 85 | + vi.restoreAllMocks(); |
| 86 | + }); |
| 87 | + |
| 88 | + it("honors cancellation while first-turn query preparation is pending", async () => { |
| 89 | + const prep = deferred(); |
| 90 | + vi.doMock("./prompt-commands.js", async (importOriginal) => { |
| 91 | + const actual = await importOriginal<typeof import("./prompt-commands.js")>(); |
| 92 | + return { |
| 93 | + ...actual, |
| 94 | + preparePromptCommandQuery: vi.fn(async () => { |
| 95 | + await prep.promise; |
| 96 | + return null; |
| 97 | + }), |
| 98 | + }; |
| 99 | + }); |
| 100 | + const { provider, chat, abort } = createProvider(); |
| 101 | + const { runTuiQuery, abortTuiQuery, resetTuiLoop } = await import("./main-query.js"); |
| 102 | + |
| 103 | + resetTuiLoop(); |
| 104 | + const run = runTuiQuery(createRunOptions(provider)); |
| 105 | + await Promise.resolve(); |
| 106 | + |
| 107 | + abortTuiQuery(); |
| 108 | + prep.resolve(); |
| 109 | + |
| 110 | + const result = await run; |
| 111 | + expect(abort).toHaveBeenCalledTimes(1); |
| 112 | + expect(chat).not.toHaveBeenCalled(); |
| 113 | + expect(result.status).toBe("aborted"); |
| 114 | + resetTuiLoop(); |
| 115 | + }); |
| 116 | + |
| 117 | + it("preserves cancellation during query preparation for an existing TUI loop", async () => { |
| 118 | + const secondPrep = deferred(); |
| 119 | + let prepareCallCount = 0; |
| 120 | + vi.doMock("./prompt-commands.js", async (importOriginal) => { |
| 121 | + const actual = await importOriginal<typeof import("./prompt-commands.js")>(); |
| 122 | + return { |
| 123 | + ...actual, |
| 124 | + preparePromptCommandQuery: vi.fn(async () => { |
| 125 | + prepareCallCount += 1; |
| 126 | + if (prepareCallCount === 2) await secondPrep.promise; |
| 127 | + return null; |
| 128 | + }), |
| 129 | + }; |
| 130 | + }); |
| 131 | + const { provider, chat } = createProvider(); |
| 132 | + const { runTuiQuery, abortTuiQuery, resetTuiLoop } = await import("./main-query.js"); |
| 133 | + |
| 134 | + resetTuiLoop(); |
| 135 | + await expect(runTuiQuery(createRunOptions(provider))).resolves.toMatchObject({ status: "success" }); |
| 136 | + expect(chat).toHaveBeenCalledTimes(1); |
| 137 | + |
| 138 | + const secondRun = runTuiQuery(createRunOptions(provider)); |
| 139 | + await Promise.resolve(); |
| 140 | + abortTuiQuery(); |
| 141 | + secondPrep.resolve(); |
| 142 | + |
| 143 | + await expect(secondRun).resolves.toMatchObject({ status: "aborted" }); |
| 144 | + expect(chat).toHaveBeenCalledTimes(1); |
| 145 | + resetTuiLoop(); |
| 146 | + }); |
| 147 | +}); |
0 commit comments