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

AzureDevOpsCli.test.ts

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