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

remoteOpen.test.ts

356 lines13.0 KB

1import {
2 BearerConnectionProfile,
3 BearerConnectionTarget,
4 PrimaryConnectionTarget,
5 RelayConnectionTarget,
6 SshConnectionProfile,
7 SshConnectionTarget,
8} from "@t3tools/client-runtime/connection";
9import { buildRemoteOpenUrl, EnvironmentId } from "@t3tools/contracts";
10import * as Option from "effect/Option";
11import { describe, expect, it } from "vite-plus/test";
12​
13import { remoteFallbackEditors, resolveRemoteOpenState } from "./remoteOpen";
14​
15const environmentId = EnvironmentId.make("environment-1");
16​
17const primaryTarget = (httpBaseUrl: string) =>
18 new PrimaryConnectionTarget({
19 environmentId,
20 label: "sol",
21 httpBaseUrl,
22 wsBaseUrl: httpBaseUrl.replace("http", "ws"),
23 });
24​
25const sshRoute = (target: { readonly alias: string; readonly username: string | null }) => ({
26 target: new SshConnectionTarget({ environmentId, label: "sol", connectionId: "ssh-1" }),
27 profile: Option.some(
28 new SshConnectionProfile({
29 connectionId: "ssh-1",
30 environmentId,
31 label: "sol",
32 target: { ...target, hostname: "sol.tail1234.ts.net", port: null },
33 }),
34 ),
35});
36​
37const TAILSCALE_TARGETS = [
38 { kind: "tailscale", host: "sol.tail1234.ts.net" },
39 { kind: "mdns", host: "sol.local" },
40] as const;
41​
42describe("resolveRemoteOpenState", () => {
43 it("keeps exec behavior for a loopback primary target", () => {
44 expect(
45 resolveRemoteOpenState({
46 target: primaryTarget("http://127.0.0.1:8000"),
47 routes: [],
48 isDesktopRenderer: false,
49 remoteOpenTargets: TAILSCALE_TARGETS,
50 hostEnvironmentId: null,
51 }),
52 ).toEqual({ mode: "local-exec" });
53 });
54​
55 it("uses deep links for a primary target reached over the network", () => {
56 expect(
57 resolveRemoteOpenState({
58 target: primaryTarget("https://sol.tail1234.ts.net"),
59 routes: [],
60 isDesktopRenderer: false,
61 remoteOpenTargets: TAILSCALE_TARGETS,
62 hostEnvironmentId: null,
63 }),
64 ).toEqual({
65 mode: "remote-links",
66 host: { kind: "tailscale", host: "sol.tail1234.ts.net" },
67 });
68 });
69​
70 it("keeps exec behavior for the desktop app's own primary even on a NAT URL", () => {
71 // wsl-only binds the desktop-owned primary to its WSL2 NAT address.
72 expect(
73 resolveRemoteOpenState({
74 target: primaryTarget("http://172.29.112.1:14369"),
75 routes: [],
76 isDesktopRenderer: true,
77 remoteOpenTargets: TAILSCALE_TARGETS,
78 hostEnvironmentId: null,
79 }),
80 ).toEqual({ mode: "local-exec" });
81 });
82​
83 it("keeps exec behavior for the environment that owns the desktop's T3 home", () => {
84 // With Local environment off, a background service on this machine can own the
85 // desktop's home and still be reached through T3 Connect.
86 expect(
87 resolveRemoteOpenState({
88 target: new RelayConnectionTarget({ environmentId, label: "sol" }),
89 routes: [],
90 isDesktopRenderer: true,
91 remoteOpenTargets: TAILSCALE_TARGETS,
92 hostEnvironmentId: environmentId,
93 }),
94 ).toEqual({ mode: "local-exec" });
95 });
96​
97 it("uses deep links for other environments when the desktop home has its own", () => {
98 expect(
99 resolveRemoteOpenState({
100 target: new RelayConnectionTarget({ environmentId, label: "sol" }),
101 routes: [],
102 isDesktopRenderer: true,
103 remoteOpenTargets: TAILSCALE_TARGETS,
104 hostEnvironmentId: EnvironmentId.make("environment-host"),
105 }),
106 ).toEqual({
107 mode: "remote-links",
108 host: { kind: "tailscale", host: "sol.tail1234.ts.net" },
109 });
110 });
111​
112 it("leaves a bare desktop SSH alias for the editor to resolve through SSH config", () => {
113 const state = resolveRemoteOpenState({
114 target: new SshConnectionTarget({ environmentId, label: "sol", connectionId: "ssh-1" }),
115 routes: [sshRoute({ alias: "sol", username: null })],
116 isDesktopRenderer: true,
117 remoteOpenTargets: [
118 { kind: "tailscale", host: "sol.tail1234.ts.net", username: "server-user" },
119 ],
120 hostEnvironmentId: null,
121 });
122 expect(state).toEqual({ mode: "remote-links", host: { kind: "ssh-alias", host: "sol" } });
123 if (state.mode !== "remote-links") throw new Error("Expected a remote link");
124 expect(
125 buildRemoteOpenUrl({ editor: "vscode", ...state.host, absolutePath: "/work/project" }),
126 ).toBe("vscode://vscode-remote/ssh-remote+sol/work/project?windowId=_blank");
127 expect(
128 buildRemoteOpenUrl({ editor: "zed", ...state.host, absolutePath: "/work/project" }),
129 ).toBe("zed://ssh/sol/work/project");
130 expect(
131 buildRemoteOpenUrl({ editor: "idea", ...state.host, absolutePath: "/work/project" }),
132 ).toBe(
133 "jetbrains://gateway/ssh/environment?h=sol&launchIde=true&ideHint=IU&projectHint=%2Fwork%2Fproject",
134 );
135 });
136​
137 it.each([
138 { alias: "server", username: "admin-user" },
139 { alias: "admin-user@server", username: null },
140 { alias: "old-user@server", username: "admin-user" },
141 { alias: "admin-user@server", username: "" },
142 { alias: "admin-user@server", username: " " },
143 ])("keeps one login username in editor links for $alias (username: '$username')", (sshTarget) => {
144 const state = resolveRemoteOpenState({
145 target: new SshConnectionTarget({ environmentId, label: "server", connectionId: "ssh-1" }),
146 routes: [sshRoute(sshTarget)],
147 isDesktopRenderer: true,
148 remoteOpenTargets: TAILSCALE_TARGETS,
149 hostEnvironmentId: null,
150 });
151 expect(state.mode).toBe("remote-links");
152 if (state.mode !== "remote-links") throw new Error("Expected a remote link");
153 expect(
154 buildRemoteOpenUrl({ editor: "vscode", ...state.host, absolutePath: "/work/project" }),
155 ).toBe("vscode://vscode-remote/ssh-remote+admin-user%40server/work/project?windowId=_blank");
156 expect(
157 buildRemoteOpenUrl({ editor: "zed", ...state.host, absolutePath: "/work/project" }),
158 ).toBe("zed://ssh/admin-user@server/work/project");
159 expect(
160 buildRemoteOpenUrl({ editor: "idea", ...state.host, absolutePath: "/work/project" }),
161 ).toBe(
162 "jetbrains://gateway/ssh/environment?h=server&launchIde=true&ideHint=IU&projectHint=%2Fwork%2Fproject&u=admin-user",
163 );
164 });
165​
166 it("uses the advertised login username in editor links over a relay", () => {
167 const state = resolveRemoteOpenState({
168 target: new RelayConnectionTarget({ environmentId, label: "server" }),
169 routes: [],
170 isDesktopRenderer: false,
171 remoteOpenTargets: [
172 { kind: "tailscale", host: "server.tail1234.ts.net", username: "admin-user" },
173 ],
174 hostEnvironmentId: null,
175 });
176 expect(state.mode).toBe("remote-links");
177 if (state.mode !== "remote-links") throw new Error("Expected a remote link");
178 expect(
179 buildRemoteOpenUrl({ editor: "cursor", ...state.host, absolutePath: "/work/project" }),
180 ).toBe(
181 "cursor://vscode-remote/ssh-remote+admin-user%40server.tail1234.ts.net/work/project?windowId=_blank",
182 );
183 });
184​
185 it("reports unavailable when a remote environment advertises no hosts", () => {
186 for (const remoteOpenTargets of [[], undefined] as const) {
187 expect(
188 resolveRemoteOpenState({
189 target: new RelayConnectionTarget({ environmentId, label: "sol" }),
190 routes: [],
191 isDesktopRenderer: false,
192 remoteOpenTargets,
193 hostEnvironmentId: null,
194 }),
195 ).toEqual({ mode: "remote-unavailable" });
196 }
197 });
198 it("uses a saved SSH route's alias when a Tailscale route is preferred", () => {
199 const target = new BearerConnectionTarget({
200 environmentId,
201 label: "sol",
202 connectionId: "ts-1",
203 });
204 const state = resolveRemoteOpenState({
205 target,
206 routes: [
207 {
208 target,
209 profile: Option.some(
210 new BearerConnectionProfile({
211 connectionId: "ts-1",
212 environmentId,
213 label: "sol",
214 httpBaseUrl: "https://sol.tail1234.ts.net",
215 wsBaseUrl: "wss://sol.tail1234.ts.net",
216 network: "tailscale",
217 }),
218 ),
219 },
220 sshRoute({ alias: "sol-alias", username: null }),
221 ],
222 isDesktopRenderer: false,
223 remoteOpenTargets: [
224 { kind: "tailscale", host: "sol.tail1234.ts.net", username: "server-user" },
225 ],
226 hostEnvironmentId: null,
227 });
228 expect(state).toEqual({ mode: "remote-links", host: { kind: "ssh-alias", host: "sol-alias" } });
229 if (state.mode !== "remote-links") throw new Error("Expected a remote link");
230 expect(
231 buildRemoteOpenUrl({ editor: "zed", ...state.host, absolutePath: "/work/project" }),
232 ).toBe("zed://ssh/sol-alias/work/project");
233 });
234});
235​
236describe("remoteFallbackEditors", () => {
237 it.each([
238 {
239 // iPadOS Safari requests the desktop site with a Macintosh user agent by default.
240 device: "iPad with a desktop user agent",
241 userAgent:
242 "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 Safari/605.1.15",
243 platform: "MacIntel",
244 maxTouchPoints: 5,
245 },
246 {
247 device: "iPhone",
248 userAgent:
249 "Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 Mobile/15E148 Safari/604.1",
250 platform: "iPhone",
251 maxTouchPoints: 5,
252 },
253 {
254 device: "Android phone",
255 userAgent:
256 "Mozilla/5.0 (Linux; Android 14; Pixel 7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Mobile Safari/537.36",
257 platform: "Linux armv81",
258 maxTouchPoints: 5,
259 },
260 ])("offers no desktop editor for remote links on an $device", ({ device: _, ...identity }) => {
261 expect(remoteFallbackEditors(identity)).toEqual([]);
262 });
263​
264 it("keeps the VS Code fallback for remote links on a Mac", () => {
265 expect(
266 remoteFallbackEditors({
267 userAgent:
268 "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 Safari/605.1.15",
269 platform: "MacIntel",
270 maxTouchPoints: 0,
271 }),
272 ).toEqual(["vscode"]);
273 });
274});
275​
276describe("buildRemoteOpenUrl", () => {
277 it("roots Windows paths", () => {
278 expect(
279 buildRemoteOpenUrl({ editor: "vscode", host: "sol", absolutePath: "C:\\Users\\theo" }),
280 ).toBe("vscode://vscode-remote/ssh-remote+sol/C%3A/Users/theo?windowId=_blank");
281 });
282​
283 it("keeps a plus in the SSH username through Remote-SSH's authority parsing", () => {
284 const url = buildRemoteOpenUrl({
285 editor: "vscode",
286 host: "ssh.example.com",
287 username: "vm+demo",
288 absolutePath: "/workspace/project",
289 });
290 // VS Code decodes the authority, then Remote-SSH (0.129) takes `authority.split("+")[1]` and
291 // reads it as hex-encoded JSON `{ hostName, user }` or as `user@host`.
292 const authority = decodeURIComponent(new URL(url ?? "").pathname.split("/")[1] ?? "");
293 const destination = authority.split("+")[1] ?? "";
294 const hexBytes = /^(?:[0-9a-f]{2})+$/i.test(destination)
295 ? destination.match(/../g)?.map((byte) => Number.parseInt(byte, 16))
296 : undefined;
297 const resolved =
298 hexBytes === undefined
299 ? destination.match(/^(?:(?<user>.*)@)?(?<hostName>.+?)$/)?.groups
300 : JSON.parse(new TextDecoder().decode(new Uint8Array(hexBytes)));
301 expect({ user: resolved?.user, hostName: resolved?.hostName }).toEqual({
302 user: "vm+demo",
303 hostName: "ssh.example.com",
304 });
305 });
306​
307 it("asks VS Code to open a remote project folder in a new window", () => {
308 // Without windowId=_blank, VS Code reuses (and replaces) the last active window.
309 const url = buildRemoteOpenUrl({
310 editor: "vscode",
311 host: "sol",
312 absolutePath: "/work/project",
313 });
314 expect(new URL(url ?? "").searchParams.get("windowId")).toBe("_blank");
315 });
316​
317 it("drops the Windows drive letter for Zed", () => {
318 expect(
319 buildRemoteOpenUrl({ editor: "zed", host: "sol", absolutePath: "C:\\Users\\theo" }),
320 ).toBe("zed://ssh/sol/Users/theo");
321 expect(buildRemoteOpenUrl({ editor: "zed", host: "sol", absolutePath: "/C:/project" })).toBe(
322 "zed://ssh/sol/C%3A/project",
323 );
324 });
325​
326 it("adds a file position only for confirmed VS Code-family file targets", () => {
327 const targets = [
328 ["vscode", true, "vscode://vscode-remote/ssh-remote+sol/tmp/my%20file.md:1:1"],
329 ["cursor", true, "cursor://vscode-remote/ssh-remote+sol/tmp/my%20file.md:1:1"],
330 ["vscode", false, "vscode://vscode-remote/ssh-remote+sol/tmp/my%20file.md?windowId=_blank"],
331 [
332 "vscode",
333 undefined,
334 "vscode://vscode-remote/ssh-remote+sol/tmp/my%20file.md?windowId=_blank",
335 ],
336 ["zed", true, "zed://ssh/sol/tmp/my%20file.md"],
337 [
338 "pycharm",
339 true,
340 "jetbrains://gateway/ssh/environment?h=sol&launchIde=true&ideHint=PY&projectHint=%2Ftmp%2Fmy+file.md",
341 ],
342 ] as const;
343​
344 for (const [editor, isFile, expected] of targets) {
345 expect(
346 buildRemoteOpenUrl({
347 editor,
348 host: "sol",
349 absolutePath: "/tmp/my file.md",
350 ...(isFile === undefined ? {} : { isFile }),
351 }),
352 `${editor}, isFile=${isFile}`,
353 ).toBe(expected);
354 }
355 });
356});