The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 03:37 UTC
| 1 | // `test` for web scenarios: e2e's web test plus `t3`, which seeds state and opens the signed-in |
| 2 | // app. `t3-mobile.ts` is the same for the iOS app. |
| 3 | import * as NodeCrypto from "@effect/platform-node/NodeCrypto"; |
| 4 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 5 | import * as NodeWS from "@effect/platform-node/NodeWS"; |
| 6 | import { test as base } from "@e2e-dev/web"; |
| 7 | import { expect } from "e2e"; |
| 8 | import { |
| 9 | CommandId, |
| 10 | EnvironmentHttpApi, |
| 11 | ORCHESTRATION_PROTOCOL_VERSION, |
| 12 | type ProjectMutation, |
| 13 | ProjectId, |
| 14 | WsRpcGroup, |
| 15 | } from "@t3tools/contracts"; |
| 16 | import * as HostProcess from "@t3tools/shared/HostProcess"; |
| 17 | import * as SecureRandom from "effect/crypto/SecureRandom"; |
| 18 | import * as Effect from "effect/Effect"; |
| 19 | import * as FileSystem from "effect/FileSystem"; |
| 20 | import * as HttpApiClient from "effect/http-api/HttpApiClient"; |
| 21 | import * as Layer from "effect/Layer"; |
| 22 | import * as Path from "effect/Path"; |
| 23 | import * as Schedule from "effect/Schedule"; |
| 24 | import * as Schema from "effect/Schema"; |
| 25 | import { FetchHttpClient, HttpClient, HttpClientRequest, HttpClientResponse } from "effect/http"; |
| 26 | import { RpcClient, RpcSerialization } from "effect/rpc"; |
| 27 | import { Socket } from "effect/socket"; |
| 28 | |
| 29 | import { APP_DIR, T3_HOME } from "../e2e.config.ts"; |
| 30 | import { run, ScenarioError } from "./shell.ts"; |
| 31 | |
| 32 | export interface T3State { |
| 33 | /** Creates a git repository holding `files` and adds it as a project. Returns its path. */ |
| 34 | readonly project: ( |
| 35 | title: string, |
| 36 | files?: Readonly<Record<string, string | Uint8Array>>, |
| 37 | ) => Promise<string>; |
| 38 | /** |
| 39 | * Starts a thread in a project and plays one replayed turn per message before returning. |
| 40 | * `model` is a dev replay recording: showcase, todo-list, approval, question, multi-turn, |
| 41 | * command-output. |
| 42 | */ |
| 43 | readonly thread: (input: { |
| 44 | readonly project: string; |
| 45 | readonly title: string; |
| 46 | readonly messages: ReadonlyArray<string>; |
| 47 | readonly model?: string; |
| 48 | }) => Promise<T3Thread>; |
| 49 | } |
| 50 | |
| 51 | export interface T3Thread { |
| 52 | readonly threadId: string; |
| 53 | } |
| 54 | |
| 55 | export interface T3 extends T3State { |
| 56 | /** |
| 57 | * Opens the app, signed in, at a path or on a thread `t3.thread` returned. Opening the thread |
| 58 | * by its link is the reliable way to reach it: a sidebar row covers its own title text. |
| 59 | */ |
| 60 | readonly open: (target?: string | T3Thread) => Promise<void>; |
| 61 | } |
| 62 | |
| 63 | /** e2e's fixtures are promise functions; this is the one place an Effect becomes a promise. */ |
| 64 | export const runPromise = <A, E>(effect: Effect.Effect<A, E, NodeServices.NodeServices>) => |
| 65 | Effect.runPromise(effect.pipe(Effect.provide(NodeServices.layer))); |
| 66 | |
| 67 | const decodeLaunched = Schema.decodeUnknownEffect( |
| 68 | Schema.fromJsonString(Schema.Struct({ threadId: Schema.String })), |
| 69 | ); |
| 70 | |
| 71 | const decodeRuntimeState = Schema.decodeUnknownEffect( |
| 72 | Schema.fromJsonString(Schema.Struct({ origin: Schema.String, startedAt: Schema.String })), |
| 73 | ); |
| 74 | |
| 75 | type ServerApi = { |
| 76 | readonly client: HttpApiClient.ForApi<typeof EnvironmentHttpApi>; |
| 77 | readonly headers: { readonly authorization: string }; |
| 78 | }; |
| 79 | |
| 80 | /** |
| 81 | * One API client per running server, signed in once. Each CLI call starts a process that takes |
| 82 | * about 0.7 s before it does anything, so per-test state goes through the server's HTTP API. A |
| 83 | * server started again on the same home, as the second server is for each test, gets a new one. |
| 84 | */ |
| 85 | const serverApis = new Map<string, Promise<ServerApi>>(); |
| 86 | |
| 87 | const uuid = SecureRandom.SecureRandom.pipe( |
| 88 | Effect.flatMap((random) => random.uuidV4()), |
| 89 | Effect.provide(NodeCrypto.layer), |
| 90 | ); |
| 91 | |
| 92 | /** State for the server on `home`, made through the app's own CLI and API: real events, not inserted rows. */ |
| 93 | export function makeState(home: string, env: NodeJS.ProcessEnv = {}) { |
| 94 | const cli = Effect.fn("scenario.cli")(function* ( |
| 95 | args: ReadonlyArray<string>, |
| 96 | allowExit: ReadonlyArray<number> = [0], |
| 97 | ) { |
| 98 | const hostEnvironment = yield* HostProcess.Environment; |
| 99 | return yield* run( |
| 100 | yield* HostProcess.ExecutablePath, |
| 101 | ["apps/server/src/bin.ts", ...args, "--base-dir", home], |
| 102 | { |
| 103 | cwd: APP_DIR, |
| 104 | env: { PATH: hostEnvironment.PATH, HOME: hostEnvironment.HOME, ...env }, |
| 105 | allowExit, |
| 106 | }, |
| 107 | ); |
| 108 | }); |
| 109 | |
| 110 | // The server writes its origin beside its database once it listens. It also prints its pairing |
| 111 | // token, which can come first, so a missing file is read again. |
| 112 | const runtimeState = Effect.gen(function* () { |
| 113 | const fs = yield* FileSystem.FileSystem; |
| 114 | const path = yield* Path.Path; |
| 115 | return yield* decodeRuntimeState( |
| 116 | yield* fs.readFileString(path.join(home, "userdata", "server-runtime.json")), |
| 117 | ); |
| 118 | }).pipe( |
| 119 | Effect.retry({ |
| 120 | schedule: Schedule.spaced("50 millis"), |
| 121 | times: 100, |
| 122 | while: (error) => error._tag === "PlatformError" && error.reason._tag === "NotFound", |
| 123 | }), |
| 124 | ); |
| 125 | const connect = Effect.fn("scenario.connect")(function* (origin: string) { |
| 126 | const issued = yield* cli(["auth", "session", "issue", "--token-only", "--label", "scenario"]); |
| 127 | const client = yield* HttpApiClient.make(EnvironmentHttpApi, { baseUrl: origin }); |
| 128 | return { client, headers: { authorization: `Bearer ${issued.stdout.trim()}` } }; |
| 129 | }, Effect.provide(FetchHttpClient.layer)); |
| 130 | const api = Effect.gen(function* () { |
| 131 | const { origin, startedAt } = yield* runtimeState; |
| 132 | const key = `${home}\n${origin}\n${startedAt}`; |
| 133 | return yield* Effect.tryPromise({ |
| 134 | try: () => { |
| 135 | const known = serverApis.get(key); |
| 136 | if (known !== undefined) return known; |
| 137 | const connecting = runPromise(connect(origin)); |
| 138 | serverApis.set(key, connecting); |
| 139 | // A failed sign-in is not kept: the next test tries again. |
| 140 | connecting.catch(() => serverApis.delete(key)); |
| 141 | return connecting; |
| 142 | }, |
| 143 | catch: (cause) => new ScenarioError({ message: `Could not reach the server API: ${cause}` }), |
| 144 | }); |
| 145 | }); |
| 146 | const mutate = Effect.fn("scenario.mutate")(function* (mutation: ProjectMutation) { |
| 147 | const { client, headers } = yield* api; |
| 148 | return yield* client.projects |
| 149 | .mutate({ headers, payload: mutation }) |
| 150 | .pipe( |
| 151 | Effect.mapError((cause) => new ScenarioError({ message: `${mutation.type}: ${cause}` })), |
| 152 | ); |
| 153 | }); |
| 154 | const projects: Array<ProjectId> = []; |
| 155 | |
| 156 | const project = Effect.fn("scenario.project")(function* ( |
| 157 | title: string, |
| 158 | files: Readonly<Record<string, string | Uint8Array>>, |
| 159 | ) { |
| 160 | const fs = yield* FileSystem.FileSystem; |
| 161 | const path = yield* Path.Path; |
| 162 | // Its own folder per test: the server and this home outlive one test. |
| 163 | yield* fs.makeDirectory(path.join(home, "workspaces"), { recursive: true }); |
| 164 | const parent = yield* fs.makeTempDirectory({ |
| 165 | directory: path.join(home, "workspaces"), |
| 166 | prefix: "w-", |
| 167 | }); |
| 168 | const workspace = path.join(parent, title); |
| 169 | for (const [file, content] of Object.entries({ "README.md": "", ...files })) { |
| 170 | yield* fs.makeDirectory(path.dirname(path.join(workspace, file)), { recursive: true }); |
| 171 | yield* typeof content === "string" |
| 172 | ? fs.writeFileString(path.join(workspace, file), content) |
| 173 | : fs.writeFile(path.join(workspace, file), content); |
| 174 | } |
| 175 | const git = (args: ReadonlyArray<string>) => run("git", args, { cwd: workspace }); |
| 176 | const author = ["-c", "user.name=Scenario", "-c", "user.email=scenario@example.invalid"]; |
| 177 | yield* git(["init", "--quiet", "--initial-branch=main"]); |
| 178 | yield* git(["add", "--all"]); |
| 179 | yield* git([...author, "commit", "--quiet", "--message", "Initial commit"]); |
| 180 | const created = yield* mutate({ |
| 181 | type: "project.create", |
| 182 | commandId: CommandId.make(yield* uuid), |
| 183 | projectId: ProjectId.make(yield* uuid), |
| 184 | title, |
| 185 | workspaceRoot: workspace, |
| 186 | }); |
| 187 | projects.push(created.id); |
| 188 | return workspace; |
| 189 | }); |
| 190 | |
| 191 | const thread = Effect.fn("scenario.thread")(function* (input: { |
| 192 | readonly project: string; |
| 193 | readonly title: string; |
| 194 | readonly messages: ReadonlyArray<string>; |
| 195 | readonly model?: string; |
| 196 | }) { |
| 197 | // Exit code 2 means the turn waits for an answer, which a scenario may want to start from. |
| 198 | const turn = (args: ReadonlyArray<string>) => |
| 199 | cli(["thread", ...args, "--wait", "--json"], [0, 2]); |
| 200 | const [first = "Start", ...rest] = input.messages; |
| 201 | const replay = ["--provider", "devReplay", "--model", input.model ?? "multi-turn"]; |
| 202 | const started = yield* turn(["start", input.project, first, "--title", input.title, ...replay]); |
| 203 | const { threadId } = yield* decodeLaunched(started.stdout); |
| 204 | for (const message of rest) yield* turn(["send", threadId, message]); |
| 205 | return { threadId } satisfies T3Thread; |
| 206 | }); |
| 207 | |
| 208 | return { |
| 209 | cli, |
| 210 | state: { |
| 211 | project: (title, files = {}) => runPromise(project(title, files)), |
| 212 | thread: (input) => runPromise(thread(input)), |
| 213 | } satisfies T3State, |
| 214 | // The server outlives the test, so the next one must not see this one's projects. |
| 215 | cleanup: Effect.forEach( |
| 216 | projects, |
| 217 | (projectId) => |
| 218 | Effect.flatMap(uuid, (commandId) => |
| 219 | mutate({ |
| 220 | type: "project.delete", |
| 221 | commandId: CommandId.make(commandId), |
| 222 | projectId, |
| 223 | force: true, |
| 224 | }), |
| 225 | ), |
| 226 | { discard: true }, |
| 227 | ), |
| 228 | }; |
| 229 | } |
| 230 | |
| 231 | const readPairingPath = Effect.gen(function* () { |
| 232 | const fs = yield* FileSystem.FileSystem; |
| 233 | const path = yield* Path.Path; |
| 234 | return yield* fs.readFileString(path.join(T3_HOME, "pairing-path")); |
| 235 | }); |
| 236 | |
| 237 | /** The id every thread link starts with, asked of the server the app talks to. */ |
| 238 | export const environmentId = (baseUrl: string) => |
| 239 | HttpClient.get(`${baseUrl}/.well-known/t3/environment`).pipe( |
| 240 | Effect.flatMap(HttpClientResponse.filterStatusOk), |
| 241 | Effect.flatMap( |
| 242 | HttpClientResponse.schemaBodyJson(Schema.Struct({ environmentId: Schema.String })), |
| 243 | ), |
| 244 | Effect.map((environment) => environment.environmentId), |
| 245 | Effect.provide(FetchHttpClient.layer), |
| 246 | ); |
| 247 | |
| 248 | /** The typed RPC client of the server on `home` at `origin`, signed in through its own CLI. */ |
| 249 | export const rpcClient = Effect.fn("scenario.rpcClient")(function* (home: string, origin: string) { |
| 250 | const issued = yield* makeState(home).cli(["auth", "session", "issue", "--token-only"]); |
| 251 | const { ticket } = yield* HttpClient.execute( |
| 252 | HttpClientRequest.post(`${origin}/api/auth/websocket-ticket`).pipe( |
| 253 | HttpClientRequest.bearerToken(issued.stdout.trim()), |
| 254 | ), |
| 255 | ).pipe( |
| 256 | Effect.flatMap(HttpClientResponse.filterStatusOk), |
| 257 | Effect.flatMap(HttpClientResponse.schemaBodyJson(Schema.Struct({ ticket: Schema.String }))), |
| 258 | Effect.provide(FetchHttpClient.layer), |
| 259 | ); |
| 260 | // Built into the caller's scope: `Effect.provide` would close the socket once `make` returns. |
| 261 | const protocol = yield* Layer.build( |
| 262 | RpcClient.layerProtocolSocket().pipe( |
| 263 | Layer.provide( |
| 264 | Socket.layerWebSocket( |
| 265 | `${origin.replace(/^http/, "ws")}/ws?orchestrationProtocol=${ORCHESTRATION_PROTOCOL_VERSION}&wsTicket=${encodeURIComponent(ticket)}`, |
| 266 | ), |
| 267 | ), |
| 268 | Layer.provide(RpcSerialization.layerJson), |
| 269 | Layer.provide(NodeWS.layerWebSocketConstructor), |
| 270 | ), |
| 271 | ); |
| 272 | return yield* RpcClient.make(WsRpcGroup).pipe(Effect.provideContext(protocol)); |
| 273 | }); |
| 274 | |
| 275 | export const test = base.extend<{ t3: T3 }>({ |
| 276 | t3: async ({ app, browser }, use) => { |
| 277 | const { state, cleanup } = makeState(T3_HOME); |
| 278 | await use({ |
| 279 | ...state, |
| 280 | open: async (target = "/") => { |
| 281 | await app.open(await runPromise(readPairingPath)); |
| 282 | // The pairing page signs in and then leaves itself. Going anywhere before it has done |
| 283 | // so cancels the sign-in, and the next page is the pairing page again. |
| 284 | await expect(browser).not.toHaveURL(/\/pair(?:[?#]|$)/, { timeout: 60_000 }); |
| 285 | if (typeof target !== "string") { |
| 286 | const environment = await runPromise(environmentId(app.baseUrl ?? "")); |
| 287 | await app.open(`/${environment}/${target.threadId}`); |
| 288 | } else if (target !== "/") await app.open(target); |
| 289 | }, |
| 290 | }); |
| 291 | await runPromise(cleanup); |
| 292 | }, |
| 293 | }); |
| 294 | |
| 295 | /** Test option for a web scenario: the iOS target skips it. */ |
| 296 | export const WEB = { platforms: ["web"] } as const; |