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

presentation.test.ts

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