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.baseRefName, "main");
68 assert.strictEqual(result.headRefName, "feature/source-control");
69 assert.strictEqual(result.state, "open");
70 assert.deepStrictEqual(result.updatedAt._tag, Option.some(1)._tag);
71 assert.deepStrictEqual(mockRun.mock.calls.at(-1)?.[0], {
72 operation: "AzureDevOpsCli.execute",
73 command: "az",
74 args: [
75 "repos",
76 "pr",
77 "show",
78 "--detect",
79 "true",
80 "--id",
81 "42",
82 "--only-show-errors",
83 "--output",
84 "json",
85 ],
86 cwd: "/repo",
87 timeoutMs: 30_000,
88 });
89 }).pipe(Effect.provide(layer)),
90 );
91
92 it.effect("lists pull requests with Azure status and source branch arguments", () =>
93 Effect.gen(function* () {
94 mockRun.mockReturnValueOnce(
95 Effect.succeed(
96 processOutput(
97 // @effect-diagnostics-next-line preferSchemaOverJson:off
98 JSON.stringify([
99 {
100 pullRequestId: 7,
101 title: "Merged work",
102 sourceRefName: "refs/heads/feature/merged",
103 targetRefName: "refs/heads/main",
104 status: "completed",
105 closedDate: "2026-01-03T00:00:00.000Z",
106 _links: {
107 web: {
108 href: "https://dev.azure.com/acme/project/_git/repo/pullrequest/7",
109 },
110 },
111 },
112 ]),
113 ),
114 ),
115 );
116
117 const az = yield* AzureDevOpsCli.AzureDevOpsCli;
118 const result = yield* az.listPullRequests({
119 cwd: "/repo",
120 headSelector: "origin:feature/merged",
121 state: "merged",
122 limit: 10,
123 });
124
125 assert.strictEqual(result[0]?.state, "merged");
126 expect(mockRun).toHaveBeenCalledWith({
127 operation: "AzureDevOpsCli.execute",
128 command: "az",
129 args: [
130 "repos",
131 "pr",
132 "list",
133 "--detect",
134 "true",
135 "--source-branch",
136 "feature/merged",
137 "--status",
138 "completed",
139 "--top",
140 "10",
141 "--only-show-errors",
142 "--output",
143 "json",
144 ],
145 cwd: "/repo",
146 timeoutMs: 30_000,
147 });
148 }).pipe(Effect.provide(layer)),
149 );
150
151 it.effect("reads repository clone URLs", () =>
152 Effect.gen(function* () {
153 mockRun.mockReturnValueOnce(
154 Effect.succeed(
155 processOutput(
156 // @effect-diagnostics-next-line preferSchemaOverJson:off
157 JSON.stringify({
158 name: "repo",
159 webUrl: "https://dev.azure.com/acme/project/_git/repo",
160 remoteUrl: "https://dev.azure.com/acme/project/_git/repo",
161 sshUrl: "git@ssh.dev.azure.com:v3/acme/project/repo",
162 project: {
163 name: "project",
164 },
165 }),
166 ),
167 ),
168 );
169
170 const az = yield* AzureDevOpsCli.AzureDevOpsCli;
171 const result = yield* az.getRepositoryCloneUrls({
172 cwd: "/repo",
173 repository: "repo",
174 });
175
176 assert.deepStrictEqual(result, {
177 nameWithOwner: "project/repo",
178 url: "https://dev.azure.com/acme/project/_git/repo",
179 sshUrl: "git@ssh.dev.azure.com:v3/acme/project/repo",
180 });
181 }).pipe(Effect.provide(layer)),
182 );
183
184 it.effect("creates repositories through Azure Repos", () =>
185 Effect.gen(function* () {
186 mockRun.mockReturnValueOnce(
187 Effect.succeed(
188 processOutput(
189 // @effect-diagnostics-next-line preferSchemaOverJson:off
190 JSON.stringify({
191 name: "repo",
192 webUrl: "https://dev.azure.com/acme/project/_git/repo",
193 remoteUrl: "https://dev.azure.com/acme/project/_git/repo",
194 sshUrl: "git@ssh.dev.azure.com:v3/acme/project/repo",
195 project: {
196 name: "project",
197 },
198 }),
199 ),
200 ),
201 );
202
203 const az = yield* AzureDevOpsCli.AzureDevOpsCli;
204 const result = yield* az.createRepository({
205 cwd: "/repo",
206 repository: "project/repo",
207 visibility: "private",
208 });
209
210 assert.deepStrictEqual(result, {
211 nameWithOwner: "project/repo",
212 url: "https://dev.azure.com/acme/project/_git/repo",
213 sshUrl: "git@ssh.dev.azure.com:v3/acme/project/repo",
214 });
215 expect(mockRun).toHaveBeenCalledWith({
216 operation: "AzureDevOpsCli.execute",
217 command: "az",
218 args: [
219 "repos",
220 "create",
221 "--detect",
222 "true",
223 "--name",
224 "repo",
225 "--project",
226 "project",
227 "--only-show-errors",
228 "--output",
229 "json",
230 ],
231 cwd: "/repo",
232 timeoutMs: 30_000,
233 });
234 }).pipe(Effect.provide(layer)),
235 );
236
237 it.effect("creates pull requests using the body file as the Azure description", () =>
238 Effect.gen(function* () {
239 const fileSystem = yield* FileSystem.FileSystem;
240 const bodyFile = `/tmp/t3code-azure-devops-cli-.md`;
241 yield* fileSystem.writeFileString(bodyFile, "Generated body");
242 mockRun.mockReturnValueOnce(Effect.succeed(processOutput("{}")));
243
244 const az = yield* AzureDevOpsCli.AzureDevOpsCli;
245 yield* az.createPullRequest({
246 cwd: "/repo",
247 baseBranch: "main",
248 headSelector: "feature/provider",
249 title: "Provider PR",
250 bodyFile,
251 });
252
253 expect(mockRun).toHaveBeenCalledWith(
254 expect.objectContaining({
255 command: "az",
256 cwd: "/repo",
257 args: expect.arrayContaining(["--description", `@${bodyFile}`]),
258 }),
259 );
260 expect(mockRun.mock.calls[0]?.[0].args).not.toContain("--output");
261 }).pipe(Effect.provide(layer)),
262 );
263
264 it.effect("does not force JSON output on checkout side-effect commands", () =>
265 Effect.gen(function* () {
266 mockRun.mockReturnValueOnce(Effect.succeed(processOutput("")));
267
268 const az = yield* AzureDevOpsCli.AzureDevOpsCli;
269 yield* az.checkoutPullRequest({
270 cwd: "/repo",
271 reference: "42",
272 });
273
274 expect(mockRun).toHaveBeenCalledWith({
275 operation: "AzureDevOpsCli.execute",
276 command: "az",
277 args: [
278 "repos",
279 "pr",
280 "checkout",
281 "--only-show-errors",
282 "--detect",
283 "true",
284 "--id",
285 "42",
286 "--remote-name",
287 "origin",
288 ],
289 cwd: "/repo",
290 timeoutMs: 30_000,
291 });
292 }).pipe(Effect.provide(layer)),
293 );
294});