Skip to content

Commit a4fff39

Browse files
committed
test(e2e): cover queued api input on protected command asks
1 parent a46474a commit a4fff39

3 files changed

Lines changed: 150 additions & 0 deletions

File tree

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { LLMock } from "@copilotkit/aimock"
2+
import type { ChatCompletionRequest } from "@copilotkit/aimock"
3+
4+
export const QUEUED_API_INPUT_PROMPT = "QUEUED_API_INPUT_APPROVAL: Run the marker command."
5+
export const QUEUED_API_INPUT_MARKER_FILE = "queued-api-input-marker.txt"
6+
export const QUEUED_API_INPUT_MESSAGE = "Steering note from the API."
7+
export const QUEUED_API_INPUT_RESPONSE_LATENCY_MS = 2_000
8+
9+
const COMMAND_CALL_ID = "call_queued_api_input_command_001"
10+
11+
export function addQueuedApiInputFixtures(mock: InstanceType<typeof LLMock>) {
12+
mock.addFixture({
13+
match: {
14+
predicate: (req: ChatCompletionRequest) => {
15+
const messages = Array.isArray(req?.messages) ? req.messages : []
16+
const lastUser = messages.filter((m) => m?.role === "user").at(-1)
17+
return JSON.stringify(lastUser?.content ?? "").includes(QUEUED_API_INPUT_PROMPT)
18+
},
19+
},
20+
streamingProfile: { ttft: QUEUED_API_INPUT_RESPONSE_LATENCY_MS },
21+
response: {
22+
toolCalls: [
23+
{
24+
name: "execute_command",
25+
arguments: JSON.stringify({ command: `touch ${QUEUED_API_INPUT_MARKER_FILE}` }),
26+
id: COMMAND_CALL_ID,
27+
},
28+
],
29+
},
30+
})
31+
32+
mock.addFixture({
33+
match: {
34+
predicate: (req: ChatCompletionRequest) => {
35+
const messages = Array.isArray(req?.messages) ? req.messages : []
36+
return messages.filter((m) => m?.role === "tool").at(-1)?.tool_call_id === COMMAND_CALL_ID
37+
},
38+
},
39+
response: {
40+
toolCalls: [
41+
{
42+
name: "attempt_completion",
43+
arguments: JSON.stringify({ result: "Ran the marker command." }),
44+
id: "call_queued_api_input_completion_002",
45+
},
46+
],
47+
},
48+
})
49+
}

‎apps/vscode-e2e/src/runTest.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { addTerminalProfileResultFixtures } from "./fixtures/terminal-profile"
1818
import { addListFilesResultFixtures } from "./fixtures/list-files"
1919
import { addReadFileResultFixtures } from "./fixtures/read-file"
2020
import { addSearchFilesResultFixtures } from "./fixtures/search-files"
21+
import { addQueuedApiInputFixtures } from "./fixtures/queued-api-input"
2122
import { addSubtaskFixtures } from "./fixtures/subtasks"
2223
import { addUseMcpToolResultFixtures } from "./fixtures/use-mcp-tool"
2324
import { addWriteToFileResultFixtures } from "./fixtures/write-to-file"
@@ -140,6 +141,7 @@ async function main() {
140141
addReadFileResultFixtures(mock)
141142
addSearchFilesResultFixtures(mock)
142143
addSubtaskFixtures(mock)
144+
addQueuedApiInputFixtures(mock)
143145
addUseMcpToolResultFixtures(mock)
144146
addWriteToFileResultFixtures(mock)
145147
addDeepSeekV4Fixtures(mock)
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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

Comments
 (0)