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 { BearerConnectionProfile, type ConnectionCatalogEntry } from "./catalog.ts";
6import {
7 BearerConnectionTarget,
8 ConnectionBlockedError,
9 ConnectionTransientError,
10 type SupervisorConnectionState,
11} from "./model.ts";
12import {
13 connectionCatalogDisplayUrl,
14 environmentMcpUrl,
15 connectionStatusText,
16 connectionStatusTitle,
17 presentEnvironmentConnection,
18 presentConnectionState,
19} from "./presentation.ts";
20
21const TARGET = new BearerConnectionTarget({
22 environmentId: EnvironmentId.make("environment-1"),
23 label: "Remote environment",
24 connectionId: "connection-1",
25});
26
27const ENTRY: ConnectionCatalogEntry = {
28 target: TARGET,
29 profile: Option.some(
30 new BearerConnectionProfile({
31 connectionId: TARGET.connectionId,
32 environmentId: TARGET.environmentId,
33 label: TARGET.label,
34 httpBaseUrl: "https://environment.example.test",
35 wsBaseUrl: "wss://environment.example.test",
36 }),
37 ),
38 enabled: true,
39};
40
41function supervisorState(overrides: Partial<SupervisorConnectionState>): SupervisorConnectionState {
42 return {
43 desired: true,
44 network: "online",
45 phase: "connecting",
46 stage: "preparing",
47 attempt: 1,
48 generation: 0,
49 lastFailure: null,
50 retryAt: null,
51 ...overrides,
52 };
53}
54
55describe("connection presentation", () => {
56 it("labels a blocked protocol as unsupported", () => {
57 const connection = presentConnectionState(
58 supervisorState({
59 phase: "blocked",
60 lastFailure: new ConnectionBlockedError({
61 reason: "unsupported",
62 detail: "Update your app.",
63 }),
64 }),
65 );
66 expect(connection.phase).toBe("unsupported");
67 expect(connection.error).toBe("Update your app.");
68 expect(connectionStatusText(connection)).toBe("Client not supported");
69 });
70
71 it("preserves profile display information without exposing credentials", () => {
72 expect(connectionCatalogDisplayUrl(ENTRY)).toBe("https://environment.example.test");
73 });
74
75 it("offers an MCP address only where an MCP client can sign in", () => {
76 expect(environmentMcpUrl({ entry: ENTRY })).toBe("https://environment.example.test/mcp");
77 const withBase = (httpBaseUrl: string): ConnectionCatalogEntry => ({
78 ...ENTRY,
79 profile: Option.some(
80 new BearerConnectionProfile({
81 connectionId: TARGET.connectionId,
82 environmentId: TARGET.environmentId,
83 label: TARGET.label,
84 httpBaseUrl,
85 wsBaseUrl: httpBaseUrl.replace(/^http/, "ws"),
86 }),
87 ),
88 });
89 expect(environmentMcpUrl({ entry: withBase("http://127.0.0.1:3773/") })).toBe(
90 "http://127.0.0.1:3773/mcp",
91 );
92 // A plain-http LAN or tailnet address is refused by MCP clients' token checks.
93 expect(environmentMcpUrl({ entry: withBase("http://100.81.102.68:3773") })).toBeNull();
94 });
95
96 it("distinguishes initial connection, reconnect, and retry errors", () => {
97 expect(presentConnectionState(supervisorState({ phase: "connecting", attempt: 1 }))).toEqual({
98 phase: "connecting",
99 error: null,
100 traceId: null,
101 });
102 expect(
103 presentConnectionState(
104 supervisorState({
105 phase: "connecting",
106 attempt: 2,
107 lastFailure: new ConnectionTransientError({
108 reason: "transport",
109 detail: "Socket closed.",
110 traceId: "trace-previous",
111 }),
112 }),
113 ),
114 ).toEqual({
115 phase: "reconnecting",
116 error: "Socket closed.",
117 traceId: "trace-previous",
118 });
119 expect(
120 presentConnectionState(
121 supervisorState({
122 phase: "backoff",
123 attempt: 2,
124 retryAt: 1,
125 lastFailure: new ConnectionTransientError({
126 reason: "transport",
127 detail: "Disconnected.",
128 traceId: "trace-1",
129 }),
130 }),
131 ),
132 ).toEqual({
133 phase: "reconnecting",
134 error: "Disconnected.",
135 traceId: "trace-1",
136 });
137 });
138
139 it("preserves the latest failure while the next attempt is active", () => {
140 expect(
141 presentEnvironmentConnection(
142 supervisorState({
143 phase: "connecting",
144 stage: "opening",
145 attempt: 2,
146 lastFailure: new ConnectionTransientError({
147 reason: "transport",
148 detail: "Relay connection timed out.",
149 traceId: "trace-retry",
150 }),
151 }),
152 ),
153 ).toEqual({
154 phase: "reconnecting",
155 error: "Relay connection timed out.",
156 traceId: "trace-retry",
157 });
158 });
159
160 it("combines reconnect progress with the latest failure", () => {
161 const connection = {
162 phase: "reconnecting",
163 error: "Relay request timed out.",
164 traceId: "trace-retry",
165 } as const;
166 expect(connectionStatusText(connection)).toBe(
167 "Failed to connect. Reconnecting... Reason: Relay request timed out.",
168 );
169 expect(connectionStatusTitle(connection)).toBe("Failed to connect. Reconnecting...");
170 });
171
172 it("presents the supervisor's offline state without consulting shell state", () => {
173 expect(
174 presentEnvironmentConnection(
175 supervisorState({
176 network: "offline",
177 phase: "offline",
178 stage: null,
179 }),
180 ),
181 ).toEqual({
182 phase: "offline",
183 error: null,
184 traceId: null,
185 });
186 });
187
188 it("presents a connected supervisor snapshot as connected", () => {
189 expect(
190 presentEnvironmentConnection(
191 supervisorState({
192 phase: "connected",
193 stage: null,
194 generation: 1,
195 }),
196 ),
197 ).toEqual({
198 phase: "connected",
199 error: null,
200 traceId: null,
201 });
202 });
203
204 it("preserves an explicitly available environment while offline", () => {
205 expect(
206 presentEnvironmentConnection(
207 supervisorState({
208 desired: false,
209 network: "offline",
210 phase: "available",
211 stage: null,
212 attempt: 0,
213 }),
214 ),
215 ).toEqual({
216 phase: "available",
217 error: null,
218 traceId: null,
219 });
220 });
221});