Open History to see the last commit
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 };
58}
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 // Not connected: the preferred route, plain http or not.
110 expect(environmentMcpUrl({ entry })).toBe("http://192.168.4.53:3773/mcp");
111 });
112
113 it("passes over routes without an address of their own", () => {
114 const relay = new RelayConnectionTarget({
115 environmentId: TARGET.environmentId,
116 label: TARGET.label,
117 });
118 const entry: ConnectionCatalogEntry = {
119 ...ENTRY,
120 target: relay,
121 profile: Option.none(),
122 alternateRoutes: [{ target: ENTRY.target, profile: ENTRY.profile }],
123 };
124
125 // Relay discovery has not reported the tunnel address yet.
126 expect(environmentMcpUrl({ entry, connectedTarget: relay })).toBe(
127 "https://environment.example.test/mcp",
128 );
129 expect(
130 environmentMcpUrl({
131 entry,
132 connectedTarget: relay,
133 relayHttpBaseUrl: "https://tunnel.example.test",
134 }),
135 ).toBe("https://tunnel.example.test/mcp");
136 });
137
138 it("distinguishes initial connection, reconnect, and retry errors", () => {
139 expect(presentConnectionState(supervisorState({ phase: "connecting", attempt: 1 }))).toEqual({
140 phase: "connecting",
141 error: null,
142 traceId: null,
143 });
144 expect(
145 presentConnectionState(
146 supervisorState({
147 phase: "connecting",
148 attempt: 2,
149 lastFailure: new ConnectionTransientError({
150 reason: "transport",
151 detail: "Socket closed.",
152 traceId: "trace-previous",
153 }),
154 }),
155 ),
156 ).toEqual({
157 phase: "reconnecting",
158 error: "Socket closed.",
159 traceId: "trace-previous",
160 });
161 expect(
162 presentConnectionState(
163 supervisorState({
164 phase: "backoff",
165 attempt: 2,
166 retryAt: 1,
167 lastFailure: new ConnectionTransientError({
168 reason: "transport",
169 detail: "Disconnected.",
170 traceId: "trace-1",
171 }),
172 }),
173 ),
174 ).toEqual({
175 phase: "reconnecting",
176 error: "Disconnected.",
177 traceId: "trace-1",
178 });
179 });
180
181 it("preserves the latest failure while the next attempt is active", () => {
182 expect(
183 presentEnvironmentConnection(
184 supervisorState({
185 phase: "connecting",
186 stage: "opening",
187 attempt: 2,
188 lastFailure: new ConnectionTransientError({
189 reason: "transport",
190 detail: "Relay connection timed out.",
191 traceId: "trace-retry",
192 }),
193 }),
194 ),
195 ).toEqual({
196 phase: "reconnecting",
197 error: "Relay connection timed out.",
198 traceId: "trace-retry",
199 });
200 });
201
202 it("combines reconnect progress with the latest failure", () => {
203 const connection = {
204 phase: "reconnecting",
205 error: "Relay request timed out.",
206 traceId: "trace-retry",
207 } as const;
208 expect(connectionStatusText(connection)).toBe(
209 "Failed to connect. Reconnecting... Reason: Relay request timed out.",
210 );
211 expect(connectionStatusTitle(connection)).toBe("Failed to connect. Reconnecting...");
212 });
213
214 it("presents the supervisor's offline state without consulting shell state", () => {
215 expect(
216 presentEnvironmentConnection(
217 supervisorState({
218 network: "offline",
219 phase: "offline",
220 stage: null,
221 }),
222 ),
223 ).toEqual({
224 phase: "offline",
225 error: null,
226 traceId: null,
227 });
228 });
229
230 it("presents a connected supervisor snapshot as connected", () => {
231 expect(
232 presentEnvironmentConnection(
233 supervisorState({
234 phase: "connected",
235 stage: null,
236 generation: 1,
237 }),
238 ),
239 ).toEqual({
240 phase: "connected",
241 error: null,
242 traceId: null,
243 });
244 });
245
246 it("preserves an explicitly available environment while offline", () => {
247 expect(
248 presentEnvironmentConnection(
249 supervisorState({
250 desired: false,
251 network: "offline",
252 phase: "available",
253 stage: null,
254 attempt: 0,
255 }),
256 ),
257 ).toEqual({
258 phase: "available",
259 error: null,
260 traceId: null,
261 });
262 });
263});