The best way to control Claude Code, Codex and more from any computer or phone
| 1 | // Scenarios: e2e tests that run against the real dev stack. See docs/operations/scenarios.md. |
| 2 | import * as NodeCrypto from "@effect/platform-node/NodeCrypto"; |
| 3 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 4 | import { mobile, type DeviceProvider } from "@e2e-dev/mobile"; |
| 5 | import { web } from "@e2e-dev/web"; |
| 6 | import * as HostProcess from "@t3tools/shared/HostProcess"; |
| 7 | import { createAgentDeviceClient } from "agent-device"; |
| 8 | import type { E2EConfig } from "e2e"; |
| 9 | import * as Effect from "effect/Effect"; |
| 10 | import * as Path from "effect/Path"; |
| 11 | import * as SecureRandom from "effect/crypto/SecureRandom"; |
| 12 | import * as Schedule from "effect/Schedule"; |
| 13 | |
| 14 | import { run, ScenarioError } from "./scenarios/shell.ts"; |
| 15 | |
| 16 | // e2e loads this module for its values, so the paths and the host are resolved once, here. |
| 17 | const resolved = await Effect.gen(function* () { |
| 18 | const path = yield* Path.Path; |
| 19 | const env = yield* HostProcess.Environment; |
| 20 | const scripts = yield* path.fromFileUrl(new URL(".", import.meta.url)); |
| 21 | const home = path.resolve(scripts, "../.t3/scenarios"); |
| 22 | // A pooled device reaches this machine over the tailnet, not localhost. |
| 23 | const host = yield* run("tailscale", ["ip", "-4"]).pipe( |
| 24 | Effect.map((result) => result.stdout.trim().split("\n")[0] ?? ""), |
| 25 | Effect.orElseSucceed(() => ""), |
| 26 | ); |
| 27 | return { |
| 28 | node: yield* HostProcess.ExecutablePath, |
| 29 | scripts, |
| 30 | /** The checkout whose app runs. `prove-scenario.ts` points it at a base or head worktree. */ |
| 31 | appDir: path.resolve(scripts, env.T3_SCENARIO_APP_DIR ?? ".."), |
| 32 | home, |
| 33 | startApp: path.join(scripts, "scenarios/start-app.ts"), |
| 34 | // e2e gives the app only PATH, HOME and the temp directory. A scenario that needs more names |
| 35 | // it here: exact names or prefixes ending in `*`, separated by commas. |
| 36 | inheritEnv: (env.T3_SCENARIO_INHERIT_ENV ?? "") |
| 37 | .split(",") |
| 38 | .map((name) => name.trim()) |
| 39 | .filter((name) => name !== ""), |
| 40 | mobile: { |
| 41 | host: host || "127.0.0.1", |
| 42 | // A mobile target has no `app.url`, so e2e cannot pick these ports. |
| 43 | serverPort: Number(env.T3_SCENARIO_SERVER_PORT ?? 13_990), |
| 44 | metroPort: Number(env.T3_SCENARIO_METRO_PORT ?? 8_190), |
| 45 | }, |
| 46 | }; |
| 47 | }).pipe(Effect.provide(NodeServices.layer), Effect.runPromise); |
| 48 | |
| 49 | export const APP_DIR = resolved.appDir; |
| 50 | /** T3 homes only scenarios use, recreated on every run. `.t3` is gitignored. */ |
| 51 | export const T3_HOME = resolved.home; |
| 52 | export const MOBILE_T3_HOME = `${T3_HOME}-mobile`; |
| 53 | export const MOBILE = resolved.mobile; |
| 54 | const startApp = resolved.startApp; |
| 55 | /** |
| 56 | * The one address e2e opens the web app at and the app is told to bind. Never `localhost`: the |
| 57 | * runner and the app can resolve that name to different addresses. |
| 58 | */ |
| 59 | const LOOPBACK = "127.0.0.1"; |
| 60 | |
| 61 | /** |
| 62 | * Leases a device from the Simlock pool, whose gateway speaks agent-device's lease protocol. |
| 63 | * `retainOnClose` keeps the device when the engine closes its session between attempts. |
| 64 | */ |
| 65 | const simlock = (device: string): DeviceProvider => { |
| 66 | const connect = Effect.fn("simlock.connect")(function* ( |
| 67 | env: Readonly<Record<string, string | undefined>>, |
| 68 | ) { |
| 69 | const daemonBaseUrl = env.SIMLOCK_AGENT_DEVICE_URL; |
| 70 | const daemonAuthToken = env.AGENT_DEVICE_DAEMON_AUTH_TOKEN; |
| 71 | if (daemonBaseUrl === undefined || daemonAuthToken === undefined) { |
| 72 | return yield* new ScenarioError({ |
| 73 | message: "SIMLOCK_AGENT_DEVICE_URL and AGENT_DEVICE_DAEMON_AUTH_TOKEN are not set.", |
| 74 | }); |
| 75 | } |
| 76 | return { daemonBaseUrl, daemonAuthToken }; |
| 77 | }); |
| 78 | const call = <A>(description: string, request: () => Promise<A>) => |
| 79 | Effect.tryPromise({ |
| 80 | try: request, |
| 81 | catch: (cause) => new ScenarioError({ message: `${description}: ${String(cause)}` }), |
| 82 | }); |
| 83 | const acquire = Effect.fn("simlock.acquire")(function* ( |
| 84 | request: Parameters<DeviceProvider["acquire"]>[0], |
| 85 | ) { |
| 86 | const daemon = yield* connect(request.env); |
| 87 | const client = createAgentDeviceClient(daemon); |
| 88 | const platform = request.platform === "android" ? "android" : "ios"; |
| 89 | // A cache miss builds the dev client, which outlasts a test and can outlast a lease. |
| 90 | request.log(`preparing the ${platform} dev client`); |
| 91 | yield* run( |
| 92 | resolved.node, |
| 93 | ["scripts/mobile-native-client.ts", "ensure", platform, "--prepare"], |
| 94 | { |
| 95 | cwd: APP_DIR, |
| 96 | }, |
| 97 | ); |
| 98 | const scope = { tenant: "t3-scenarios", runId: `${request.runId}-${request.slot}` }; |
| 99 | // Simlock takes the lease id from the caller: 32 hex characters. |
| 100 | const leaseId = (yield* (yield* SecureRandom.SecureRandom).uuidV4()).replaceAll("-", ""); |
| 101 | const lease = yield* call(`The pool leased no ${device}`, () => |
| 102 | client.leases.allocate({ ...scope, platform, device, retainOnClose: true, leaseId }), |
| 103 | ).pipe( |
| 104 | // A busy gateway answers its health probe late, which the client reports as unavailable. |
| 105 | Effect.tapError(() => Effect.sync(() => request.log("the pool is busy; asking again"))), |
| 106 | Effect.retry({ schedule: Schedule.spaced("10 seconds"), times: 12 }), |
| 107 | ); |
| 108 | const leaseScope = { ...scope, leaseId: lease.leaseId, leaseBackend: lease.backend }; |
| 109 | // The pool answers `boot` with the leased device; a worker hosts several, so name ours. |
| 110 | const leased = yield* call("The pool did not name the leased device", () => |
| 111 | createAgentDeviceClient({ ...daemon, ...leaseScope }).devices.boot({ platform }), |
| 112 | ).pipe( |
| 113 | // e2e releases only the leases `acquire` returned. |
| 114 | Effect.tapError(() => |
| 115 | call("release", () => client.leases.release({ leaseId: lease.leaseId })).pipe( |
| 116 | Effect.ignore, |
| 117 | ), |
| 118 | ), |
| 119 | ); |
| 120 | return { |
| 121 | id: lease.leaseId, |
| 122 | daemon: { baseUrl: daemon.daemonBaseUrl, authToken: daemon.daemonAuthToken }, |
| 123 | deviceId: leased.id, |
| 124 | client: { |
| 125 | ...leaseScope, |
| 126 | ...(lease.leaseProvider === undefined ? {} : { leaseProvider: lease.leaseProvider }), |
| 127 | }, |
| 128 | }; |
| 129 | }); |
| 130 | return { |
| 131 | name: "simlock", |
| 132 | acquire: (request) => |
| 133 | acquire(request).pipe( |
| 134 | Effect.provide([NodeCrypto.layer, NodeServices.layer]), |
| 135 | Effect.runPromise, |
| 136 | ), |
| 137 | release: (lease, context) => |
| 138 | connect(context.env).pipe( |
| 139 | Effect.flatMap((daemon) => |
| 140 | call("release", () => |
| 141 | createAgentDeviceClient(daemon).leases.release({ leaseId: lease.id }), |
| 142 | ), |
| 143 | ), |
| 144 | Effect.asVoid, |
| 145 | Effect.runPromise, |
| 146 | ), |
| 147 | }; |
| 148 | }; |
| 149 | |
| 150 | export default { |
| 151 | tests: "scenarios/**/*.e2e.ts", |
| 152 | targets: [ |
| 153 | { |
| 154 | name: "web", |
| 155 | engine: web({ viewport: { width: 1280, height: 800 } }), |
| 156 | app: { |
| 157 | url: `http://${LOOPBACK}:0`, |
| 158 | // Lets a scenario put an image on the clipboard for a real keyboard paste. |
| 159 | permissions: { "clipboard-write": "grant" }, |
| 160 | // Vite answers before the server does; this path is proxied to the server. |
| 161 | readyUrl: `http://${LOOPBACK}:{port}/.well-known/t3/environment`, |
| 162 | command: { |
| 163 | executable: resolved.node, |
| 164 | args: [startApp, "web", T3_HOME, LOOPBACK, "{port}"], |
| 165 | cwd: APP_DIR, |
| 166 | inheritEnv: resolved.inheritEnv, |
| 167 | startupTimeout: 300_000, |
| 168 | log: ".e2e/app.log", |
| 169 | }, |
| 170 | }, |
| 171 | }, |
| 172 | { |
| 173 | name: "ios", |
| 174 | engine: mobile({ |
| 175 | platform: "ios", |
| 176 | device: simlock("iPhone 18 Pro"), |
| 177 | session: "t3-scenarios", |
| 178 | }), |
| 179 | app: { |
| 180 | bundleId: "com.t3tools.t3code.dev", |
| 181 | // Metro starts after the server listens, so it answering means both are up. |
| 182 | readyUrl: `http://127.0.0.1:${MOBILE.metroPort}/status`, |
| 183 | command: { |
| 184 | executable: resolved.node, |
| 185 | args: [ |
| 186 | startApp, |
| 187 | "mobile", |
| 188 | MOBILE_T3_HOME, |
| 189 | String(MOBILE.serverPort), |
| 190 | String(MOBILE.metroPort), |
| 191 | MOBILE.host, |
| 192 | ], |
| 193 | cwd: APP_DIR, |
| 194 | inheritEnv: resolved.inheritEnv, |
| 195 | startupTimeout: 300_000, |
| 196 | log: ".e2e/mobile-app.log", |
| 197 | }, |
| 198 | }, |
| 199 | }, |
| 200 | ], |
| 201 | // One server holds every test's state, and a dev stack is heavy. |
| 202 | workers: 1, |
| 203 | timeout: 300_000, |
| 204 | assertionTimeout: 15_000, |
| 205 | video: "on", |
| 206 | trace: "on", |
| 207 | reporters: ["list", "markdown"], |
| 208 | } satisfies E2EConfig; |