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

remoteOpen.test.ts

188 lines7.0 KB

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 }),
35 ).toEqual({ mode: "local-exec" });
36 });
37​
38 it("uses deep links for a primary target reached over the network", () => {
39 expect(
40 resolveRemoteOpenState({
41 target: primaryTarget("https://sol.tail1234.ts.net"),
42 sshTarget: null,
43 isDesktopRenderer: false,
44 remoteOpenTargets: TAILSCALE_TARGETS,
45 }),
46 ).toEqual({
47 mode: "remote-links",
48 host: { kind: "tailscale", host: "sol.tail1234.ts.net" },
49 });
50 });
51​
52 it("keeps exec behavior for the desktop app's own primary even on a NAT URL", () => {
53 // wsl-only binds the desktop-owned primary to its WSL2 NAT address.
54 expect(
55 resolveRemoteOpenState({
56 target: primaryTarget("http://172.29.112.1:14369"),
57 sshTarget: null,
58 isDesktopRenderer: true,
59 remoteOpenTargets: TAILSCALE_TARGETS,
60 }),
61 ).toEqual({ mode: "local-exec" });
62 });
63​
64 it("leaves a bare desktop SSH alias for the editor to resolve through SSH config", () => {
65 const state = resolveRemoteOpenState({
66 target: new SshConnectionTarget({ environmentId, label: "sol", connectionId: "ssh-1" }),
67 sshTarget: { alias: "sol", username: null },
68 isDesktopRenderer: true,
69 remoteOpenTargets: [
70 { kind: "tailscale", host: "sol.tail1234.ts.net", username: "server-user" },
71 ],
72 });
73 expect(state).toEqual({ mode: "remote-links", host: { kind: "ssh-alias", host: "sol" } });
74 if (state.mode !== "remote-links") throw new Error("Expected a remote link");
75 expect(
76 buildRemoteOpenUrl({ editor: "vscode", ...state.host, absolutePath: "/work/project" }),
77 ).toBe("vscode://vscode-remote/ssh-remote+sol/work/project");
78 expect(
79 buildRemoteOpenUrl({ editor: "zed", ...state.host, absolutePath: "/work/project" }),
80 ).toBe("zed://ssh/sol/work/project");
81 expect(
82 buildRemoteOpenUrl({ editor: "idea", ...state.host, absolutePath: "/work/project" }),
83 ).toBe(
84 "jetbrains://gateway/ssh/environment?h=sol&launchIde=true&ideHint=IU&projectHint=%2Fwork%2Fproject",
85 );
86 });
87​
88 it.each([
89 { alias: "server", username: "admin-user" },
90 { alias: "admin-user@server", username: null },
91 { alias: "old-user@server", username: "admin-user" },
92 { alias: "admin-user@server", username: "" },
93 { alias: "admin-user@server", username: " " },
94 ])("keeps one login username in editor links for $alias (username: '$username')", (sshTarget) => {
95 const state = resolveRemoteOpenState({
96 target: new SshConnectionTarget({ environmentId, label: "server", connectionId: "ssh-1" }),
97 sshTarget,
98 isDesktopRenderer: true,
99 remoteOpenTargets: TAILSCALE_TARGETS,
100 });
101 expect(state.mode).toBe("remote-links");
102 if (state.mode !== "remote-links") throw new Error("Expected a remote link");
103 expect(
104 buildRemoteOpenUrl({ editor: "vscode", ...state.host, absolutePath: "/work/project" }),
105 ).toBe("vscode://vscode-remote/ssh-remote+admin-user%40server/work/project");
106 expect(
107 buildRemoteOpenUrl({ editor: "zed", ...state.host, absolutePath: "/work/project" }),
108 ).toBe("zed://ssh/admin-user@server/work/project");
109 expect(
110 buildRemoteOpenUrl({ editor: "idea", ...state.host, absolutePath: "/work/project" }),
111 ).toBe(
112 "jetbrains://gateway/ssh/environment?h=server&launchIde=true&ideHint=IU&projectHint=%2Fwork%2Fproject&u=admin-user",
113 );
114 });
115​
116 it("uses the advertised login username in editor links over a relay", () => {
117 const state = resolveRemoteOpenState({
118 target: new RelayConnectionTarget({ environmentId, label: "server" }),
119 sshTarget: null,
120 isDesktopRenderer: false,
121 remoteOpenTargets: [
122 { kind: "tailscale", host: "server.tail1234.ts.net", username: "admin-user" },
123 ],
124 });
125 expect(state.mode).toBe("remote-links");
126 if (state.mode !== "remote-links") throw new Error("Expected a remote link");
127 expect(
128 buildRemoteOpenUrl({ editor: "cursor", ...state.host, absolutePath: "/work/project" }),
129 ).toBe("cursor://vscode-remote/ssh-remote+admin-user%40server.tail1234.ts.net/work/project");
130 });
131​
132 it("reports unavailable when a remote environment advertises no hosts", () => {
133 for (const remoteOpenTargets of [[], undefined] as const) {
134 expect(
135 resolveRemoteOpenState({
136 target: new RelayConnectionTarget({ environmentId, label: "sol" }),
137 sshTarget: null,
138 isDesktopRenderer: false,
139 remoteOpenTargets,
140 }),
141 ).toEqual({ mode: "remote-unavailable" });
142 }
143 });
144});
145​
146describe("buildRemoteOpenUrl", () => {
147 it("roots Windows paths", () => {
148 expect(
149 buildRemoteOpenUrl({ editor: "vscode", host: "sol", absolutePath: "C:\\Users\\theo" }),
150 ).toBe("vscode://vscode-remote/ssh-remote+sol/C%3A/Users/theo");
151 });
152​
153 it("drops the Windows drive letter for Zed", () => {
154 expect(
155 buildRemoteOpenUrl({ editor: "zed", host: "sol", absolutePath: "C:\\Users\\theo" }),
156 ).toBe("zed://ssh/sol/Users/theo");
157 expect(buildRemoteOpenUrl({ editor: "zed", host: "sol", absolutePath: "/C:/project" })).toBe(
158 "zed://ssh/sol/C%3A/project",
159 );
160 });
161​
162 it("adds a file position only for confirmed VS Code-family file targets", () => {
163 const targets = [
164 ["vscode", true, "vscode://vscode-remote/ssh-remote+sol/tmp/my%20file.md:1:1"],
165 ["cursor", true, "cursor://vscode-remote/ssh-remote+sol/tmp/my%20file.md:1:1"],
166 ["vscode", false, "vscode://vscode-remote/ssh-remote+sol/tmp/my%20file.md"],
167 ["vscode", undefined, "vscode://vscode-remote/ssh-remote+sol/tmp/my%20file.md"],
168 ["zed", true, "zed://ssh/sol/tmp/my%20file.md"],
169 [
170 "pycharm",
171 true,
172 "jetbrains://gateway/ssh/environment?h=sol&launchIde=true&ideHint=PY&projectHint=%2Ftmp%2Fmy+file.md",
173 ],
174 ] as const;
175​
176 for (const [editor, isFile, expected] of targets) {
177 expect(
178 buildRemoteOpenUrl({
179 editor,
180 host: "sol",
181 absolutePath: "/tmp/my file.md",
182 ...(isFile === undefined ? {} : { isFile }),
183 }),
184 `${editor}, isFile=${isFile}`,
185 ).toBe(expected);
186 }
187 });
188});