|
1 | 1 | import fs from "node:fs"; |
2 | 2 | import path from "node:path"; |
3 | | -import {afterEach, beforeEach, expect, it} from "vitest"; |
| 3 | +import {afterEach, beforeEach, expect, it, onTestFinished} from "vitest"; |
4 | 4 | import {AgentMode} from "../../../AgentMode"; |
5 | 5 | import {ApprovalOptionId} from "../../../ApprovalOptionId"; |
6 | 6 | import { |
7 | 7 | createAuthenticatedFixture, |
8 | 8 | createPermissionResponder, |
9 | 9 | describeE2E, |
| 10 | + expectNoPermissionRequests, |
| 11 | + expectPermissionRequests, |
| 12 | + generateFileNameForTest, |
10 | 13 | type SpawnedAgentFixture, |
11 | 14 | } from "./acp-e2e-test-utils"; |
12 | 15 |
|
13 | | -const FILE_NAME = "approval-file.txt"; |
14 | 16 | const FILE_CONTENT = "file approval e2e"; |
15 | 17 |
|
16 | 18 | describeE2E("E2E file approval tests", () => { |
17 | 19 | let fixture: SpawnedAgentFixture; |
18 | | - let sessionId: string; |
19 | 20 |
|
20 | 21 | beforeEach(async () => { |
21 | 22 | fixture = await createAuthenticatedFixture(AgentMode.ReadOnly); |
22 | | - sessionId = (await fixture.createSession()).sessionId; |
23 | 23 | }); |
24 | 24 |
|
25 | 25 | afterEach(async () => { |
26 | 26 | await fixture.dispose(); |
27 | 27 | }); |
28 | 28 |
|
29 | | - async function expectFileApproval( |
30 | | - optionId: ApprovalOptionId, |
31 | | - expectedStopReason: "end_turn" | "cancelled", |
32 | | - ): Promise<void> { |
33 | | - fixture.setPermissionResponder(createPermissionResponder("edit", optionId)); |
34 | | - const response = await fixture.connection.prompt({ |
35 | | - sessionId, |
36 | | - prompt: [{ |
37 | | - type: "text", |
38 | | - text: `Create ${FILE_NAME} by editing files directly. Content must be exactly: ${FILE_CONTENT}. Do not use shell commands, and stop if the edit is rejected.`, |
39 | | - }], |
40 | | - }); |
41 | | - expect(response.stopReason).toBe(expectedStopReason); |
42 | | - expect(fixture.readPermissionRequests(sessionId, "edit").length).toBe(1); |
43 | | - expect(fixture.readPermissionRequests(sessionId, "execute").length).toBe(0); |
44 | | - } |
45 | | - |
46 | 29 | it("applies approved file edits", async () => { |
47 | | - await expectFileApproval(ApprovalOptionId.AllowOnce, "end_turn"); |
48 | | - const filePath = path.join(fixture.workspaceDir, FILE_NAME); |
49 | | - expect(fs.existsSync(filePath)).toBe(true); |
50 | | - expect(fs.readFileSync(filePath, "utf8").trim()).toBe(FILE_CONTENT); |
| 30 | + fixture.setPermissionResponder(createPermissionResponder("edit", ApprovalOptionId.AllowOnce)); |
| 31 | + const sessionId = await editFileDirectly(fixture, path.join(fixture.workspaceDir, generateFileNameForTest()), true); |
| 32 | + expectPermissionRequests(fixture, sessionId, {edit: 1, execute: 0}); |
51 | 33 | }); |
52 | 34 |
|
53 | 35 | it("does not apply rejected file edits", async () => { |
54 | | - await expectFileApproval(ApprovalOptionId.RejectOnce, "cancelled"); |
55 | | - expect(fs.existsSync(path.join(fixture.workspaceDir, FILE_NAME))).toBe(false); |
| 36 | + fixture.setPermissionResponder(createPermissionResponder("edit", ApprovalOptionId.RejectOnce)); |
| 37 | + const sessionId = await editFileDirectly(fixture, path.join(fixture.workspaceDir, generateFileNameForTest()), false); |
| 38 | + expectPermissionRequests(fixture, sessionId, {edit: 1, execute: 0}); |
| 39 | + }); |
| 40 | +}); |
| 41 | + |
| 42 | +describeE2E("E2E Agent mode file permission tests", () => { |
| 43 | + let fixture: SpawnedAgentFixture; |
| 44 | + |
| 45 | + beforeEach(async () => { |
| 46 | + fixture = await createAuthenticatedFixture(AgentMode.Agent); |
| 47 | + }); |
| 48 | + |
| 49 | + afterEach(async () => { |
| 50 | + await fixture.dispose(); |
| 51 | + }); |
| 52 | + |
| 53 | + it("edits a workspace file without prompting for permission", async () => { |
| 54 | + const sessionId = await editFileDirectly(fixture, path.join(fixture.workspaceDir, generateFileNameForTest()), true); |
| 55 | + expectNoPermissionRequests(fixture, sessionId); |
| 56 | + }); |
| 57 | + |
| 58 | + it("can't edit file outside workspace", async () => { |
| 59 | + const dir = createDirOutsideWorkspace(fixture); |
| 60 | + await editFileDirectly(fixture, path.join(dir, generateFileNameForTest()), false); |
56 | 61 | }); |
57 | 62 | }); |
| 63 | + |
| 64 | +describeE2E("E2E Agent with full access file permission tests", () => { |
| 65 | + let fixture: SpawnedAgentFixture; |
| 66 | + |
| 67 | + beforeEach(async () => { |
| 68 | + fixture = await createAuthenticatedFixture(AgentMode.AgentFullAccess); |
| 69 | + }); |
| 70 | + |
| 71 | + afterEach(async () => { |
| 72 | + await fixture.dispose(); |
| 73 | + }); |
| 74 | + |
| 75 | + it("edits a file outside workspace without prompting for permission", async () => { |
| 76 | + const dir = createDirOutsideWorkspace(fixture); |
| 77 | + const sessionId = await editFileDirectly(fixture, path.join(dir, generateFileNameForTest()), true); |
| 78 | + expectNoPermissionRequests(fixture, sessionId); |
| 79 | + }); |
| 80 | +}); |
| 81 | + |
| 82 | +async function editFileDirectly( |
| 83 | + fixture: SpawnedAgentFixture, |
| 84 | + filePath: string, |
| 85 | + expectSuccess: boolean, |
| 86 | +): Promise<string> { |
| 87 | + const sessionId = (await fixture.createSession()).sessionId; |
| 88 | + await fixture.connection.prompt({ |
| 89 | + sessionId, |
| 90 | + prompt: [{ |
| 91 | + type: "text", |
| 92 | + text: `Create ${filePath} by editing files directly. Content must be exactly: ${FILE_CONTENT}. Do not use shell commands.`, |
| 93 | + }], |
| 94 | + }); |
| 95 | + if (expectSuccess) { |
| 96 | + expect(fs.readFileSync(filePath, "utf8").trim()).toBe(FILE_CONTENT); |
| 97 | + } else { |
| 98 | + expect(fs.existsSync(filePath)).toBe(false); |
| 99 | + } |
| 100 | + return sessionId; |
| 101 | +} |
| 102 | + |
| 103 | +function createDirOutsideWorkspace(fixture: SpawnedAgentFixture): string { |
| 104 | + const outsideWorkspaceDir = path.join(path.dirname(fixture.workspaceDir), "outside-workspace"); |
| 105 | + onTestFinished(() => fs.rmSync(outsideWorkspaceDir, {recursive: true, force: true})); |
| 106 | + fs.mkdirSync(outsideWorkspaceDir); |
| 107 | + return outsideWorkspaceDir; |
| 108 | +} |
0 commit comments