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

presentation.test.ts

234 lines6.7 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 ConnectionTransientError,
13 PrimaryConnectionTarget,
14 RelayConnectionTarget,
15 type SupervisorConnectionState,
16} from "./model.ts";
17import {
18 connectionStatusText,
19 connectionStatusTitle,
20 environmentMcpUrl,
21 localEnvironmentMcpUrl,
22 presentConnectionState,
23} from "./presentation.ts";
24​
25const TARGET = new BearerConnectionTarget({
26 environmentId: EnvironmentId.make("environment-1"),
27 label: "Remote environment",
28 connectionId: "connection-1",
29});
30​
31const ENTRY: ConnectionCatalogEntry = {
32 target: TARGET,
33 profile: Option.some(
34 new BearerConnectionProfile({
35 connectionId: TARGET.connectionId,
36 environmentId: TARGET.environmentId,
37 label: TARGET.label,
38 httpBaseUrl: "https://environment.example.test",
39 wsBaseUrl: "wss://environment.example.test",
40 }),
41 ),
42 enabled: true,
43};
44​
45function supervisorState(overrides: Partial<SupervisorConnectionState>): SupervisorConnectionState {
46 return {
47 desired: true,
48 network: "online",
49 phase: "connecting",
50 stage: "preparing",
51 attempt: 1,
52 generation: 0,
53 lastFailure: null,
54 retryAt: null,
55 refusedRouteIds: [],
56 ...overrides,
57 };
59​
60describe("connection presentation", () => {
61 it("copies the T3 Connect MCP address first, then the route this device is connected over", () => {
62 const route = (connectionId: string, httpBaseUrl: string): ConnectionRoute => {
63 const target = new BearerConnectionTarget({ ...TARGET, connectionId });
64 return {
65 target,
66 profile: Option.some(
67 new BearerConnectionProfile({
68 connectionId,
69 environmentId: TARGET.environmentId,
70 label: TARGET.label,
71 httpBaseUrl,
72 wsBaseUrl: httpBaseUrl.replace(/^http/, "ws"),
73 }),
74 ),
75 };
76 };
77 const lan = route("lan", "http://192.168.4.53:3773/");
78 const tailnet = route("tailnet", "http://100.115.1.44:3773/");
79 const serve = route("serve", "https://machine.tailnet.ts.net/");
80 const entry: ConnectionCatalogEntry = {
81 ...ENTRY,
82 target: lan.target,
83 profile: lan.profile,
84 alternateRoutes: [tailnet, serve],
85 };
86​
87 expect(environmentMcpUrl({ entry, connectedTarget: tailnet.target })).toBe(
88 "http://100.115.1.44:3773/mcp",
89 );
90 expect(environmentMcpUrl({ entry })).toBe("http://192.168.4.53:3773/mcp");
91 expect(
92 environmentMcpUrl({
93 entry,
94 connectedTarget: lan.target,
95 relayHttpBaseUrl: "https://tunnel.example.test",
96 }),
97 ).toBe("https://tunnel.example.test/mcp");
98 });
99​
100 // regression: #16909 Copy MCP URL falls back to an alternate route when the relay route has no address
101 it("passes over routes without an address of their own", () => {
102 const relay = new RelayConnectionTarget({
103 environmentId: TARGET.environmentId,
104 label: TARGET.label,
105 });
106 const entry: ConnectionCatalogEntry = {
107 ...ENTRY,
108 target: relay,
109 profile: Option.none(),
110 alternateRoutes: [{ target: ENTRY.target, profile: ENTRY.profile }],
111 };
112​
113 expect(environmentMcpUrl({ entry, connectedTarget: relay })).toBe(
114 "https://environment.example.test/mcp",
115 );
116 expect(
117 environmentMcpUrl({
118 entry,
119 connectedTarget: relay,
120 relayHttpBaseUrl: "https://tunnel.example.test",
121 }),
122 ).toBe("https://tunnel.example.test/mcp");
123 });
124​
125 it("offers loopback and https, but never a plain-http LAN address", () => {
126 const primary = new PrimaryConnectionTarget({
127 environmentId: TARGET.environmentId,
128 label: "This machine",
129 httpBaseUrl: "http://127.0.0.1:3773",
130 wsBaseUrl: "ws://127.0.0.1:3773",
131 });
132 const entry: ConnectionCatalogEntry = {
133 ...ENTRY,
134 target: primary,
135 profile: Option.none(),
136 };
137​
138 expect(localEnvironmentMcpUrl({ entry })).toBe("http://127.0.0.1:3773/mcp");
139​
140 const lan: ConnectionCatalogEntry = {
141 ...entry,
142 target: new PrimaryConnectionTarget({
143 environmentId: TARGET.environmentId,
144 label: "This machine",
145 httpBaseUrl: "http://192.168.4.53:3773",
146 wsBaseUrl: "ws://192.168.4.53:3773",
147 }),
148 };
149 expect(localEnvironmentMcpUrl({ entry: lan })).toBeNull();
150 });
151​
152 it("distinguishes initial connection, reconnect, and retry errors", () => {
153 expect(presentConnectionState(supervisorState({ phase: "connecting", attempt: 1 }))).toEqual({
154 phase: "connecting",
155 error: null,
156 traceId: null,
157 });
158 expect(
159 presentConnectionState(
160 supervisorState({
161 phase: "connecting",
162 attempt: 2,
163 lastFailure: new ConnectionTransientError({
164 reason: "transport",
165 detail: "Socket closed.",
166 traceId: "trace-previous",
167 }),
168 }),
169 ),
170 ).toEqual({
171 phase: "reconnecting",
172 error: "Socket closed.",
173 traceId: "trace-previous",
174 });
175 expect(
176 presentConnectionState(
177 supervisorState({
178 phase: "backoff",
179 attempt: 2,
180 retryAt: 1,
181 lastFailure: new ConnectionTransientError({
182 reason: "transport",
183 detail: "Disconnected.",
184 traceId: "trace-1",
185 }),
186 }),
187 ),
188 ).toEqual({
189 phase: "reconnecting",
190 error: "Disconnected.",
191 traceId: "trace-1",
192 });
193 const awaitingApproval = presentConnectionState(
194 supervisorState({
195 phase: "backoff",
196 attempt: 1,
197 retryAt: 1,
198 lastFailure: new ConnectionTransientError({
199 reason: "approval-pending",
200 detail: "Approve this device on Mac mini. It shows code K7QM-2XFD.",
201 traceId: "trace-2",
202 }),
203 }),
204 );
205 expect(awaitingApproval).toEqual({
206 phase: "reconnecting",
207 error: "Approve this device on Mac mini. It shows code K7QM-2XFD.",
208 traceId: "trace-2",
209 awaitingApproval: true,
210 });
211 expect(connectionStatusText(awaitingApproval)).toBe(
212 "Approve this device on Mac mini. It shows code K7QM-2XFD.",
213 );
214 expect(connectionStatusTitle(awaitingApproval)).toBe("Waiting for approval");
215 });
216​
217 it("preserves an explicitly available environment while offline", () => {
218 expect(
219 presentConnectionState(
220 supervisorState({
221 desired: false,
222 network: "offline",
223 phase: "available",
224 stage: null,
225 attempt: 0,
226 }),
227 ),
228 ).toEqual({
229 phase: "available",
230 error: null,
231 traceId: null,
232 });
233 });
234});