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

presentation.test.ts

174 lines4.8 KB

1import { EnvironmentId } from "@t3tools/contracts";
2import { describe, expect, it } from "@effect/vitest";
3import * as Option from "effect/Option";
4​
5import {
6 BearerConnectionProfile,
7 type ConnectionCatalogEntry,
8 type ConnectionRoute,
9} from "./catalog.ts";
10import {
11 BearerConnectionTarget,
12 ConnectionTransientError,
13 RelayConnectionTarget,
14 type SupervisorConnectionState,
15} from "./model.ts";
16import {
17 environmentMcpUrl,
18 presentEnvironmentConnection,
19 presentConnectionState,
20} from "./presentation.ts";
21​
22const TARGET = new BearerConnectionTarget({
23 environmentId: EnvironmentId.make("environment-1"),
24 label: "Remote environment",
25 connectionId: "connection-1",
26});
27​
28const ENTRY: ConnectionCatalogEntry = {
29 target: TARGET,
30 profile: Option.some(
31 new BearerConnectionProfile({
32 connectionId: TARGET.connectionId,
33 environmentId: TARGET.environmentId,
34 label: TARGET.label,
35 httpBaseUrl: "https://environment.example.test",
36 wsBaseUrl: "wss://environment.example.test",
37 }),
38 ),
39 enabled: true,
40};
41​
42function supervisorState(overrides: Partial<SupervisorConnectionState>): SupervisorConnectionState {
43 return {
44 desired: true,
45 network: "online",
46 phase: "connecting",
47 stage: "preparing",
48 attempt: 1,
49 generation: 0,
50 lastFailure: null,
51 retryAt: null,
52 ...overrides,
53 };
55​
56describe("connection presentation", () => {
57 it("copies the MCP address of the route this device is connected over", () => {
58 const route = (connectionId: string, httpBaseUrl: string): ConnectionRoute => {
59 const target = new BearerConnectionTarget({ ...TARGET, connectionId });
60 return {
61 target,
62 profile: Option.some(
63 new BearerConnectionProfile({
64 connectionId,
65 environmentId: TARGET.environmentId,
66 label: TARGET.label,
67 httpBaseUrl,
68 wsBaseUrl: httpBaseUrl.replace(/^http/, "ws"),
69 }),
70 ),
71 };
72 };
73 const lan = route("lan", "http://192.168.4.53:3773/");
74 const tailnet = route("tailnet", "http://100.115.1.44:3773/");
75 const serve = route("serve", "https://machine.tailnet.ts.net/");
76 const entry: ConnectionCatalogEntry = {
77 ...ENTRY,
78 target: lan.target,
79 profile: lan.profile,
80 alternateRoutes: [tailnet, serve],
81 };
82​
83 expect(environmentMcpUrl({ entry, connectedTarget: tailnet.target })).toBe(
84 "http://100.115.1.44:3773/mcp",
85 );
86 expect(environmentMcpUrl({ entry })).toBe("http://192.168.4.53:3773/mcp");
87 });
88​
89 // regression: #16909 Copy MCP URL falls back to an alternate route when the relay route has no address
90 it("passes over routes without an address of their own", () => {
91 const relay = new RelayConnectionTarget({
92 environmentId: TARGET.environmentId,
93 label: TARGET.label,
94 });
95 const entry: ConnectionCatalogEntry = {
96 ...ENTRY,
97 target: relay,
98 profile: Option.none(),
99 alternateRoutes: [{ target: ENTRY.target, profile: ENTRY.profile }],
100 };
101​
102 expect(environmentMcpUrl({ entry, connectedTarget: relay })).toBe(
103 "https://environment.example.test/mcp",
104 );
105 expect(
106 environmentMcpUrl({
107 entry,
108 connectedTarget: relay,
109 relayHttpBaseUrl: "https://tunnel.example.test",
110 }),
111 ).toBe("https://tunnel.example.test/mcp");
112 });
113​
114 it("distinguishes initial connection, reconnect, and retry errors", () => {
115 expect(presentConnectionState(supervisorState({ phase: "connecting", attempt: 1 }))).toEqual({
116 phase: "connecting",
117 error: null,
118 traceId: null,
119 });
120 expect(
121 presentConnectionState(
122 supervisorState({
123 phase: "connecting",
124 attempt: 2,
125 lastFailure: new ConnectionTransientError({
126 reason: "transport",
127 detail: "Socket closed.",
128 traceId: "trace-previous",
129 }),
130 }),
131 ),
132 ).toEqual({
133 phase: "reconnecting",
134 error: "Socket closed.",
135 traceId: "trace-previous",
136 });
137 expect(
138 presentConnectionState(
139 supervisorState({
140 phase: "backoff",
141 attempt: 2,
142 retryAt: 1,
143 lastFailure: new ConnectionTransientError({
144 reason: "transport",
145 detail: "Disconnected.",
146 traceId: "trace-1",
147 }),
148 }),
149 ),
150 ).toEqual({
151 phase: "reconnecting",
152 error: "Disconnected.",
153 traceId: "trace-1",
154 });
155 });
156​
157 it("preserves an explicitly available environment while offline", () => {
158 expect(
159 presentEnvironmentConnection(
160 supervisorState({
161 desired: false,
162 network: "offline",
163 phase: "available",
164 stage: null,
165 attempt: 0,
166 }),
167 ),
168 ).toEqual({
169 phase: "available",
170 error: null,
171 traceId: null,
172 });
173 });
174});