-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathacp-e2e-shell-approval.test.ts
More file actions
71 lines (63 loc) · 3 KB
/
Copy pathacp-e2e-shell-approval.test.ts
File metadata and controls
71 lines (63 loc) · 3 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
import fs from "node:fs";
import path from "node:path";
import {afterEach, beforeEach, expect, it} from "vitest";
import {ApprovalOptionId} from "../../../ApprovalOptionId";
import {
createPermissionResponse,
createPermissionResponder,
createReadOnlyFixture,
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 createReadOnlyFixture();
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);
});
});