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 SshConnectionProfile,
9} from "./catalog.ts";
10import {
11 BearerConnectionTarget,
12 ConnectionBlockedError,
13 ConnectionTransientError,
14 SshConnectionTarget,
15 type SupervisorConnectionState,
16} from "./model.ts";
17import {
18 connectionCatalogDisplayUrl,
19 connectionStatusText,
20 connectionStatusTitle,
21 presentEnvironmentConnection,
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 ...overrides,
56 };
57}
58
59describe("connection presentation", () => {
60 it("labels a blocked protocol as unsupported", () => {
61 const connection = presentConnectionState(
62 supervisorState({
63 phase: "blocked",
64 lastFailure: new ConnectionBlockedError({
65 reason: "unsupported",
66 detail: "Update your app.",
67 }),
68 }),
69 );
70 expect(connection.phase).toBe("unsupported");
71 expect(connection.error).toBe("Update your app.");
72 expect(connectionStatusText(connection)).toBe("Client not supported");
73 });
74
75 it("preserves profile display information without exposing credentials", () => {
76 expect(connectionCatalogDisplayUrl(ENTRY)).toBe("https://environment.example.test");
77 });
78
79 it("formats SSH display information without a missing username and preserves the port", () => {
80 const target = new SshConnectionTarget({
81 environmentId: EnvironmentId.make("environment-ssh"),
82 label: "SSH environment",
83 connectionId: "connection-ssh",
84 });
85 const entry: ConnectionCatalogEntry = {
86 target,
87 profile: Option.some(
88 new SshConnectionProfile({
89 connectionId: target.connectionId,
90 environmentId: target.environmentId,
91 label: target.label,
92 target: {
93 alias: "devbox",
94 hostname: "devbox.example.test",
95 username: null,
96 port: 2222,
97 },
98 }),
99 ),
100 };
101
102 expect(connectionCatalogDisplayUrl(entry)).toBe("devbox.example.test:2222");
103 });
104
105 it("distinguishes initial connection, reconnect, and retry errors", () => {
106 expect(presentConnectionState(supervisorState({ phase: "connecting", attempt: 1 }))).toEqual({
107 phase: "connecting",
108 error: null,
109 traceId: null,
110 });
111 expect(
112 presentConnectionState(
113 supervisorState({
114 phase: "connecting",
115 attempt: 2,
116 lastFailure: new ConnectionTransientError({
117 reason: "transport",
118 detail: "Socket closed.",
119 traceId: "trace-previous",
120 }),
121 }),
122 ),
123 ).toEqual({
124 phase: "reconnecting",
125 error: "Socket closed.",
126 traceId: "trace-previous",
127 });
128 expect(
129 presentConnectionState(
130 supervisorState({
131 phase: "backoff",
132 attempt: 2,
133 retryAt: 1,
134 lastFailure: new ConnectionTransientError({
135 reason: "transport",
136 detail: "Disconnected.",
137 traceId: "trace-1",
138 }),
139 }),
140 ),
141 ).toEqual({
142 phase: "reconnecting",
143 error: "Disconnected.",
144 traceId: "trace-1",
145 });
146 });
147
148 it("preserves the latest failure while the next attempt is active", () => {
149 expect(
150 presentEnvironmentConnection(
151 supervisorState({
152 phase: "connecting",
153 stage: "opening",
154 attempt: 2,
155 lastFailure: new ConnectionTransientError({
156 reason: "transport",
157 detail: "Relay connection timed out.",
158 traceId: "trace-retry",
159 }),
160 }),
161 ),
162 ).toEqual({
163 phase: "reconnecting",
164 error: "Relay connection timed out.",
165 traceId: "trace-retry",
166 });
167 });
168
169 it("combines reconnect progress with the latest failure", () => {
170 const connection = {
171 phase: "reconnecting",
172 error: "Relay request timed out.",
173 traceId: "trace-retry",
174 } as const;
175 expect(connectionStatusText(connection)).toBe(
176 "Failed to connect. Reconnecting... Reason: Relay request timed out.",
177 );
178 expect(connectionStatusTitle(connection)).toBe("Failed to connect. Reconnecting...");
179 });
180
181 it("presents the supervisor's offline state without consulting shell state", () => {
182 expect(
183 presentEnvironmentConnection(
184 supervisorState({
185 network: "offline",
186 phase: "offline",
187 stage: null,
188 }),
189 ),
190 ).toEqual({
191 phase: "offline",
192 error: null,
193 traceId: null,
194 });
195 });
196
197 it("presents a connected supervisor snapshot as connected", () => {
198 expect(
199 presentEnvironmentConnection(
200 supervisorState({
201 phase: "connected",
202 stage: null,
203 generation: 1,
204 }),
205 ),
206 ).toEqual({
207 phase: "connected",
208 error: null,
209 traceId: null,
210 });
211 });
212
213 it("preserves an explicitly available environment while offline", () => {
214 expect(
215 presentEnvironmentConnection(
216 supervisorState({
217 desired: false,
218 network: "offline",
219 phase: "available",
220 stage: null,
221 attempt: 0,
222 }),
223 ),
224 ).toEqual({
225 phase: "available",
226 error: null,
227 traceId: null,
228 });
229 });
230});