Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

AzureDevOpsCli.test.ts

309 lines10.4 KB

1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { assert, it, afterEach, describe, vi } from "@effect/vitest";
3import * as Effect from "effect/Effect";
4import * as Layer from "effect/Layer";
5import * as Option from "effect/Option";
6import * as PlatformError from "effect/PlatformError";
7import { ChildProcessSpawner } from "effect/process";
8import { VcsProcessExitError, VcsProcessSpawnError } from "@t3tools/contracts";
9​
10import type * as SourceControlHost from "@t3tools/source-control-core/server/SourceControlHost";
11import * as TestSourceControlHost from "@t3tools/source-control-testing/TestSourceControlHost";
12import * as AzureDevOpsCli from "./AzureDevOpsCli.ts";
13​
14const processOutput = (stdout: string): SourceControlHost.SourceControlProcessOutput => ({
15 exitCode: ChildProcessSpawner.ExitCode(0),
16 stdout,
17 stderr: "",
18 stdoutTruncated: false,
19 stderrTruncated: false,
20});
21​
22const mockRun = vi.fn<SourceControlHost.SourceControlHost["Service"]["process"]["run"]>();
23​
24const layerSupport = Layer.mergeAll(
25 TestSourceControlHost.layer({ process: { run: mockRun } }),
26 NodeServices.layer,
27);
28const layer = Layer.mergeAll(AzureDevOpsCli.layer.pipe(Layer.provide(layerSupport)), layerSupport);
29​
30afterEach(() => {
31 mockRun.mockReset();
32});
33​
34describe("AzureDevOpsCli.layer", () => {
35 it.effect("parses pull request view output", () =>
36 Effect.gen(function* () {
37 mockRun.mockReturnValueOnce(
38 Effect.succeed(
39 processOutput(
40 JSON.stringify({
41 pullRequestId: 42,
42 title: "Add Azure provider",
43 sourceRefName: "refs/heads/feature/source-control",
44 targetRefName: "refs/heads/main",
45 lastMergeSourceCommit: { commitId: "a".repeat(40) },
46 status: "active",
47 creationDate: "2026-01-02T00:00:00.000Z",
48 closedDate: null,
49 _links: {
50 web: {
51 href: "https://dev.azure.com/acme/project/_git/repo/pullrequest/42",
52 },
53 },
54 }),
55 ),
56 ),
57 );
58​
59 const az = yield* AzureDevOpsCli.AzureDevOpsCli;
60 const result = yield* az.getPullRequest({
61 cwd: "/repo",
62 reference: "#42",
63 });
64​
65 assert.strictEqual(result.number, 42);
66 assert.strictEqual(result.title, "Add Azure provider");
67 assert.strictEqual(result.url, "https://dev.azure.com/acme/project/_git/repo/pullrequest/42");
68 assert.strictEqual(result.baseRefName, "main");
69 assert.strictEqual(result.headRefName, "feature/source-control");
70 assert.strictEqual(result.headSha, "a".repeat(40));
71 assert.strictEqual(result.state, "open");
72 assert.deepStrictEqual(result.updatedAt._tag, Option.some(1)._tag);
73 const { classifyFailure, ...call } = mockRun.mock.calls.at(-1)?.[0] ?? {};
74 assert.strictEqual(
75 classifyFailure?.("Please run az login to setup account."),
76 "authentication",
77 );
78 assert.strictEqual(
79 classifyFailure?.("Please run 'az login' to setup account."),
80 "authentication",
81 );
82 assert.strictEqual(
83 classifyFailure?.("TF401180: The requested pull request was not found."),
84 "not-found",
85 );
86 assert.deepStrictEqual(call, {
87 operation: "AzureDevOpsCli.execute",
88 command: "az",
89 args: [
90 "repos",
91 "pr",
92 "show",
93 "--detect",
94 "true",
95 "--id",
96 "42",
97 "--only-show-errors",
98 "--output",
99 "json",
100 ],
101 cwd: "/repo",
102 notFoundDetail: "Pull request not found.",
103 timeoutMs: 30_000,
104 });
105 assert.strictEqual(result.isCrossRepository, false);
106 }).pipe(Effect.provide(layer)),
107 );
108​
109 it.effect("marks a pull request from a fork as cross-repository", () =>
110 Effect.gen(function* () {
111 mockRun.mockReturnValueOnce(
112 Effect.succeed(
113 processOutput(
114 JSON.stringify({
115 pullRequestId: 43,
116 title: "From a fork",
117 sourceRefName: "refs/heads/main",
118 targetRefName: "refs/heads/main",
119 status: "active",
120 forkSource: { name: "refs/heads/main", repository: { name: "repo-fork" } },
121 }),
122 ),
123 ),
124 );
125​
126 const az = yield* AzureDevOpsCli.AzureDevOpsCli;
127 const result = yield* az.getPullRequest({ cwd: "/repo", reference: "43" });
128​
129 assert.strictEqual(result.isCrossRepository, true);
130 }).pipe(Effect.provide(layer)),
131 );
132​
133 it.effect("builds a web URL when Azure returns only the pull request REST URL", () =>
134 Effect.gen(function* () {
135 mockRun.mockReturnValueOnce(
136 Effect.succeed(
137 processOutput(
138 JSON.stringify({
139 pullRequestId: 863,
140 title: "Fix Azure link",
141 url: "https://dev.azure.com/saplyai/a8fe4088-ad76-4eda-aaaa-e3329713a097/_apis/git/repositories/16108a25-93a3-4eff-b77d-85e894ee0fe4/pullRequests/863",
142 repository: {
143 name: "CV-engine",
144 project: {
145 name: "CV-engine",
146 },
147 },
148 sourceRefName: "refs/heads/feature/azure-pr-link",
149 targetRefName: "refs/heads/main",
150 status: "active",
151 }),
152 ),
153 ),
154 );
155​
156 const az = yield* AzureDevOpsCli.AzureDevOpsCli;
157 const result = yield* az.getPullRequest({
158 cwd: "/repo",
159 reference: "863",
160 });
161​
162 assert.strictEqual(
163 result.url,
164 "https://dev.azure.com/saplyai/CV-engine/_git/CV-engine/pullrequest/863",
165 );
166 }).pipe(Effect.provide(layer)),
167 );
168​
169 it.effect("reads repository clone URLs", () =>
170 Effect.gen(function* () {
171 mockRun.mockReturnValueOnce(
172 Effect.succeed(
173 processOutput(
174 JSON.stringify({
175 name: "repo",
176 webUrl: "https://dev.azure.com/acme/project/_git/repo",
177 remoteUrl: "https://dev.azure.com/acme/project/_git/repo",
178 sshUrl: "git@ssh.dev.azure.com:v3/acme/project/repo",
179 project: {
180 name: "project",
181 },
182 }),
183 ),
184 ),
185 );
186​
187 const az = yield* AzureDevOpsCli.AzureDevOpsCli;
188 const result = yield* az.getRepositoryCloneUrls({
189 cwd: "/repo",
190 repository: "repo",
191 });
192​
193 assert.deepStrictEqual(result, {
194 nameWithOwner: "project/repo",
195 url: "https://dev.azure.com/acme/project/_git/repo",
196 sshUrl: "git@ssh.dev.azure.com:v3/acme/project/repo",
197 });
198 }).pipe(Effect.provide(layer)),
199 );
200​
201 it.effect("looks up a project/repository path as that project's repository", () =>
202 Effect.gen(function* () {
203 mockRun.mockReturnValueOnce(
204 Effect.succeed(
205 processOutput(
206 JSON.stringify({
207 name: "repo",
208 webUrl: "https://dev.azure.com/acme/project/_git/repo",
209 remoteUrl: "https://dev.azure.com/acme/project/_git/repo",
210 sshUrl: "git@ssh.dev.azure.com:v3/acme/project/repo",
211 project: { name: "project" },
212 }),
213 ),
214 ),
215 );
216​
217 const az = yield* AzureDevOpsCli.AzureDevOpsCli;
218 yield* az.getRepositoryCloneUrls({ cwd: "/somewhere", repository: "project/repo" });
219​
220 assert.deepStrictEqual(mockRun.mock.calls.at(-1)?.[0].args, [
221 "repos",
222 "show",
223 "--detect",
224 "true",
225 "--repository",
226 "repo",
227 "--project",
228 "project",
229 "--only-show-errors",
230 "--output",
231 "json",
232 ]);
233 }).pipe(Effect.provide(layer)),
234 );
235​
236 it.effect("preserves VCS causes without copying upstream details into messages", () =>
237 Effect.gen(function* () {
238 const cause = new VcsProcessExitError({
239 operation: "AzureDevOpsCli.execute",
240 command: "az repos list --organization sensitive-upstream-detail",
241 cwd: "/repo",
242 exitCode: 1,
243 detail: "sensitive-upstream-detail",
244 });
245 mockRun.mockReturnValueOnce(Effect.fail(cause));
246​
247 const az = yield* AzureDevOpsCli.AzureDevOpsCli;
248 const error = yield* az.execute({ cwd: "/repo", args: ["repos", "list"] }).pipe(Effect.flip);
249​
250 assert.instanceOf(error, AzureDevOpsCli.AzureDevOpsCommandFailedError);
251 assert.strictEqual(error.operation, "execute");
252 assert.strictEqual(error.command, "az");
253 assert.strictEqual(error.cwd, "/repo");
254 assert.strictEqual(error.argumentCount, 2);
255 assert.strictEqual(error.detail, "Azure DevOps CLI command failed.");
256 assert.strictEqual(error.cause, cause);
257 assert.equal(error.message.includes("sensitive-upstream-detail"), false);
258 }).pipe(Effect.provide(layer)),
259 );
260​
261 it.effect("does not report a missing working directory as a missing Azure CLI", () =>
262 Effect.gen(function* () {
263 const cwd = "/missing/repo";
264 const platformCause = PlatformError.systemError({
265 _tag: "NotFound",
266 module: "ChildProcess",
267 method: "spawn",
268 syscall: "chdir",
269 pathOrDescriptor: cwd,
270 });
271 const cause = new VcsProcessSpawnError({
272 operation: "AzureDevOpsCli.execute",
273 command: "az",
274 cwd,
275 argumentCount: 2,
276 cause: platformCause,
277 });
278 mockRun.mockReturnValueOnce(Effect.fail(cause));
279​
280 const az = yield* AzureDevOpsCli.AzureDevOpsCli;
281 const error = yield* az.execute({ cwd, args: ["repos", "list"] }).pipe(Effect.flip);
282​
283 assert.instanceOf(error, AzureDevOpsCli.AzureDevOpsCommandFailedError);
284 assert.strictEqual(error.cwd, cwd);
285 assert.strictEqual(error.cause, cause);
286 }).pipe(Effect.provide(layer)),
287 );
288​
289 it.effect("preserves rate-limit failures as a distinct error", () =>
290 Effect.gen(function* () {
291 const cause = new VcsProcessExitError({
292 operation: "AzureDevOpsCli.execute",
293 command: "az",
294 cwd: "/repo",
295 argumentCount: 2,
296 exitCode: 1,
297 detail: "API rate limit exceeded.",
298 failureKind: "rate-limited",
299 });
300 mockRun.mockReturnValueOnce(Effect.fail(cause));
301​
302 const az = yield* AzureDevOpsCli.AzureDevOpsCli;
303 const error = yield* az.execute({ cwd: "/repo", args: ["repos", "list"] }).pipe(Effect.flip);
304​
305 assert.instanceOf(error, AzureDevOpsCli.AzureDevOpsCliRateLimitError);
306 assert.strictEqual(error.cause, cause);
307 }).pipe(Effect.provide(layer)),
308 );
309});