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

AzureDevOpsCli.test.ts

341 lines11.5 KB

1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { assert, it, afterEach, describe, vi } from "@effect/vitest";
3import * as Effect from "effect/Effect";
4import * as Layer from "effect/Layer";
5import * as Option from "effect/Option";
6import * as PlatformError from "effect/PlatformError";
7import { ChildProcessSpawner } from "effect/process";
8import { VcsProcessExitError, VcsProcessSpawnError } from "@t3tools/contracts";
9​
10import type * as SourceControlHost from "@t3tools/source-control-core/server/SourceControlHost";
11import * as TestSourceControlHost from "@t3tools/source-control-testing/TestSourceControlHost";
12import * as AzureDevOpsCli from "./AzureDevOpsCli.ts";
13​
14const processOutput = (stdout: string): SourceControlHost.SourceControlProcessOutput => ({
15 exitCode: ChildProcessSpawner.ExitCode(0),
16 stdout,
17 stderr: "",
18 stdoutTruncated: false,
19 stderrTruncated: false,
20});
21​
22const mockRun = vi.fn<SourceControlHost.SourceControlHost["Service"]["process"]["run"]>();
23​
24const layerSupport = Layer.mergeAll(
25 TestSourceControlHost.layer({ process: { run: mockRun } }),
26 NodeServices.layer,
27);
28const layer = Layer.mergeAll(AzureDevOpsCli.layer.pipe(Layer.provide(layerSupport)), layerSupport);
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 JSON.stringify({
41 pullRequestId: 42,
42 title: "Add Azure provider",
43 sourceRefName: "refs/heads/feature/source-control",
44 targetRefName: "refs/heads/main",
45 lastMergeSourceCommit: { commitId: "a".repeat(40) },
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.headSha, "a".repeat(40));
71 assert.strictEqual(result.state, "open");
72 assert.deepStrictEqual(result.updatedAt._tag, Option.some(1)._tag);
73 const { classifyFailure, ...call } = mockRun.mock.calls.at(-1)?.[0] ?? {};
74 assert.strictEqual(
75 classifyFailure?.("Please run az login to setup account."),
76 "authentication",
77 );
78 assert.strictEqual(
79 classifyFailure?.("Please run 'az login' to setup account."),
80 "authentication",
81 );
82 assert.strictEqual(
83 classifyFailure?.("TF401180: The requested pull request was not found."),
84 "not-found",
85 );
86 assert.deepStrictEqual(call, {
87 operation: "AzureDevOpsCli.execute",
88 command: "az",
89 args: [
90 "repos",
91 "pr",
92 "show",
93 "--detect",
94 "true",
95 "--id",
96 "42",
97 "--only-show-errors",
98 "--output",
99 "json",
100 ],
101 cwd: "/repo",
102 notFoundDetail: "Pull request not found.",
103 timeoutMs: 30_000,
104 });
105 assert.strictEqual(result.isCrossRepository, false);
106 }).pipe(Effect.provide(layer)),
107 );
108​
109 it.effect("marks a pull request from a fork as cross-repository", () =>
110 Effect.gen(function* () {
111 mockRun.mockReturnValueOnce(
112 Effect.succeed(
113 processOutput(
114 JSON.stringify({
115 pullRequestId: 43,
116 title: "From a fork",
117 sourceRefName: "refs/heads/main",
118 targetRefName: "refs/heads/main",
119 status: "active",
120 forkSource: { name: "refs/heads/main", repository: { name: "repo-fork" } },
121 }),
122 ),
123 ),
124 );
125​
126 const az = yield* AzureDevOpsCli.AzureDevOpsCli;
127 const result = yield* az.getPullRequest({ cwd: "/repo", reference: "43" });
128​
129 assert.strictEqual(result.isCrossRepository, true);
130 }).pipe(Effect.provide(layer)),
131 );
132​
133 it.effect("builds a web URL when Azure returns only the pull request REST URL", () =>
134 Effect.gen(function* () {
135 mockRun.mockReturnValueOnce(
136 Effect.succeed(
137 processOutput(
138 JSON.stringify({
139 pullRequestId: 863,
140 title: "Fix Azure link",
141 url: "https://dev.azure.com/saplyai/a8fe4088-ad76-4eda-aaaa-e3329713a097/_apis/git/repositories/16108a25-93a3-4eff-b77d-85e894ee0fe4/pullRequests/863",
142 repository: {
143 name: "CV-engine",
144 project: {
145 name: "CV-engine",
146 },
147 },
148 sourceRefName: "refs/heads/feature/azure-pr-link",
149 targetRefName: "refs/heads/main",
150 status: "active",
151 }),
152 ),
153 ),
154 );
155​
156 const az = yield* AzureDevOpsCli.AzureDevOpsCli;
157 const result = yield* az.getPullRequest({
158 cwd: "/repo",
159 reference: "863",
160 });
161​
162 assert.strictEqual(
163 result.url,
164 "https://dev.azure.com/saplyai/CV-engine/_git/CV-engine/pullrequest/863",
165 );
166 }).pipe(Effect.provide(layer)),
167 );
168​
169 it.effect("reads repository clone URLs", () =>
170 Effect.gen(function* () {
171 mockRun.mockReturnValueOnce(
172 Effect.succeed(
173 processOutput(
174 JSON.stringify({
175 name: "repo",
176 webUrl: "https://dev.azure.com/acme/project/_git/repo",
177 remoteUrl: "https://dev.azure.com/acme/project/_git/repo",
178 sshUrl: "git@ssh.dev.azure.com:v3/acme/project/repo",
179 project: {
180 name: "project",
181 },
182 }),
183 ),
184 ),
185 );
186​
187 const az = yield* AzureDevOpsCli.AzureDevOpsCli;
188 const result = yield* az.getRepositoryCloneUrls({
189 cwd: "/repo",
190 repository: "repo",
191 });
192​
193 assert.deepStrictEqual(result, {
194 nameWithOwner: "project/repo",
195 url: "https://dev.azure.com/acme/project/_git/repo",
196 sshUrl: "git@ssh.dev.azure.com:v3/acme/project/repo",
197 });
198 }).pipe(Effect.provide(layer)),
199 );
200​
201 it.effect("looks up a project/repository path as that project's repository", () =>
202 Effect.gen(function* () {
203 mockRun.mockReturnValueOnce(
204 Effect.succeed(
205 processOutput(
206 JSON.stringify({
207 name: "repo",
208 webUrl: "https://dev.azure.com/acme/project/_git/repo",
209 remoteUrl: "https://dev.azure.com/acme/project/_git/repo",
210 sshUrl: "git@ssh.dev.azure.com:v3/acme/project/repo",
211 project: { name: "project" },
212 }),
213 ),
214 ),
215 );
216​
217 const az = yield* AzureDevOpsCli.AzureDevOpsCli;
218 yield* az.getRepositoryCloneUrls({ cwd: "/somewhere", repository: "project/repo" });
219​
220 assert.deepStrictEqual(mockRun.mock.calls.at(-1)?.[0].args, [
221 "repos",
222 "show",
223 "--detect",
224 "true",
225 "--repository",
226 "repo",
227 "--project",
228 "project",
229 "--only-show-errors",
230 "--output",
231 "json",
232 ]);
233 }).pipe(Effect.provide(layer)),
234 );
235​
236 it.effect("percent-encodes the source branch while preserving recognized ref prefixes", () =>
237 Effect.gen(function* () {
238 mockRun.mockReturnValueOnce(Effect.succeed(processOutput("[]")));
239​
240 const az = yield* AzureDevOpsCli.AzureDevOpsCli;
241 yield* az.listPullRequests({
242 cwd: "/somewhere",
243 headSelector: "user/feat/1-(#2)-slug",
244 state: "open",
245 });
246​
247 const args = mockRun.mock.calls.at(-1)?.[0].args ?? [];
248 assert.strictEqual(args[args.indexOf("--source-branch") + 1], "user%2Ffeat%2F1-(%232)-slug");
249​
250 for (const prefix of ["refs/heads/", "refs/pull/", "refs/tags/"]) {
251 mockRun.mockReturnValueOnce(Effect.succeed(processOutput("[]")));
252 yield* az.listPullRequests({
253 cwd: "/somewhere",
254 headSelector: "fallback",
255 source: { refName: `${prefix}user/feat/1-(#2)-slug` },
256 state: "open",
257 });
258​
259 const refArgs = mockRun.mock.calls.at(-1)?.[0].args ?? [];
260 assert.strictEqual(
261 refArgs[refArgs.indexOf("--source-branch") + 1],
262 `${prefix}user%2Ffeat%2F1-(%232)-slug`,
263 );
264 }
265 }).pipe(Effect.provide(layer)),
266 );
267​
268 it.effect("preserves VCS causes without copying upstream details into messages", () =>
269 Effect.gen(function* () {
270 const cause = new VcsProcessExitError({
271 operation: "AzureDevOpsCli.execute",
272 command: "az repos list --organization sensitive-upstream-detail",
273 cwd: "/repo",
274 exitCode: 1,
275 detail: "sensitive-upstream-detail",
276 });
277 mockRun.mockReturnValueOnce(Effect.fail(cause));
278​
279 const az = yield* AzureDevOpsCli.AzureDevOpsCli;
280 const error = yield* az.execute({ cwd: "/repo", args: ["repos", "list"] }).pipe(Effect.flip);
281​
282 assert.instanceOf(error, AzureDevOpsCli.AzureDevOpsCommandFailedError);
283 assert.strictEqual(error.operation, "execute");
284 assert.strictEqual(error.command, "az");
285 assert.strictEqual(error.cwd, "/repo");
286 assert.strictEqual(error.argumentCount, 2);
287 assert.strictEqual(error.detail, "Azure DevOps CLI command failed.");
288 assert.strictEqual(error.cause, cause);
289 assert.equal(error.message.includes("sensitive-upstream-detail"), false);
290 }).pipe(Effect.provide(layer)),
291 );
292​
293 it.effect("does not report a missing working directory as a missing Azure CLI", () =>
294 Effect.gen(function* () {
295 const cwd = "/missing/repo";
296 const platformCause = PlatformError.systemError({
297 _tag: "NotFound",
298 module: "ChildProcess",
299 method: "spawn",
300 syscall: "chdir",
301 pathOrDescriptor: cwd,
302 });
303 const cause = new VcsProcessSpawnError({
304 operation: "AzureDevOpsCli.execute",
305 command: "az",
306 cwd,
307 argumentCount: 2,
308 cause: platformCause,
309 });
310 mockRun.mockReturnValueOnce(Effect.fail(cause));
311​
312 const az = yield* AzureDevOpsCli.AzureDevOpsCli;
313 const error = yield* az.execute({ cwd, args: ["repos", "list"] }).pipe(Effect.flip);
314​
315 assert.instanceOf(error, AzureDevOpsCli.AzureDevOpsCommandFailedError);
316 assert.strictEqual(error.cwd, cwd);
317 assert.strictEqual(error.cause, cause);
318 }).pipe(Effect.provide(layer)),
319 );
320​
321 it.effect("preserves rate-limit failures as a distinct error", () =>
322 Effect.gen(function* () {
323 const cause = new VcsProcessExitError({
324 operation: "AzureDevOpsCli.execute",
325 command: "az",
326 cwd: "/repo",
327 argumentCount: 2,
328 exitCode: 1,
329 detail: "API rate limit exceeded.",
330 failureKind: "rate-limited",
331 });
332 mockRun.mockReturnValueOnce(Effect.fail(cause));
333​
334 const az = yield* AzureDevOpsCli.AzureDevOpsCli;
335 const error = yield* az.execute({ cwd: "/repo", args: ["repos", "list"] }).pipe(Effect.flip);
336​
337 assert.instanceOf(error, AzureDevOpsCli.AzureDevOpsCliRateLimitError);
338 assert.strictEqual(error.cause, cause);
339 }).pipe(Effect.provide(layer)),
340 );
341});