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

presentation.test.ts

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