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

presentation.test.ts

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