The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { describe, expect, it } from "@effect/vitest"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as Schema from "effect/Schema"; |
| 4 | |
| 5 | import { EnvironmentAuthInvalidError } from "@t3tools/contracts"; |
| 6 | import { hashTelemetryId } from "@t3tools/shared/telemetryId"; |
| 7 | import { |
| 8 | bootstrapRemoteBearerSession, |
| 9 | exchangeRemoteDpopAccessToken, |
| 10 | issueRemoteDpopWebSocketTicket, |
| 11 | issueRemoteWebSocketTicket, |
| 12 | resolveRemoteDpopWebSocketConnectionUrl, |
| 13 | resolveRemoteWebSocketConnectionUrl, |
| 14 | } from "./remote.ts"; |
| 15 | import * as ClientCapabilities from "../platform/capabilities.ts"; |
| 16 | import * as RpcHttp from "../rpc/http.ts"; |
| 17 | |
| 18 | const isEnvironmentAuthInvalidError = Schema.is(EnvironmentAuthInvalidError); |
| 19 | |
| 20 | type FetchCall = readonly [input: RequestInfo | URL, init: RequestInit]; |
| 21 | |
| 22 | const recordedFetch = (...responses: ReadonlyArray<Response>) => { |
| 23 | const calls: Array<FetchCall> = []; |
| 24 | let responseIndex = 0; |
| 25 | const fetchFn = ((input, init) => { |
| 26 | calls.push([input, init ?? {}]); |
| 27 | const response = responses[responseIndex++]; |
| 28 | if (!response) { |
| 29 | return Promise.reject(new Error("Unexpected fetch call")); |
| 30 | } |
| 31 | return Promise.resolve(response); |
| 32 | }) satisfies typeof fetch; |
| 33 | |
| 34 | return { fetchFn, calls }; |
| 35 | }; |
| 36 | |
| 37 | const provideRemoteHttp = (fetchFn: typeof fetch) => |
| 38 | Effect.provide(RpcHttp.layerRemoteHttpClient(fetchFn)); |
| 39 | |
| 40 | const expectFetchCall = ( |
| 41 | calls: ReadonlyArray<FetchCall>, |
| 42 | index: number, |
| 43 | expected: { |
| 44 | readonly url: string; |
| 45 | readonly method: string; |
| 46 | readonly headers?: Record<string, string>; |
| 47 | readonly body?: string; |
| 48 | }, |
| 49 | ): void => { |
| 50 | const call = calls[index - 1]; |
| 51 | expect(call).toBeDefined(); |
| 52 | if (!call) { |
| 53 | return; |
| 54 | } |
| 55 | |
| 56 | const [url, init] = call; |
| 57 | expect(String(url)).toBe(expected.url); |
| 58 | expect(init).toEqual( |
| 59 | expect.objectContaining({ |
| 60 | method: expected.method, |
| 61 | }), |
| 62 | ); |
| 63 | expect(init.headers).toEqual(expect.objectContaining(expected.headers ?? {})); |
| 64 | |
| 65 | if ("body" in expected) { |
| 66 | const body = init.body; |
| 67 | if (typeof body === "string") { |
| 68 | expect(body).toBe(expected.body); |
| 69 | } else if (body instanceof Uint8Array) { |
| 70 | expect(new TextDecoder().decode(body)).toBe(expected.body); |
| 71 | } else { |
| 72 | throw new Error("Expected fetch request body"); |
| 73 | } |
| 74 | } |
| 75 | }; |
| 76 | |
| 77 | describe("remote environment authorization", () => { |
| 78 | it.effect("exchanges managed credentials and admits websocket requests with DPoP", () => |
| 79 | Effect.gen(function* () { |
| 80 | const fetch = recordedFetch( |
| 81 | Response.json({ |
| 82 | access_token: "dpop-access-token", |
| 83 | issued_token_type: "urn:ietf:params:oauth:token-type:access_token", |
| 84 | token_type: "DPoP", |
| 85 | expires_in: 3600, |
| 86 | scope: "orchestration:read orchestration:operate terminal:operate review:write", |
| 87 | }), |
| 88 | Response.json({ |
| 89 | ticket: "ws-ticket", |
| 90 | expiresAt: "2026-05-01T12:05:00.000Z", |
| 91 | }), |
| 92 | ); |
| 93 | |
| 94 | const token = yield* exchangeRemoteDpopAccessToken({ |
| 95 | httpBaseUrl: "https://remote.example.com/", |
| 96 | credential: "one-time-credential", |
| 97 | dpopProof: "token-proof", |
| 98 | clientMetadata: { |
| 99 | label: "T3 Code Mobile", |
| 100 | deviceType: "mobile", |
| 101 | os: "iOS", |
| 102 | }, |
| 103 | }).pipe(provideRemoteHttp(fetch.fetchFn)); |
| 104 | yield* issueRemoteDpopWebSocketTicket({ |
| 105 | httpBaseUrl: "https://remote.example.com/", |
| 106 | accessToken: token.access_token, |
| 107 | dpopProof: "resource-proof", |
| 108 | }).pipe(provideRemoteHttp(fetch.fetchFn)); |
| 109 | |
| 110 | expectFetchCall(fetch.calls, 1, { |
| 111 | url: "https://remote.example.com/oauth/token", |
| 112 | method: "POST", |
| 113 | headers: { dpop: "token-proof", "content-type": "application/x-www-form-urlencoded" }, |
| 114 | body: "grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Atoken-exchange&subject_token=one-time-credential&subject_token_type=urn%3At3%3Aparams%3Aoauth%3Atoken-type%3Aenvironment-bootstrap&requested_token_type=urn%3Aietf%3Aparams%3Aoauth%3Atoken-type%3Aaccess_token&client_label=T3+Code+Mobile&client_device_type=mobile&client_os=iOS", |
| 115 | }); |
| 116 | expectFetchCall(fetch.calls, 2, { |
| 117 | url: "https://remote.example.com/api/auth/websocket-ticket", |
| 118 | method: "POST", |
| 119 | headers: { |
| 120 | authorization: "DPoP dpop-access-token", |
| 121 | dpop: "resource-proof", |
| 122 | }, |
| 123 | }); |
| 124 | }), |
| 125 | ); |
| 126 | |
| 127 | it.effect("allows a client to explicitly narrow a pairing grant", () => |
| 128 | Effect.gen(function* () { |
| 129 | const fetch = recordedFetch( |
| 130 | Response.json( |
| 131 | { |
| 132 | access_token: "read-only-token", |
| 133 | issued_token_type: "urn:ietf:params:oauth:token-type:access_token", |
| 134 | token_type: "Bearer", |
| 135 | expires_in: 3600, |
| 136 | scope: "orchestration:read", |
| 137 | }, |
| 138 | { status: 200 }, |
| 139 | ), |
| 140 | ); |
| 141 | |
| 142 | yield* bootstrapRemoteBearerSession({ |
| 143 | httpBaseUrl: "https://remote.example.com/", |
| 144 | credential: "pairing-token", |
| 145 | scopes: ["orchestration:read"], |
| 146 | }).pipe(provideRemoteHttp(fetch.fetchFn)); |
| 147 | |
| 148 | expectFetchCall(fetch.calls, 1, { |
| 149 | url: "https://remote.example.com/oauth/token", |
| 150 | method: "POST", |
| 151 | body: "grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Atoken-exchange&subject_token=pairing-token&subject_token_type=urn%3At3%3Aparams%3Aoauth%3Atoken-type%3Aenvironment-bootstrap&requested_token_type=urn%3Aietf%3Aparams%3Aoauth%3Atoken-type%3Aaccess_token&scope=orchestration%3Aread", |
| 152 | }); |
| 153 | }), |
| 154 | ); |
| 155 | |
| 156 | it.effect("revives declared typed errors from remote auth failures", () => |
| 157 | Effect.gen(function* () { |
| 158 | const fetch = recordedFetch( |
| 159 | Response.json( |
| 160 | { |
| 161 | _tag: "EnvironmentAuthInvalidError", |
| 162 | code: "auth_invalid", |
| 163 | reason: "missing_credential", |
| 164 | traceId: "trace-auth-test", |
| 165 | }, |
| 166 | { status: 401 }, |
| 167 | ), |
| 168 | ); |
| 169 | |
| 170 | const error = yield* issueRemoteWebSocketTicket({ |
| 171 | httpBaseUrl: "https://remote.example.com/", |
| 172 | bearerToken: "expired-token", |
| 173 | }).pipe(provideRemoteHttp(fetch.fetchFn), Effect.flip); |
| 174 | |
| 175 | expect(isEnvironmentAuthInvalidError(error)).toBe(true); |
| 176 | if (isEnvironmentAuthInvalidError(error)) { |
| 177 | expect(error.reason).toBe("missing_credential"); |
| 178 | expect(error.traceId).toBe("trace-auth-test"); |
| 179 | } |
| 180 | }), |
| 181 | ); |
| 182 | |
| 183 | it.effect("mints a websocket url that targets the rpc route with a short-lived ticket", () => |
| 184 | Effect.gen(function* () { |
| 185 | const fetch = recordedFetch( |
| 186 | Response.json( |
| 187 | { |
| 188 | ticket: "ws-ticket", |
| 189 | expiresAt: "2026-05-01T12:05:00.000Z", |
| 190 | }, |
| 191 | { status: 200 }, |
| 192 | ), |
| 193 | ); |
| 194 | |
| 195 | const url = yield* resolveRemoteWebSocketConnectionUrl({ |
| 196 | wsBaseUrl: "wss://remote.example.com/", |
| 197 | httpBaseUrl: "https://remote.example.com/", |
| 198 | bearerToken: "bearer-token", |
| 199 | clientMetadata: { |
| 200 | surface: "mobile", |
| 201 | appVersion: "1.2.3", |
| 202 | deviceType: "mobile", |
| 203 | os: "Android", |
| 204 | osMajorVersion: 15, |
| 205 | deviceModel: "Pixel 9", |
| 206 | }, |
| 207 | connectionMethod: "relay", |
| 208 | }).pipe(provideRemoteHttp(fetch.fetchFn)); |
| 209 | |
| 210 | expect(url).toBe( |
| 211 | "wss://remote.example.com/ws?wsTicket=ws-ticket&clientSurface=mobile&clientAppVersion=1.2.3&clientDeviceType=phone&clientOs=Android&clientOsMajorVersion=15&clientDeviceModel=Pixel+9&connectionMethod=relay", |
| 212 | ); |
| 213 | }), |
| 214 | ); |
| 215 | |
| 216 | it.effect("binds a DPoP client's rpc ticket to its key and proves the upgrade", () => |
| 217 | Effect.gen(function* () { |
| 218 | const fetch = recordedFetch( |
| 219 | Response.json({ ticket: "ws-ticket", expiresAt: "2026-05-01T12:05:00.000Z" }), |
| 220 | ); |
| 221 | const proofs: Array<{ method: string; url: string; accessToken: string }> = []; |
| 222 | |
| 223 | const url = yield* resolveRemoteDpopWebSocketConnectionUrl({ |
| 224 | wsBaseUrl: "wss://remote.example.com/", |
| 225 | httpBaseUrl: "https://remote.example.com/", |
| 226 | accessToken: "dpop-access-token", |
| 227 | createProof: (input) => { |
| 228 | proofs.push(input); |
| 229 | return Effect.succeed(`proof-${proofs.length}`); |
| 230 | }, |
| 231 | connectionMethod: "relay", |
| 232 | }).pipe(provideRemoteHttp(fetch.fetchFn)); |
| 233 | |
| 234 | expectFetchCall(fetch.calls, 1, { |
| 235 | url: "https://remote.example.com/api/auth/websocket-ticket/rpc", |
| 236 | method: "POST", |
| 237 | headers: { authorization: "DPoP dpop-access-token", dpop: "proof-1" }, |
| 238 | }); |
| 239 | expect(proofs).toEqual([ |
| 240 | { |
| 241 | method: "POST", |
| 242 | url: "https://remote.example.com/api/auth/websocket-ticket/rpc", |
| 243 | accessToken: "dpop-access-token", |
| 244 | }, |
| 245 | { method: "GET", url: "https://remote.example.com/ws", accessToken: "ws-ticket" }, |
| 246 | ]); |
| 247 | expect(url).toBe( |
| 248 | "wss://remote.example.com/ws?wsTicket=ws-ticket&dpop=proof-2&connectionMethod=relay", |
| 249 | ); |
| 250 | }), |
| 251 | ); |
| 252 | |
| 253 | it.effect("requests the audience route even without a fallback proof", () => |
| 254 | Effect.gen(function* () { |
| 255 | const fetch = recordedFetch( |
| 256 | Response.json({ ticket: "media-ticket", expiresAt: "2026-05-01T12:05:00.000Z" }), |
| 257 | ); |
| 258 | yield* issueRemoteDpopWebSocketTicket({ |
| 259 | httpBaseUrl: "https://remote.example.com/", |
| 260 | accessToken: "dpop-access-token", |
| 261 | dpopProof: "media-proof", |
| 262 | use: "media", |
| 263 | }).pipe(provideRemoteHttp(fetch.fetchFn)); |
| 264 | expectFetchCall(fetch.calls, 1, { |
| 265 | url: "https://remote.example.com/api/auth/websocket-ticket/media", |
| 266 | method: "POST", |
| 267 | headers: { dpop: "media-proof" }, |
| 268 | }); |
| 269 | }), |
| 270 | ); |
| 271 | |
| 272 | it.effect("falls back to the bare ticket path on a server without the audience route", () => |
| 273 | Effect.gen(function* () { |
| 274 | const fetch = recordedFetch( |
| 275 | Response.json({ message: "Not Found" }, { status: 404 }), |
| 276 | Response.json({ ticket: "old-ticket", expiresAt: "2026-05-01T12:05:00.000Z" }), |
| 277 | ); |
| 278 | const proofs: Array<{ method: string; url: string; accessToken: string }> = []; |
| 279 | |
| 280 | const url = yield* resolveRemoteDpopWebSocketConnectionUrl({ |
| 281 | wsBaseUrl: "wss://remote.example.com/", |
| 282 | httpBaseUrl: "https://remote.example.com/", |
| 283 | accessToken: "dpop-access-token", |
| 284 | createProof: (input) => { |
| 285 | proofs.push(input); |
| 286 | return Effect.succeed(`proof-${proofs.length}`); |
| 287 | }, |
| 288 | }).pipe(provideRemoteHttp(fetch.fetchFn)); |
| 289 | |
| 290 | expect(fetch.calls.map(([target]) => String(target))).toEqual([ |
| 291 | "https://remote.example.com/api/auth/websocket-ticket/rpc", |
| 292 | "https://remote.example.com/api/auth/websocket-ticket", |
| 293 | ]); |
| 294 | expect(proofs).toEqual([ |
| 295 | { |
| 296 | method: "POST", |
| 297 | url: "https://remote.example.com/api/auth/websocket-ticket/rpc", |
| 298 | accessToken: "dpop-access-token", |
| 299 | }, |
| 300 | { |
| 301 | method: "POST", |
| 302 | url: "https://remote.example.com/api/auth/websocket-ticket", |
| 303 | accessToken: "dpop-access-token", |
| 304 | }, |
| 305 | { method: "GET", url: "https://remote.example.com/ws", accessToken: "old-ticket" }, |
| 306 | ]); |
| 307 | expectFetchCall(fetch.calls, 2, { |
| 308 | url: "https://remote.example.com/api/auth/websocket-ticket", |
| 309 | method: "POST", |
| 310 | headers: { dpop: "proof-2" }, |
| 311 | }); |
| 312 | expect(new URL(url).searchParams.get("wsTicket")).toBe("old-ticket"); |
| 313 | expect(new URL(url).searchParams.get("dpop")).toBe("proof-3"); |
| 314 | }), |
| 315 | ); |
| 316 | |
| 317 | it.effect("names the signed-in Connect account and this device by hash only", () => |
| 318 | Effect.gen(function* () { |
| 319 | const fetch = recordedFetch( |
| 320 | Response.json({ ticket: "ws-ticket", expiresAt: "2026-05-01T12:05:00.000Z" }), |
| 321 | ); |
| 322 | |
| 323 | const url = new URL( |
| 324 | yield* resolveRemoteWebSocketConnectionUrl({ |
| 325 | wsBaseUrl: "wss://remote.example.com/", |
| 326 | httpBaseUrl: "https://remote.example.com/", |
| 327 | bearerToken: "bearer-token", |
| 328 | }).pipe( |
| 329 | provideRemoteHttp(fetch.fetchFn), |
| 330 | Effect.provideService( |
| 331 | ClientCapabilities.CloudSession, |
| 332 | ClientCapabilities.CloudSession.of({ |
| 333 | identity: Effect.succeedSome({ accountId: "user_connect" }), |
| 334 | clerkToken: Effect.succeed("clerk-token"), |
| 335 | }), |
| 336 | ), |
| 337 | Effect.provideService(ClientCapabilities.AnalyticsDevice, { |
| 338 | id: Effect.succeedSome("device-1"), |
| 339 | }), |
| 340 | ), |
| 341 | ); |
| 342 | |
| 343 | expect(url.searchParams.get("clientOwner")).toBe(hashTelemetryId("user_connect")); |
| 344 | expect(url.searchParams.get("clientDevice")).toBe(hashTelemetryId("device-1")); |
| 345 | expect(url.toString()).not.toContain("user_connect"); |
| 346 | expect(url.toString()).not.toContain("device-1"); |
| 347 | }), |
| 348 | ); |
| 349 | }); |