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

presentation.test.ts

199 lines5.2 KB

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