Open History to see the last commit
1import {
2 PrimaryConnectionTarget,
3 RelayConnectionTarget,
4 SshConnectionTarget,
5} from "@t3tools/client-runtime/connection";
6import { buildRemoteOpenUrl, EnvironmentId } from "@t3tools/contracts";
7import { describe, expect, it } from "vite-plus/test";
8
9import { resolveRemoteOpenState } from "./remoteOpen";
10
11const environmentId = EnvironmentId.make("environment-1");
12
13const primaryTarget = (httpBaseUrl: string) =>
14 new PrimaryConnectionTarget({
15 environmentId,
16 label: "sol",
17 httpBaseUrl,
18 wsBaseUrl: httpBaseUrl.replace("http", "ws"),
19 });
20
21const TAILSCALE_TARGETS = [
22 { kind: "tailscale", host: "sol.tail1234.ts.net" },
23 { kind: "mdns", host: "sol.local" },
24] as const;
25
26describe("resolveRemoteOpenState", () => {
27 it("keeps exec behavior for a loopback primary target", () => {
28 expect(
29 resolveRemoteOpenState({
30 target: primaryTarget("http://127.0.0.1:8000"),
31 sshTarget: null,
32 isDesktopRenderer: false,
33 remoteOpenTargets: TAILSCALE_TARGETS,
34 hostEnvironmentId: null,
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 hostEnvironmentId: null,
47 }),
48 ).toEqual({
49 mode: "remote-links",
50 host: { kind: "tailscale", host: "sol.tail1234.ts.net" },
51 });
52 });
53
54 it("keeps exec behavior for the desktop app's own primary even on a NAT URL", () => {
55 // wsl-only binds the desktop-owned primary to its WSL2 NAT address.
56 expect(
57 resolveRemoteOpenState({
58 target: primaryTarget("http://172.29.112.1:14369"),
59 sshTarget: null,
60 isDesktopRenderer: true,
61 remoteOpenTargets: TAILSCALE_TARGETS,
62 hostEnvironmentId: null,
63 }),
64 ).toEqual({ mode: "local-exec" });
65 });
66
67 it("keeps exec behavior for the environment that owns the desktop's T3 home", () => {
68 // With Local environment off, a background service on this machine can own the
69 // desktop's home and still be reached through T3 Connect.
70 expect(
71 resolveRemoteOpenState({
72 target: new RelayConnectionTarget({ environmentId, label: "sol" }),
73 sshTarget: null,
74 isDesktopRenderer: true,
75 remoteOpenTargets: TAILSCALE_TARGETS,
76 hostEnvironmentId: environmentId,
77 }),
78 ).toEqual({ mode: "local-exec" });
79 });
80
81 it("uses deep links for other environments when the desktop home has its own", () => {
82 expect(
83 resolveRemoteOpenState({
84 target: new RelayConnectionTarget({ environmentId, label: "sol" }),
85 sshTarget: null,
86 isDesktopRenderer: true,
87 remoteOpenTargets: TAILSCALE_TARGETS,
88 hostEnvironmentId: EnvironmentId.make("environment-host"),
89 }),
90 ).toEqual({
91 mode: "remote-links",
92 host: { kind: "tailscale", host: "sol.tail1234.ts.net" },
93 });
94 });
95
96 it("leaves a bare desktop SSH alias for the editor to resolve through SSH config", () => {
97 const state = resolveRemoteOpenState({
98 target: new SshConnectionTarget({ environmentId, label: "sol", connectionId: "ssh-1" }),
99 sshTarget: { alias: "sol", username: null },
100 isDesktopRenderer: true,
101 remoteOpenTargets: [
102 { kind: "tailscale", host: "sol.tail1234.ts.net", username: "server-user" },
103 ],
104 hostEnvironmentId: null,
105 });
106 expect(state).toEqual({ mode: "remote-links", host: { kind: "ssh-alias", host: "sol" } });
107 if (state.mode !== "remote-links") throw new Error("Expected a remote link");
108 expect(
109 buildRemoteOpenUrl({ editor: "vscode", ...state.host, absolutePath: "/work/project" }),
110 ).toBe("vscode://vscode-remote/ssh-remote+sol/work/project");
111 expect(
112 buildRemoteOpenUrl({ editor: "zed", ...state.host, absolutePath: "/work/project" }),
113 ).toBe("zed://ssh/sol/work/project");
114 expect(
115 buildRemoteOpenUrl({ editor: "idea", ...state.host, absolutePath: "/work/project" }),
116 ).toBe(
117 "jetbrains://gateway/ssh/environment?h=sol&launchIde=true&ideHint=IU&projectHint=%2Fwork%2Fproject",
118 );
119 });
120
121 it.each([
122 { alias: "server", username: "admin-user" },
123 { alias: "admin-user@server", username: null },
124 { alias: "old-user@server", username: "admin-user" },
125 { alias: "admin-user@server", username: "" },
126 { alias: "admin-user@server", username: " " },
127 ])("keeps one login username in editor links for $alias (username: '$username')", (sshTarget) => {
128 const state = resolveRemoteOpenState({
129 target: new SshConnectionTarget({ environmentId, label: "server", connectionId: "ssh-1" }),
130 sshTarget,
131 isDesktopRenderer: true,
132 remoteOpenTargets: TAILSCALE_TARGETS,
133 hostEnvironmentId: null,
134 });
135 expect(state.mode).toBe("remote-links");
136 if (state.mode !== "remote-links") throw new Error("Expected a remote link");
137 expect(
138 buildRemoteOpenUrl({ editor: "vscode", ...state.host, absolutePath: "/work/project" }),
139 ).toBe("vscode://vscode-remote/ssh-remote+admin-user%40server/work/project");
140 expect(
141 buildRemoteOpenUrl({ editor: "zed", ...state.host, absolutePath: "/work/project" }),
142 ).toBe("zed://ssh/admin-user@server/work/project");
143 expect(
144 buildRemoteOpenUrl({ editor: "idea", ...state.host, absolutePath: "/work/project" }),
145 ).toBe(
146 "jetbrains://gateway/ssh/environment?h=server&launchIde=true&ideHint=IU&projectHint=%2Fwork%2Fproject&u=admin-user",
147 );
148 });
149
150 it("uses the advertised login username in editor links over a relay", () => {
151 const state = resolveRemoteOpenState({
152 target: new RelayConnectionTarget({ environmentId, label: "server" }),
153 sshTarget: null,
154 isDesktopRenderer: false,
155 remoteOpenTargets: [
156 { kind: "tailscale", host: "server.tail1234.ts.net", username: "admin-user" },
157 ],
158 hostEnvironmentId: null,
159 });
160 expect(state.mode).toBe("remote-links");
161 if (state.mode !== "remote-links") throw new Error("Expected a remote link");
162 expect(
163 buildRemoteOpenUrl({ editor: "cursor", ...state.host, absolutePath: "/work/project" }),
164 ).toBe("cursor://vscode-remote/ssh-remote+admin-user%40server.tail1234.ts.net/work/project");
165 });
166
167 it("reports unavailable when a remote environment advertises no hosts", () => {
168 for (const remoteOpenTargets of [[], undefined] as const) {
169 expect(
170 resolveRemoteOpenState({
171 target: new RelayConnectionTarget({ environmentId, label: "sol" }),
172 sshTarget: null,
173 isDesktopRenderer: false,
174 remoteOpenTargets,
175 hostEnvironmentId: null,
176 }),
177 ).toEqual({ mode: "remote-unavailable" });
178 }
179 });
180});
181
182describe("buildRemoteOpenUrl", () => {
183 it("roots Windows paths", () => {
184 expect(
185 buildRemoteOpenUrl({ editor: "vscode", host: "sol", absolutePath: "C:\\Users\\theo" }),
186 ).toBe("vscode://vscode-remote/ssh-remote+sol/C%3A/Users/theo");
187 });
188
189 it("drops the Windows drive letter for Zed", () => {
190 expect(
191 buildRemoteOpenUrl({ editor: "zed", host: "sol", absolutePath: "C:\\Users\\theo" }),
192 ).toBe("zed://ssh/sol/Users/theo");
193 expect(buildRemoteOpenUrl({ editor: "zed", host: "sol", absolutePath: "/C:/project" })).toBe(
194 "zed://ssh/sol/C%3A/project",
195 );
196 });
197
198 it("adds a file position only for confirmed VS Code-family file targets", () => {
199 const targets = [
200 ["vscode", true, "vscode://vscode-remote/ssh-remote+sol/tmp/my%20file.md:1:1"],
201 ["cursor", true, "cursor://vscode-remote/ssh-remote+sol/tmp/my%20file.md:1:1"],
202 ["vscode", false, "vscode://vscode-remote/ssh-remote+sol/tmp/my%20file.md"],
203 ["vscode", undefined, "vscode://vscode-remote/ssh-remote+sol/tmp/my%20file.md"],
204 ["zed", true, "zed://ssh/sol/tmp/my%20file.md"],
205 [
206 "pycharm",
207 true,
208 "jetbrains://gateway/ssh/environment?h=sol&launchIde=true&ideHint=PY&projectHint=%2Ftmp%2Fmy+file.md",
209 ],
210 ] as const;
211
212 for (const [editor, isFile, expected] of targets) {
213 expect(
214 buildRemoteOpenUrl({
215 editor,
216 host: "sol",
217 absolutePath: "/tmp/my file.md",
218 ...(isFile === undefined ? {} : { isFile }),
219 }),
220 `${editor}, isFile=${isFile}`,
221 ).toBe(expected);
222 }
223 });
224});