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

AzureDevOpsCli.test.ts

448 lines14.2 KB

1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { assert, it, afterEach, describe, expect, vi } from "@effect/vitest";
3import * as Effect from "effect/Effect";
4import * as FileSystem from "effect/FileSystem";
5import * as Layer from "effect/Layer";
6import * as Option from "effect/Option";
7import * as PlatformError from "effect/PlatformError";
8import { ChildProcessSpawner } from "effect/process";
9import { VcsProcessExitError, VcsProcessSpawnError } from "@t3tools/contracts";
10​
11import * as VcsProcess from "../vcs/VcsProcess.ts";
12import * as AzureDevOpsCli from "./AzureDevOpsCli.ts";
13​
14const processOutput = (stdout: string): VcsProcess.VcsProcessOutput => ({
15 exitCode: ChildProcessSpawner.ExitCode(0),
16 stdout,
17 stderr: "",
18 stdoutTruncated: false,
19 stderrTruncated: false,
20});
21​
22const mockRun = vi.fn<VcsProcess.VcsProcess["Service"]["run"]>();
23​
24const layerSupport = Layer.mergeAll(
25 Layer.mock(VcsProcess.VcsProcess)({
26 run: mockRun,
27 }),
28 NodeServices.layer,
29);
30const layer = Layer.mergeAll(AzureDevOpsCli.layer.pipe(Layer.provide(layerSupport)), layerSupport);
31​
32afterEach(() => {
33 mockRun.mockReset();
34});
35​
36describe("AzureDevOpsCli.layer", () => {
37 it.effect("parses pull request view output", () =>
38 Effect.gen(function* () {
39 mockRun.mockReturnValueOnce(
40 Effect.succeed(
41 processOutput(
42 JSON.stringify({
43 pullRequestId: 42,
44 title: "Add Azure provider",
45 sourceRefName: "refs/heads/feature/source-control",
46 targetRefName: "refs/heads/main",
47 status: "active",
48 creationDate: "2026-01-02T00:00:00.000Z",
49 closedDate: null,
50 _links: {
51 web: {
52 href: "https://dev.azure.com/acme/project/_git/repo/pullrequest/42",
53 },
54 },
55 }),
56 ),
57 ),
58 );
59​
60 const az = yield* AzureDevOpsCli.AzureDevOpsCli;
61 const result = yield* az.getPullRequest({
62 cwd: "/repo",
63 reference: "#42",
64 });
65​
66 assert.strictEqual(result.number, 42);
67 assert.strictEqual(result.title, "Add Azure provider");
68 assert.strictEqual(result.url, "https://dev.azure.com/acme/project/_git/repo/pullrequest/42");
69 assert.strictEqual(result.baseRefName, "main");
70 assert.strictEqual(result.headRefName, "feature/source-control");
71 assert.strictEqual(result.state, "open");
72 assert.deepStrictEqual(result.updatedAt._tag, Option.some(1)._tag);
73 assert.deepStrictEqual(mockRun.mock.calls.at(-1)?.[0], {
74 operation: "AzureDevOpsCli.execute",
75 command: "az",
76 args: [
77 "repos",
78 "pr",
79 "show",
80 "--detect",
81 "true",
82 "--id",
83 "42",
84 "--only-show-errors",
85 "--output",
86 "json",
87 ],
88 cwd: "/repo",
89 timeoutMs: 30_000,
90 });
91 }).pipe(Effect.provide(layer)),
92 );
93​
94 it.effect("builds a web URL when Azure returns only the pull request REST URL", () =>
95 Effect.gen(function* () {
96 mockRun.mockReturnValueOnce(
97 Effect.succeed(
98 processOutput(
99 JSON.stringify({
100 pullRequestId: 863,
101 title: "Fix Azure link",
102 url: "https://dev.azure.com/saplyai/a8fe4088-ad76-4eda-aaaa-e3329713a097/_apis/git/repositories/16108a25-93a3-4eff-b77d-85e894ee0fe4/pullRequests/863",
103 repository: {
104 name: "CV-engine",
105 project: {
106 name: "CV-engine",
107 },
108 },
109 sourceRefName: "refs/heads/feature/azure-pr-link",
110 targetRefName: "refs/heads/main",
111 status: "active",
112 }),
113 ),
114 ),
115 );
116​
117 const az = yield* AzureDevOpsCli.AzureDevOpsCli;
118 const result = yield* az.getPullRequest({
119 cwd: "/repo",
120 reference: "863",
121 });
122​
123 assert.strictEqual(
124 result.url,
125 "https://dev.azure.com/saplyai/CV-engine/_git/CV-engine/pullrequest/863",
126 );
127 }).pipe(Effect.provide(layer)),
128 );
129​
130 it.effect("lists pull requests with Azure status and source branch arguments", () =>
131 Effect.gen(function* () {
132 mockRun.mockReturnValueOnce(
133 Effect.succeed(
134 processOutput(
135 JSON.stringify([
136 {
137 pullRequestId: 7,
138 title: "Merged work",
139 sourceRefName: "refs/heads/feature/merged",
140 targetRefName: "refs/heads/main",
141 status: "completed",
142 closedDate: "2026-01-03T00:00:00.000Z",
143 _links: {
144 web: {
145 href: "https://dev.azure.com/acme/project/_git/repo/pullrequest/7",
146 },
147 },
148 },
149 ]),
150 ),
151 ),
152 );
153​
154 const az = yield* AzureDevOpsCli.AzureDevOpsCli;
155 const result = yield* az.listPullRequests({
156 cwd: "/repo",
157 headSelector: "origin:feature/merged",
158 state: "merged",
159 limit: 10,
160 });
161​
162 assert.strictEqual(result[0]?.state, "merged");
163 assert.strictEqual(result[0]?.mergedAt, "2026-01-03T00:00:00.000Z");
164 assert.strictEqual(result[0]?.closedAt, null);
165 expect(mockRun).toHaveBeenCalledWith({
166 operation: "AzureDevOpsCli.execute",
167 command: "az",
168 args: [
169 "repos",
170 "pr",
171 "list",
172 "--detect",
173 "true",
174 "--source-branch",
175 "feature/merged",
176 "--status",
177 "completed",
178 "--top",
179 "10",
180 "--only-show-errors",
181 "--output",
182 "json",
183 ],
184 cwd: "/repo",
185 timeoutMs: 30_000,
186 });
187 }).pipe(Effect.provide(layer)),
188 );
189​
190 it.effect("reads repository clone URLs", () =>
191 Effect.gen(function* () {
192 mockRun.mockReturnValueOnce(
193 Effect.succeed(
194 processOutput(
195 JSON.stringify({
196 name: "repo",
197 webUrl: "https://dev.azure.com/acme/project/_git/repo",
198 remoteUrl: "https://dev.azure.com/acme/project/_git/repo",
199 sshUrl: "git@ssh.dev.azure.com:v3/acme/project/repo",
200 project: {
201 name: "project",
202 },
203 }),
204 ),
205 ),
206 );
207​
208 const az = yield* AzureDevOpsCli.AzureDevOpsCli;
209 const result = yield* az.getRepositoryCloneUrls({
210 cwd: "/repo",
211 repository: "repo",
212 });
213​
214 assert.deepStrictEqual(result, {
215 nameWithOwner: "project/repo",
216 url: "https://dev.azure.com/acme/project/_git/repo",
217 sshUrl: "git@ssh.dev.azure.com:v3/acme/project/repo",
218 });
219 }).pipe(Effect.provide(layer)),
220 );
221​
222 it.effect("creates repositories through Azure Repos", () =>
223 Effect.gen(function* () {
224 mockRun.mockReturnValueOnce(
225 Effect.succeed(
226 processOutput(
227 JSON.stringify({
228 name: "repo",
229 webUrl: "https://dev.azure.com/acme/project/_git/repo",
230 remoteUrl: "https://dev.azure.com/acme/project/_git/repo",
231 sshUrl: "git@ssh.dev.azure.com:v3/acme/project/repo",
232 project: {
233 name: "project",
234 },
235 }),
236 ),
237 ),
238 );
239​
240 const az = yield* AzureDevOpsCli.AzureDevOpsCli;
241 const result = yield* az.createRepository({
242 cwd: "/repo",
243 repository: "project/repo",
244 visibility: "private",
245 });
246​
247 assert.deepStrictEqual(result, {
248 nameWithOwner: "project/repo",
249 url: "https://dev.azure.com/acme/project/_git/repo",
250 sshUrl: "git@ssh.dev.azure.com:v3/acme/project/repo",
251 });
252 expect(mockRun).toHaveBeenCalledWith({
253 operation: "AzureDevOpsCli.execute",
254 command: "az",
255 args: [
256 "repos",
257 "create",
258 "--detect",
259 "true",
260 "--name",
261 "repo",
262 "--project",
263 "project",
264 "--only-show-errors",
265 "--output",
266 "json",
267 ],
268 cwd: "/repo",
269 timeoutMs: 30_000,
270 });
271 }).pipe(Effect.provide(layer)),
272 );
273​
274 it.effect("creates pull requests using the body file as the Azure description", () =>
275 Effect.gen(function* () {
276 const fileSystem = yield* FileSystem.FileSystem;
277 const bodyFile = `/tmp/t3code-azure-devops-cli-.md`;
278 yield* fileSystem.writeFileString(bodyFile, "Generated body");
279 mockRun.mockReturnValueOnce(Effect.succeed(processOutput("{}")));
280​
281 const az = yield* AzureDevOpsCli.AzureDevOpsCli;
282 yield* az.createPullRequest({
283 cwd: "/repo",
284 baseBranch: "main",
285 headSelector: "feature/provider",
286 title: "Provider PR",
287 bodyFile,
288 });
289​
290 expect(mockRun).toHaveBeenCalledWith(
291 expect.objectContaining({
292 command: "az",
293 cwd: "/repo",
294 args: expect.arrayContaining(["--description", `@${bodyFile}`]),
295 }),
296 );
297 expect(mockRun.mock.calls[0]?.[0].args).not.toContain("--output");
298 }).pipe(Effect.provide(layer)),
299 );
300​
301 it.effect("does not force JSON output on checkout side-effect commands", () =>
302 Effect.gen(function* () {
303 mockRun.mockReturnValueOnce(Effect.succeed(processOutput("")));
304​
305 const az = yield* AzureDevOpsCli.AzureDevOpsCli;
306 yield* az.checkoutPullRequest({
307 cwd: "/repo",
308 reference: "42",
309 });
310​
311 expect(mockRun).toHaveBeenCalledWith({
312 operation: "AzureDevOpsCli.execute",
313 command: "az",
314 args: [
315 "repos",
316 "pr",
317 "checkout",
318 "--only-show-errors",
319 "--detect",
320 "true",
321 "--id",
322 "42",
323 "--remote-name",
324 "origin",
325 ],
326 cwd: "/repo",
327 timeoutMs: 30_000,
328 });
329 }).pipe(Effect.provide(layer)),
330 );
331​
332 it.effect("forwards explicit output limits to the process boundary", () =>
333 Effect.gen(function* () {
334 mockRun.mockReturnValueOnce(Effect.succeed(processOutput("")));
335​
336 const az = yield* AzureDevOpsCli.AzureDevOpsCli;
337 yield* az.execute({
338 cwd: "/repo",
339 args: ["repos", "pr", "list"],
340 maxOutputBytes: 16 * 1024 * 1024,
341 });
342​
343 expect(mockRun).toHaveBeenCalledWith({
344 operation: "AzureDevOpsCli.execute",
345 command: "az",
346 args: ["repos", "pr", "list"],
347 cwd: "/repo",
348 timeoutMs: 30_000,
349 maxOutputBytes: 16 * 1024 * 1024,
350 });
351 }).pipe(Effect.provide(layer)),
352 );
353​
354 it.effect("preserves VCS causes without copying upstream details into messages", () =>
355 Effect.gen(function* () {
356 const cause = new VcsProcessExitError({
357 operation: "AzureDevOpsCli.execute",
358 command: "az repos list --organization sensitive-upstream-detail",
359 cwd: "/repo",
360 exitCode: 1,
361 detail: "sensitive-upstream-detail",
362 });
363 mockRun.mockReturnValueOnce(Effect.fail(cause));
364​
365 const az = yield* AzureDevOpsCli.AzureDevOpsCli;
366 const error = yield* az.execute({ cwd: "/repo", args: ["repos", "list"] }).pipe(Effect.flip);
367​
368 assert.instanceOf(error, AzureDevOpsCli.AzureDevOpsCommandFailedError);
369 assert.strictEqual(error.operation, "execute");
370 assert.strictEqual(error.command, "az");
371 assert.strictEqual(error.cwd, "/repo");
372 assert.strictEqual(error.argumentCount, 2);
373 assert.strictEqual(error.detail, "Azure DevOps CLI command failed.");
374 assert.strictEqual(error.cause, cause);
375 assert.equal(error.message.includes("sensitive-upstream-detail"), false);
376 }).pipe(Effect.provide(layer)),
377 );
378​
379 it.effect("does not report a missing working directory as a missing Azure CLI", () =>
380 Effect.gen(function* () {
381 const cwd = "/missing/repo";
382 const platformCause = PlatformError.systemError({
383 _tag: "NotFound",
384 module: "ChildProcess",
385 method: "spawn",
386 syscall: "chdir",
387 pathOrDescriptor: cwd,
388 });
389 const cause = new VcsProcessSpawnError({
390 operation: "AzureDevOpsCli.execute",
391 command: "az",
392 cwd,
393 argumentCount: 2,
394 cause: platformCause,
395 });
396 mockRun.mockReturnValueOnce(Effect.fail(cause));
397​
398 const az = yield* AzureDevOpsCli.AzureDevOpsCli;
399 const error = yield* az.execute({ cwd, args: ["repos", "list"] }).pipe(Effect.flip);
400​
401 assert.instanceOf(error, AzureDevOpsCli.AzureDevOpsCommandFailedError);
402 assert.strictEqual(error.cwd, cwd);
403 assert.strictEqual(error.cause, cause);
404 }).pipe(Effect.provide(layer)),
405 );
406​
407 it.effect("keeps invalid pull request output diagnostics structured", () =>
408 Effect.gen(function* () {
409 mockRun.mockReturnValueOnce(Effect.succeed(processOutput("not-json")));
410​
411 const az = yield* AzureDevOpsCli.AzureDevOpsCli;
412 const error = yield* az.getPullRequest({ cwd: "/repo", reference: "42" }).pipe(Effect.flip);
413​
414 assert.instanceOf(error, AzureDevOpsCli.AzureDevOpsPullRequestDecodeError);
415 assert.strictEqual(error.operation, "getPullRequest");
416 assert.strictEqual(error.command, "az");
417 assert.strictEqual(error.cwd, "/repo");
418 assert.strictEqual(error.outputLength, 8);
419 assert.strictEqual(error.detail, "Azure DevOps CLI returned invalid pull request JSON.");
420 assert.exists(error.cause);
421 assert.strictEqual(
422 error.message,
423 "Azure DevOps CLI failed in getPullRequest: Azure DevOps CLI returned invalid pull request JSON.",
424 );
425 }).pipe(Effect.provide(layer)),
426 );
427​
428 it.effect("preserves rate-limit failures as a distinct error", () =>
429 Effect.gen(function* () {
430 const cause = new VcsProcessExitError({
431 operation: "AzureDevOpsCli.execute",
432 command: "az",
433 cwd: "/repo",
434 argumentCount: 2,
435 exitCode: 1,
436 detail: "API rate limit exceeded.",
437 failureKind: "rate-limited",
438 });
439 mockRun.mockReturnValueOnce(Effect.fail(cause));
440​
441 const az = yield* AzureDevOpsCli.AzureDevOpsCli;
442 const error = yield* az.execute({ cwd: "/repo", args: ["repos", "list"] }).pipe(Effect.flip);
443​
444 assert.instanceOf(error, AzureDevOpsCli.AzureDevOpsCliRateLimitError);
445 assert.strictEqual(error.cause, cause);
446 }).pipe(Effect.provide(layer)),
447 );
448});