Open History to see the last commit
1import { assert, describe, it } from "@effect/vitest";
2import * as NodeServices from "@effect/platform-node/NodeServices";
3import { detectSourceControlProviderFromRemoteUrl } from "@t3tools/shared/sourceControl";
4import * as Duration from "effect/Duration";
5import * as Effect from "effect/Effect";
6import * as Fiber from "effect/Fiber";
7import * as Layer from "effect/Layer";
8import * as Sink from "effect/Sink";
9import * as Stream from "effect/Stream";
10import * as TestClock from "effect/testing/TestClock";
11import { ChildProcessSpawner } from "effect/unstable/process";
12
13import {
14 gitRemoteSshAliasToResolve,
15 resolveGitRemoteForSourceControl,
16 SSH_CONFIG_RESOLVE_TIMEOUT_MS,
17} from "./resolveGitRemoteForSourceControl.ts";
18
19const encoder = new TextEncoder();
20
21const makeFailedProcess = (input: { readonly stdout: string; readonly stderr?: string }) => {
22 const stdoutStream = Stream.make(encoder.encode(input.stdout));
23 const stderrStream = input.stderr ? Stream.make(encoder.encode(input.stderr)) : Stream.empty;
24 return ChildProcessSpawner.makeHandle({
25 pid: ChildProcessSpawner.ProcessId(123),
26 stdout: stdoutStream,
27 stderr: stderrStream,
28 all: Stream.empty,
29 exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(1)),
30 isRunning: Effect.succeed(false),
31 kill: () => Effect.void,
32 stdin: Sink.drain,
33 getInputFd: () => Sink.drain,
34 getOutputFd: () => Stream.empty,
35 unref: Effect.succeed(Effect.void),
36 });
37};
38
39const makeNeverFinishingProcess = () => {
40 let finish: ((exitCode: ChildProcessSpawner.ExitCode) => void) | null = null;
41 return ChildProcessSpawner.makeHandle({
42 pid: ChildProcessSpawner.ProcessId(123),
43 stdout: Stream.empty,
44 stderr: Stream.empty,
45 all: Stream.empty,
46 exitCode: Effect.callback<ChildProcessSpawner.ExitCode>((resume) => {
47 finish = (exitCode) => resume(Effect.succeed(exitCode));
48 return Effect.sync(() => {
49 finish = null;
50 });
51 }),
52 isRunning: Effect.succeed(true),
53 kill: () =>
54 Effect.sync(() => {
55 finish?.(ChildProcessSpawner.ExitCode(143));
56 }),
57 stdin: Sink.drain,
58 getInputFd: () => Sink.drain,
59 getOutputFd: () => Stream.empty,
60 unref: Effect.succeed(Effect.void),
61 });
62};
63
64describe("gitRemoteSshAliasToResolve", () => {
65 it("selects undotted SSH aliases and skips real hostnames", () => {
66 assert.strictEqual(
67 gitRemoteSshAliasToResolve("git@github-personal:owner/repo.git"),
68 "github-personal",
69 );
70 assert.strictEqual(
71 gitRemoteSshAliasToResolve("ssh://git@gitlab-work/group/project.git"),
72 "gitlab-work",
73 );
74 assert.strictEqual(gitRemoteSshAliasToResolve("git@github.com:owner/repo.git"), null);
75 assert.strictEqual(gitRemoteSshAliasToResolve("https://github.com/owner/repo.git"), null);
76 });
77});
78
79describe("resolveGitRemoteForSourceControl", () => {
80 it.effect("rewrites an SSH alias to the resolved HostName", () =>
81 Effect.gen(function* () {
82 const rewritten = yield* resolveGitRemoteForSourceControl(
83 "git@github-personal:owner/repo.git",
84 () => Effect.succeed("github.com"),
85 );
86
87 assert.strictEqual(rewritten, "git@github.com:owner/repo.git");
88 assert.deepStrictEqual(detectSourceControlProviderFromRemoteUrl(rewritten), {
89 kind: "github",
90 name: "GitHub",
91 baseUrl: "https://github.com",
92 });
93 }),
94 );
95
96 it.effect("leaves the original remote when resolve fails", () =>
97 Effect.gen(function* () {
98 const original = "git@github-personal:owner/repo.git";
99 const rewritten = yield* resolveGitRemoteForSourceControl(original, () =>
100 Effect.succeed(null),
101 );
102
103 assert.strictEqual(rewritten, original);
104 assert.strictEqual(detectSourceControlProviderFromRemoteUrl(rewritten)?.kind, "unknown");
105 }),
106 );
107
108 it.effect("does not invent github.com and does not resolve dotted hosts", () =>
109 Effect.gen(function* () {
110 let resolveCalls = 0;
111 const resolve = () => {
112 resolveCalls += 1;
113 return Effect.succeed("evil.example");
114 };
115
116 assert.strictEqual(
117 yield* resolveGitRemoteForSourceControl("git@github.com:owner/repo.git", resolve),
118 "git@github.com:owner/repo.git",
119 );
120 assert.strictEqual(resolveCalls, 0);
121 }),
122 );
123
124 it.effect("leaves the original remote when ssh -G fails", () => {
125 const spawner = ChildProcessSpawner.make(() =>
126 Effect.succeed(makeFailedProcess({ stdout: "", stderr: "ssh: Could not resolve hostname" })),
127 );
128 const processLayer = Layer.mergeAll(
129 NodeServices.layer,
130 Layer.succeed(ChildProcessSpawner.ChildProcessSpawner, spawner),
131 );
132
133 return Effect.gen(function* () {
134 const original = "git@github-personal:owner/repo.git";
135 const rewritten = yield* resolveGitRemoteForSourceControl(original);
136
137 assert.strictEqual(rewritten, original);
138 assert.strictEqual(detectSourceControlProviderFromRemoteUrl(rewritten)?.kind, "unknown");
139 }).pipe(Effect.provide(processLayer));
140 });
141
142 it.effect("leaves the original remote when ssh -G times out", () => {
143 const spawner = ChildProcessSpawner.make(() => Effect.succeed(makeNeverFinishingProcess()));
144 const processLayer = Layer.mergeAll(
145 NodeServices.layer,
146 Layer.succeed(ChildProcessSpawner.ChildProcessSpawner, spawner),
147 TestClock.layer(),
148 );
149
150 return Effect.gen(function* () {
151 const original = "git@github-personal:owner/repo.git";
152 const fiber = yield* Effect.forkChild(resolveGitRemoteForSourceControl(original));
153 yield* Effect.yieldNow;
154 yield* TestClock.adjust(Duration.millis(SSH_CONFIG_RESOLVE_TIMEOUT_MS));
155
156 const rewritten = yield* Fiber.join(fiber);
157
158 assert.strictEqual(rewritten, original);
159 assert.strictEqual(detectSourceControlProviderFromRemoteUrl(rewritten)?.kind, "unknown");
160 }).pipe(Effect.provide(processLayer));
161 });
162});