|
| 1 | +import { assert, it } from "@effect/vitest"; |
| 2 | +import * as Effect from "effect/Effect"; |
| 3 | +import * as Option from "effect/Option"; |
| 4 | +import { ChildProcessSpawner } from "effect/unstable/process"; |
| 5 | +import { |
| 6 | + type VcsError, |
| 7 | + VcsProcessExitError, |
| 8 | + VcsProcessSpawnError, |
| 9 | + VcsProcessTimeoutError, |
| 10 | +} from "@t3tools/contracts"; |
| 11 | + |
| 12 | +import * as VcsProcess from "../vcs/VcsProcess.ts"; |
| 13 | +import * as AzureDevOps from "./AzureDevOpsSourceControlProvider.ts"; |
| 14 | +import { probeSourceControlProvider } from "./SourceControlProviderDiscovery.ts"; |
| 15 | + |
| 16 | +const processOutput = ( |
| 17 | + stdout: string, |
| 18 | + options?: { |
| 19 | + readonly stderr?: string; |
| 20 | + readonly exitCode?: ChildProcessSpawner.ExitCode; |
| 21 | + }, |
| 22 | +): VcsProcess.VcsProcessOutput => ({ |
| 23 | + exitCode: options?.exitCode ?? ChildProcessSpawner.ExitCode(0), |
| 24 | + stdout, |
| 25 | + stderr: options?.stderr ?? "", |
| 26 | + stdoutTruncated: false, |
| 27 | + stderrTruncated: false, |
| 28 | +}); |
| 29 | + |
| 30 | +interface CapturingProcess { |
| 31 | + readonly process: VcsProcess.VcsProcess["Service"]; |
| 32 | + readonly calls: ReadonlyArray<VcsProcess.VcsProcessInput>; |
| 33 | +} |
| 34 | + |
| 35 | +const makeProcess = ( |
| 36 | + run: (input: VcsProcess.VcsProcessInput) => Effect.Effect<VcsProcess.VcsProcessOutput, VcsError>, |
| 37 | +): CapturingProcess => { |
| 38 | + const calls: VcsProcess.VcsProcessInput[] = []; |
| 39 | + return { |
| 40 | + calls, |
| 41 | + process: { |
| 42 | + run: (input) => { |
| 43 | + calls.push(input); |
| 44 | + return run(input); |
| 45 | + }, |
| 46 | + }, |
| 47 | + }; |
| 48 | +}; |
| 49 | + |
| 50 | +const isVersionCall = (input: VcsProcess.VcsProcessInput): boolean => input.args[0] === "--version"; |
| 51 | +const isAuthCall = (input: VcsProcess.VcsProcessInput): boolean => input.args[0] === "account"; |
| 52 | + |
| 53 | +it.effect( |
| 54 | + "reports Azure CLI as available and still runs the auth probe when `--version` times out", |
| 55 | + () => |
| 56 | + Effect.gen(function* () { |
| 57 | + const { process, calls } = makeProcess((input) => { |
| 58 | + if (isVersionCall(input)) { |
| 59 | + return Effect.fail( |
| 60 | + new VcsProcessTimeoutError({ |
| 61 | + operation: input.operation, |
| 62 | + command: input.command, |
| 63 | + cwd: input.cwd, |
| 64 | + timeoutMs: input.timeoutMs ?? 0, |
| 65 | + }), |
| 66 | + ); |
| 67 | + } |
| 68 | + return Effect.succeed(processOutput("[email protected]\n")); |
| 69 | + }); |
| 70 | + |
| 71 | + const item = yield* probeSourceControlProvider({ |
| 72 | + spec: AzureDevOps.discovery, |
| 73 | + process, |
| 74 | + cwd: "/repo", |
| 75 | + }); |
| 76 | + |
| 77 | + assert.strictEqual(item.status, "available"); |
| 78 | + assert.strictEqual(item.auth.status, "authenticated"); |
| 79 | + assert.deepStrictEqual(item.auth.account, Option.some("[email protected]")); |
| 80 | + assert.ok( |
| 81 | + calls.some(isAuthCall), |
| 82 | + "expected an `account show` auth probe to run after a slow `--version`", |
| 83 | + ); |
| 84 | + }), |
| 85 | +); |
| 86 | + |
| 87 | +it.effect( |
| 88 | + "reports Azure CLI as missing and skips the auth probe when `--version` cannot spawn", |
| 89 | + () => |
| 90 | + Effect.gen(function* () { |
| 91 | + const { process, calls } = makeProcess((input) => { |
| 92 | + if (isVersionCall(input)) { |
| 93 | + return Effect.fail( |
| 94 | + new VcsProcessSpawnError({ |
| 95 | + operation: input.operation, |
| 96 | + command: input.command, |
| 97 | + cwd: input.cwd, |
| 98 | + cause: Object.assign(new Error("spawn az ENOENT"), { code: "ENOENT" }), |
| 99 | + }), |
| 100 | + ); |
| 101 | + } |
| 102 | + return Effect.succeed(processOutput("[email protected]\n")); |
| 103 | + }); |
| 104 | + |
| 105 | + const item = yield* probeSourceControlProvider({ |
| 106 | + spec: AzureDevOps.discovery, |
| 107 | + process, |
| 108 | + cwd: "/repo", |
| 109 | + }); |
| 110 | + |
| 111 | + assert.strictEqual(item.status, "missing"); |
| 112 | + assert.strictEqual(item.auth.status, "unknown"); |
| 113 | + assert.strictEqual(calls.length, 1); |
| 114 | + assert.strictEqual(calls[0]?.args[0], "--version"); |
| 115 | + }), |
| 116 | +); |
| 117 | + |
| 118 | +it.effect( |
| 119 | + "raises the `--version` probe timeout above 15s and reports the version on the happy path", |
| 120 | + () => |
| 121 | + Effect.gen(function* () { |
| 122 | + const { process, calls } = makeProcess((input) => { |
| 123 | + if (isVersionCall(input)) { |
| 124 | + return Effect.succeed(processOutput("azure-cli 2.87.0\n")); |
| 125 | + } |
| 126 | + return Effect.succeed(processOutput("[email protected]\n")); |
| 127 | + }); |
| 128 | + |
| 129 | + const item = yield* probeSourceControlProvider({ |
| 130 | + spec: AzureDevOps.discovery, |
| 131 | + process, |
| 132 | + cwd: "/repo", |
| 133 | + }); |
| 134 | + |
| 135 | + const versionCall = calls.find(isVersionCall); |
| 136 | + assert.ok(versionCall, "expected a `--version` probe to run"); |
| 137 | + assert.ok( |
| 138 | + (versionCall.timeoutMs ?? 0) >= 15_000, |
| 139 | + `expected the \`--version\` probe timeout to be >= 15000ms, got ${String(versionCall.timeoutMs)}`, |
| 140 | + ); |
| 141 | + assert.deepStrictEqual(item.version, Option.some("azure-cli 2.87.0")); |
| 142 | + assert.strictEqual(item.status, "available"); |
| 143 | + assert.strictEqual(item.auth.status, "authenticated"); |
| 144 | + }), |
| 145 | +); |
| 146 | + |
| 147 | +it.effect( |
| 148 | + "reports available but unauthenticated when `--version` times out and `account show` exits non-zero", |
| 149 | + () => |
| 150 | + Effect.gen(function* () { |
| 151 | + const { process } = makeProcess((input) => { |
| 152 | + if (isVersionCall(input)) { |
| 153 | + return Effect.fail( |
| 154 | + new VcsProcessTimeoutError({ |
| 155 | + operation: input.operation, |
| 156 | + command: input.command, |
| 157 | + cwd: input.cwd, |
| 158 | + timeoutMs: input.timeoutMs ?? 0, |
| 159 | + }), |
| 160 | + ); |
| 161 | + } |
| 162 | + return Effect.succeed( |
| 163 | + processOutput("", { |
| 164 | + stderr: "ERROR: Please run 'az login' to setup account.", |
| 165 | + exitCode: ChildProcessSpawner.ExitCode(1), |
| 166 | + }), |
| 167 | + ); |
| 168 | + }); |
| 169 | + |
| 170 | + const item = yield* probeSourceControlProvider({ |
| 171 | + spec: AzureDevOps.discovery, |
| 172 | + process, |
| 173 | + cwd: "/repo", |
| 174 | + }); |
| 175 | + |
| 176 | + assert.strictEqual(item.status, "available"); |
| 177 | + assert.strictEqual(item.auth.status, "unauthenticated"); |
| 178 | + }), |
| 179 | +); |
| 180 | + |
| 181 | +it.effect( |
| 182 | + "treats a non-spawn `--version` failure (e.g. a non-zero exit) as present and still runs the auth probe", |
| 183 | + () => |
| 184 | + Effect.gen(function* () { |
| 185 | + const { process, calls } = makeProcess((input) => { |
| 186 | + if (isVersionCall(input)) { |
| 187 | + return Effect.fail( |
| 188 | + new VcsProcessExitError({ |
| 189 | + operation: input.operation, |
| 190 | + command: input.command, |
| 191 | + cwd: input.cwd, |
| 192 | + exitCode: 1, |
| 193 | + detail: "Process exited with a non-zero status.", |
| 194 | + }), |
| 195 | + ); |
| 196 | + } |
| 197 | + return Effect.succeed(processOutput("[email protected]\n")); |
| 198 | + }); |
| 199 | + |
| 200 | + const item = yield* probeSourceControlProvider({ |
| 201 | + spec: AzureDevOps.discovery, |
| 202 | + process, |
| 203 | + cwd: "/repo", |
| 204 | + }); |
| 205 | + |
| 206 | + // Only a genuine spawn failure means "missing"; any other runtime failure of |
| 207 | + // `--version` (here a non-zero exit) means the CLI is present, so it stays |
| 208 | + // available and the auth probe still runs. (issue #4) |
| 209 | + assert.strictEqual(item.status, "available"); |
| 210 | + assert.strictEqual(item.auth.status, "authenticated"); |
| 211 | + assert.ok( |
| 212 | + calls.some(isAuthCall), |
| 213 | + "expected the auth probe to run after a non-spawn `--version` failure", |
| 214 | + ); |
| 215 | + }), |
| 216 | +); |
0 commit comments