|
1 | | -// @effect-diagnostics nodeBuiltinImport:off |
| 1 | +// @effect-diagnostics nodeBuiltinImport:off globalTimers:off |
2 | 2 | // Scenarios: e2e tests that run against the real dev stack. See docs/operations/scenarios.md. |
| 3 | +import * as NodeChildProcess from "node:child_process"; |
| 4 | +import * as NodeCrypto from "node:crypto"; |
3 | 5 | import * as NodePath from "node:path"; |
| 6 | +import * as NodeTimersPromises from "node:timers/promises"; |
4 | 7 |
|
| 8 | +import { mobile, type DeviceProvider } from "@e2e-dev/mobile"; |
5 | 9 | import { web } from "@e2e-dev/web"; |
| 10 | +import { createAgentDeviceClient } from "agent-device"; |
6 | 11 | import type { E2EConfig } from "e2e"; |
7 | 12 |
|
8 | 13 | /** The checkout whose app runs. `prove-scenario.ts` points it at a base or head worktree. */ |
9 | 14 | export const APP_DIR = NodePath.resolve(process.env.T3_SCENARIO_APP_DIR ?? ".."); |
10 | | -/** A T3 home only scenarios use, recreated on every run. `.t3` is gitignored. */ |
| 15 | +/** T3 homes only scenarios use, recreated on every run. `.t3` is gitignored. */ |
11 | 16 | export const T3_HOME = NodePath.resolve(import.meta.dirname, "../.t3/scenarios"); |
| 17 | +export const MOBILE_T3_HOME = `${T3_HOME}-mobile`; |
| 18 | + |
| 19 | +/** |
| 20 | + * A pooled device reaches this machine over the tailnet, not localhost. A mobile target has no |
| 21 | + * `app.url`, so e2e cannot pick these ports; override them when another run holds them. |
| 22 | + */ |
| 23 | +export const MOBILE = { |
| 24 | + host: |
| 25 | + NodeChildProcess.spawnSync("tailscale", ["ip", "-4"], { encoding: "utf8" }) |
| 26 | + .stdout?.trim() |
| 27 | + .split("\n")[0] || "127.0.0.1", |
| 28 | + serverPort: Number(process.env.T3_SCENARIO_SERVER_PORT ?? 13_990), |
| 29 | + metroPort: Number(process.env.T3_SCENARIO_METRO_PORT ?? 8_190), |
| 30 | +}; |
| 31 | + |
| 32 | +const startApp = NodePath.join(import.meta.dirname, "scenarios/start-app.ts"); |
| 33 | + |
| 34 | +/** |
| 35 | + * Leases a device from the Simlock pool, whose gateway speaks agent-device's lease protocol. |
| 36 | + * `retainOnClose` keeps the device when the engine closes its session between attempts. |
| 37 | + */ |
| 38 | +const simlock = (device: string): DeviceProvider => { |
| 39 | + const connect = (env: Readonly<Record<string, string | undefined>>) => { |
| 40 | + const daemonBaseUrl = env.SIMLOCK_AGENT_DEVICE_URL; |
| 41 | + const daemonAuthToken = env.AGENT_DEVICE_DAEMON_AUTH_TOKEN; |
| 42 | + if (daemonBaseUrl === undefined || daemonAuthToken === undefined) { |
| 43 | + throw new Error("SIMLOCK_AGENT_DEVICE_URL and AGENT_DEVICE_DAEMON_AUTH_TOKEN are not set"); |
| 44 | + } |
| 45 | + return { |
| 46 | + daemon: { baseUrl: daemonBaseUrl, authToken: daemonAuthToken }, |
| 47 | + client: createAgentDeviceClient({ daemonBaseUrl, daemonAuthToken }), |
| 48 | + }; |
| 49 | + }; |
| 50 | + return { |
| 51 | + name: "simlock", |
| 52 | + acquire: async (request) => { |
| 53 | + const { daemon, client } = connect(request.env); |
| 54 | + const scope = { tenant: "t3-scenarios", runId: `${request.runId}-${request.slot}` }; |
| 55 | + const allocate = () => |
| 56 | + client.leases.allocate({ |
| 57 | + ...scope, |
| 58 | + platform: request.platform === "android" ? "android" : "ios", |
| 59 | + device, |
| 60 | + retainOnClose: true, |
| 61 | + // Simlock takes the lease id from the caller. |
| 62 | + leaseId: NodeCrypto.randomBytes(16).toString("hex"), |
| 63 | + }); |
| 64 | + // A busy gateway answers its health probe late, which the client reports as unavailable. |
| 65 | + let lease = await allocate().catch(() => undefined); |
| 66 | + for (let attempt = 0; lease === undefined && attempt < 12; attempt++) { |
| 67 | + request.log(`the pool is busy; asking again in 10s`); |
| 68 | + await NodeTimersPromises.setTimeout(10_000, undefined, { signal: request.signal }); |
| 69 | + lease = await (attempt === 11 ? allocate() : allocate().catch(() => undefined)); |
| 70 | + } |
| 71 | + if (lease === undefined) throw new Error(`The pool leased no ${device}`); |
| 72 | + const leaseScope = { ...scope, leaseId: lease.leaseId, leaseBackend: lease.backend }; |
| 73 | + // The pool answers `boot` with the leased device; a worker hosts several, so name ours. |
| 74 | + const platform = request.platform === "android" ? "android" : "ios"; |
| 75 | + const leased = await createAgentDeviceClient({ |
| 76 | + daemonBaseUrl: daemon.baseUrl, |
| 77 | + daemonAuthToken: daemon.authToken, |
| 78 | + ...leaseScope, |
| 79 | + }).devices.boot({ platform }); |
| 80 | + return { |
| 81 | + id: lease.leaseId, |
| 82 | + daemon, |
| 83 | + deviceId: leased.id, |
| 84 | + client: { |
| 85 | + ...leaseScope, |
| 86 | + ...(lease.leaseProvider === undefined ? {} : { leaseProvider: lease.leaseProvider }), |
| 87 | + }, |
| 88 | + }; |
| 89 | + }, |
| 90 | + release: async (lease, context) => { |
| 91 | + await connect(context.env).client.leases.release({ leaseId: lease.id }); |
| 92 | + }, |
| 93 | + }; |
| 94 | +}; |
12 | 95 |
|
13 | 96 | export default { |
14 | 97 | tests: "scenarios/**/*.e2e.ts", |
15 | 98 | targets: [ |
16 | 99 | { |
| 100 | + name: "web", |
17 | 101 | engine: web({ viewport: { width: 1280, height: 800 } }), |
18 | 102 | app: { |
19 | 103 | url: "http://127.0.0.1:0", |
20 | 104 | // Vite answers before the server does; this path is proxied to the server. |
21 | 105 | readyUrl: "http://127.0.0.1:{port}/.well-known/t3/environment", |
22 | 106 | command: { |
23 | 107 | executable: process.execPath, |
24 | | - args: [NodePath.join(import.meta.dirname, "scenarios/start-app.ts"), T3_HOME, "{port}"], |
| 108 | + args: [startApp, "web", T3_HOME, "{port}"], |
25 | 109 | cwd: APP_DIR, |
26 | 110 | startupTimeout: 300_000, |
27 | 111 | log: ".e2e/app.log", |
28 | 112 | }, |
29 | 113 | }, |
30 | 114 | }, |
| 115 | + { |
| 116 | + name: "ios", |
| 117 | + engine: mobile({ |
| 118 | + platform: "ios", |
| 119 | + device: simlock("iPhone 18 Pro"), |
| 120 | + session: "t3-scenarios", |
| 121 | + }), |
| 122 | + app: { |
| 123 | + bundleId: "com.t3tools.t3code.dev", |
| 124 | + // Metro starts after the server listens, so it answering means both are up. |
| 125 | + readyUrl: `http://127.0.0.1:${MOBILE.metroPort}/status`, |
| 126 | + command: { |
| 127 | + executable: process.execPath, |
| 128 | + args: [ |
| 129 | + startApp, |
| 130 | + "mobile", |
| 131 | + MOBILE_T3_HOME, |
| 132 | + String(MOBILE.serverPort), |
| 133 | + String(MOBILE.metroPort), |
| 134 | + MOBILE.host, |
| 135 | + ], |
| 136 | + cwd: APP_DIR, |
| 137 | + startupTimeout: 300_000, |
| 138 | + log: ".e2e/mobile-app.log", |
| 139 | + }, |
| 140 | + }, |
| 141 | + }, |
31 | 142 | ], |
32 | 143 | // One server holds every test's state, and a dev stack is heavy. |
33 | 144 | workers: 1, |
34 | | - timeout: 120_000, |
| 145 | + timeout: 300_000, |
35 | 146 | assertionTimeout: 15_000, |
36 | 147 | video: "on", |
37 | 148 | trace: "on", |
|
0 commit comments