Skip to content

Commit bb74eb2

Browse files
committed
Add e2e tests for commands cancelation
1 parent a2e2a86 commit bb74eb2

1 file changed

Lines changed: 46 additions & 1 deletion

File tree

src/__tests__/CodexACPAgent/e2e/acp-e2e-shell-approval.test.ts

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import fs from "node:fs";
22
import path from "node:path";
3-
import {afterEach, beforeEach, expect, it} from "vitest";
3+
import {afterEach, beforeEach, expect, it, vi} from "vitest";
44
import {ApprovalOptionId} from "../../../ApprovalOptionId";
55
import {
6+
createAuthenticatedFixture,
67
createPermissionResponse,
78
createPermissionResponder,
89
createReadOnlyFixture,
@@ -69,3 +70,47 @@ describeE2E("E2E shell approval tests", () => {
6970
expect(fixture.readPermissionRequests(sessionId, "execute").length).toBe(2);
7071
});
7172
});
73+
74+
describeE2E("E2E shell cancellation tests", () => {
75+
let fixture: SpawnedAgentFixture | null = null;
76+
77+
afterEach(async () => {
78+
await fixture?.dispose();
79+
fixture = null;
80+
});
81+
82+
function isProcessRunning(pid: number): boolean {
83+
try {
84+
process.kill(pid, 0);
85+
return true;
86+
} catch {
87+
return false;
88+
}
89+
}
90+
91+
it("cancels a running shell command", async () => {
92+
fixture = await createAuthenticatedFixture();
93+
const sessionId = (await fixture.createSession()).sessionId;
94+
const pidFilePath = path.join(fixture.workspaceDir, "cancel-command.pid");
95+
const command = `/bin/sh -c 'echo $$ > "${pidFilePath}"; exec sleep 100'`;
96+
97+
const promptResponse = fixture.connection.prompt({
98+
sessionId,
99+
prompt: [{type: "text", text: `Use your shell tool to run exactly \`${command}\`.`}],
100+
});
101+
102+
const pid = await vi.waitFor(() => {
103+
const content = fs.existsSync(pidFilePath) ? fs.readFileSync(pidFilePath, "utf8").trim() : "";
104+
const parsed = Number.parseInt(content, 10);
105+
expect(parsed).toBeGreaterThan(0);
106+
return parsed;
107+
}, {timeout: 10_000});
108+
expect(isProcessRunning(pid)).toBe(true);
109+
await fixture.connection.cancel({sessionId});
110+
111+
expect((await promptResponse).stopReason).toBe("cancelled");
112+
await vi.waitFor(() => {
113+
expect(isProcessRunning(pid)).toBe(false);
114+
}, {timeout: 5_000});
115+
});
116+
});

0 commit comments

Comments
 (0)