Open History to see the last commit
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("preserves VCS causes without copying upstream details into messages", () =>
202 Effect.gen(function* () {
203 const cause = new VcsProcessExitError({
204 operation: "AzureDevOpsCli.execute",
205 command: "az repos list --organization sensitive-upstream-detail",
206 cwd: "/repo",
207 exitCode: 1,
208 detail: "sensitive-upstream-detail",
209 });
210 mockRun.mockReturnValueOnce(Effect.fail(cause));
211
212 const az = yield* AzureDevOpsCli.AzureDevOpsCli;
213 const error = yield* az.execute({ cwd: "/repo", args: ["repos", "list"] }).pipe(Effect.flip);
214
215 assert.instanceOf(error, AzureDevOpsCli.AzureDevOpsCommandFailedError);
216 assert.strictEqual(error.operation, "execute");
217 assert.strictEqual(error.command, "az");
218 assert.strictEqual(error.cwd, "/repo");
219 assert.strictEqual(error.argumentCount, 2);
220 assert.strictEqual(error.detail, "Azure DevOps CLI command failed.");
221 assert.strictEqual(error.cause, cause);
222 assert.equal(error.message.includes("sensitive-upstream-detail"), false);
223 }).pipe(Effect.provide(layer)),
224 );
225
226 it.effect("does not report a missing working directory as a missing Azure CLI", () =>
227 Effect.gen(function* () {
228 const cwd = "/missing/repo";
229 const platformCause = PlatformError.systemError({
230 _tag: "NotFound",
231 module: "ChildProcess",
232 method: "spawn",
233 syscall: "chdir",
234 pathOrDescriptor: cwd,
235 });
236 const cause = new VcsProcessSpawnError({
237 operation: "AzureDevOpsCli.execute",
238 command: "az",
239 cwd,
240 argumentCount: 2,
241 cause: platformCause,
242 });
243 mockRun.mockReturnValueOnce(Effect.fail(cause));
244
245 const az = yield* AzureDevOpsCli.AzureDevOpsCli;
246 const error = yield* az.execute({ cwd, args: ["repos", "list"] }).pipe(Effect.flip);
247
248 assert.instanceOf(error, AzureDevOpsCli.AzureDevOpsCommandFailedError);
249 assert.strictEqual(error.cwd, cwd);
250 assert.strictEqual(error.cause, cause);
251 }).pipe(Effect.provide(layer)),
252 );
253
254 it.effect("preserves rate-limit failures as a distinct error", () =>
255 Effect.gen(function* () {
256 const cause = new VcsProcessExitError({
257 operation: "AzureDevOpsCli.execute",
258 command: "az",
259 cwd: "/repo",
260 argumentCount: 2,
261 exitCode: 1,
262 detail: "API rate limit exceeded.",
263 failureKind: "rate-limited",
264 });
265 mockRun.mockReturnValueOnce(Effect.fail(cause));
266
267 const az = yield* AzureDevOpsCli.AzureDevOpsCli;
268 const error = yield* az.execute({ cwd: "/repo", args: ["repos", "list"] }).pipe(Effect.flip);
269
270 assert.instanceOf(error, AzureDevOpsCli.AzureDevOpsCliRateLimitError);
271 assert.strictEqual(error.cause, cause);
272 }).pipe(Effect.provide(layer)),
273 );
274});