Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

previewTunnel.test.ts

56 lines2.1 KB

1import { describe, expect, it } from "@effect/vitest";
2import { EnvironmentId } from "@t3tools/contracts";
3import * as Effect from "effect/Effect";
4import * as Fiber from "effect/Fiber";
5import * as Layer from "effect/Layer";
6import * as Option from "effect/Option";
7import { HttpClient, HttpClientResponse } from "effect/http";
8import { Atom, AtomRegistry } from "effect/reactivity";
9​
10import { BearerConnectionTarget, type PreparedConnection } from "../connection/model.ts";
11import { createPreviewTunnelUrlReader } from "./previewTunnel.ts";
12​
13const ENVIRONMENT_ID = EnvironmentId.make("environment-1");
14const 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​
27describe("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});