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

presentation.test.ts

187 lines4.9 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 ConnectionTransientError,
9 type SupervisorConnectionState,
10} from "./model.ts";
11import {
12 connectionCatalogDisplayUrl,
13 connectionPhaseMessage,
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};
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 };
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("gives offline status precedence in global messaging", () => {
123 expect(connectionPhaseMessage("connected", TARGET.label, "offline")).toBe("You are offline");
124 });
125​
126 it("combines reconnect progress with the latest failure", () => {
127 const connection = {
128 phase: "reconnecting",
129 error: "Relay request timed out.",
130 traceId: "trace-retry",
131 } as const;
132 expect(connectionStatusText(connection)).toBe(
133 "Failed to connect. Reconnecting... Reason: Relay request timed out.",
134 );
135 expect(connectionStatusTitle(connection)).toBe("Failed to connect. Reconnecting...");
136 });
137​
138 it("presents the supervisor's offline state without consulting shell state", () => {
139 expect(
140 presentEnvironmentConnection(
141 supervisorState({
142 network: "offline",
143 phase: "offline",
144 stage: null,
145 }),
146 ),
147 ).toEqual({
148 phase: "offline",
149 error: null,
150 traceId: null,
151 });
152 });
153​
154 it("presents a connected supervisor snapshot as connected", () => {
155 expect(
156 presentEnvironmentConnection(
157 supervisorState({
158 phase: "connected",
159 stage: null,
160 generation: 1,
161 }),
162 ),
163 ).toEqual({
164 phase: "connected",
165 error: null,
166 traceId: null,
167 });
168 });
169​
170 it("preserves an explicitly available environment while offline", () => {
171 expect(
172 presentEnvironmentConnection(
173 supervisorState({
174 desired: false,
175 network: "offline",
176 phase: "available",
177 stage: null,
178 attempt: 0,
179 }),
180 ),
181 ).toEqual({
182 phase: "available",
183 error: null,
184 traceId: null,
185 });
186 });
187});