The best way to control Claude Code, Codex and more from any computer or phone

main
ts 115 lines 4.56 KB
Raw
1 // @effect-diagnostics nodeBuiltinImport:off -- The scenario stages a local ACP executable.
2 import * as NodeFSP from "node:fs/promises";
3 import * as NodePath from "node:path";
4
5 import { expect } from "e2e";
6
7 import { T3_HOME } from "../e2e.config.ts";
8 import { makeState, runPromise, test, WEB } from "./t3.ts";
9
10 const HOME_PATH = "~/.claude/CLAUDE.md";
11 const REPLY = "Updated your global instructions.";
12 const ACP = `
13 import { createInterface } from "node:readline";
14
15 const sessionId = "home-path-scenario";
16 const path = ${JSON.stringify(HOME_PATH)};
17 const send = (message) => process.stdout.write(JSON.stringify({ jsonrpc: "2.0", ...message }) + "\\n");
18 const update = (value) => send({ method: "session/update", params: { sessionId, update: value } });
19 for await (const line of createInterface({ input: process.stdin })) {
20 const { id, method } = JSON.parse(line);
21 if (id === undefined) continue;
22 if (method === "initialize") {
23 send({ id, result: { protocolVersion: 1, agentCapabilities: {}, authMethods: [] } });
24 } else if (method === "session/new") {
25 send({ id, result: { sessionId } });
26 } else if (method === "session/prompt") {
27 update({ sessionUpdate: "tool_call", toolCallId: "read", kind: "read", title: "Read", status: "pending", rawInput: { path } });
28 update({
29 sessionUpdate: "tool_call_update",
30 toolCallId: "read",
31 status: "completed",
32 content: [{ type: "content", content: { type: "text", text: "# Global instructions" } }],
33 });
34 update({
35 sessionUpdate: "tool_call",
36 toolCallId: "edit",
37 kind: "edit",
38 title: "Edit global instructions",
39 status: "completed",
40 content: [{
41 type: "diff",
42 path,
43 oldText: "# Global instructions\\n",
44 newText: "# Global instructions\\nPrefer small diffs.\\n",
45 changes: [{ operation: "update", path }],
46 }],
47 });
48 update({ sessionUpdate: "agent_message_chunk", content: { type: "text", text: ${JSON.stringify(REPLY)} } });
49 send({ id, result: { stopReason: "end_turn" } });
50 } else {
51 send({ id, error: { code: -32601, message: "Method not found" } });
52 }
53 }
54 `;
55
56 test(
57 "Files an agent read and edited in the home folder show their ~/ paths, not paths under the project",
58 WEB,
59 async ({ t3, app, screen }) => {
60 const project = await t3.project("demo");
61 const script = NodePath.join(project, "home-path-acp.mjs");
62 const settingsPath = NodePath.join(T3_HOME, "userdata/settings.json");
63 const originalSettings = await NodeFSP.readFile(settingsPath, "utf8");
64 const settings = JSON.parse(originalSettings);
65 await NodeFSP.writeFile(script, ACP);
66 settings.providerInstances["home-path-agent"] = {
67 driver: "acpRegistry",
68 displayName: "Home path agent",
69 enabled: true,
70 config: { source: "local", commandPath: process.execPath, commandArgs: [script] },
71 };
72 try {
73 await NodeFSP.writeFile(settingsPath, JSON.stringify(settings));
74 const { cli } = makeState(T3_HOME);
75 // The watcher reloads settings asynchronously. Retry only before a thread was created.
76 let threadId: string | undefined;
77 for (let attempt = 0; attempt < 8 && threadId === undefined; attempt++) {
78 const started = await runPromise(
79 cli(
80 [
81 "thread",
82 "start",
83 project,
84 "Tidy up my global instructions",
85 "--title",
86 "Home path tools",
87 "--provider",
88 "home-path-agent",
89 "--model",
90 "default",
91 "--wait",
92 "--json",
93 ],
94 [0, 1],
95 ),
96 );
97 if (!started.stdout.includes("threadId")) continue;
98 threadId = (JSON.parse(started.stdout) as { threadId: string }).threadId;
99 }
100 if (threadId === undefined) throw new Error("Could not start the home path turn.");
101
102 await t3.open({ threadId });
103 await screen.getByText(REPLY, { exact: true }).waitFor({ timeout: 60_000 });
104 await screen.getByRole("button", "Worked for", { exact: false }).tap();
105 await screen.getByRole("button", "Read 1 file and changed 1 file").tap();
106 await screen.getByRole("button", /^Read .*CLAUDE\.md/).tap();
107 await screen.getByText('"# Global instructions"', { exact: true }).waitFor();
108 await app.screenshot("read-and-edit-rows");
109 await expect(screen.getByText("/~/", { exact: false })).toHaveCount(0);
110 await expect(screen.getByText(HOME_PATH, { exact: true })).toHaveCount(2);
111 } finally {
112 await NodeFSP.writeFile(settingsPath, originalSettings);
113 }
114 },
115 );