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 { ChildProcessSpawner } from "effect/unstable/process";
8
9import * as VcsProcess from "../vcs/VcsProcess.ts";
10import * as AzureDevOpsCli from "./AzureDevOpsCli.ts";
11
12const processOutput = (stdout: string): VcsProcess.VcsProcessOutput => ({
13 exitCode: ChildProcessSpawner.ExitCode(0),
14 stdout,
15 stderr: "",
16 stdoutTruncated: false,
17 stderrTruncated: false,
18});
19
20const mockRun = vi.fn<VcsProcess.VcsProcessShape["run"]>();
21
22const supportLayer = Layer.mergeAll(
23 Layer.mock(VcsProcess.VcsProcess)({
24 run: mockRun,
25 }),
26 NodeServices.layer,
27);
28const layer = Layer.mergeAll(AzureDevOpsCli.layer.pipe(Layer.provide(supportLayer)), supportLayer);
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 // @effect-diagnostics-next-line preferSchemaOverJson:off
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 // @effect-diagnostics-next-line preferSchemaOverJson:off
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 // @effect-diagnostics-next-line preferSchemaOverJson:off
136 JSON.stringify([
137 {
138 pullRequestId: 7,
139 title: "Merged work",
140 sourceRefName: "refs/heads/feature/merged",
141 targetRefName: "refs/heads/main",
142 status: "completed",
143 closedDate: "2026-01-03T00:00:00.000Z",
144 _links: {
145 web: {
146 href: "https://dev.azure.com/acme/project/_git/repo/pullrequest/7",
147 },
148 },
149 },
150 ]),
151 ),
152 ),
153 );
154
155 const az = yield* AzureDevOpsCli.AzureDevOpsCli;
156 const result = yield* az.listPullRequests({
157 cwd: "/repo",
158 headSelector: "origin:feature/merged",
159 state: "merged",
160 limit: 10,
161 });
162
163 assert.strictEqual(result[0]?.state, "merged");
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 // @effect-diagnostics-next-line preferSchemaOverJson:off
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 // @effect-diagnostics-next-line preferSchemaOverJson:off
228 JSON.stringify({
229 name: "repo",
230 webUrl: "https://dev.azure.com/acme/project/_git/repo",
231 remoteUrl: "https://dev.azure.com/acme/project/_git/repo",
232 sshUrl: "git@ssh.dev.azure.com:v3/acme/project/repo",
233 project: {
234 name: "project",
235 },
236 }),
237 ),
238 ),
239 );
240
241 const az = yield* AzureDevOpsCli.AzureDevOpsCli;
242 const result = yield* az.createRepository({
243 cwd: "/repo",
244 repository: "project/repo",
245 visibility: "private",
246 });
247
248 assert.deepStrictEqual(result, {
249 nameWithOwner: "project/repo",
250 url: "https://dev.azure.com/acme/project/_git/repo",
251 sshUrl: "git@ssh.dev.azure.com:v3/acme/project/repo",
252 });
253 expect(mockRun).toHaveBeenCalledWith({
254 operation: "AzureDevOpsCli.execute",
255 command: "az",
256 args: [
257 "repos",
258 "create",
259 "--detect",
260 "true",
261 "--name",
262 "repo",
263 "--project",
264 "project",
265 "--only-show-errors",
266 "--output",
267 "json",
268 ],
269 cwd: "/repo",
270 timeoutMs: 30_000,
271 });
272 }).pipe(Effect.provide(layer)),
273 );
274
275 it.effect("creates pull requests using the body file as the Azure description", () =>
276 Effect.gen(function* () {
277 const fileSystem = yield* FileSystem.FileSystem;
278 const bodyFile = `/tmp/t3code-azure-devops-cli-.md`;
279 yield* fileSystem.writeFileString(bodyFile, "Generated body");
280 mockRun.mockReturnValueOnce(Effect.succeed(processOutput("{}")));
281
282 const az = yield* AzureDevOpsCli.AzureDevOpsCli;
283 yield* az.createPullRequest({
284 cwd: "/repo",
285 baseBranch: "main",
286 headSelector: "feature/provider",
287 title: "Provider PR",
288 bodyFile,
289 });
290
291 expect(mockRun).toHaveBeenCalledWith(
292 expect.objectContaining({
293 command: "az",
294 cwd: "/repo",
295 args: expect.arrayContaining(["--description", `@${bodyFile}`]),
296 }),
297 );
298 expect(mockRun.mock.calls[0]?.[0].args).not.toContain("--output");
299 }).pipe(Effect.provide(layer)),
300 );
301
302 it.effect("does not force JSON output on checkout side-effect commands", () =>
303 Effect.gen(function* () {
304 mockRun.mockReturnValueOnce(Effect.succeed(processOutput("")));
305
306 const az = yield* AzureDevOpsCli.AzureDevOpsCli;
307 yield* az.checkoutPullRequest({
308 cwd: "/repo",
309 reference: "42",
310 });
311
312 expect(mockRun).toHaveBeenCalledWith({
313 operation: "AzureDevOpsCli.execute",
314 command: "az",
315 args: [
316 "repos",
317 "pr",
318 "checkout",
319 "--only-show-errors",
320 "--detect",
321 "true",
322 "--id",
323 "42",
324 "--remote-name",
325 "origin",
326 ],
327 cwd: "/repo",
328 timeoutMs: 30_000,
329 });
330 }).pipe(Effect.provide(layer)),
331 );
332});