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 assert.deepStrictEqual(mockRun.mock.calls.at(-1)?.[0], {
74 operation: "AzureDevOpsCli.execute",
75 command: "az",
76 args: [
77 "repos",
78 "pr",
79 "show",
80 "--detect",
81 "true",
82 "--id",
83 "42",
84 "--only-show-errors",
85 "--output",
86 "json",
87 ],
88 cwd: "/repo",
89 timeoutMs: 30_000,
90 });
91 assert.strictEqual(result.isCrossRepository, false);
92 }).pipe(Effect.provide(layer)),
93 );
94
95 it.effect("marks a pull request from a fork as cross-repository", () =>
96 Effect.gen(function* () {
97 mockRun.mockReturnValueOnce(
98 Effect.succeed(
99 processOutput(
100 JSON.stringify({
101 pullRequestId: 43,
102 title: "From a fork",
103 sourceRefName: "refs/heads/main",
104 targetRefName: "refs/heads/main",
105 status: "active",
106 forkSource: { name: "refs/heads/main", repository: { name: "repo-fork" } },
107 }),
108 ),
109 ),
110 );
111
112 const az = yield* AzureDevOpsCli.AzureDevOpsCli;
113 const result = yield* az.getPullRequest({ cwd: "/repo", reference: "43" });
114
115 assert.strictEqual(result.isCrossRepository, true);
116 }).pipe(Effect.provide(layer)),
117 );
118
119 it.effect("builds a web URL when Azure returns only the pull request REST URL", () =>
120 Effect.gen(function* () {
121 mockRun.mockReturnValueOnce(
122 Effect.succeed(
123 processOutput(
124 JSON.stringify({
125 pullRequestId: 863,
126 title: "Fix Azure link",
127 url: "https://dev.azure.com/saplyai/a8fe4088-ad76-4eda-aaaa-e3329713a097/_apis/git/repositories/16108a25-93a3-4eff-b77d-85e894ee0fe4/pullRequests/863",
128 repository: {
129 name: "CV-engine",
130 project: {
131 name: "CV-engine",
132 },
133 },
134 sourceRefName: "refs/heads/feature/azure-pr-link",
135 targetRefName: "refs/heads/main",
136 status: "active",
137 }),
138 ),
139 ),
140 );
141
142 const az = yield* AzureDevOpsCli.AzureDevOpsCli;
143 const result = yield* az.getPullRequest({
144 cwd: "/repo",
145 reference: "863",
146 });
147
148 assert.strictEqual(
149 result.url,
150 "https://dev.azure.com/saplyai/CV-engine/_git/CV-engine/pullrequest/863",
151 );
152 }).pipe(Effect.provide(layer)),
153 );
154
155 it.effect("reads repository clone URLs", () =>
156 Effect.gen(function* () {
157 mockRun.mockReturnValueOnce(
158 Effect.succeed(
159 processOutput(
160 JSON.stringify({
161 name: "repo",
162 webUrl: "https://dev.azure.com/acme/project/_git/repo",
163 remoteUrl: "https://dev.azure.com/acme/project/_git/repo",
164 sshUrl: "git@ssh.dev.azure.com:v3/acme/project/repo",
165 project: {
166 name: "project",
167 },
168 }),
169 ),
170 ),
171 );
172
173 const az = yield* AzureDevOpsCli.AzureDevOpsCli;
174 const result = yield* az.getRepositoryCloneUrls({
175 cwd: "/repo",
176 repository: "repo",
177 });
178
179 assert.deepStrictEqual(result, {
180 nameWithOwner: "project/repo",
181 url: "https://dev.azure.com/acme/project/_git/repo",
182 sshUrl: "git@ssh.dev.azure.com:v3/acme/project/repo",
183 });
184 }).pipe(Effect.provide(layer)),
185 );
186
187 it.effect("preserves VCS causes without copying upstream details into messages", () =>
188 Effect.gen(function* () {
189 const cause = new VcsProcessExitError({
190 operation: "AzureDevOpsCli.execute",
191 command: "az repos list --organization sensitive-upstream-detail",
192 cwd: "/repo",
193 exitCode: 1,
194 detail: "sensitive-upstream-detail",
195 });
196 mockRun.mockReturnValueOnce(Effect.fail(cause));
197
198 const az = yield* AzureDevOpsCli.AzureDevOpsCli;
199 const error = yield* az.execute({ cwd: "/repo", args: ["repos", "list"] }).pipe(Effect.flip);
200
201 assert.instanceOf(error, AzureDevOpsCli.AzureDevOpsCommandFailedError);
202 assert.strictEqual(error.operation, "execute");
203 assert.strictEqual(error.command, "az");
204 assert.strictEqual(error.cwd, "/repo");
205 assert.strictEqual(error.argumentCount, 2);
206 assert.strictEqual(error.detail, "Azure DevOps CLI command failed.");
207 assert.strictEqual(error.cause, cause);
208 assert.equal(error.message.includes("sensitive-upstream-detail"), false);
209 }).pipe(Effect.provide(layer)),
210 );
211
212 it.effect("does not report a missing working directory as a missing Azure CLI", () =>
213 Effect.gen(function* () {
214 const cwd = "/missing/repo";
215 const platformCause = PlatformError.systemError({
216 _tag: "NotFound",
217 module: "ChildProcess",
218 method: "spawn",
219 syscall: "chdir",
220 pathOrDescriptor: cwd,
221 });
222 const cause = new VcsProcessSpawnError({
223 operation: "AzureDevOpsCli.execute",
224 command: "az",
225 cwd,
226 argumentCount: 2,
227 cause: platformCause,
228 });
229 mockRun.mockReturnValueOnce(Effect.fail(cause));
230
231 const az = yield* AzureDevOpsCli.AzureDevOpsCli;
232 const error = yield* az.execute({ cwd, args: ["repos", "list"] }).pipe(Effect.flip);
233
234 assert.instanceOf(error, AzureDevOpsCli.AzureDevOpsCommandFailedError);
235 assert.strictEqual(error.cwd, cwd);
236 assert.strictEqual(error.cause, cause);
237 }).pipe(Effect.provide(layer)),
238 );
239
240 it.effect("preserves rate-limit failures as a distinct error", () =>
241 Effect.gen(function* () {
242 const cause = new VcsProcessExitError({
243 operation: "AzureDevOpsCli.execute",
244 command: "az",
245 cwd: "/repo",
246 argumentCount: 2,
247 exitCode: 1,
248 detail: "API rate limit exceeded.",
249 failureKind: "rate-limited",
250 });
251 mockRun.mockReturnValueOnce(Effect.fail(cause));
252
253 const az = yield* AzureDevOpsCli.AzureDevOpsCli;
254 const error = yield* az.execute({ cwd: "/repo", args: ["repos", "list"] }).pipe(Effect.flip);
255
256 assert.instanceOf(error, AzureDevOpsCli.AzureDevOpsCliRateLimitError);
257 assert.strictEqual(error.cause, cause);
258 }).pipe(Effect.provide(layer)),
259 );
260});