Open History to see the last commit
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 expect(mockRun).toHaveBeenCalledWith({
167 operation: "AzureDevOpsCli.execute",
168 command: "az",
169 args: [
170 "repos",
171 "pr",
172 "list",
173 "--detect",
174 "true",
175 "--source-branch",
176 "feature/merged",
177 "--status",
178 "completed",
179 "--top",
180 "10",
181 "--only-show-errors",
182 "--output",
183 "json",
184 ],
185 cwd: "/repo",
186 timeoutMs: 30_000,
187 });
188 }).pipe(Effect.provide(layer)),
189 );
190
191 it.effect("reads repository clone URLs", () =>
192 Effect.gen(function* () {
193 mockRun.mockReturnValueOnce(
194 Effect.succeed(
195 processOutput(
196 // @effect-diagnostics-next-line preferSchemaOverJson:off
197 JSON.stringify({
198 name: "repo",
199 webUrl: "https://dev.azure.com/acme/project/_git/repo",
200 remoteUrl: "https://dev.azure.com/acme/project/_git/repo",
201 sshUrl: "git@ssh.dev.azure.com:v3/acme/project/repo",
202 project: {
203 name: "project",
204 },
205 }),
206 ),
207 ),
208 );
209
210 const az = yield* AzureDevOpsCli.AzureDevOpsCli;
211 const result = yield* az.getRepositoryCloneUrls({
212 cwd: "/repo",
213 repository: "repo",
214 });
215
216 assert.deepStrictEqual(result, {
217 nameWithOwner: "project/repo",
218 url: "https://dev.azure.com/acme/project/_git/repo",
219 sshUrl: "git@ssh.dev.azure.com:v3/acme/project/repo",
220 });
221 }).pipe(Effect.provide(layer)),
222 );
223
224 it.effect("creates repositories through Azure Repos", () =>
225 Effect.gen(function* () {
226 mockRun.mockReturnValueOnce(
227 Effect.succeed(
228 processOutput(
229 // @effect-diagnostics-next-line preferSchemaOverJson:off
230 JSON.stringify({
231 name: "repo",
232 webUrl: "https://dev.azure.com/acme/project/_git/repo",
233 remoteUrl: "https://dev.azure.com/acme/project/_git/repo",
234 sshUrl: "git@ssh.dev.azure.com:v3/acme/project/repo",
235 project: {
236 name: "project",
237 },
238 }),
239 ),
240 ),
241 );
242
243 const az = yield* AzureDevOpsCli.AzureDevOpsCli;
244 const result = yield* az.createRepository({
245 cwd: "/repo",
246 repository: "project/repo",
247 visibility: "private",
248 });
249
250 assert.deepStrictEqual(result, {
251 nameWithOwner: "project/repo",
252 url: "https://dev.azure.com/acme/project/_git/repo",
253 sshUrl: "git@ssh.dev.azure.com:v3/acme/project/repo",
254 });
255 expect(mockRun).toHaveBeenCalledWith({
256 operation: "AzureDevOpsCli.execute",
257 command: "az",
258 args: [
259 "repos",
260 "create",
261 "--detect",
262 "true",
263 "--name",
264 "repo",
265 "--project",
266 "project",
267 "--only-show-errors",
268 "--output",
269 "json",
270 ],
271 cwd: "/repo",
272 timeoutMs: 30_000,
273 });
274 }).pipe(Effect.provide(layer)),
275 );
276
277 it.effect("creates pull requests using the body file as the Azure description", () =>
278 Effect.gen(function* () {
279 const fileSystem = yield* FileSystem.FileSystem;
280 const bodyFile = `/tmp/t3code-azure-devops-cli-.md`;
281 yield* fileSystem.writeFileString(bodyFile, "Generated body");
282 mockRun.mockReturnValueOnce(Effect.succeed(processOutput("{}")));
283
284 const az = yield* AzureDevOpsCli.AzureDevOpsCli;
285 yield* az.createPullRequest({
286 cwd: "/repo",
287 baseBranch: "main",
288 headSelector: "feature/provider",
289 title: "Provider PR",
290 bodyFile,
291 });
292
293 expect(mockRun).toHaveBeenCalledWith(
294 expect.objectContaining({
295 command: "az",
296 cwd: "/repo",
297 args: expect.arrayContaining(["--description", `@${bodyFile}`]),
298 }),
299 );
300 expect(mockRun.mock.calls[0]?.[0].args).not.toContain("--output");
301 }).pipe(Effect.provide(layer)),
302 );
303
304 it.effect("does not force JSON output on checkout side-effect commands", () =>
305 Effect.gen(function* () {
306 mockRun.mockReturnValueOnce(Effect.succeed(processOutput("")));
307
308 const az = yield* AzureDevOpsCli.AzureDevOpsCli;
309 yield* az.checkoutPullRequest({
310 cwd: "/repo",
311 reference: "42",
312 });
313
314 expect(mockRun).toHaveBeenCalledWith({
315 operation: "AzureDevOpsCli.execute",
316 command: "az",
317 args: [
318 "repos",
319 "pr",
320 "checkout",
321 "--only-show-errors",
322 "--detect",
323 "true",
324 "--id",
325 "42",
326 "--remote-name",
327 "origin",
328 ],
329 cwd: "/repo",
330 timeoutMs: 30_000,
331 });
332 }).pipe(Effect.provide(layer)),
333 );
334
335 it.effect("forwards explicit output limits to the process boundary", () =>
336 Effect.gen(function* () {
337 mockRun.mockReturnValueOnce(Effect.succeed(processOutput("")));
338
339 const az = yield* AzureDevOpsCli.AzureDevOpsCli;
340 yield* az.execute({
341 cwd: "/repo",
342 args: ["repos", "pr", "list"],
343 maxOutputBytes: 16 * 1024 * 1024,
344 });
345
346 expect(mockRun).toHaveBeenCalledWith({
347 operation: "AzureDevOpsCli.execute",
348 command: "az",
349 args: ["repos", "pr", "list"],
350 cwd: "/repo",
351 timeoutMs: 30_000,
352 maxOutputBytes: 16 * 1024 * 1024,
353 });
354 }).pipe(Effect.provide(layer)),
355 );
356
357 it.effect("preserves VCS causes without copying upstream details into messages", () =>
358 Effect.gen(function* () {
359 const cause = new VcsProcessExitError({
360 operation: "AzureDevOpsCli.execute",
361 command: "az repos list --organization sensitive-upstream-detail",
362 cwd: "/repo",
363 exitCode: 1,
364 detail: "sensitive-upstream-detail",
365 });
366 mockRun.mockReturnValueOnce(Effect.fail(cause));
367
368 const az = yield* AzureDevOpsCli.AzureDevOpsCli;
369 const error = yield* az.execute({ cwd: "/repo", args: ["repos", "list"] }).pipe(Effect.flip);
370
371 assert.instanceOf(error, AzureDevOpsCli.AzureDevOpsCommandFailedError);
372 assert.strictEqual(error.operation, "execute");
373 assert.strictEqual(error.command, "az");
374 assert.strictEqual(error.cwd, "/repo");
375 assert.strictEqual(error.argumentCount, 2);
376 assert.strictEqual(error.detail, "Azure DevOps CLI command failed.");
377 assert.strictEqual(error.cause, cause);
378 assert.equal(error.message.includes("sensitive-upstream-detail"), false);
379 }).pipe(Effect.provide(layer)),
380 );
381
382 it.effect("does not report a missing working directory as a missing Azure CLI", () =>
383 Effect.gen(function* () {
384 const cwd = "/missing/repo";
385 const platformCause = PlatformError.systemError({
386 _tag: "NotFound",
387 module: "ChildProcess",
388 method: "spawn",
389 syscall: "chdir",
390 pathOrDescriptor: cwd,
391 });
392 const cause = new VcsProcessSpawnError({
393 operation: "AzureDevOpsCli.execute",
394 command: "az",
395 cwd,
396 argumentCount: 2,
397 cause: platformCause,
398 });
399 mockRun.mockReturnValueOnce(Effect.fail(cause));
400
401 const az = yield* AzureDevOpsCli.AzureDevOpsCli;
402 const error = yield* az.execute({ cwd, args: ["repos", "list"] }).pipe(Effect.flip);
403
404 assert.instanceOf(error, AzureDevOpsCli.AzureDevOpsCommandFailedError);
405 assert.strictEqual(error.cwd, cwd);
406 assert.strictEqual(error.cause, cause);
407 }).pipe(Effect.provide(layer)),
408 );
409
410 it.effect("keeps invalid pull request output diagnostics structured", () =>
411 Effect.gen(function* () {
412 mockRun.mockReturnValueOnce(Effect.succeed(processOutput("not-json")));
413
414 const az = yield* AzureDevOpsCli.AzureDevOpsCli;
415 const error = yield* az.getPullRequest({ cwd: "/repo", reference: "42" }).pipe(Effect.flip);
416
417 assert.instanceOf(error, AzureDevOpsCli.AzureDevOpsPullRequestDecodeError);
418 assert.strictEqual(error.operation, "getPullRequest");
419 assert.strictEqual(error.command, "az");
420 assert.strictEqual(error.cwd, "/repo");
421 assert.strictEqual(error.outputLength, 8);
422 assert.strictEqual(error.detail, "Azure DevOps CLI returned invalid pull request JSON.");
423 assert.exists(error.cause);
424 assert.strictEqual(
425 error.message,
426 "Azure DevOps CLI failed in getPullRequest: Azure DevOps CLI returned invalid pull request JSON.",
427 );
428 }).pipe(Effect.provide(layer)),
429 );
430
431 it.effect("preserves rate-limit failures as a distinct error", () =>
432 Effect.gen(function* () {
433 const cause = new VcsProcessExitError({
434 operation: "AzureDevOpsCli.execute",
435 command: "az",
436 cwd: "/repo",
437 argumentCount: 2,
438 exitCode: 1,
439 detail: "API rate limit exceeded.",
440 failureKind: "rate-limited",
441 });
442 mockRun.mockReturnValueOnce(Effect.fail(cause));
443
444 const az = yield* AzureDevOpsCli.AzureDevOpsCli;
445 const error = yield* az.execute({ cwd: "/repo", args: ["repos", "list"] }).pipe(Effect.flip);
446
447 assert.instanceOf(error, AzureDevOpsCli.AzureDevOpsCliRateLimitError);
448 assert.strictEqual(error.cause, cause);
449 }).pipe(Effect.provide(layer)),
450 );
451});