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