Open History to see the last commit
1import {
2 BearerConnectionTarget,
3 PrimaryConnectionTarget,
4 RelayConnectionTarget,
5 SshConnectionTarget,
6} from "@t3tools/client-runtime/connection";
7import { buildRemoteOpenUrl, EnvironmentId } from "@t3tools/contracts";
8import { describe, expect, it } from "vite-plus/test";
9
10import { resolveRemoteOpenState } from "./remoteOpen";
11
12const environmentId = EnvironmentId.make("environment-1");
13
14const primaryTarget = (httpBaseUrl: string) =>
15 new PrimaryConnectionTarget({
16 environmentId,
17 label: "sol",
18 httpBaseUrl,
19 wsBaseUrl: httpBaseUrl.replace("http", "ws"),
20 });
21
22const TAILSCALE_TARGETS = [
23 { kind: "tailscale", host: "sol.tail1234.ts.net" },
24 { kind: "mdns", host: "sol.local" },
25] as const;
26
27describe("resolveRemoteOpenState", () => {
28 it("keeps exec behavior for a loopback primary target", () => {
29 expect(
30 resolveRemoteOpenState({
31 target: primaryTarget("http://127.0.0.1:8000"),
32 sshTarget: null,
33 isDesktopRenderer: false,
34 remoteOpenTargets: TAILSCALE_TARGETS,
35 }),
36 ).toEqual({ mode: "local-exec" });
37 });
38
39 it("uses deep links for a primary target reached over the network", () => {
40 expect(
41 resolveRemoteOpenState({
42 target: primaryTarget("https://sol.tail1234.ts.net"),
43 sshTarget: null,
44 isDesktopRenderer: false,
45 remoteOpenTargets: TAILSCALE_TARGETS,
46 }),
47 ).toEqual({
48 mode: "remote-links",
49 host: { kind: "tailscale", host: "sol.tail1234.ts.net" },
50 });
51 });
52
53 it("keeps exec behavior for the desktop app's own primary even on a NAT URL", () => {
54 // wsl-only mode binds the primary to the WSL2 NAT address; it is still
55 // this machine because the desktop app manages its own primary backend.
56 expect(
57 resolveRemoteOpenState({
58 target: primaryTarget("http://172.29.112.1:14369"),
59 sshTarget: null,
60 isDesktopRenderer: true,
61 remoteOpenTargets: TAILSCALE_TARGETS,
62 }),
63 ).toEqual({ mode: "local-exec" });
64 });
65
66 it("keeps exec behavior for desktop-local secondary backends", () => {
67 expect(
68 resolveRemoteOpenState({
69 target: new BearerConnectionTarget({
70 environmentId,
71 label: "WSL (Ubuntu)",
72 connectionId: "local:wsl-1",
73 }),
74 sshTarget: null,
75 isDesktopRenderer: false,
76 remoteOpenTargets: TAILSCALE_TARGETS,
77 }),
78 ).toEqual({ mode: "local-exec" });
79 });
80
81 it("leaves a bare desktop SSH alias for the editor to resolve through SSH config", () => {
82 const state = resolveRemoteOpenState({
83 target: new SshConnectionTarget({ environmentId, label: "sol", connectionId: "ssh-1" }),
84 sshTarget: { alias: "sol", username: null },
85 isDesktopRenderer: true,
86 remoteOpenTargets: [
87 { kind: "tailscale", host: "sol.tail1234.ts.net", username: "server-user" },
88 ],
89 });
90 expect(state).toEqual({ mode: "remote-links", host: { kind: "ssh-alias", host: "sol" } });
91 if (state.mode !== "remote-links") throw new Error("Expected a remote link");
92 expect(
93 buildRemoteOpenUrl({ editor: "vscode", ...state.host, absolutePath: "/work/project" }),
94 ).toBe("vscode://vscode-remote/ssh-remote+sol/work/project");
95 expect(
96 buildRemoteOpenUrl({ editor: "zed", ...state.host, absolutePath: "/work/project" }),
97 ).toBe("zed://ssh/sol/work/project");
98 expect(
99 buildRemoteOpenUrl({ editor: "idea", ...state.host, absolutePath: "/work/project" }),
100 ).toBe(
101 "jetbrains://gateway/ssh/environment?h=sol&launchIde=true&ideHint=IU&projectHint=%2Fwork%2Fproject",
102 );
103 });
104
105 it.each([
106 { alias: "server", username: "admin-user" },
107 { alias: "admin-user@server", username: null },
108 { alias: "old-user@server", username: "admin-user" },
109 { alias: "admin-user@server", username: "" },
110 { alias: "admin-user@server", username: " " },
111 ])("keeps one login username in editor links for $alias (username: '$username')", (sshTarget) => {
112 const state = resolveRemoteOpenState({
113 target: new SshConnectionTarget({ environmentId, label: "server", connectionId: "ssh-1" }),
114 sshTarget,
115 isDesktopRenderer: true,
116 remoteOpenTargets: TAILSCALE_TARGETS,
117 });
118 expect(state.mode).toBe("remote-links");
119 if (state.mode !== "remote-links") throw new Error("Expected a remote link");
120 expect(
121 buildRemoteOpenUrl({ editor: "vscode", ...state.host, absolutePath: "/work/project" }),
122 ).toBe("vscode://vscode-remote/ssh-remote+admin-user%40server/work/project");
123 expect(
124 buildRemoteOpenUrl({ editor: "zed", ...state.host, absolutePath: "/work/project" }),
125 ).toBe("zed://ssh/admin-user@server/work/project");
126 expect(
127 buildRemoteOpenUrl({ editor: "idea", ...state.host, absolutePath: "/work/project" }),
128 ).toBe(
129 "jetbrains://gateway/ssh/environment?h=server&launchIde=true&ideHint=IU&projectHint=%2Fwork%2Fproject&u=admin-user",
130 );
131 });
132
133 it("uses the advertised login username in editor links over a relay", () => {
134 const state = resolveRemoteOpenState({
135 target: new RelayConnectionTarget({ environmentId, label: "server" }),
136 sshTarget: null,
137 isDesktopRenderer: false,
138 remoteOpenTargets: [
139 { kind: "tailscale", host: "server.tail1234.ts.net", username: "admin-user" },
140 ],
141 });
142 expect(state.mode).toBe("remote-links");
143 if (state.mode !== "remote-links") throw new Error("Expected a remote link");
144 expect(
145 buildRemoteOpenUrl({ editor: "cursor", ...state.host, absolutePath: "/work/project" }),
146 ).toBe("cursor://vscode-remote/ssh-remote+admin-user%40server.tail1234.ts.net/work/project");
147 });
148
149 it("reports unavailable when a remote environment advertises no hosts", () => {
150 for (const remoteOpenTargets of [[], undefined] as const) {
151 expect(
152 resolveRemoteOpenState({
153 target: new RelayConnectionTarget({ environmentId, label: "sol" }),
154 sshTarget: null,
155 isDesktopRenderer: false,
156 remoteOpenTargets,
157 }),
158 ).toEqual({ mode: "remote-unavailable" });
159 }
160 });
161
162 it("falls back to exec when the environment has no catalog entry", () => {
163 expect(
164 resolveRemoteOpenState({
165 target: null,
166 sshTarget: null,
167 isDesktopRenderer: false,
168 remoteOpenTargets: undefined,
169 }),
170 ).toEqual({ mode: "local-exec" });
171 });
172});
173
174describe("buildRemoteOpenUrl", () => {
175 it("builds a vscode-remote deep link", () => {
176 expect(
177 buildRemoteOpenUrl({
178 editor: "vscode",
179 host: "sol.tail1234.ts.net",
180 absolutePath: "/home/theo/code/my repo",
181 }),
182 ).toBe("vscode://vscode-remote/ssh-remote+sol.tail1234.ts.net/home/theo/code/my%20repo");
183 });
184
185 it("uses the fork's scheme", () => {
186 expect(buildRemoteOpenUrl({ editor: "cursor", host: "sol", absolutePath: "/tmp/x" })).toBe(
187 "cursor://vscode-remote/ssh-remote+sol/tmp/x",
188 );
189 });
190
191 it("roots Windows paths", () => {
192 expect(
193 buildRemoteOpenUrl({ editor: "vscode", host: "sol", absolutePath: "C:\\Users\\theo" }),
194 ).toBe("vscode://vscode-remote/ssh-remote+sol/C%3A/Users/theo");
195 });
196
197 it("builds Zed's ssh deep link", () => {
198 expect(
199 buildRemoteOpenUrl({
200 editor: "zed",
201 host: "sol.tail1234.ts.net",
202 absolutePath: "/home/theo/code/my repo",
203 }),
204 ).toBe("zed://ssh/sol.tail1234.ts.net/home/theo/code/my%20repo");
205 });
206
207 it("drops the Windows drive letter for Zed", () => {
208 expect(
209 buildRemoteOpenUrl({ editor: "zed", host: "sol", absolutePath: "C:\\Users\\theo" }),
210 ).toBe("zed://ssh/sol/Users/theo");
211 expect(buildRemoteOpenUrl({ editor: "zed", host: "sol", absolutePath: "/C:/project" })).toBe(
212 "zed://ssh/sol/C%3A/project",
213 );
214 });
215
216 it("builds a Toolbox ssh deep link for JetBrains IDEs", () => {
217 expect(
218 buildRemoteOpenUrl({
219 editor: "pycharm",
220 host: "sol.tail1234.ts.net",
221 absolutePath: "/home/theo/code/my repo",
222 }),
223 ).toBe(
224 "jetbrains://gateway/ssh/environment?h=sol.tail1234.ts.net&launchIde=true&ideHint=PY&projectHint=%2Fhome%2Ftheo%2Fcode%2Fmy+repo",
225 );
226 expect(
227 buildRemoteOpenUrl({ editor: "idea", host: "sol", absolutePath: "C:\\Users\\theo" }),
228 ).toBe(
229 "jetbrains://gateway/ssh/environment?h=sol&launchIde=true&ideHint=IU&projectHint=C%3A%2FUsers%2Ftheo",
230 );
231 });
232
233 it("adds a file position only for confirmed VS Code-family file targets", () => {
234 const targets = [
235 ["vscode", true, "vscode://vscode-remote/ssh-remote+sol/tmp/my%20file.md:1:1"],
236 ["cursor", true, "cursor://vscode-remote/ssh-remote+sol/tmp/my%20file.md:1:1"],
237 ["vscode", false, "vscode://vscode-remote/ssh-remote+sol/tmp/my%20file.md"],
238 ["vscode", undefined, "vscode://vscode-remote/ssh-remote+sol/tmp/my%20file.md"],
239 ["zed", true, "zed://ssh/sol/tmp/my%20file.md"],
240 [
241 "pycharm",
242 true,
243 "jetbrains://gateway/ssh/environment?h=sol&launchIde=true&ideHint=PY&projectHint=%2Ftmp%2Fmy+file.md",
244 ],
245 ] as const;
246
247 for (const [editor, isFile, expected] of targets) {
248 expect(
249 buildRemoteOpenUrl({
250 editor,
251 host: "sol",
252 absolutePath: "/tmp/my file.md",
253 ...(isFile === undefined ? {} : { isFile }),
254 }),
255 `${editor}, isFile=${isFile}`,
256 ).toBe(expected);
257 }
258 });
259
260 it("returns undefined for editors without remote support", () => {
261 expect(buildRemoteOpenUrl({ editor: "kiro", host: "sol", absolutePath: "/tmp/x" })).toBe(
262 undefined,
263 );
264 });
265});