|
1 | 1 | import { assert, describe, it } from "@effect/vitest"; |
| 2 | +import * as NodeServices from "@effect/platform-node/NodeServices"; |
2 | 3 | import { detectSourceControlProviderFromRemoteUrl } from "@t3tools/shared/sourceControl"; |
| 4 | +import * as Duration from "effect/Duration"; |
3 | 5 | import * as Effect from "effect/Effect"; |
| 6 | +import * as Fiber from "effect/Fiber"; |
| 7 | +import * as Layer from "effect/Layer"; |
| 8 | +import * as Sink from "effect/Sink"; |
| 9 | +import * as Stream from "effect/Stream"; |
| 10 | +import * as TestClock from "effect/testing/TestClock"; |
| 11 | +import { ChildProcessSpawner } from "effect/unstable/process"; |
4 | 12 |
|
5 | 13 | import { |
6 | 14 | gitRemoteSshAliasToResolve, |
7 | 15 | resolveGitRemoteForSourceControl, |
| 16 | + SSH_CONFIG_RESOLVE_TIMEOUT_MS, |
8 | 17 | } from "./resolveGitRemoteForSourceControl.ts"; |
9 | 18 |
|
| 19 | +const encoder = new TextEncoder(); |
| 20 | + |
| 21 | +const 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 | + |
| 39 | +const 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 | + |
10 | 64 | describe("gitRemoteSshAliasToResolve", () => { |
11 | 65 | it("selects undotted SSH aliases and skips real hostnames", () => { |
12 | 66 | assert.strictEqual( |
@@ -66,4 +120,43 @@ describe("resolveGitRemoteForSourceControl", () => { |
66 | 120 | assert.strictEqual(resolveCalls, 0); |
67 | 121 | }), |
68 | 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 | + }); |
69 | 162 | }); |
0 commit comments