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

presentation.test.ts

261 lines7.1 KB

1import { EnvironmentId } from "@t3tools/contracts";
2import { describe, expect, it } from "@effect/vitest";
3import * as Option from "effect/Option";
4​
5import {
6 BearerConnectionProfile,
7 type ConnectionCatalogEntry,
8 type ConnectionRoute,
9} from "./catalog.ts";
10import {
11 BearerConnectionTarget,
12 ConnectionBlockedError,
13 ConnectionTransientError,
14 RelayConnectionTarget,
15 type SupervisorConnectionState,
16} from "./model.ts";
17import {
18 connectionCatalogDisplayUrl,
19 environmentMcpUrl,
20 connectionStatusText,
21 connectionStatusTitle,
22 presentEnvironmentConnection,
23 presentConnectionState,
24} from "./presentation.ts";
25​
26const TARGET = new BearerConnectionTarget({
27 environmentId: EnvironmentId.make("environment-1"),
28 label: "Remote environment",
29 connectionId: "connection-1",
30});
31​
32const ENTRY: ConnectionCatalogEntry = {
33 target: TARGET,
34 profile: Option.some(
35 new BearerConnectionProfile({
36 connectionId: TARGET.connectionId,
37 environmentId: TARGET.environmentId,
38 label: TARGET.label,
39 httpBaseUrl: "https://environment.example.test",
40 wsBaseUrl: "wss://environment.example.test",
41 }),
42 ),
43 enabled: true,
44};
45​
46function supervisorState(overrides: Partial<SupervisorConnectionState>): SupervisorConnectionState {
47 return {
48 desired: true,
49 network: "online",
50 phase: "connecting",
51 stage: "preparing",
52 attempt: 1,
53 generation: 0,
54 lastFailure: null,
55 retryAt: null,
56 ...overrides,
57 };
59​
60describe("connection presentation", () => {
61 it("labels a blocked protocol as unsupported", () => {
62 const connection = presentConnectionState(
63 supervisorState({
64 phase: "blocked",
65 lastFailure: new ConnectionBlockedError({
66 reason: "unsupported",
67 detail: "Update your app.",
68 }),
69 }),
70 );
71 expect(connection.phase).toBe("unsupported");
72 expect(connection.error).toBe("Update your app.");
73 expect(connectionStatusText(connection)).toBe("Client not supported");
74 });
75​
76 it("preserves profile display information without exposing credentials", () => {
77 expect(connectionCatalogDisplayUrl(ENTRY)).toBe("https://environment.example.test");
78 });
79​
80 it("copies the MCP address of the route this device is connected over", () => {
81 const route = (connectionId: string, httpBaseUrl: string): ConnectionRoute => {
82 const target = new BearerConnectionTarget({ ...TARGET, connectionId });
83 return {
84 target,
85 profile: Option.some(
86 new BearerConnectionProfile({
87 connectionId,
88 environmentId: TARGET.environmentId,
89 label: TARGET.label,
90 httpBaseUrl,
91 wsBaseUrl: httpBaseUrl.replace(/^http/, "ws"),
92 }),
93 ),
94 };
95 };
96 const lan = route("lan", "http://192.168.4.53:3773/");
97 const tailnet = route("tailnet", "http://100.115.1.44:3773/");
98 const serve = route("serve", "https://machine.tailnet.ts.net/");
99 const entry: ConnectionCatalogEntry = {
100 ...ENTRY,
101 target: lan.target,
102 profile: lan.profile,
103 alternateRoutes: [tailnet, serve],
104 };
105​
106 expect(environmentMcpUrl({ entry, connectedTarget: tailnet.target })).toBe(
107 "http://100.115.1.44:3773/mcp",
108 );
109 expect(environmentMcpUrl({ entry })).toBe("http://192.168.4.53:3773/mcp");
110 });
111​
112 it("passes over routes without an address of their own", () => {
113 const relay = new RelayConnectionTarget({
114 environmentId: TARGET.environmentId,
115 label: TARGET.label,
116 });
117 const entry: ConnectionCatalogEntry = {
118 ...ENTRY,
119 target: relay,
120 profile: Option.none(),
121 alternateRoutes: [{ target: ENTRY.target, profile: ENTRY.profile }],
122 };
123​
124 expect(environmentMcpUrl({ entry, connectedTarget: relay })).toBe(
125 "https://environment.example.test/mcp",
126 );
127 expect(
128 environmentMcpUrl({
129 entry,
130 connectedTarget: relay,
131 relayHttpBaseUrl: "https://tunnel.example.test",
132 }),
133 ).toBe("https://tunnel.example.test/mcp");
134 });
135​
136 it("distinguishes initial connection, reconnect, and retry errors", () => {
137 expect(presentConnectionState(supervisorState({ phase: "connecting", attempt: 1 }))).toEqual({
138 phase: "connecting",
139 error: null,
140 traceId: null,
141 });
142 expect(
143 presentConnectionState(
144 supervisorState({
145 phase: "connecting",
146 attempt: 2,
147 lastFailure: new ConnectionTransientError({
148 reason: "transport",
149 detail: "Socket closed.",
150 traceId: "trace-previous",
151 }),
152 }),
153 ),
154 ).toEqual({
155 phase: "reconnecting",
156 error: "Socket closed.",
157 traceId: "trace-previous",
158 });
159 expect(
160 presentConnectionState(
161 supervisorState({
162 phase: "backoff",
163 attempt: 2,
164 retryAt: 1,
165 lastFailure: new ConnectionTransientError({
166 reason: "transport",
167 detail: "Disconnected.",
168 traceId: "trace-1",
169 }),
170 }),
171 ),
172 ).toEqual({
173 phase: "reconnecting",
174 error: "Disconnected.",
175 traceId: "trace-1",
176 });
177 });
178​
179 it("preserves the latest failure while the next attempt is active", () => {
180 expect(
181 presentEnvironmentConnection(
182 supervisorState({
183 phase: "connecting",
184 stage: "opening",
185 attempt: 2,
186 lastFailure: new ConnectionTransientError({
187 reason: "transport",
188 detail: "Relay connection timed out.",
189 traceId: "trace-retry",
190 }),
191 }),
192 ),
193 ).toEqual({
194 phase: "reconnecting",
195 error: "Relay connection timed out.",
196 traceId: "trace-retry",
197 });
198 });
199​
200 it("combines reconnect progress with the latest failure", () => {
201 const connection = {
202 phase: "reconnecting",
203 error: "Relay request timed out.",
204 traceId: "trace-retry",
205 } as const;
206 expect(connectionStatusText(connection)).toBe(
207 "Failed to connect. Reconnecting... Reason: Relay request timed out.",
208 );
209 expect(connectionStatusTitle(connection)).toBe("Failed to connect. Reconnecting...");
210 });
211​
212 it("presents the supervisor's offline state without consulting shell state", () => {
213 expect(
214 presentEnvironmentConnection(
215 supervisorState({
216 network: "offline",
217 phase: "offline",
218 stage: null,
219 }),
220 ),
221 ).toEqual({
222 phase: "offline",
223 error: null,
224 traceId: null,
225 });
226 });
227​
228 it("presents a connected supervisor snapshot as connected", () => {
229 expect(
230 presentEnvironmentConnection(
231 supervisorState({
232 phase: "connected",
233 stage: null,
234 generation: 1,
235 }),
236 ),
237 ).toEqual({
238 phase: "connected",
239 error: null,
240 traceId: null,
241 });
242 });
243​
244 it("preserves an explicitly available environment while offline", () => {
245 expect(
246 presentEnvironmentConnection(
247 supervisorState({
248 desired: false,
249 network: "offline",
250 phase: "available",
251 stage: null,
252 attempt: 0,
253 }),
254 ),
255 ).toEqual({
256 phase: "available",
257 error: null,
258 traceId: null,
259 });
260 });
261});