Open History to see the last commit
1import { VcsProcessTimeoutError } from "@t3tools/contracts";
2import { expect, it } from "@effect/vitest";
3import * as TestClock from "effect/testing/TestClock";
4import * as Effect from "effect/Effect";
5import { ChildProcessSpawner } from "effect/process";
6import { resolveSshRemoteUrl } from "./sshRemote.ts";
7
8it.effect("resolves GitHub.com aliases and preserves other hosting endpoints", () =>
9 Effect.gen(function* () {
10 for (const [remoteUrl, hostname, expected] of [
11 [
12 "git@github.com-work:example/project.git",
13 "github.com",
14 "git@github.com:example/project.git",
15 ],
16 [
17 "ssh://git@work:2222/example/project.git",
18 "github.example.test",
19 "ssh://git@work:2222/example/project.git",
20 ],
21 ["git@work:example/project.git", "gitlab.com", "git@work:example/project.git"],
22 [
23 "ssh://git@work:2222/group/../project%20name.git",
24 "github.com",
25 "ssh://git@github.com:2222/group/../project%20name.git",
26 ],
27 ["git@work:group/project name.git", "github.com", "git@github.com:group/project name.git"],
28 ] as const) {
29 const result = yield* resolveSshRemoteUrl({
30 remoteUrl,
31 cwd: "/repo",
32 process: {
33 run: () =>
34 Effect.succeed({
35 stdout: `hostname ${hostname}\n`,
36 stderr: "",
37 exitCode: ChildProcessSpawner.ExitCode(0),
38 stdoutTruncated: false,
39 stderrTruncated: false,
40 }),
41 },
42 });
43 expect(result).toBe(expected);
44 }
45 }),
46);
47
48it.effect("leaves HTTPS remotes alone and keeps SSH URLs when resolution fails", () =>
49 Effect.gen(function* () {
50 const remoteUrl = "https://github.com-work/example/project.git";
51 const result = yield* resolveSshRemoteUrl({
52 remoteUrl,
53 cwd: "/repo",
54 process: { run: () => Effect.die("HTTPS must not invoke ssh") },
55 });
56 expect(result).toBe(remoteUrl);
57 const ssh = "git@github.com-work:example/project.git";
58 expect(
59 yield* resolveSshRemoteUrl({
60 remoteUrl: ssh,
61 cwd: "/repo",
62 process: {
63 run: () =>
64 Effect.fail(
65 new VcsProcessTimeoutError({
66 operation: "test",
67 command: "ssh",
68 cwd: "/repo",
69 timeoutMs: 5000,
70 }),
71 ),
72 },
73 }),
74 ).toBe(ssh);
75 }),
76);
77
78it.effect("keeps the original remote when SSH has no usable hostname", () =>
79 Effect.gen(function* () {
80 const remoteUrl = "ssh://git@work:2222/example/project.git";
81 for (const [stdout, exitCode] of [
82 ["user git\n", 0],
83 ["hostname -bad-host\n", 0],
84 ["hostname github.com\n", 1],
85 ] as const) {
86 expect(
87 yield* resolveSshRemoteUrl({
88 remoteUrl,
89 cwd: "/repo with spaces",
90 process: {
91 run: () =>
92 Effect.succeed({
93 stdout,
94 stderr: "",
95 exitCode: ChildProcessSpawner.ExitCode(exitCode),
96 stdoutTruncated: false,
97 stderrTruncated: false,
98 }),
99 },
100 }),
101 ).toBe(remoteUrl);
102 }
103 }),
104);
105
106it.effect("reads the SSH URL's user, alias, and transport port", () =>
107 Effect.gen(function* () {
108 const remoteUrl = "ssh://git@github.com-work:2222/example/project.git";
109 const result = yield* resolveSshRemoteUrl({
110 remoteUrl,
111 cwd: "/repo with spaces",
112 process: {
113 run: (input) => {
114 expect(input.args).toEqual(["-G", "-l", "git", "-p", "2222", "github.com-work"]);
115 return Effect.succeed({
116 stdout: "hostname github.com\n",
117 stderr: "",
118 exitCode: ChildProcessSpawner.ExitCode(0),
119 stdoutTruncated: false,
120 stderrTruncated: false,
121 });
122 },
123 },
124 });
125 expect(result).toBe("ssh://git@github.com:2222/example/project.git");
126 }),
127);
128
129it.effect("keeps public API hosts when SSH uses an alternate endpoint or IP address", () =>
130 Effect.gen(function* () {
131 for (const hostname of ["github.com", "gitlab.com", "bitbucket.org"]) {
132 const remoteUrl = `git@${hostname}:example/project.git`;
133 expect(
134 yield* resolveSshRemoteUrl({
135 remoteUrl,
136 cwd: "/repo",
137 process: { run: () => Effect.die("Known API hosts do not need SSH resolution") },
138 }),
139 ).toBe(remoteUrl);
140 }
141 for (const [hostname, expectedHost] of [
142 ["ssh.github.com", "github.com"],
143 ["altssh.gitlab.com", "work"],
144 ["altssh.bitbucket.org", "work"],
145 ["ssh.github.example.test", "work"],
146 ["gitea.example.test", "work"],
147 ["192.0.2.1", "work"],
148 ]) {
149 expect(
150 yield* resolveSshRemoteUrl({
151 remoteUrl: "git@work:example/project.git",
152 cwd: "/repo",
153 process: {
154 run: () =>
155 Effect.succeed({
156 stdout: `hostname ${hostname}\r\n`,
157 stderr: "",
158 exitCode: ChildProcessSpawner.ExitCode(0),
159 stdoutTruncated: false,
160 stderrTruncated: false,
161 }),
162 },
163 }),
164 ).toBe(`git@${expectedHost}:example/project.git`);
165 }
166 }),
167);
168
169it.effect("caches SSH hostnames for 30 seconds while preserving each repository path", () =>
170 Effect.gen(function* () {
171 let calls = 0;
172 const process = {
173 run: () =>
174 Effect.sync(() => {
175 calls++;
176 return {
177 stdout: "hostname github.com\n",
178 stderr: "",
179 exitCode: ChildProcessSpawner.ExitCode(0),
180 stdoutTruncated: false,
181 stderrTruncated: false,
182 };
183 }),
184 };
185 const resolve = (remoteUrl: string, cwd = "/repo") =>
186 resolveSshRemoteUrl({ remoteUrl, cwd, process });
187 yield* resolve("git@alias:example/one.git");
188 yield* TestClock.adjust("29 seconds");
189 expect(yield* resolve("git@alias:example/two.git")).toBe("git@github.com:example/two.git");
190 expect(calls).toBe(1);
191 yield* TestClock.adjust("1 second");
192 yield* resolve("git@alias:example/one.git");
193 expect(calls).toBe(2);
194 yield* resolve("git@alias:example/one.git", "/other");
195 yield* resolve("other@alias:example/one.git");
196 yield* resolve("ssh://git@alias:2222/example/one.git");
197 yield* resolve("git@other-alias:example/one.git");
198 expect(calls).toBe(6);
199 }),
200);
201
202it.effect("retries failed SSH lookups immediately", () =>
203 Effect.gen(function* () {
204 for (const failure of ["exit", "missing", "error"] as const) {
205 let calls = 0;
206 const process = {
207 run: () =>
208 Effect.suspend(() => {
209 calls++;
210 if (calls === 1 && failure === "error")
211 return Effect.fail(
212 new VcsProcessTimeoutError({
213 operation: "test",
214 command: "ssh",
215 cwd: "/repo",
216 timeoutMs: 5000,
217 }),
218 );
219 return Effect.succeed({
220 stdout: calls === 1 && failure === "missing" ? "user git\n" : "hostname github.com\n",
221 stderr: "",
222 exitCode: ChildProcessSpawner.ExitCode(calls === 1 && failure === "exit" ? 1 : 0),
223 stdoutTruncated: false,
224 stderrTruncated: false,
225 });
226 }),
227 };
228 const input = { remoteUrl: "git@alias:example/project.git", cwd: "/repo", process };
229 expect(yield* resolveSshRemoteUrl(input)).toBe(input.remoteUrl);
230 expect(yield* resolveSshRemoteUrl(input)).toBe("git@github.com:example/project.git");
231 expect(calls).toBe(2);
232 }
233 }),
234);