The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as NodeNet from "node:net"; |
| 2 | import { expect } from "e2e"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import * as FileSystem from "effect/FileSystem"; |
| 5 | import * as Schema from "effect/Schema"; |
| 6 | |
| 7 | import { APP_DIR, T3_HOME } from "../e2e.config.ts"; |
| 8 | import { run } from "./shell.ts"; |
| 9 | import { runPromise, test, WEB } from "./t3.ts"; |
| 10 | |
| 11 | const decodeLaunched = Schema.decodeUnknownEffect( |
| 12 | Schema.fromJsonString(Schema.Struct({ threadId: Schema.String })), |
| 13 | ); |
| 14 | |
| 15 | test( |
| 16 | "Cancelling worktree setup during its post-checkout hook removes the new worktree", |
| 17 | WEB, |
| 18 | async ({ t3, app, screen }) => { |
| 19 | const project = await t3.project("demo"); |
| 20 | const git = (...args: ReadonlyArray<string>) => |
| 21 | runPromise(run("git", args, { cwd: project })).then(({ stdout }) => stdout); |
| 22 | const connected = Promise.withResolvers<void>(); |
| 23 | const sockets = new Set<NodeNet.Socket>(); |
| 24 | const server = NodeNet.createServer((socket) => { |
| 25 | sockets.add(socket); |
| 26 | connected.resolve(); |
| 27 | }); |
| 28 | using _gateServer = { |
| 29 | [Symbol.dispose]() { |
| 30 | for (const socket of sockets) socket.destroy(); |
| 31 | server.close(); |
| 32 | }, |
| 33 | }; |
| 34 | await new Promise<void>((resolve, reject) => { |
| 35 | server.once("error", reject); |
| 36 | server.listen(0, "127.0.0.1", resolve); |
| 37 | }); |
| 38 | const address = server.address(); |
| 39 | if (address === null || typeof address === "string") throw new Error("Missing hook gate port"); |
| 40 | await runPromise( |
| 41 | Effect.gen(function* () { |
| 42 | const fs = yield* FileSystem.FileSystem; |
| 43 | const hook = `${project}/.git/hooks/post-checkout`; |
| 44 | const gate = `${project}/hook-gate.cjs`; |
| 45 | yield* fs.writeFileString( |
| 46 | gate, |
| 47 | `const s = require('node:net').connect(${address.port}, '127.0.0.1'); s.on('connect', () => process.stderr.write('Updating files: 50% (1/1000)\\n')); s.resume();`, |
| 48 | ); |
| 49 | yield* fs.writeFileString( |
| 50 | hook, |
| 51 | `#!/bin/sh\nexec "${process.execPath.replaceAll("\\", "/")}" "${gate.replaceAll("\\", "/")}"\n`, |
| 52 | ); |
| 53 | yield* fs.chmod(hook, 0o755); |
| 54 | }), |
| 55 | ); |
| 56 | const started = await runPromise( |
| 57 | run( |
| 58 | process.execPath, |
| 59 | [ |
| 60 | "apps/server/src/bin.ts", |
| 61 | "thread", |
| 62 | "start", |
| 63 | project, |
| 64 | "hello", |
| 65 | "--worktree", |
| 66 | "main", |
| 67 | "--provider", |
| 68 | "devReplay", |
| 69 | "--model", |
| 70 | "showcase", |
| 71 | "--json", |
| 72 | "--base-dir", |
| 73 | T3_HOME, |
| 74 | ], |
| 75 | { cwd: APP_DIR, env: { PATH: process.env.PATH, HOME: process.env.HOME } }, |
| 76 | ).pipe(Effect.flatMap(({ stdout }) => decodeLaunched(stdout))), |
| 77 | ); |
| 78 | |
| 79 | await connected.promise; |
| 80 | await t3.open(started); |
| 81 | await expect(screen.getByText("50%")).toBeVisible(); |
| 82 | await screen.getByRole("button", "Cancel").tap(); |
| 83 | await app.screenshot("after-cancel"); |
| 84 | |
| 85 | // The project's own checkout is the only one left once the cancel has cleaned up. |
| 86 | await expect |
| 87 | .poll( |
| 88 | async () => (await git("worktree", "list", "--porcelain")).match(/^worktree /gm)?.length, |
| 89 | ) |
| 90 | .toBe(1); |
| 91 | await expect.poll(() => git("branch", "--list", "t3/*")).toBe(""); |
| 92 | }, |
| 93 | ); |