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

remoteOpen.test.ts

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