Open History to see the last commit
1import { EnvironmentId } from "@t3tools/contracts";
2import { RelayEnvironmentStatusScope } from "@t3tools/contracts/relay";
3import { describe, expect, it } from "@effect/vitest";
4import * as Duration from "effect/Duration";
5import * as Effect from "effect/Effect";
6import * as Fiber from "effect/Fiber";
7import * as Layer from "effect/Layer";
8import * as TestClock from "effect/testing/TestClock";
9
10import {
11 MANAGED_RELAY_REQUEST_TIMEOUT_MS,
12 ManagedRelayClient,
13 ManagedRelayDpopSigner,
14 managedRelayClientLayer,
15 type ManagedRelayDpopProofInput,
16} from "./managedRelay.ts";
17import { remoteHttpClientLayer } from "./remote.ts";
18
19function managedRelayTestLayer(
20 fetchFn: typeof globalThis.fetch,
21 relayUrl = "https://relay.example.test",
22) {
23 const httpClientLayer = remoteHttpClientLayer(fetchFn);
24 const signerLayer = Layer.succeed(
25 ManagedRelayDpopSigner,
26 ManagedRelayDpopSigner.of({
27 thumbprint: Effect.succeed("client-thumbprint"),
28 createProof: (input: ManagedRelayDpopProofInput) => Effect.succeed(`proof:${input.url}`),
29 }),
30 );
31 return managedRelayClientLayer({
32 relayUrl,
33 clientId: "t3-mobile",
34 }).pipe(Layer.provide(signerLayer), Layer.provide(httpClientLayer));
35}
36
37describe("ManagedRelayClient", () => {
38 it.effect("rejects unsafe relay URLs before sending credentials", () => {
39 let requestCount = 0;
40 const fetchFn = (() => {
41 requestCount += 1;
42 return Promise.resolve(Response.json({}));
43 }) satisfies typeof globalThis.fetch;
44
45 return Effect.gen(function* () {
46 const relayClient = yield* ManagedRelayClient;
47 const error = yield* relayClient
48 .listEnvironments({ clerkToken: "clerk-token" })
49 .pipe(Effect.flip);
50
51 expect(error).toMatchObject({
52 _tag: "ManagedRelayClientError",
53 message: "Relay URL must be a secure absolute HTTPS origin.",
54 });
55 expect(requestCount).toBe(0);
56 }).pipe(Effect.provide(managedRelayTestLayer(fetchFn, "http://relay.example.test")));
57 });
58
59 it.effect("reuses usable DPoP tokens and refreshes cleared or expiring cache entries", () => {
60 let tokenExchangeCount = 0;
61 const fetchFn = ((input) => {
62 const url = String(input);
63 if (url.endsWith("/v1/client/dpop-token")) {
64 tokenExchangeCount += 1;
65 return Promise.resolve(
66 Response.json({
67 access_token: `relay-token-${tokenExchangeCount}`,
68 issued_token_type: "urn:ietf:params:oauth:token-type:access_token",
69 token_type: "DPoP",
70 expires_in: 10,
71 scope: RelayEnvironmentStatusScope,
72 }),
73 );
74 }
75 return Promise.resolve(
76 Response.json({
77 environmentId: "env-1",
78 endpoint: {
79 httpBaseUrl: "https://desktop.example.test/",
80 wsBaseUrl: "wss://desktop.example.test/ws",
81 providerKind: "cloudflare_tunnel",
82 },
83 status: "online",
84 checkedAt: "2026-05-25T00:01:00.000Z",
85 descriptor: {
86 environmentId: "env-1",
87 label: "Desktop",
88 platform: { os: "darwin", arch: "arm64" },
89 serverVersion: "0.0.0-test",
90 capabilities: { repositoryIdentity: true },
91 },
92 }),
93 );
94 }) satisfies typeof globalThis.fetch;
95
96 return Effect.gen(function* () {
97 const relayClient = yield* ManagedRelayClient;
98 const statusInput = {
99 clerkToken: "clerk-token",
100 scopes: [RelayEnvironmentStatusScope],
101 environmentId: EnvironmentId.make("env-1"),
102 } as const;
103
104 yield* relayClient.getEnvironmentStatus(statusInput);
105 yield* relayClient.getEnvironmentStatus(statusInput);
106 expect(tokenExchangeCount).toBe(1);
107
108 yield* TestClock.adjust(Duration.seconds(6));
109 yield* relayClient.getEnvironmentStatus(statusInput);
110 expect(tokenExchangeCount).toBe(2);
111
112 yield* relayClient.resetTokenCache;
113 yield* relayClient.getEnvironmentStatus(statusInput);
114 expect(tokenExchangeCount).toBe(3);
115 }).pipe(Effect.provide(managedRelayTestLayer(fetchFn)));
116 });
117
118 it.effect("times out stalled relay environment listing requests", () => {
119 const fetchFn = (() =>
120 new Promise<Response>(() => undefined)) satisfies typeof globalThis.fetch;
121
122 return Effect.gen(function* () {
123 const relayClient = yield* ManagedRelayClient;
124 const errorFiber = yield* relayClient
125 .listEnvironments({ clerkToken: "clerk-token" })
126 .pipe(Effect.flip, Effect.forkScoped);
127
128 yield* Effect.yieldNow;
129 yield* TestClock.adjust(Duration.millis(MANAGED_RELAY_REQUEST_TIMEOUT_MS));
130 const error = yield* Fiber.join(errorFiber);
131
132 expect(error).toMatchObject({
133 _tag: "ManagedRelayClientError",
134 message: "Relay environment listing timed out.",
135 });
136 }).pipe(Effect.provide(Layer.merge(TestClock.layer(), managedRelayTestLayer(fetchFn))));
137 });
138
139 it.effect("lists account devices through the Clerk bearer client endpoint", () => {
140 const fetchFn = ((input, init) => {
141 expect(String(input)).toBe("https://relay.example.test/v1/client/devices");
142 expect(init?.headers).toMatchObject({
143 authorization: "Bearer clerk-token",
144 });
145 return Promise.resolve(
146 Response.json({
147 devices: [
148 {
149 deviceId: "device-1",
150 label: "Julius's iPhone",
151 platform: "ios",
152 iosMajorVersion: 18,
153 appVersion: "1.0.0",
154 notifications: {
155 enabled: false,
156 notifyOnApproval: true,
157 notifyOnInput: true,
158 notifyOnCompletion: true,
159 notifyOnFailure: true,
160 },
161 liveActivities: {
162 enabled: true,
163 },
164 updatedAt: "2026-06-01T00:00:00.000Z",
165 },
166 ],
167 }),
168 );
169 }) satisfies typeof globalThis.fetch;
170
171 return Effect.gen(function* () {
172 const relayClient = yield* ManagedRelayClient;
173 const devices = yield* relayClient.listDevices({ clerkToken: "clerk-token" });
174 expect(devices).toMatchObject([
175 {
176 deviceId: "device-1",
177 label: "Julius's iPhone",
178 notifications: {
179 enabled: false,
180 },
181 },
182 ]);
183 }).pipe(Effect.provide(managedRelayTestLayer(fetchFn)));
184 });
185});