Open History to see the last commit
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 { gitHubRoutingConnectionKey } from "./githubRoutingPermissions.ts";
11import { BearerConnectionTarget, RelayConnectionTarget } from "./model.ts";
12import {
13 connectionRouteId,
14 connectionRouteKind,
15 connectionRouteLabel,
16 credentialConnectionId,
17 insertRoute,
18 entryWithRoutes,
19 mergeLearnedRoutes,
20 removedWithRelay,
21 routesAfterRemoving,
22 upsertRoute,
23} from "./routes.ts";
24
25const ENVIRONMENT_ID = EnvironmentId.make("environment-1");
26
27function direct(id: string, httpBaseUrl: string): ConnectionRoute {
28 return {
29 target: new BearerConnectionTarget({
30 environmentId: ENVIRONMENT_ID,
31 label: "Desk",
32 connectionId: id,
33 }),
34 profile: Option.some(
35 new BearerConnectionProfile({
36 connectionId: id,
37 environmentId: ENVIRONMENT_ID,
38 label: "Desk",
39 httpBaseUrl,
40 wsBaseUrl: httpBaseUrl.replace(/^http/, "ws"),
41 }),
42 ),
43 };
44}
45
46const RELAY: ConnectionRoute = {
47 target: new RelayConnectionTarget({ environmentId: ENVIRONMENT_ID, label: "Desk" }),
48 profile: Option.none(),
49};
50const LAN = direct("lan", "http://192.168.1.10:3773/");
51const TAILNET = direct("tailnet", "https://desk.tail1234.ts.net/");
52const PUBLIC = direct("public", "https://desk.example.com/");
53
54describe("connection routes", () => {
55 it("classifies direct routes by address", () => {
56 expect(connectionRouteKind(LAN)).toBe("lan");
57 expect(connectionRouteKind(direct("ip", "http://100.101.102.103:3773/"))).toBe("tailnet");
58 expect(connectionRouteKind(TAILNET)).toBe("tailnet");
59 expect(connectionRouteKind(PUBLIC)).toBe("public");
60 expect(connectionRouteKind(direct("lo", "http://127.0.0.1:3773/"))).toBe("loopback");
61 expect(connectionRouteKind(direct("ts6", "http://[fd7a:115c:a1e0::1]:3773/"))).toBe("tailnet");
62 expect(connectionRouteLabel(TAILNET)).toBe("Tailscale");
63 expect(connectionRouteLabel(RELAY)).toBe("T3 Connect");
64 });
65
66 it("places a new route after faster kinds and ahead of T3 Connect", () => {
67 expect(insertRoute([RELAY], LAN)).toEqual([LAN, RELAY]);
68 expect(insertRoute([LAN, RELAY], TAILNET)).toEqual([LAN, TAILNET, RELAY]);
69 expect(insertRoute([TAILNET], LAN)).toEqual([LAN, TAILNET]);
70 expect(insertRoute([LAN, TAILNET], RELAY)).toEqual([LAN, TAILNET, RELAY]);
71 });
72
73 it("keeps a user's order when a saved route is replaced", () => {
74 // The user preferred T3 Connect over the LAN; re-pairing the LAN keeps that.
75 const repaired = direct("lan", "http://192.168.1.11:3773/");
76 expect(upsertRoute([RELAY, LAN], repaired)).toEqual([RELAY, repaired]);
77 });
78});
79
80describe("learned routes", () => {
81 const relayOnly: ConnectionCatalogEntry = {
82 target: RELAY.target,
83 profile: RELAY.profile,
84 enabled: true,
85 };
86 const ids = (routes: ReadonlyArray<ConnectionRoute> | null) =>
87 routes?.map((route) => connectionRouteId(route.target)) ?? null;
88 const profileOf = (route: ConnectionRoute) => Option.getOrThrow(route.profile);
89
90 it("learns a LAN address over T3 Connect, ahead of it, using the T3 Connect credential", () => {
91 const routes = mergeLearnedRoutes({
92 entry: relayOnly,
93 activeRoute: RELAY,
94 reported: [{ httpBaseUrl: "http://192.168.1.10:3773/" }],
95 allowInsecure: true,
96 });
97 expect(ids(routes)).toEqual([`learned:${ENVIRONMENT_ID}:http://192.168.1.10:3773`, "relay"]);
98 expect(profileOf(routes![0]!)).toMatchObject({
99 learned: true,
100 authorization: "t3-connect",
101 wsBaseUrl: "ws://192.168.1.10:3773/",
102 });
103 });
104
105 it("replaces a learned LAN address when the server reports a new one", () => {
106 const first = mergeLearnedRoutes({
107 entry: relayOnly,
108 activeRoute: RELAY,
109 reported: [{ httpBaseUrl: "http://192.168.1.10:3773/" }],
110 allowInsecure: true,
111 })!;
112 const entry: ConnectionCatalogEntry = {
113 ...relayOnly,
114 target: first[0]!.target,
115 profile: first[0]!.profile,
116 alternateRoutes: first.slice(1),
117 };
118 const moved = mergeLearnedRoutes({
119 entry,
120 activeRoute: RELAY,
121 reported: [{ httpBaseUrl: "http://10.0.0.42:3773/" }],
122 allowInsecure: true,
123 });
124 expect(ids(moved)).toEqual([`learned:${ENVIRONMENT_ID}:http://10.0.0.42:3773`, "relay"]);
125 });
126
127 it("keeps a learned route where the user moved it while the server reports it", () => {
128 const lan = { httpBaseUrl: "http://192.168.1.10:3773/" };
129 const first = mergeLearnedRoutes({
130 entry: relayOnly,
131 activeRoute: RELAY,
132 reported: [lan],
133 allowInsecure: true,
134 })!;
135 // The user prefers T3 Connect over the learned LAN address.
136 const reordered = entryWithRoutes(relayOnly, [first[1]!, first[0]!]);
137 expect(
138 mergeLearnedRoutes({
139 entry: reordered,
140 activeRoute: RELAY,
141 reported: [lan],
142 allowInsecure: true,
143 }),
144 ).toBeNull();
145 // A newly reported address is still placed by speed.
146 const next = mergeLearnedRoutes({
147 entry: reordered,
148 activeRoute: RELAY,
149 reported: [lan, { httpBaseUrl: "http://100.101.102.103:3773/" }],
150 allowInsecure: true,
151 });
152 expect(ids(next)).toEqual([
153 `learned:${ENVIRONMENT_ID}:http://100.101.102.103:3773`,
154 "relay",
155 `learned:${ENVIRONMENT_ID}:http://192.168.1.10:3773`,
156 ]);
157 });
158
159 it("leaves user routes alone and does not learn an address already saved", () => {
160 const entry: ConnectionCatalogEntry = {
161 target: LAN.target,
162 profile: LAN.profile,
163 alternateRoutes: [RELAY],
164 enabled: true,
165 };
166 expect(
167 mergeLearnedRoutes({
168 entry,
169 activeRoute: RELAY,
170 reported: [{ httpBaseUrl: "http://192.168.1.10:3773" }],
171 allowInsecure: true,
172 }),
173 ).toBeNull();
174 // The server stops reporting the LAN address; the paired route stays.
175 expect(
176 mergeLearnedRoutes({ entry, activeRoute: RELAY, reported: [], allowInsecure: true }),
177 ).toBeNull();
178 });
179
180 it("borrows the paired token when learned over a bearer route", () => {
181 const entry: ConnectionCatalogEntry = {
182 ...relayOnly,
183 target: LAN.target,
184 profile: LAN.profile,
185 };
186 const routes = mergeLearnedRoutes({
187 entry,
188 activeRoute: LAN,
189 reported: [{ httpBaseUrl: "https://desk.tail1234.ts.net/" }],
190 allowInsecure: true,
191 })!;
192 const learned = routes.find((route) => connectionRouteKind(route) === "tailnet")!;
193 expect(profileOf(learned)).not.toHaveProperty("authorization");
194 expect(credentialConnectionId(connectionRouteId(learned.target))).toBe("lan");
195 });
196
197 it("skips plain HTTP from an HTTPS page and never learns loopback", () => {
198 expect(
199 mergeLearnedRoutes({
200 entry: relayOnly,
201 activeRoute: RELAY,
202 reported: [
203 { httpBaseUrl: "http://192.168.1.10:3773/" },
204 { httpBaseUrl: "http://127.0.0.1:3773/" },
205 ],
206 allowInsecure: false,
207 }),
208 ).toBeNull();
209 });
210
211 it("removes learned routes along with the route whose credential they borrow", () => {
212 const overRelay = mergeLearnedRoutes({
213 entry: relayOnly,
214 activeRoute: RELAY,
215 reported: [{ httpBaseUrl: "http://192.168.1.10:3773/" }],
216 allowInsecure: true,
217 })!;
218 expect(routesAfterRemoving(overRelay, "relay")).toEqual([]);
219
220 const paired: ConnectionCatalogEntry = {
221 ...relayOnly,
222 target: LAN.target,
223 profile: LAN.profile,
224 alternateRoutes: [RELAY],
225 };
226 const overLan = mergeLearnedRoutes({
227 entry: paired,
228 activeRoute: LAN,
229 reported: [{ httpBaseUrl: "https://desk.tail1234.ts.net/" }],
230 allowInsecure: true,
231 })!;
232 expect(ids(routesAfterRemoving(overLan, "lan"))).toEqual(["relay"]);
233 // Removing T3 Connect keeps the paired LAN and what it learned.
234 expect(ids(routesAfterRemoving(overLan, "relay"))).toHaveLength(2);
235 });
236
237 it("counts an environment reached only through T3 Connect as removed with it", () => {
238 const learned = mergeLearnedRoutes({
239 entry: relayOnly,
240 activeRoute: RELAY,
241 reported: [{ httpBaseUrl: "http://192.168.1.10:3773/" }],
242 allowInsecure: true,
243 })!;
244 expect(removedWithRelay(relayOnly)).toBe(true);
245 expect(removedWithRelay(entryWithRoutes(relayOnly, learned))).toBe(true);
246 expect(removedWithRelay(entryWithRoutes(relayOnly, [LAN, RELAY]))).toBe(false);
247 });
248
249 it("keeps GitHub routing trust when a route is learned", () => {
250 const learned = mergeLearnedRoutes({
251 entry: relayOnly,
252 activeRoute: RELAY,
253 reported: [{ httpBaseUrl: "http://192.168.1.10:3773/" }],
254 allowInsecure: true,
255 })!;
256 const withLearned: ConnectionCatalogEntry = {
257 ...relayOnly,
258 target: learned[0]!.target,
259 profile: learned[0]!.profile,
260 alternateRoutes: learned.slice(1),
261 };
262 expect(gitHubRoutingConnectionKey(withLearned)).toBe(gitHubRoutingConnectionKey(relayOnly));
263 });
264
265 it("keeps the T3 Connect credential when learning over a learned T3 Connect route", () => {
266 const first = mergeLearnedRoutes({
267 entry: relayOnly,
268 activeRoute: RELAY,
269 reported: [{ httpBaseUrl: "http://192.168.1.10:3773/" }],
270 allowInsecure: true,
271 })!;
272 const learnedLan = first[0]!;
273 const entry: ConnectionCatalogEntry = {
274 ...relayOnly,
275 target: learnedLan.target,
276 profile: learnedLan.profile,
277 alternateRoutes: first.slice(1),
278 };
279 const next = mergeLearnedRoutes({
280 entry,
281 activeRoute: learnedLan,
282 reported: [
283 { httpBaseUrl: "http://192.168.1.10:3773/" },
284 { httpBaseUrl: "https://desk.tail1234.ts.net/" },
285 ],
286 allowInsecure: true,
287 })!;
288 for (const route of next.filter((candidate) => connectionRouteKind(candidate) !== "relay")) {
289 expect(profileOf(route)).toMatchObject({ authorization: "t3-connect" });
290 expect(connectionRouteId(route.target)).not.toContain("@");
291 }
292 });
293
294 it("saves a scheme change on the same host as a new address", () => {
295 const first = mergeLearnedRoutes({
296 entry: relayOnly,
297 activeRoute: RELAY,
298 reported: [{ httpBaseUrl: "http://desk.local:3773/" }],
299 allowInsecure: true,
300 })!;
301 const entry: ConnectionCatalogEntry = {
302 ...relayOnly,
303 target: first[0]!.target,
304 profile: first[0]!.profile,
305 alternateRoutes: first.slice(1),
306 };
307 const moved = mergeLearnedRoutes({
308 entry,
309 activeRoute: RELAY,
310 reported: [{ httpBaseUrl: "https://desk.local:3773/" }],
311 allowInsecure: true,
312 });
313 expect(moved).not.toBeNull();
314 expect(profileOf(moved![0]!)).toMatchObject({ httpBaseUrl: "https://desk.local:3773/" });
315 });
316});