The best way to control Claude Code, Codex and more from any computer or phone
| 1 | // @effect-diagnostics-next-line nodeBuiltinImport:off -- Effect's Crypto has no generateKeyPairSync or sign. |
| 2 | import * as NodeCrypto from "node:crypto"; |
| 3 | import * as NodeCryptoLayer from "@effect/platform-node/NodeCrypto"; |
| 4 | |
| 5 | import { |
| 6 | RelayCloudEnvironmentHealthRequest, |
| 7 | RelayCloudMintCredentialRequest, |
| 8 | RelayCloudEnvironmentHealthProofPayload, |
| 9 | RelayCloudMintCredentialProofPayload, |
| 10 | RelayEnvironmentHealthResponse, |
| 11 | RelayEnvironmentHealthResponseProofPayload, |
| 12 | RelayEnvironmentMintResponse, |
| 13 | RelayEnvironmentMintResponseProofPayload, |
| 14 | } from "@t3tools/contracts/relay"; |
| 15 | import { describe, expect, it } from "@effect/vitest"; |
| 16 | import * as DateTime from "effect/DateTime"; |
| 17 | import { RELAY_HEALTH_RESPONSE_TYP, RELAY_MINT_RESPONSE_TYP } from "@t3tools/shared/relayJwt"; |
| 18 | import * as Effect from "effect/Effect"; |
| 19 | import * as Layer from "effect/Layer"; |
| 20 | import * as Redacted from "effect/Redacted"; |
| 21 | import * as Result from "effect/Result"; |
| 22 | import * as Schema from "effect/Schema"; |
| 23 | import * as Tracer from "effect/Tracer"; |
| 24 | import { HttpClient, HttpClientRequest, HttpClientResponse } from "effect/http"; |
| 25 | |
| 26 | import * as CloudMintSigner from "../auth/CloudMintSigner.ts"; |
| 27 | import * as EnvironmentLinks from "./EnvironmentLinks.ts"; |
| 28 | import * as RelayConfiguration from "../Config.ts"; |
| 29 | import * as EnvironmentConnector from "./EnvironmentConnector.ts"; |
| 30 | import * as ManagedEndpointAllocations from "./ManagedEndpointAllocations.ts"; |
| 31 | |
| 32 | const cloudKeyPair = NodeCrypto.generateKeyPairSync("ed25519", { |
| 33 | privateKeyEncoding: { format: "pem", type: "pkcs8" }, |
| 34 | publicKeyEncoding: { format: "pem", type: "spki" }, |
| 35 | }); |
| 36 | |
| 37 | const environmentKeyPair = NodeCrypto.generateKeyPairSync("ed25519", { |
| 38 | privateKeyEncoding: { format: "pem", type: "pkcs8" }, |
| 39 | publicKeyEncoding: { format: "pem", type: "spki" }, |
| 40 | }); |
| 41 | |
| 42 | const otherEnvironmentKeyPair = NodeCrypto.generateKeyPairSync("ed25519", { |
| 43 | privateKeyEncoding: { format: "pem", type: "pkcs8" }, |
| 44 | publicKeyEncoding: { format: "pem", type: "spki" }, |
| 45 | }); |
| 46 | |
| 47 | const decodeHealthRequestBody = Schema.decodeUnknownSync( |
| 48 | Schema.fromJsonString(RelayCloudEnvironmentHealthRequest), |
| 49 | ); |
| 50 | const decodeMintRequestBody = Schema.decodeUnknownSync( |
| 51 | Schema.fromJsonString(RelayCloudMintCredentialRequest), |
| 52 | ); |
| 53 | const isEnvironmentConnectNotAuthorized = Schema.is( |
| 54 | EnvironmentConnector.EnvironmentConnectNotAuthorized, |
| 55 | ); |
| 56 | |
| 57 | function requestBodyText(request: HttpClientRequest.HttpClientRequest): string { |
| 58 | return request.body._tag === "Uint8Array" ? new TextDecoder().decode(request.body.body) : "{}"; |
| 59 | } |
| 60 | |
| 61 | const settings = RelayConfiguration.RelayConfiguration.of({ |
| 62 | relayIssuer: "https://relay.example.test", |
| 63 | apns: { |
| 64 | environment: "sandbox", |
| 65 | teamId: "team-id", |
| 66 | keyId: "key-id", |
| 67 | privateKey: Redacted.make("private-key"), |
| 68 | bundleId: "com.t3tools.t3code.dev", |
| 69 | }, |
| 70 | apnsDeliveryJobSigningSecret: Redacted.make("job-secret"), |
| 71 | clerkSecretKey: Redacted.make("clerk-secret"), |
| 72 | clerkPublishableKey: "pk_test_test", |
| 73 | clerkJwtAudience: "t3-code-relay", |
| 74 | cloudMintPrivateKey: Redacted.make(cloudKeyPair.privateKey), |
| 75 | cloudMintPublicKey: cloudKeyPair.publicKey, |
| 76 | managedEndpointBaseDomain: "example.test", |
| 77 | managedEndpointNamespace: undefined, |
| 78 | }); |
| 79 | |
| 80 | function signTestJwt(payload: object, typ: string, privateKey: string): string { |
| 81 | const header = Buffer.from(JSON.stringify({ alg: "EdDSA", typ })).toString("base64url"); |
| 82 | const encodedPayload = Buffer.from(JSON.stringify(payload)).toString("base64url"); |
| 83 | const input = `${header}.${encodedPayload}`; |
| 84 | return `${input}.${NodeCrypto.sign(null, Buffer.from(input), privateKey).toString("base64url")}`; |
| 85 | } |
| 86 | |
| 87 | function decodeRequestProof<T>(proof: string): T { |
| 88 | const payload = proof.split(".")[1]; |
| 89 | if (!payload) throw new Error("Missing JWT payload."); |
| 90 | return JSON.parse(Buffer.from(payload, "base64url").toString("utf8")) as T; |
| 91 | } |
| 92 | |
| 93 | function signMintResponse( |
| 94 | request: RelayCloudMintCredentialRequest, |
| 95 | overrides: Partial<RelayEnvironmentMintResponseProofPayload> = {}, |
| 96 | privateKey = environmentKeyPair.privateKey, |
| 97 | ): RelayEnvironmentMintResponse { |
| 98 | const requestProof = decodeRequestProof<RelayCloudMintCredentialProofPayload>(request.proof); |
| 99 | const payload = { |
| 100 | iss: `t3-env:${requestProof.environmentId}`, |
| 101 | aud: "https://relay.example.test", |
| 102 | sub: requestProof.environmentId, |
| 103 | jti: "mint-response-jti", |
| 104 | iat: requestProof.iat, |
| 105 | exp: requestProof.exp, |
| 106 | environmentId: requestProof.environmentId, |
| 107 | clientProofKeyThumbprint: requestProof.clientProofKeyThumbprint, |
| 108 | requestNonce: requestProof.nonce, |
| 109 | credential: "pairing_credential", |
| 110 | ...overrides, |
| 111 | } satisfies RelayEnvironmentMintResponseProofPayload; |
| 112 | return { |
| 113 | credential: payload.credential, |
| 114 | expiresAt: DateTime.formatIso(DateTime.makeUnsafe(payload.exp * 1_000)), |
| 115 | proof: signTestJwt(payload, RELAY_MINT_RESPONSE_TYP, privateKey), |
| 116 | }; |
| 117 | } |
| 118 | |
| 119 | function signHealthResponse( |
| 120 | request: RelayCloudEnvironmentHealthRequest, |
| 121 | privateKey = environmentKeyPair.privateKey, |
| 122 | overrides: Partial<RelayEnvironmentHealthResponse> = {}, |
| 123 | payloadOverrides: Partial<RelayEnvironmentHealthResponseProofPayload> = {}, |
| 124 | ): RelayEnvironmentHealthResponse { |
| 125 | const requestProof = decodeRequestProof<RelayCloudEnvironmentHealthProofPayload>(request.proof); |
| 126 | const payload = { |
| 127 | iss: `t3-env:${requestProof.environmentId}`, |
| 128 | aud: "https://relay.example.test", |
| 129 | sub: requestProof.environmentId, |
| 130 | jti: "health-response-jti", |
| 131 | iat: requestProof.iat, |
| 132 | exp: requestProof.exp, |
| 133 | environmentId: requestProof.environmentId, |
| 134 | requestNonce: requestProof.nonce, |
| 135 | status: "online", |
| 136 | descriptor: { |
| 137 | environmentId: requestProof.environmentId, |
| 138 | label: "Connector Test Environment", |
| 139 | platform: { os: "darwin", arch: "arm64" }, |
| 140 | serverVersion: "0.0.0-test", |
| 141 | capabilities: { repositoryIdentity: true }, |
| 142 | }, |
| 143 | checkedAt: DateTime.formatIso(DateTime.makeUnsafe(requestProof.iat * 1_000)), |
| 144 | ...payloadOverrides, |
| 145 | } satisfies RelayEnvironmentHealthResponseProofPayload; |
| 146 | return { |
| 147 | environmentId: payload.environmentId, |
| 148 | status: "online", |
| 149 | descriptor: payload.descriptor, |
| 150 | checkedAt: payload.checkedAt, |
| 151 | proof: signTestJwt(payload, RELAY_HEALTH_RESPONSE_TYP, privateKey), |
| 152 | ...overrides, |
| 153 | }; |
| 154 | } |
| 155 | |
| 156 | function layerConnectorTest( |
| 157 | execute: ( |
| 158 | request: HttpClientRequest.HttpClientRequest, |
| 159 | ) => Effect.Effect<HttpClientResponse.HttpClientResponse>, |
| 160 | options?: { |
| 161 | readonly links?: EnvironmentLinks.EnvironmentLinks["Service"]; |
| 162 | readonly allocations?: ManagedEndpointAllocations.ManagedEndpointAllocations["Service"]; |
| 163 | }, |
| 164 | ) { |
| 165 | return EnvironmentConnector.layer.pipe( |
| 166 | Layer.provide(CloudMintSigner.layer), |
| 167 | Layer.provide(NodeCryptoLayer.layer), |
| 168 | Layer.provide(Layer.succeed(EnvironmentLinks.EnvironmentLinks, options?.links ?? makeLinks())), |
| 169 | Layer.provide( |
| 170 | Layer.succeed( |
| 171 | ManagedEndpointAllocations.ManagedEndpointAllocations, |
| 172 | options?.allocations ?? makeAllocations(), |
| 173 | ), |
| 174 | ), |
| 175 | Layer.provide(RelayConfiguration.layer(settings)), |
| 176 | Layer.provide(Layer.succeed(HttpClient.HttpClient, HttpClient.make(execute))), |
| 177 | ); |
| 178 | } |
| 179 | |
| 180 | function makeAllocations( |
| 181 | allocation: ManagedEndpointAllocations.ManagedEndpointAllocation | null = { |
| 182 | userId: "user_123", |
| 183 | environmentId: "env-connector-test", |
| 184 | hostname: "env.example.test", |
| 185 | tunnelId: "tunnel-id", |
| 186 | tunnelName: "tunnel-name", |
| 187 | dnsRecordId: "dns-record-id", |
| 188 | readyAt: "2026-05-25T00:00:00.000Z", |
| 189 | origin: { localHttpHost: "127.0.0.1", localHttpPort: 3773 }, |
| 190 | updatedAt: "2026-05-25T00:00:00.000Z", |
| 191 | generation: 1, |
| 192 | tunnelReleasedAt: null, |
| 193 | }, |
| 194 | ): ManagedEndpointAllocations.ManagedEndpointAllocations["Service"] { |
| 195 | return { |
| 196 | getByTunnelName: () => Effect.die("unused getByTunnelName"), |
| 197 | get: () => Effect.succeed(allocation), |
| 198 | reserve: () => Effect.die("unused"), |
| 199 | recordTunnel: () => Effect.die("unused"), |
| 200 | recordDns: () => Effect.die("unused"), |
| 201 | markReady: () => Effect.die("unused"), |
| 202 | enableRecovery: () => Effect.die("unused"), |
| 203 | listByTunnelNames: () => Effect.die("unused"), |
| 204 | claimRelease: () => Effect.die("unused"), |
| 205 | withClaimedTunnel: () => Effect.die("unused"), |
| 206 | claimDeprovision: () => Effect.die("unused"), |
| 207 | removeClaimed: () => Effect.die("unused"), |
| 208 | }; |
| 209 | } |
| 210 | |
| 211 | function makeLinks( |
| 212 | overrides: Partial<EnvironmentLinks.RelayLinkedEnvironmentRecord> = {}, |
| 213 | ): EnvironmentLinks.EnvironmentLinks["Service"] { |
| 214 | return { |
| 215 | upsert: () => Effect.void, |
| 216 | listDeliveryUsersForEnvironment: () => Effect.succeed([]), |
| 217 | listForUser: () => Effect.succeed([]), |
| 218 | getForUser: () => |
| 219 | Effect.succeed({ |
| 220 | environmentId: "env-connector-test" as never, |
| 221 | label: "Connector Test Environment", |
| 222 | endpoint: { |
| 223 | httpBaseUrl: "https://env.example.test/", |
| 224 | wsBaseUrl: "wss://env.example.test/ws", |
| 225 | providerKind: "cloudflare_tunnel", |
| 226 | }, |
| 227 | linkedAt: "2026-05-25T00:00:00.000Z", |
| 228 | environmentPublicKey: environmentKeyPair.publicKey, |
| 229 | ...overrides, |
| 230 | }), |
| 231 | findActiveManagedForEnvironment: () => Effect.succeed([]), |
| 232 | updateLiveActivityPreference: () => Effect.die("unexpected Live Activity preference update"), |
| 233 | setHoldWebhooksWhileOffline: () => Effect.void, |
| 234 | revokeForUser: () => Effect.succeed(false), |
| 235 | }; |
| 236 | } |
| 237 | |
| 238 | describe("EnvironmentConnector", () => { |
| 239 | it.effect("checks linked environment health through the managed endpoint", () => { |
| 240 | const seenUrls: Array<string> = []; |
| 241 | const seenProofs: Array<RelayCloudEnvironmentHealthProofPayload> = []; |
| 242 | const execute = (request: HttpClientRequest.HttpClientRequest) => |
| 243 | Effect.sync(() => { |
| 244 | const healthRequest = decodeHealthRequestBody(requestBodyText(request)); |
| 245 | seenUrls.push(request.url); |
| 246 | seenProofs.push(decodeRequestProof(healthRequest.proof)); |
| 247 | return HttpClientResponse.fromWeb( |
| 248 | request, |
| 249 | Response.json(signHealthResponse(healthRequest), { status: 200 }), |
| 250 | ); |
| 251 | }); |
| 252 | |
| 253 | return Effect.gen(function* () { |
| 254 | const connector = yield* EnvironmentConnector.EnvironmentConnector; |
| 255 | const result = yield* connector.status({ |
| 256 | userId: "user_123", |
| 257 | environmentId: "env-connector-test", |
| 258 | }); |
| 259 | |
| 260 | expect(seenUrls).toEqual(["https://env.example.test/api/t3-connect/health"]); |
| 261 | expect(seenProofs[0]).toMatchObject({ |
| 262 | iss: "https://relay.example.test", |
| 263 | aud: "t3-env:env-connector-test", |
| 264 | sub: "user_123", |
| 265 | environmentId: "env-connector-test", |
| 266 | scope: ["environment:status"], |
| 267 | }); |
| 268 | expect(result).toMatchObject({ |
| 269 | environmentId: "env-connector-test", |
| 270 | status: "online", |
| 271 | descriptor: { |
| 272 | environmentId: "env-connector-test", |
| 273 | label: "Connector Test Environment", |
| 274 | }, |
| 275 | }); |
| 276 | }).pipe(Effect.provide(layerConnectorTest(execute))); |
| 277 | }); |
| 278 | |
| 279 | it.effect("rejects manual endpoints before sending a health request", () => { |
| 280 | let requestCount = 0; |
| 281 | const execute = () => |
| 282 | Effect.sync(() => { |
| 283 | requestCount += 1; |
| 284 | throw new Error("unexpected request"); |
| 285 | }); |
| 286 | |
| 287 | return Effect.gen(function* () { |
| 288 | const connector = yield* EnvironmentConnector.EnvironmentConnector; |
| 289 | const result = yield* Effect.result( |
| 290 | connector.status({ |
| 291 | userId: "user_123", |
| 292 | environmentId: "env-connector-test", |
| 293 | }), |
| 294 | ); |
| 295 | |
| 296 | expect(Result.isFailure(result)).toBe(true); |
| 297 | if (Result.isFailure(result)) { |
| 298 | expect(isEnvironmentConnectNotAuthorized(result.failure)).toBe(true); |
| 299 | if (isEnvironmentConnectNotAuthorized(result.failure)) { |
| 300 | expect(result.failure).toMatchObject({ |
| 301 | operation: "status", |
| 302 | reason: "endpoint_provider_not_managed", |
| 303 | }); |
| 304 | } |
| 305 | } |
| 306 | expect(requestCount).toBe(0); |
| 307 | }).pipe( |
| 308 | Effect.provide( |
| 309 | layerConnectorTest(execute, { |
| 310 | links: makeLinks({ |
| 311 | endpoint: { |
| 312 | httpBaseUrl: "https://127.0.0.1/", |
| 313 | wsBaseUrl: "wss://127.0.0.1/ws", |
| 314 | providerKind: "manual", |
| 315 | }, |
| 316 | }), |
| 317 | }), |
| 318 | ), |
| 319 | ); |
| 320 | }); |
| 321 | |
| 322 | it.effect("rejects stale managed endpoints before sending a mint request", () => { |
| 323 | let requestCount = 0; |
| 324 | const spans: Array<Tracer.NativeSpan> = []; |
| 325 | const tracer = Tracer.make({ |
| 326 | span: (options) => { |
| 327 | const span = new Tracer.NativeSpan(options); |
| 328 | spans.push(span); |
| 329 | return span; |
| 330 | }, |
| 331 | }); |
| 332 | const execute = () => |
| 333 | Effect.sync(() => { |
| 334 | requestCount += 1; |
| 335 | throw new Error("unexpected request"); |
| 336 | }); |
| 337 | |
| 338 | return Effect.gen(function* () { |
| 339 | const connector = yield* EnvironmentConnector.EnvironmentConnector; |
| 340 | const result = yield* Effect.result( |
| 341 | connector.connect({ |
| 342 | userId: "user_123", |
| 343 | environmentId: "env-connector-test", |
| 344 | clientProofKeyThumbprint: "client-proof-key-thumbprint", |
| 345 | }), |
| 346 | ); |
| 347 | |
| 348 | expect(Result.isFailure(result)).toBe(true); |
| 349 | if (Result.isFailure(result)) { |
| 350 | expect(isEnvironmentConnectNotAuthorized(result.failure)).toBe(true); |
| 351 | if (isEnvironmentConnectNotAuthorized(result.failure)) { |
| 352 | expect(result.failure).toMatchObject({ |
| 353 | operation: "connect", |
| 354 | reason: "managed_endpoint_mismatch", |
| 355 | }); |
| 356 | } |
| 357 | } |
| 358 | const resolutionSpan = spans.find( |
| 359 | (span) => span.name === "relay.environment_connector.resolve_managed_endpoint", |
| 360 | ); |
| 361 | expect(Object.fromEntries(resolutionSpan?.attributes ?? [])).toMatchObject({ |
| 362 | "relay.authorization.allocation_hostname": "env.example.test", |
| 363 | "relay.authorization.allocation_has_ready_at": true, |
| 364 | "relay.authorization.allocation_has_tunnel_id": true, |
| 365 | "relay.authorization.allocation_has_dns_record_id": true, |
| 366 | "relay.authorization.linked_http_base_url": "https://attacker.example.test/", |
| 367 | "relay.authorization.linked_ws_base_url": "wss://attacker.example.test/ws", |
| 368 | "relay.authorization.resolved_http_base_url": "https://env.example.test/", |
| 369 | "relay.authorization.resolved_ws_base_url": "wss://env.example.test/ws", |
| 370 | }); |
| 371 | expect(requestCount).toBe(0); |
| 372 | }).pipe( |
| 373 | Effect.provide( |
| 374 | layerConnectorTest(execute, { |
| 375 | links: makeLinks({ |
| 376 | endpoint: { |
| 377 | httpBaseUrl: "https://attacker.example.test/", |
| 378 | wsBaseUrl: "wss://attacker.example.test/ws", |
| 379 | providerKind: "cloudflare_tunnel", |
| 380 | }, |
| 381 | }), |
| 382 | }), |
| 383 | ), |
| 384 | Effect.provideService(Tracer.Tracer, tracer), |
| 385 | ); |
| 386 | }); |
| 387 | |
| 388 | it.effect("rejects unready managed endpoint allocations before sending a request", () => { |
| 389 | let requestCount = 0; |
| 390 | const execute = () => |
| 391 | Effect.sync(() => { |
| 392 | requestCount += 1; |
| 393 | throw new Error("unexpected request"); |
| 394 | }); |
| 395 | |
| 396 | return Effect.gen(function* () { |
| 397 | const connector = yield* EnvironmentConnector.EnvironmentConnector; |
| 398 | const result = yield* Effect.result( |
| 399 | connector.status({ |
| 400 | userId: "user_123", |
| 401 | environmentId: "env-connector-test", |
| 402 | }), |
| 403 | ); |
| 404 | |
| 405 | expect(Result.isFailure(result)).toBe(true); |
| 406 | if (Result.isFailure(result)) { |
| 407 | expect(isEnvironmentConnectNotAuthorized(result.failure)).toBe(true); |
| 408 | if (isEnvironmentConnectNotAuthorized(result.failure)) { |
| 409 | expect(result.failure).toMatchObject({ |
| 410 | operation: "status", |
| 411 | reason: "managed_endpoint_allocation_not_ready", |
| 412 | }); |
| 413 | } |
| 414 | } |
| 415 | expect(requestCount).toBe(0); |
| 416 | }).pipe( |
| 417 | Effect.provide( |
| 418 | layerConnectorTest(execute, { |
| 419 | allocations: makeAllocations({ |
| 420 | userId: "user_123", |
| 421 | environmentId: "env-connector-test", |
| 422 | hostname: "env.example.test", |
| 423 | tunnelId: "tunnel-id", |
| 424 | tunnelName: "tunnel-name", |
| 425 | dnsRecordId: "dns-record-id", |
| 426 | readyAt: null, |
| 427 | origin: null, |
| 428 | updatedAt: "2026-05-25T00:00:00.000Z", |
| 429 | generation: 1, |
| 430 | tunnelReleasedAt: null, |
| 431 | }), |
| 432 | }), |
| 433 | ), |
| 434 | ); |
| 435 | }); |
| 436 | |
| 437 | it.effect("rejects signed health responses with stale checkedAt timestamps", () => { |
| 438 | const execute = (request: HttpClientRequest.HttpClientRequest) => |
| 439 | Effect.sync(() => { |
| 440 | const healthRequest = decodeHealthRequestBody(requestBodyText(request)); |
| 441 | return HttpClientResponse.fromWeb( |
| 442 | request, |
| 443 | Response.json( |
| 444 | signHealthResponse( |
| 445 | healthRequest, |
| 446 | environmentKeyPair.privateKey, |
| 447 | {}, |
| 448 | { |
| 449 | checkedAt: "2026-05-24T00:00:00.000Z", |
| 450 | }, |
| 451 | ), |
| 452 | { status: 200 }, |
| 453 | ), |
| 454 | ); |
| 455 | }); |
| 456 | |
| 457 | return Effect.gen(function* () { |
| 458 | const connector = yield* EnvironmentConnector.EnvironmentConnector; |
| 459 | const result = yield* Effect.exit( |
| 460 | connector.status({ |
| 461 | userId: "user_123", |
| 462 | environmentId: "env-connector-test", |
| 463 | }), |
| 464 | ); |
| 465 | |
| 466 | expect(result._tag).toBe("Failure"); |
| 467 | if (result._tag === "Failure") { |
| 468 | expect(result.cause.toString()).toContain("EnvironmentMintResponseInvalid"); |
| 469 | } |
| 470 | }).pipe(Effect.provide(layerConnectorTest(execute))); |
| 471 | }); |
| 472 | |
| 473 | it.effect("reports a released tunnel as the reason an environment is offline", () => { |
| 474 | const execute = (request: HttpClientRequest.HttpClientRequest) => |
| 475 | Effect.succeed( |
| 476 | HttpClientResponse.fromWeb(request, new Response("tunnel not found", { status: 530 })), |
| 477 | ); |
| 478 | const allocations = makeAllocations({ |
| 479 | userId: "user_123", |
| 480 | environmentId: "env-connector-test", |
| 481 | hostname: "env.example.test", |
| 482 | tunnelId: "tunnel-id", |
| 483 | tunnelName: "tunnel-name", |
| 484 | dnsRecordId: "dns-record-id", |
| 485 | readyAt: "2026-05-25T00:00:00.000Z", |
| 486 | origin: { localHttpHost: "127.0.0.1", localHttpPort: 3773 }, |
| 487 | updatedAt: "2026-05-25T00:00:00.000Z", |
| 488 | generation: 2, |
| 489 | tunnelReleasedAt: "2026-05-26T00:00:00.000Z", |
| 490 | }); |
| 491 | |
| 492 | return Effect.gen(function* () { |
| 493 | const connector = yield* EnvironmentConnector.EnvironmentConnector; |
| 494 | const result = yield* connector.status({ |
| 495 | userId: "user_123", |
| 496 | environmentId: "env-connector-test", |
| 497 | }); |
| 498 | |
| 499 | expect(result).toMatchObject({ status: "offline", offlineReason: "tunnel_released" }); |
| 500 | }).pipe(Effect.provide(layerConnectorTest(execute, { allocations }))); |
| 501 | }); |
| 502 | |
| 503 | it.effect("rejects health responses with a mismatched top-level environment id", () => { |
| 504 | const execute = (request: HttpClientRequest.HttpClientRequest) => |
| 505 | Effect.sync(() => { |
| 506 | const healthRequest = decodeHealthRequestBody(requestBodyText(request)); |
| 507 | return HttpClientResponse.fromWeb( |
| 508 | request, |
| 509 | Response.json( |
| 510 | signHealthResponse(healthRequest, environmentKeyPair.privateKey, { |
| 511 | environmentId: "other-env" as RelayEnvironmentHealthResponse["environmentId"], |
| 512 | }), |
| 513 | { status: 200 }, |
| 514 | ), |
| 515 | ); |
| 516 | }); |
| 517 | |
| 518 | return Effect.gen(function* () { |
| 519 | const connector = yield* EnvironmentConnector.EnvironmentConnector; |
| 520 | const result = yield* Effect.exit( |
| 521 | connector.status({ |
| 522 | userId: "user_123", |
| 523 | environmentId: "env-connector-test", |
| 524 | }), |
| 525 | ); |
| 526 | |
| 527 | expect(result._tag).toBe("Failure"); |
| 528 | if (result._tag === "Failure") { |
| 529 | expect(result.cause.toString()).toContain("EnvironmentMintResponseInvalid"); |
| 530 | } |
| 531 | }).pipe(Effect.provide(layerConnectorTest(execute))); |
| 532 | }); |
| 533 | |
| 534 | it.effect("rejects health responses with an unsigned top-level descriptor mutation", () => { |
| 535 | const execute = (request: HttpClientRequest.HttpClientRequest) => |
| 536 | Effect.sync(() => { |
| 537 | const healthRequest = decodeHealthRequestBody(requestBodyText(request)); |
| 538 | const response = signHealthResponse(healthRequest); |
| 539 | return HttpClientResponse.fromWeb( |
| 540 | request, |
| 541 | Response.json( |
| 542 | { |
| 543 | ...response, |
| 544 | descriptor: { |
| 545 | ...response.descriptor, |
| 546 | label: "Tampered Environment Label", |
| 547 | }, |
| 548 | } satisfies RelayEnvironmentHealthResponse, |
| 549 | { status: 200 }, |
| 550 | ), |
| 551 | ); |
| 552 | }); |
| 553 | |
| 554 | return Effect.gen(function* () { |
| 555 | const connector = yield* EnvironmentConnector.EnvironmentConnector; |
| 556 | const result = yield* Effect.exit( |
| 557 | connector.status({ |
| 558 | userId: "user_123", |
| 559 | environmentId: "env-connector-test", |
| 560 | }), |
| 561 | ); |
| 562 | |
| 563 | expect(result._tag).toBe("Failure"); |
| 564 | if (result._tag === "Failure") { |
| 565 | expect(result.cause.toString()).toContain("EnvironmentMintResponseInvalid"); |
| 566 | } |
| 567 | }).pipe(Effect.provide(layerConnectorTest(execute))); |
| 568 | }); |
| 569 | |
| 570 | it.effect("rejects health responses when the linked environment public key is malformed", () => { |
| 571 | const execute = (request: HttpClientRequest.HttpClientRequest) => |
| 572 | Effect.sync(() => { |
| 573 | const healthRequest = decodeHealthRequestBody(requestBodyText(request)); |
| 574 | return HttpClientResponse.fromWeb( |
| 575 | request, |
| 576 | Response.json(signHealthResponse(healthRequest), { status: 200 }), |
| 577 | ); |
| 578 | }); |
| 579 | |
| 580 | return Effect.gen(function* () { |
| 581 | const connector = yield* EnvironmentConnector.EnvironmentConnector; |
| 582 | const result = yield* Effect.exit( |
| 583 | connector.status({ |
| 584 | userId: "user_123", |
| 585 | environmentId: "env-connector-test", |
| 586 | }), |
| 587 | ); |
| 588 | |
| 589 | expect(result._tag).toBe("Failure"); |
| 590 | if (result._tag === "Failure") { |
| 591 | expect(result.cause.toString()).toContain("EnvironmentMintResponseInvalid"); |
| 592 | } |
| 593 | }).pipe( |
| 594 | Effect.provide( |
| 595 | layerConnectorTest(execute, { |
| 596 | links: makeLinks({ |
| 597 | environmentPublicKey: "not a pem public key", |
| 598 | }), |
| 599 | }), |
| 600 | ), |
| 601 | ); |
| 602 | }); |
| 603 | |
| 604 | it.effect("mints a one-time environment credential through the linked endpoint", () => { |
| 605 | const seenUrls: Array<string> = []; |
| 606 | const seenProofs: Array<RelayCloudMintCredentialProofPayload> = []; |
| 607 | const execute = (request: HttpClientRequest.HttpClientRequest) => |
| 608 | Effect.sync(() => { |
| 609 | const mintRequest = decodeMintRequestBody(requestBodyText(request)); |
| 610 | seenUrls.push(request.url); |
| 611 | seenProofs.push(decodeRequestProof(mintRequest.proof)); |
| 612 | return HttpClientResponse.fromWeb( |
| 613 | request, |
| 614 | Response.json(signMintResponse(mintRequest), { status: 200 }), |
| 615 | ); |
| 616 | }); |
| 617 | |
| 618 | return Effect.gen(function* () { |
| 619 | const connector = yield* EnvironmentConnector.EnvironmentConnector; |
| 620 | const result = yield* connector.connect({ |
| 621 | userId: "user_123", |
| 622 | environmentId: "env-connector-test", |
| 623 | clientProofKeyThumbprint: "client-proof-key-thumbprint", |
| 624 | deviceId: "device-123", |
| 625 | }); |
| 626 | |
| 627 | expect(seenUrls).toEqual(["https://env.example.test/api/t3-connect/mint-credential"]); |
| 628 | expect(seenProofs[0]).toMatchObject({ |
| 629 | iss: "https://relay.example.test", |
| 630 | aud: "t3-env:env-connector-test", |
| 631 | sub: "user_123", |
| 632 | environmentId: "env-connector-test", |
| 633 | clientProofKeyThumbprint: "client-proof-key-thumbprint", |
| 634 | cnf: { jkt: "client-proof-key-thumbprint" }, |
| 635 | deviceId: "device-123", |
| 636 | scope: ["environment:connect"], |
| 637 | }); |
| 638 | expect(result).toMatchObject({ |
| 639 | environmentId: "env-connector-test", |
| 640 | credential: "pairing_credential", |
| 641 | endpoint: { |
| 642 | httpBaseUrl: "https://env.example.test/", |
| 643 | wsBaseUrl: "wss://env.example.test/ws", |
| 644 | }, |
| 645 | }); |
| 646 | }).pipe(Effect.provide(layerConnectorTest(execute))); |
| 647 | }); |
| 648 | |
| 649 | it.effect("only accepts mint responses signed by the user's linked environment key", () => { |
| 650 | const execute = (request: HttpClientRequest.HttpClientRequest) => |
| 651 | Effect.sync(() => { |
| 652 | const mintRequest = decodeMintRequestBody(requestBodyText(request)); |
| 653 | return HttpClientResponse.fromWeb( |
| 654 | request, |
| 655 | Response.json(signMintResponse(mintRequest, {}, otherEnvironmentKeyPair.privateKey), { |
| 656 | status: 200, |
| 657 | }), |
| 658 | ); |
| 659 | }); |
| 660 | |
| 661 | return Effect.gen(function* () { |
| 662 | const connector = yield* EnvironmentConnector.EnvironmentConnector; |
| 663 | const result = yield* Effect.exit( |
| 664 | connector.connect({ |
| 665 | userId: "user_123", |
| 666 | environmentId: "env-connector-test", |
| 667 | clientProofKeyThumbprint: "client-proof-key-thumbprint", |
| 668 | }), |
| 669 | ); |
| 670 | |
| 671 | expect(result._tag).toBe("Failure"); |
| 672 | if (result._tag === "Failure") { |
| 673 | expect(result.cause.toString()).toContain("EnvironmentMintResponseInvalid"); |
| 674 | } |
| 675 | }).pipe(Effect.provide(layerConnectorTest(execute))); |
| 676 | }); |
| 677 | |
| 678 | it.effect("rejects environment mint responses with an overlong credential window", () => { |
| 679 | const execute = (request: HttpClientRequest.HttpClientRequest) => |
| 680 | Effect.sync(() => { |
| 681 | const mintRequest = decodeMintRequestBody(requestBodyText(request)); |
| 682 | return HttpClientResponse.fromWeb( |
| 683 | request, |
| 684 | Response.json( |
| 685 | { ...signMintResponse(mintRequest), expiresAt: "2999-01-01T00:00:00.000Z" }, |
| 686 | { status: 200 }, |
| 687 | ), |
| 688 | ); |
| 689 | }); |
| 690 | |
| 691 | return Effect.gen(function* () { |
| 692 | const connector = yield* EnvironmentConnector.EnvironmentConnector; |
| 693 | const result = yield* Effect.exit( |
| 694 | connector.connect({ |
| 695 | userId: "user_123", |
| 696 | environmentId: "env-connector-test", |
| 697 | clientProofKeyThumbprint: "client-proof-key-thumbprint", |
| 698 | }), |
| 699 | ); |
| 700 | |
| 701 | expect(result._tag).toBe("Failure"); |
| 702 | if (result._tag === "Failure") { |
| 703 | expect(result.cause.toString()).toContain("EnvironmentMintResponseInvalid"); |
| 704 | } |
| 705 | }).pipe(Effect.provide(layerConnectorTest(execute))); |
| 706 | }); |
| 707 | }); |