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 { EnvironmentId } from "@t3tools/contracts"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import * as Fiber from "effect/Fiber"; |
| 5 | import * as Layer from "effect/Layer"; |
| 6 | import * as Option from "effect/Option"; |
| 7 | import { HttpClient, HttpClientResponse } from "effect/http"; |
| 8 | import { Atom, AtomRegistry } from "effect/reactivity"; |
| 9 | |
| 10 | import { BearerConnectionTarget, type PreparedConnection } from "../connection/model.ts"; |
| 11 | import { createPreviewTunnelUrlReader } from "./previewTunnel.ts"; |
| 12 | |
| 13 | const ENVIRONMENT_ID = EnvironmentId.make("environment-1"); |
| 14 | const PREPARED: PreparedConnection = { |
| 15 | environmentId: ENVIRONMENT_ID, |
| 16 | label: "Remote environment", |
| 17 | httpBaseUrl: "https://remote.example.test", |
| 18 | socketUrl: "wss://remote.example.test/ws", |
| 19 | httpAuthorization: { _tag: "Bearer", token: "token" }, |
| 20 | target: new BearerConnectionTarget({ |
| 21 | environmentId: ENVIRONMENT_ID, |
| 22 | label: "Remote environment", |
| 23 | connectionId: "connection-1", |
| 24 | }), |
| 25 | }; |
| 26 | |
| 27 | describe("preview tunnel url", () => { |
| 28 | it.live("waits for a reconnecting environment instead of refusing the page", () => |
| 29 | Effect.gen(function* () { |
| 30 | const prepared = Atom.make(Option.none<PreparedConnection>()); |
| 31 | const client = HttpClient.make((request) => |
| 32 | Effect.succeed( |
| 33 | HttpClientResponse.fromWeb( |
| 34 | request, |
| 35 | Response.json({ ticket: "media-ticket", expiresAt: "2999-01-01T00:00:00.000Z" }), |
| 36 | ), |
| 37 | ), |
| 38 | ); |
| 39 | const registry = AtomRegistry.make(); |
| 40 | yield* Effect.addFinalizer(() => Effect.sync(() => registry.dispose())); |
| 41 | const read = createPreviewTunnelUrlReader({ |
| 42 | runtime: Atom.runtime(Layer.succeed(HttpClient.HttpClient, client)), |
| 43 | registry, |
| 44 | preparedConnectionAtom: () => prepared, |
| 45 | }); |
| 46 | |
| 47 | const pending = yield* Effect.forkChild(Effect.promise(() => read(ENVIRONMENT_ID))); |
| 48 | yield* Effect.yieldNow; |
| 49 | registry.set(prepared, Option.some(PREPARED)); |
| 50 | |
| 51 | expect(yield* Fiber.join(pending)).toBe( |
| 52 | "wss://remote.example.test/api/preview/tunnel?wsTicket=media-ticket", |
| 53 | ); |
| 54 | }), |
| 55 | ); |
| 56 | }); |