|
| 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 { type VcsError, VcsProcessSpawnError, VcsProcessTimeoutError } from "@t3tools/contracts"; |
| 6 | + |
| 7 | +import * as VcsProcess from "../vcs/VcsProcess.ts"; |
| 8 | +import * as AzureDevOps from "./AzureDevOpsSourceControlProvider.ts"; |
| 9 | +import { probeSourceControlProvider } from "./SourceControlProviderDiscovery.ts"; |
| 10 | + |
| 11 | +const processOutput = ( |
| 12 | + stdout: string, |
| 13 | + options?: { |
| 14 | + readonly stderr?: string; |
| 15 | + readonly exitCode?: ChildProcessSpawner.ExitCode; |
| 16 | + }, |
| 17 | +): VcsProcess.VcsProcessOutput => ({ |
| 18 | + exitCode: options?.exitCode ?? ChildProcessSpawner.ExitCode(0), |
| 19 | + stdout, |
| 20 | + stderr: options?.stderr ?? "", |
| 21 | + stdoutTruncated: false, |
| 22 | + stderrTruncated: false, |
| 23 | +}); |
| 24 | + |
| 25 | +interface CapturingProcess { |
| 26 | + readonly process: VcsProcess.VcsProcess["Service"]; |
| 27 | + readonly calls: ReadonlyArray<VcsProcess.VcsProcessInput>; |
| 28 | +} |
| 29 | + |
| 30 | +const makeProcess = ( |
| 31 | + run: (input: VcsProcess.VcsProcessInput) => Effect.Effect<VcsProcess.VcsProcessOutput, VcsError>, |
| 32 | +): CapturingProcess => { |
| 33 | + const calls: VcsProcess.VcsProcessInput[] = []; |
| 34 | + return { |
| 35 | + calls, |
| 36 | + process: { |
| 37 | + run: (input) => { |
| 38 | + calls.push(input); |
| 39 | + return run(input); |
| 40 | + }, |
| 41 | + }, |
| 42 | + }; |
| 43 | +}; |
| 44 | + |
| 45 | +const isVersionCall = (input: VcsProcess.VcsProcessInput): boolean => input.args[0] === "--version"; |
| 46 | +const isAuthCall = (input: VcsProcess.VcsProcessInput): boolean => input.args[0] === "account"; |
| 47 | + |
| 48 | +it.effect( |
| 49 | + "reports Azure CLI as available and still runs the auth probe when `--version` times out", |
| 50 | + () => |
| 51 | + Effect.gen(function* () { |
| 52 | + const { process, calls } = makeProcess((input) => { |
| 53 | + if (isVersionCall(input)) { |
| 54 | + return Effect.fail( |
| 55 | + new VcsProcessTimeoutError({ |
| 56 | + operation: input.operation, |
| 57 | + command: input.command, |
| 58 | + cwd: input.cwd, |
| 59 | + timeoutMs: input.timeoutMs ?? 0, |
| 60 | + }), |
| 61 | + ); |
| 62 | + } |
| 63 | + return Effect.succeed(processOutput("[email protected]\n")); |
| 64 | + }); |
| 65 | + |
| 66 | + const item = yield* probeSourceControlProvider({ |
| 67 | + spec: AzureDevOps.discovery, |
| 68 | + process, |
| 69 | + cwd: "/repo", |
| 70 | + }); |
| 71 | + |
| 72 | + assert.strictEqual(item.status, "available"); |
| 73 | + assert.strictEqual(item.auth.status, "authenticated"); |
| 74 | + assert.deepStrictEqual(item.auth.account, Option.some("[email protected]")); |
| 75 | + assert.ok( |
| 76 | + calls.some(isAuthCall), |
| 77 | + "expected an `account show` auth probe to run after a slow `--version`", |
| 78 | + ); |
| 79 | + }), |
| 80 | +); |
| 81 | + |
| 82 | +it.effect( |
| 83 | + "reports Azure CLI as missing and skips the auth probe when `--version` cannot spawn", |
| 84 | + () => |
| 85 | + Effect.gen(function* () { |
| 86 | + const { process, calls } = makeProcess((input) => { |
| 87 | + if (isVersionCall(input)) { |
| 88 | + return Effect.fail( |
| 89 | + new VcsProcessSpawnError({ |
| 90 | + operation: input.operation, |
| 91 | + command: input.command, |
| 92 | + cwd: input.cwd, |
| 93 | + cause: Object.assign(new Error("spawn az ENOENT"), { code: "ENOENT" }), |
| 94 | + }), |
| 95 | + ); |
| 96 | + } |
| 97 | + return Effect.succeed(processOutput("[email protected]\n")); |
| 98 | + }); |
| 99 | + |
| 100 | + const item = yield* probeSourceControlProvider({ |
| 101 | + spec: AzureDevOps.discovery, |
| 102 | + process, |
| 103 | + cwd: "/repo", |
| 104 | + }); |
| 105 | + |
| 106 | + assert.strictEqual(item.status, "missing"); |
| 107 | + assert.strictEqual(item.auth.status, "unknown"); |
| 108 | + assert.strictEqual(calls.length, 1); |
| 109 | + assert.strictEqual(calls[0]?.args[0], "--version"); |
| 110 | + }), |
| 111 | +); |
| 112 | + |
| 113 | +it.effect( |
| 114 | + "gives the Azure CLI a `--version` probe budget well above its ~6s Windows startup, and reports the version on the happy path", |
| 115 | + () => |
| 116 | + Effect.gen(function* () { |
| 117 | + const { process, calls } = makeProcess((input) => { |
| 118 | + if (isVersionCall(input)) { |
| 119 | + return Effect.succeed(processOutput("azure-cli 2.87.0\n")); |
| 120 | + } |
| 121 | + return Effect.succeed(processOutput("[email protected]\n")); |
| 122 | + }); |
| 123 | + |
| 124 | + const item = yield* probeSourceControlProvider({ |
| 125 | + spec: AzureDevOps.discovery, |
| 126 | + process, |
| 127 | + cwd: "/repo", |
| 128 | + }); |
| 129 | + |
| 130 | + const versionCall = calls.find(isVersionCall); |
| 131 | + assert.ok(versionCall, "expected a `--version` probe to run"); |
| 132 | + // Upstream owns the budget itself, via `probeTimeoutMs` on the Azure spec (#6223). |
| 133 | + // This guards the property issue #4 actually depends on: that az's budget stays |
| 134 | + // clear of its ~6-8s Windows startup, rather than falling back to the 5s default. |
| 135 | + assert.ok( |
| 136 | + (versionCall.timeoutMs ?? 0) >= 15_000, |
| 137 | + `expected the \`--version\` probe timeout to be >= 15000ms, got ${String(versionCall.timeoutMs)}`, |
| 138 | + ); |
| 139 | + assert.deepStrictEqual(item.version, Option.some("azure-cli 2.87.0")); |
| 140 | + assert.strictEqual(item.status, "available"); |
| 141 | + assert.strictEqual(item.auth.status, "authenticated"); |
| 142 | + }), |
| 143 | +); |
| 144 | + |
| 145 | +it.effect( |
| 146 | + "reports available but unauthenticated when `--version` times out and `account show` exits non-zero", |
| 147 | + () => |
| 148 | + Effect.gen(function* () { |
| 149 | + const { process } = makeProcess((input) => { |
| 150 | + if (isVersionCall(input)) { |
| 151 | + return Effect.fail( |
| 152 | + new VcsProcessTimeoutError({ |
| 153 | + operation: input.operation, |
| 154 | + command: input.command, |
| 155 | + cwd: input.cwd, |
| 156 | + timeoutMs: input.timeoutMs ?? 0, |
| 157 | + }), |
| 158 | + ); |
| 159 | + } |
| 160 | + return Effect.succeed( |
| 161 | + processOutput("", { |
| 162 | + stderr: "ERROR: Please run 'az login' to setup account.", |
| 163 | + exitCode: ChildProcessSpawner.ExitCode(1), |
| 164 | + }), |
| 165 | + ); |
| 166 | + }); |
| 167 | + |
| 168 | + const item = yield* probeSourceControlProvider({ |
| 169 | + spec: AzureDevOps.discovery, |
| 170 | + process, |
| 171 | + cwd: "/repo", |
| 172 | + }); |
| 173 | + |
| 174 | + assert.strictEqual(item.status, "available"); |
| 175 | + assert.strictEqual(item.auth.status, "unauthenticated"); |
| 176 | + }), |
| 177 | +); |
0 commit comments