-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathacp-e2e-shell-approval.test.ts
More file actions
118 lines (102 loc) · 4.69 KB
/
Copy pathacp-e2e-shell-approval.test.ts
File metadata and controls
118 lines (102 loc) · 4.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import fs from "node:fs";
import path from "node:path";
import {afterEach, beforeEach, expect, it, vi} from "vitest";
import {AgentMode} from "../../../AgentMode";
import {ApprovalOptionId} from "../../../ApprovalOptionId";
import {
createAuthenticatedFixture,
createPermissionResponse,
createPermissionResponder,
describeE2E,
expectEndTurn,
type SpawnedAgentFixture,
} from "./acp-e2e-test-utils";
const FIRST_FILE_NAME = "approval-first.txt";
const SECOND_FILE_NAME = "approval-second.txt";
const COMMAND = `if [ -e ${FIRST_FILE_NAME} ]; then touch ${SECOND_FILE_NAME}; else touch ${FIRST_FILE_NAME}; fi`;
describeE2E("E2E shell approval tests", () => {
let fixture: SpawnedAgentFixture;
let sessionId: string;
beforeEach(async () => {
fixture = await createAuthenticatedFixture(AgentMode.ReadOnly);
sessionId = (await fixture.createSession()).sessionId;
});
afterEach(async () => {
await fixture.dispose();
});
async function promptShellCommandTwice(): Promise<void> {
for (const text of [
`Use your shell tool to run exactly \`${COMMAND}\`.`,
`Use your shell tool to run exactly the same command again: \`${COMMAND}\`.`,
]) {
expectEndTurn(await fixture.connection.prompt({
sessionId,
prompt: [{type: "text", text}],
}));
}
}
it("prompts for every command when allow_once is selected", async () => {
const responses = [ApprovalOptionId.AllowOnce, ApprovalOptionId.RejectOnce];
fixture.setPermissionResponder((request) => createPermissionResponse(
request.toolCall.kind === "execute"
? responses.shift() ?? ApprovalOptionId.RejectOnce
: null
));
await promptShellCommandTwice();
expect(fs.existsSync(path.join(fixture.workspaceDir, FIRST_FILE_NAME))).toBe(true);
expect(fs.existsSync(path.join(fixture.workspaceDir, SECOND_FILE_NAME))).toBe(false);
expect(fixture.readPermissionRequests(sessionId, "execute").length).toBe(2);
});
it("skips subsequent approvals when allow_always is selected", async () => {
fixture.setPermissionResponder(createPermissionResponder("execute", ApprovalOptionId.AllowAlways));
await promptShellCommandTwice();
expect(fs.existsSync(path.join(fixture.workspaceDir, FIRST_FILE_NAME))).toBe(true);
expect(fs.existsSync(path.join(fixture.workspaceDir, SECOND_FILE_NAME))).toBe(true);
expect(fixture.readPermissionRequests(sessionId, "execute").length).toBe(1);
});
it("prompts for every command when reject_once is selected", async () => {
fixture.setPermissionResponder(createPermissionResponder("execute", ApprovalOptionId.RejectOnce));
await promptShellCommandTwice();
expect(fs.existsSync(path.join(fixture.workspaceDir, FIRST_FILE_NAME))).toBe(false);
expect(fs.existsSync(path.join(fixture.workspaceDir, SECOND_FILE_NAME))).toBe(false);
expect(fixture.readPermissionRequests(sessionId, "execute").length).toBe(2);
});
});
describeE2E("E2E shell cancellation tests", () => {
let fixture: SpawnedAgentFixture | null = null;
afterEach(async () => {
await fixture?.dispose();
fixture = null;
});
function isProcessRunning(pid: number): boolean {
try {
process.kill(pid, 0);
return true;
} catch {
return false;
}
}
it("cancels a running shell command", async () => {
fixture = await createAuthenticatedFixture();
fixture.setPermissionResponder(createPermissionResponder("execute", ApprovalOptionId.AllowOnce));
const sessionId = (await fixture.createSession()).sessionId;
const pidFilePath = path.join(fixture.workspaceDir, "cancel-command.pid");
const command = `/bin/sh -c 'echo $$ > "${pidFilePath}"; exec sleep 100'`;
const promptResponse = fixture.connection.prompt({
sessionId,
prompt: [{type: "text", text: `Use your shell tool to run exactly \`${command}\`.`}],
});
const pid = await vi.waitFor(() => {
const content = fs.existsSync(pidFilePath) ? fs.readFileSync(pidFilePath, "utf8").trim() : "";
const parsed = Number.parseInt(content, 10);
expect(parsed).toBeGreaterThan(0);
return parsed;
}, {timeout: 10_000});
expect(isProcessRunning(pid)).toBe(true);
await fixture.connection.cancel({sessionId});
expect((await promptResponse).stopReason).toBe("cancelled");
await vi.waitFor(() => {
expect(isProcessRunning(pid)).toBe(false);
}, {timeout: 5_000});
});
});