|
| 1 | +import { providerIdentifiers } from "@roo-code/types" |
| 2 | +import * as assert from "assert" |
| 3 | +import * as fs from "fs/promises" |
| 4 | +import * as path from "path" |
| 5 | +import * as vscode from "vscode" |
| 6 | + |
| 7 | +import { RooCodeEventName, type ClineMessage, type QueuedMessage } from "@roo-code/types" |
| 8 | + |
| 9 | +import { setDefaultSuiteTimeout } from "./test-utils" |
| 10 | +import { sleep, waitFor } from "./utils" |
| 11 | +import { |
| 12 | + QUEUED_API_INPUT_MARKER_FILE, |
| 13 | + QUEUED_API_INPUT_MESSAGE, |
| 14 | + QUEUED_API_INPUT_PROMPT, |
| 15 | +} from "../fixtures/queued-api-input" |
| 16 | + |
| 17 | +suite("Roo Code queued API input", function () { |
| 18 | + setDefaultSuiteTimeout(this) |
| 19 | + |
| 20 | + let markerPath: string |
| 21 | + |
| 22 | + suiteSetup(async () => { |
| 23 | + const aimockUrl = process.env.AIMOCK_URL |
| 24 | + await globalThis.api.setConfiguration({ |
| 25 | + apiProvider: providerIdentifiers.openrouter, |
| 26 | + openRouterApiKey: aimockUrl ? "mock-key" : process.env.OPENROUTER_API_KEY!, |
| 27 | + openRouterModelId: "anthropic/claude-sonnet-4.5", |
| 28 | + ...(aimockUrl && { openRouterBaseUrl: `${aimockUrl}/v1` }), |
| 29 | + }) |
| 30 | + markerPath = path.join(vscode.workspace.workspaceFolders![0]!.uri.fsPath, QUEUED_API_INPUT_MARKER_FILE) |
| 31 | + }) |
| 32 | + |
| 33 | + teardown(async () => { |
| 34 | + await fs.rm(markerPath, { force: true }) |
| 35 | + while (globalThis.api.getCurrentTaskStack().length > 0) { |
| 36 | + await globalThis.api.clearCurrentTask() |
| 37 | + } |
| 38 | + }) |
| 39 | + |
| 40 | + test("queued API input does not approve a protected command ask", async () => { |
| 41 | + const api = globalThis.api |
| 42 | + await fs.rm(markerPath, { force: true }) |
| 43 | + |
| 44 | + let queue: QueuedMessage[] = [] |
| 45 | + let commandAsk: ClineMessage | undefined |
| 46 | + let interactive = false |
| 47 | + |
| 48 | + const onQueue = (_taskId: string, messages: QueuedMessage[]) => { |
| 49 | + if (messages.length > 0) queue = messages |
| 50 | + } |
| 51 | + const onMessage = ({ message }: { message: ClineMessage }) => { |
| 52 | + if (message.type === "ask" && message.ask === "command" && message.partial !== true) { |
| 53 | + commandAsk = message |
| 54 | + } |
| 55 | + } |
| 56 | + const onInteractive = () => { |
| 57 | + interactive = true |
| 58 | + } |
| 59 | + |
| 60 | + api.on(RooCodeEventName.QueuedMessagesUpdated, onQueue) |
| 61 | + api.on(RooCodeEventName.Message, onMessage) |
| 62 | + api.on(RooCodeEventName.TaskInteractive, onInteractive) |
| 63 | + |
| 64 | + try { |
| 65 | + const taskId = await api.startNewTask({ |
| 66 | + configuration: { mode: "code", autoApprovalEnabled: false, enableCheckpoints: false }, |
| 67 | + text: QUEUED_API_INPUT_PROMPT, |
| 68 | + }) |
| 69 | + |
| 70 | + // Wait until the first request reaches the mock. The mock then delays its |
| 71 | + // response, so the task is still streaming when the API input arrives. |
| 72 | + await waitFor(async () => { |
| 73 | + const response = await fetch(`${process.env.AIMOCK_URL}/__aimock/journal`) |
| 74 | + return JSON.stringify(await response.json()).includes(QUEUED_API_INPUT_PROMPT) |
| 75 | + }) |
| 76 | + await api.sendMessage(QUEUED_API_INPUT_MESSAGE) |
| 77 | + |
| 78 | + await waitFor(() => commandAsk !== undefined) |
| 79 | + await waitFor(() => interactive) |
| 80 | + await sleep(1_500) |
| 81 | + |
| 82 | + assert.ok(queue.some((m) => m.text === QUEUED_API_INPUT_MESSAGE && m.origin === "api")) |
| 83 | + await assert.rejects(fs.access(markerPath), "Queued API input must not run the command") |
| 84 | + assert.strictEqual(api.getCurrentTaskStack().at(-1), taskId) |
| 85 | + |
| 86 | + await api.approveCurrentAsk() |
| 87 | + await waitFor(() => |
| 88 | + fs.access(markerPath).then( |
| 89 | + () => true, |
| 90 | + () => false, |
| 91 | + ), |
| 92 | + ) |
| 93 | + } finally { |
| 94 | + api.off(RooCodeEventName.QueuedMessagesUpdated, onQueue) |
| 95 | + api.off(RooCodeEventName.Message, onMessage) |
| 96 | + api.off(RooCodeEventName.TaskInteractive, onInteractive) |
| 97 | + } |
| 98 | + }) |
| 99 | +}) |
0 commit comments