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

AzureDevOpsCli.test.ts

288 lines8.3 KB

1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { assert, it, afterEach, describe, expect, vi } from "@effect/vitest";
3import { Effect, FileSystem, Layer, Option } from "effect";
4import { ChildProcessSpawner } from "effect/unstable/process";
5import type { VcsError } from "@t3tools/contracts";
6​
7import * as VcsProcess from "../vcs/VcsProcess.ts";
8import * as AzureDevOpsCli from "./AzureDevOpsCli.ts";
9​
10const processOutput = (stdout: string): VcsProcess.VcsProcessOutput => ({
11 exitCode: ChildProcessSpawner.ExitCode(0),
12 stdout,
13 stderr: "",
14 stdoutTruncated: false,
15 stderrTruncated: false,
16});
17​
18const mockRun = vi.fn<VcsProcess.VcsProcessShape["run"]>();
19​
20const supportLayer = Layer.mergeAll(
21 Layer.mock(VcsProcess.VcsProcess)({
22 run: mockRun,
23 }),
24 NodeServices.layer,
25);
26const layer = Layer.mergeAll(AzureDevOpsCli.layer.pipe(Layer.provide(supportLayer)), supportLayer);
27​
28afterEach(() => {
29 mockRun.mockReset();
30});
31​
32describe("AzureDevOpsCli.layer", () => {
33 it.effect("parses pull request view output", () =>
34 Effect.gen(function* () {
35 mockRun.mockReturnValueOnce(
36 Effect.succeed(
37 processOutput(
38 JSON.stringify({
39 pullRequestId: 42,
40 title: "Add Azure provider",
41 sourceRefName: "refs/heads/feature/source-control",
42 targetRefName: "refs/heads/main",
43 status: "active",
44 creationDate: "2026-01-02T00:00:00.000Z",
45 closedDate: null,
46 _links: {
47 web: {
48 href: "https://dev.azure.com/acme/project/_git/repo/pullrequest/42",
49 },
50 },
51 }),
52 ),
53 ),
54 );
55​
56 const az = yield* AzureDevOpsCli.AzureDevOpsCli;
57 const result = yield* az.getPullRequest({
58 cwd: "/repo",
59 reference: "#42",
60 });
61​
62 assert.strictEqual(result.number, 42);
63 assert.strictEqual(result.title, "Add Azure provider");
64 assert.strictEqual(result.baseRefName, "main");
65 assert.strictEqual(result.headRefName, "feature/source-control");
66 assert.strictEqual(result.state, "open");
67 assert.deepStrictEqual(result.updatedAt._tag, Option.some(1)._tag);
68 assert.deepStrictEqual(mockRun.mock.calls.at(-1)?.[0], {
69 operation: "AzureDevOpsCli.execute",
70 command: "az",
71 args: [
72 "repos",
73 "pr",
74 "show",
75 "--detect",
76 "true",
77 "--id",
78 "42",
79 "--only-show-errors",
80 "--output",
81 "json",
82 ],
83 cwd: "/repo",
84 timeoutMs: 30_000,
85 });
86 }).pipe(Effect.provide(layer)),
87 );
88​
89 it.effect("lists pull requests with Azure status and source branch arguments", () =>
90 Effect.gen(function* () {
91 mockRun.mockReturnValueOnce(
92 Effect.succeed(
93 processOutput(
94 JSON.stringify([
95 {
96 pullRequestId: 7,
97 title: "Merged work",
98 sourceRefName: "refs/heads/feature/merged",
99 targetRefName: "refs/heads/main",
100 status: "completed",
101 closedDate: "2026-01-03T00:00:00.000Z",
102 _links: {
103 web: {
104 href: "https://dev.azure.com/acme/project/_git/repo/pullrequest/7",
105 },
106 },
107 },
108 ]),
109 ),
110 ),
111 );
112​
113 const az = yield* AzureDevOpsCli.AzureDevOpsCli;
114 const result = yield* az.listPullRequests({
115 cwd: "/repo",
116 headSelector: "origin:feature/merged",
117 state: "merged",
118 limit: 10,
119 });
120​
121 assert.strictEqual(result[0]?.state, "merged");
122 expect(mockRun).toHaveBeenCalledWith({
123 operation: "AzureDevOpsCli.execute",
124 command: "az",
125 args: [
126 "repos",
127 "pr",
128 "list",
129 "--detect",
130 "true",
131 "--source-branch",
132 "feature/merged",
133 "--status",
134 "completed",
135 "--top",
136 "10",
137 "--only-show-errors",
138 "--output",
139 "json",
140 ],
141 cwd: "/repo",
142 timeoutMs: 30_000,
143 });
144 }).pipe(Effect.provide(layer)),
145 );
146​
147 it.effect("reads repository clone URLs", () =>
148 Effect.gen(function* () {
149 mockRun.mockReturnValueOnce(
150 Effect.succeed(
151 processOutput(
152 JSON.stringify({
153 name: "repo",
154 webUrl: "https://dev.azure.com/acme/project/_git/repo",
155 remoteUrl: "https://dev.azure.com/acme/project/_git/repo",
156 sshUrl: "git@ssh.dev.azure.com:v3/acme/project/repo",
157 project: {
158 name: "project",
159 },
160 }),
161 ),
162 ),
163 );
164​
165 const az = yield* AzureDevOpsCli.AzureDevOpsCli;
166 const result = yield* az.getRepositoryCloneUrls({
167 cwd: "/repo",
168 repository: "repo",
169 });
170​
171 assert.deepStrictEqual(result, {
172 nameWithOwner: "project/repo",
173 url: "https://dev.azure.com/acme/project/_git/repo",
174 sshUrl: "git@ssh.dev.azure.com:v3/acme/project/repo",
175 });
176 }).pipe(Effect.provide(layer)),
177 );
178​
179 it.effect("creates repositories through Azure Repos", () =>
180 Effect.gen(function* () {
181 mockRun.mockReturnValueOnce(
182 Effect.succeed(
183 processOutput(
184 JSON.stringify({
185 name: "repo",
186 webUrl: "https://dev.azure.com/acme/project/_git/repo",
187 remoteUrl: "https://dev.azure.com/acme/project/_git/repo",
188 sshUrl: "git@ssh.dev.azure.com:v3/acme/project/repo",
189 project: {
190 name: "project",
191 },
192 }),
193 ),
194 ),
195 );
196​
197 const az = yield* AzureDevOpsCli.AzureDevOpsCli;
198 const result = yield* az.createRepository({
199 cwd: "/repo",
200 repository: "project/repo",
201 visibility: "private",
202 });
203​
204 assert.deepStrictEqual(result, {
205 nameWithOwner: "project/repo",
206 url: "https://dev.azure.com/acme/project/_git/repo",
207 sshUrl: "git@ssh.dev.azure.com:v3/acme/project/repo",
208 });
209 expect(mockRun).toHaveBeenCalledWith({
210 operation: "AzureDevOpsCli.execute",
211 command: "az",
212 args: [
213 "repos",
214 "create",
215 "--detect",
216 "true",
217 "--name",
218 "repo",
219 "--project",
220 "project",
221 "--only-show-errors",
222 "--output",
223 "json",
224 ],
225 cwd: "/repo",
226 timeoutMs: 30_000,
227 });
228 }).pipe(Effect.provide(layer)),
229 );
230​
231 it.effect("creates pull requests using the body file as the Azure description", () =>
232 Effect.gen(function* () {
233 const fileSystem = yield* FileSystem.FileSystem;
234 const bodyFile = `/tmp/t3code-azure-devops-cli-${Date.now()}.md`;
235 yield* fileSystem.writeFileString(bodyFile, "Generated body");
236 mockRun.mockReturnValueOnce(Effect.succeed(processOutput("{}")));
237​
238 const az = yield* AzureDevOpsCli.AzureDevOpsCli;
239 yield* az.createPullRequest({
240 cwd: "/repo",
241 baseBranch: "main",
242 headSelector: "feature/provider",
243 title: "Provider PR",
244 bodyFile,
245 });
246​
247 expect(mockRun).toHaveBeenCalledWith(
248 expect.objectContaining({
249 command: "az",
250 cwd: "/repo",
251 args: expect.arrayContaining(["--description", `@${bodyFile}`]),
252 }),
253 );
254 expect(mockRun.mock.calls[0]?.[0].args).not.toContain("--output");
255 }).pipe(Effect.provide(layer)),
256 );
257​
258 it.effect("does not force JSON output on checkout side-effect commands", () =>
259 Effect.gen(function* () {
260 mockRun.mockReturnValueOnce(Effect.succeed(processOutput("")));
261​
262 const az = yield* AzureDevOpsCli.AzureDevOpsCli;
263 yield* az.checkoutPullRequest({
264 cwd: "/repo",
265 reference: "42",
266 });
267​
268 expect(mockRun).toHaveBeenCalledWith({
269 operation: "AzureDevOpsCli.execute",
270 command: "az",
271 args: [
272 "repos",
273 "pr",
274 "checkout",
275 "--only-show-errors",
276 "--detect",
277 "true",
278 "--id",
279 "42",
280 "--remote-name",
281 "origin",
282 ],
283 cwd: "/repo",
284 timeoutMs: 30_000,
285 });
286 }).pipe(Effect.provide(layer)),
287 );
288});