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