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 ConnectionTransientError,
9 type SupervisorConnectionState,
10} from "./model.ts";
11import {
12 connectionCatalogDisplayUrl,
13 connectionStatusText,
14 connectionStatusTitle,
15 presentEnvironmentConnection,
16 presentConnectionState,
17} from "./presentation.ts";
18
19const TARGET = new BearerConnectionTarget({
20 environmentId: EnvironmentId.make("environment-1"),
21 label: "Remote environment",
22 connectionId: "connection-1",
23});
24
25const ENTRY: ConnectionCatalogEntry = {
26 target: TARGET,
27 profile: Option.some(
28 new BearerConnectionProfile({
29 connectionId: TARGET.connectionId,
30 environmentId: TARGET.environmentId,
31 label: TARGET.label,
32 httpBaseUrl: "https://environment.example.test",
33 wsBaseUrl: "wss://environment.example.test",
34 }),
35 ),
36 enabled: true,
37};
38
39function supervisorState(overrides: Partial<SupervisorConnectionState>): SupervisorConnectionState {
40 return {
41 desired: true,
42 network: "online",
43 phase: "connecting",
44 stage: "preparing",
45 attempt: 1,
46 generation: 0,
47 lastFailure: null,
48 retryAt: null,
49 ...overrides,
50 };
51}
52
53describe("connection presentation", () => {
54 it("preserves profile display information without exposing credentials", () => {
55 expect(connectionCatalogDisplayUrl(ENTRY)).toBe("https://environment.example.test");
56 });
57
58 it("distinguishes initial connection, reconnect, and retry errors", () => {
59 expect(presentConnectionState(supervisorState({ phase: "connecting", attempt: 1 }))).toEqual({
60 phase: "connecting",
61 error: null,
62 traceId: null,
63 });
64 expect(
65 presentConnectionState(
66 supervisorState({
67 phase: "connecting",
68 attempt: 2,
69 lastFailure: new ConnectionTransientError({
70 reason: "transport",
71 detail: "Socket closed.",
72 traceId: "trace-previous",
73 }),
74 }),
75 ),
76 ).toEqual({
77 phase: "reconnecting",
78 error: "Socket closed.",
79 traceId: "trace-previous",
80 });
81 expect(
82 presentConnectionState(
83 supervisorState({
84 phase: "backoff",
85 attempt: 2,
86 retryAt: 1,
87 lastFailure: new ConnectionTransientError({
88 reason: "transport",
89 detail: "Disconnected.",
90 traceId: "trace-1",
91 }),
92 }),
93 ),
94 ).toEqual({
95 phase: "reconnecting",
96 error: "Disconnected.",
97 traceId: "trace-1",
98 });
99 });
100
101 it("preserves the latest failure while the next attempt is active", () => {
102 expect(
103 presentEnvironmentConnection(
104 supervisorState({
105 phase: "connecting",
106 stage: "opening",
107 attempt: 2,
108 lastFailure: new ConnectionTransientError({
109 reason: "transport",
110 detail: "Relay connection timed out.",
111 traceId: "trace-retry",
112 }),
113 }),
114 ),
115 ).toEqual({
116 phase: "reconnecting",
117 error: "Relay connection timed out.",
118 traceId: "trace-retry",
119 });
120 });
121
122 it("combines reconnect progress with the latest failure", () => {
123 const connection = {
124 phase: "reconnecting",
125 error: "Relay request timed out.",
126 traceId: "trace-retry",
127 } as const;
128 expect(connectionStatusText(connection)).toBe(
129 "Failed to connect. Reconnecting... Reason: Relay request timed out.",
130 );
131 expect(connectionStatusTitle(connection)).toBe("Failed to connect. Reconnecting...");
132 });
133
134 it("presents the supervisor's offline state without consulting shell state", () => {
135 expect(
136 presentEnvironmentConnection(
137 supervisorState({
138 network: "offline",
139 phase: "offline",
140 stage: null,
141 }),
142 ),
143 ).toEqual({
144 phase: "offline",
145 error: null,
146 traceId: null,
147 });
148 });
149
150 it("presents a connected supervisor snapshot as connected", () => {
151 expect(
152 presentEnvironmentConnection(
153 supervisorState({
154 phase: "connected",
155 stage: null,
156 generation: 1,
157 }),
158 ),
159 ).toEqual({
160 phase: "connected",
161 error: null,
162 traceId: null,
163 });
164 });
165
166 it("preserves an explicitly available environment while offline", () => {
167 expect(
168 presentEnvironmentConnection(
169 supervisorState({
170 desired: false,
171 network: "offline",
172 phase: "available",
173 stage: null,
174 attempt: 0,
175 }),
176 ),
177 ).toEqual({
178 phase: "available",
179 error: null,
180 traceId: null,
181 });
182 });
183});