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

AzureDevOpsPullRequestProvider.test.ts

440 lines15.6 KB

1import { describe, expect, it } from "@effect/vitest";
2import * as Effect from "effect/Effect";
3import * as Layer from "effect/Layer";
4​
5import * as AzureDevOpsPullRequestCli from "./AzureDevOpsPullRequestCli.ts";
6import {
7 LOCATION_CACHE_CAPACITY,
8 make,
9 MAX_DIFF_SPAWNS,
10} from "./AzureDevOpsPullRequestProvider.ts";
11import {
12 byteLength,
13 MAX_DIFF_SLICE_BYTES,
14 MAX_DIFF_SLICE_FILES,
15 MAX_FILE_DIFF_EDITS,
16 parseAzureDevOpsDiffCursor,
17} from "./azureDevOpsDiff.ts";
18import type { AzureDevOpsChangeEntry } from "./azureDevOpsPullRequestJson.ts";
19​
20const ITERATION = { id: 3, headCommit: "head", mergeBaseCommit: "base" };
21​
22const PULL_REQUEST = {
23 number: 7,
24 title: "Pull request 7",
25 url: "https://dev.azure.com/acme/web/_git/web/pullrequest/7",
26 author: null,
27 headBranch: "feat/page",
28 baseBranch: "main",
29 state: "open" as const,
30 isDraft: false,
31 mergeability: "mergeable" as const,
32 createdAt: "2026-07-01T00:00:00Z",
33 updatedAt: "2026-07-02T00:00:00Z",
34 closedAt: null,
35 body: "",
36 reviewRequestLogins: [],
37 reviewers: [],
38 location: { project: "acme", repository: "web" },
39 autoMergeEnabled: false,
40};
41​
42function change(
43 path: string,
44 changeKind: AzureDevOpsChangeEntry["changeKind"] = "change",
45): AzureDevOpsChangeEntry {
46 return { path, oldPath: path, changeKind, objectId: "8f80", originalObjectId: "0ca4" };
48​
49function side(prefix: string, lines: number, width: number): string {
50 const pad = "z".repeat(width);
51 return `${Array.from({ length: lines }, (_, line) => `${prefix} ${line} ${pad}`).join("\n")}\n`;
53​
54const readSlice = (input: {
55 readonly paths: ReadonlyArray<string>;
56 readonly lines: number;
57 readonly width: number;
58 readonly refused?: ReadonlyArray<string>;
59 readonly created?: ReadonlyArray<string>;
60 readonly cursor?: string;
61}) =>
62 Effect.gen(function* () {
63 const reads: string[] = [];
64 const refused = new Set(input.refused ?? []);
65 const created = new Set(input.created ?? []);
66 let inFlight = 0;
67 let peakInFlight = 0;
68​
69 const provider = yield* make.pipe(
70 Effect.provide(
71 Layer.mock(AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli)({
72 getPullRequest: () => Effect.succeed(PULL_REQUEST),
73 listIterations: () => Effect.succeed([ITERATION]),
74 listIterationChanges: () =>
75 Effect.succeed({
76 changes: input.paths.map((path) =>
77 change(path, created.has(path) ? "new" : "change"),
78 ),
79 truncated: false,
80 }),
81 readItemContent: (item) =>
82 Effect.gen(function* () {
83 reads.push(item.path);
84 inFlight += 1;
85 peakInFlight = Math.max(peakInFlight, inFlight);
86 // Every read suspends before it answers, as a subprocess would, so what runs at
87 // once is the scheduler's answer rather than an artefact of resolving inline.
88 const answersAfter = input.paths.length - input.paths.indexOf(item.path);
89 for (let turn = 0; turn < answersAfter; turn += 1) yield* Effect.yieldNow;
90 inFlight -= 1;
91 if (refused.has(item.path)) {
92 return yield* new AzureDevOpsPullRequestCli.AzureDevOpsPullRequestReadError({
93 command: "az",
94 cwd: "/w",
95 operation: "readItemContent",
96 cause: "refused",
97 });
98 }
99 const isOldSide = item.commit !== ITERATION.headCommit;
100 if (isOldSide && created.has(item.path)) return { contents: "", isBinary: false };
101 return {
102 contents: side(isOldSide ? "old" : "new", input.lines, input.width),
103 isBinary: false,
104 };
105 }),
106 }),
107 ),
108 );
109​
110 const slice = yield* provider.getDiff({
111 cwd: "/w",
112 repository: "acme/web",
113 host: "dev.azure.com",
114 number: 7,
115 ...(input.cursor === undefined ? {} : { cursor: input.cursor }),
116 });
117 return { slice, reads, peakInFlight };
118 });
119​
120function patchedPaths(patch: string): ReadonlyArray<string> {
121 return [...patch.matchAll(/^diff --git a\/(?<path>\S+) b\//gmu)].map(
122 (match) => match.groups?.path ?? "",
123 );
125​
126describe("getChangeRequestSummary", () => {
127 it.effect("costs the one pull request read, not the iterations changedFiles needs", () =>
128 Effect.gen(function* () {
129 let pullRequestReads = 0;
130​
131 const provider = yield* make.pipe(
132 Effect.provide(
133 Layer.mock(AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli)({
134 getPullRequest: () => {
135 pullRequestReads += 1;
136 return Effect.succeed(PULL_REQUEST);
137 },
138 }),
139 ),
140 );
141​
142 const readSummary = provider.getChangeRequestSummary;
143 if (readSummary === undefined) return yield* Effect.die("summary read was not implemented");
144 const summary = yield* readSummary({
145 cwd: "/w",
146 repository: "acme/web",
147 host: "dev.azure.com",
148 number: 7,
149 });
150​
151 expect(pullRequestReads).toBe(1);
152 expect(summary.title).toBe(PULL_REQUEST.title);
153 expect(summary.changedFiles).toBeUndefined();
154 }),
155 );
156});
157​
158describe("getChangeRequest", () => {
159 it.effect("still reports the file count the detail panel needs", () =>
160 Effect.gen(function* () {
161 const provider = yield* make.pipe(
162 Effect.provide(
163 Layer.mock(AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli)({
164 getPullRequest: () => Effect.succeed(PULL_REQUEST),
165 listIterations: () => Effect.succeed([ITERATION]),
166 listIterationChanges: () =>
167 Effect.succeed({ changes: [change("a.ts"), change("b.ts")], truncated: false }),
168 }),
169 ),
170 );
171​
172 const detail = yield* provider.getChangeRequest({
173 cwd: "/w",
174 repository: "acme/web",
175 host: "dev.azure.com",
176 number: 7,
177 });
178​
179 expect(detail.changedFiles).toBe(2);
180 expect(detail.headSha).toBe(ITERATION.headCommit);
181 }),
182 );
183​
184 it.effect("preserves the newest head when file counting fails", () =>
185 Effect.gen(function* () {
186 const provider = yield* make.pipe(
187 Effect.provide(
188 Layer.mock(AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli)({
189 getPullRequest: () => Effect.succeed(PULL_REQUEST),
190 listIterations: () =>
191 Effect.succeed([ITERATION, { ...ITERATION, id: 4, headCommit: "new-head" }]),
192 listIterationChanges: () =>
193 Effect.fail(
194 new AzureDevOpsPullRequestCli.AzureDevOpsPullRequestReadError({
195 command: "az",
196 cwd: "/w",
197 operation: "listIterationChanges",
198 cause: "unavailable",
199 }),
200 ),
201 }),
202 ),
203 );
204​
205 const detail = yield* provider.getChangeRequest({
206 cwd: "/w",
207 repository: "acme/web",
208 host: "dev.azure.com",
209 number: 7,
210 });
211 expect(detail.headSha).toBe("new-head");
212 expect(detail.changedFiles).toBe(0);
213 }),
214 );
215​
216 it.effect.each(["missing", "failed"] as const)(
217 "leaves the head unavailable when the iteration read is %s",
218 (state) =>
219 Effect.gen(function* () {
220 const provider = yield* make.pipe(
221 Effect.provide(
222 Layer.mock(AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli)({
223 getPullRequest: () => Effect.succeed(PULL_REQUEST),
224 listIterations: () =>
225 state === "missing"
226 ? Effect.succeed([])
227 : Effect.fail(
228 new AzureDevOpsPullRequestCli.AzureDevOpsPullRequestReadError({
229 command: "az",
230 cwd: "/w",
231 operation: "listIterations",
232 cause: "unavailable",
233 }),
234 ),
235 }),
236 ),
237 );
238 const detail = yield* provider.getChangeRequest({
239 cwd: "/w",
240 repository: "acme/web",
241 host: "dev.azure.com",
242 number: 7,
243 });
244 expect(detail.headSha).toBeUndefined();
245 expect(detail.changedFiles).toBe(0);
246 }),
247 );
248});
249​
250describe("getDiff reads", () => {
251 it.effect("holds every reader together to one request's worth of processes", () =>
252 Effect.gen(function* () {
253 const paths = ["a.ts", "b.ts", "c.ts", "d.ts", "e.ts", "f.ts"];
254 let inFlight = 0;
255 let peakInFlight = 0;
256​
257 const provider = yield* make.pipe(
258 Effect.provide(
259 Layer.mock(AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli)({
260 getPullRequest: () => Effect.succeed(PULL_REQUEST),
261 listIterations: () => Effect.succeed([ITERATION]),
262 listIterationChanges: () =>
263 Effect.succeed({ changes: paths.map((path) => change(path)), truncated: false }),
264 readItemContent: () =>
265 Effect.gen(function* () {
266 inFlight += 1;
267 peakInFlight = Math.max(peakInFlight, inFlight);
268 // Suspends before answering, as a subprocess would, so what is out at once is the
269 // scheduler's answer rather than an artefact of resolving inline.
270 yield* Effect.yieldNow;
271 yield* Effect.yieldNow;
272 inFlight -= 1;
273 return { contents: side("new", 2, 4), isBinary: false };
274 }),
275 }),
276 ),
277 );
278​
279 const readDiff = (number: number) =>
280 provider.getDiff({ cwd: "/w", repository: "acme/web", host: "dev.azure.com", number });
281​
282 yield* Effect.all([readDiff(7), readDiff(8)], { concurrency: 2 });
283​
284 expect(peakInFlight).toBeLessThanOrEqual(MAX_DIFF_SPAWNS);
285 }),
286 );
287​
288 it.effect("leaves a run of files it could not diff at all for the next slice", () =>
289 Effect.gen(function* () {
290 const paths = Array.from({ length: MAX_DIFF_SLICE_FILES + 20 }, (_, at) => `gen/a${at}.bin`);
291 const read = yield* readSlice({ paths, lines: 2, width: 4, refused: paths });
292​
293 expect(patchedPaths(read.slice.patch)).toHaveLength(MAX_DIFF_SLICE_FILES);
294 expect(byteLength(read.slice.patch)).toBeLessThan(MAX_DIFF_SLICE_BYTES);
295 expect(parseAzureDevOpsDiffCursor(read.slice.nextCursor)?.fileIndex).toBe(
296 MAX_DIFF_SLICE_FILES,
297 );
298 }),
299 );
300​
301 it.effect("asks for both sides of several files at once rather than one side at a time", () =>
302 Effect.gen(function* () {
303 const read = yield* readSlice({
304 paths: ["a.ts", "b.ts", "c.ts", "d.ts"],
305 lines: 2,
306 width: 4,
307 });
308​
309 expect(read.peakInFlight).toBeGreaterThan(2);
310 }),
311 );
312​
313 it.effect("holds the number of files it reads at once down", () =>
314 Effect.gen(function* () {
315 const paths = Array.from({ length: 24 }, (_, file) => `file-${file}.ts`);
316 const read = yield* readSlice({ paths, lines: 2, width: 4 });
317​
318 expect(read.reads).toHaveLength(paths.length * 2);
319 expect(read.peakInFlight).toBeLessThanOrEqual(8);
320 }),
321 );
322​
323 it.effect("keeps the patch in the order the change was listed, whoever answered first", () =>
324 Effect.gen(function* () {
325 const paths = ["a.ts", "b.ts", "c.ts", "d.ts", "e.ts"];
326 const read = yield* readSlice({ paths, lines: 2, width: 4 });
327​
328 expect(patchedPaths(read.slice.patch)).toEqual(paths);
329 expect(read.slice.nextCursor).toBeNull();
330 }),
331 );
332​
333 it.effect("leaves a file the host refused listed without its hunks, in its place", () =>
334 Effect.gen(function* () {
335 const paths = ["a.ts", "b.ts", "c.ts"];
336 const read = yield* readSlice({ paths, lines: 2, width: 4, refused: ["b.ts"] });
337​
338 expect(patchedPaths(read.slice.patch)).toEqual(paths);
339 expect(read.slice.truncated).toBe(true);
340 expect(read.slice.patch).toContain("+++ b/b.ts\ndiff --git a/c.ts");
341 expect(read.slice.patch.match(/^@@ /gmu)).toHaveLength(2);
342 }),
343 );
344});
345​
346describe("what one diff slice spends", () => {
347 it.effect("stops on the byte ceiling without carrying what it read past it", () =>
348 Effect.gen(function* () {
349 const paths = ["a.ts", "b.ts", "c.ts", "d.ts", "e.ts", "f.ts"];
350 const read = yield* readSlice({ paths, lines: 100, width: 900 });
351​
352 expect(read.slice.patch.length).toBeGreaterThan(MAX_DIFF_SLICE_BYTES);
353 expect(patchedPaths(read.slice.patch)).toEqual(["a.ts", "b.ts"]);
354 expect(read.slice.nextCursor).toBe(`${ITERATION.id}:2`);
355 expect(new Set(read.reads)).toEqual(new Set(["a.ts", "b.ts", "c.ts", "d.ts"]));
356 }),
357 );
358​
359 it.effect("narrows what it reads at once as the slice fills", () =>
360 Effect.gen(function* () {
361 const paths = ["a.ts", "b.ts", "c.ts", "d.ts", "e.ts", "f.ts", "g.ts", "h.ts"];
362 const read = yield* readSlice({ paths, lines: 50, width: 450 });
363​
364 expect(read.slice.nextCursor).not.toBeNull();
365 expect(new Set(read.reads)).toEqual(new Set(patchedPaths(read.slice.patch)));
366 }),
367 );
368​
369 it.effect("stops once the diff work one request may do is spent", () =>
370 Effect.gen(function* () {
371 const paths = Array.from({ length: 12 }, (_, file) => `file-${file}.ts`);
372 const read = yield* readSlice({ paths, lines: MAX_FILE_DIFF_EDITS / 4, width: 1 });
373​
374 expect(read.slice.patch.length).toBeLessThan(MAX_DIFF_SLICE_BYTES);
375 expect(patchedPaths(read.slice.patch)).toEqual(paths.slice(0, 5));
376 expect(read.slice.nextCursor).toBe(`${ITERATION.id}:5`);
377 }),
378 );
379​
380 it.effect("carries a whole new file and stops the slice on what it weighed", () =>
381 Effect.gen(function* () {
382 const paths = ["new.ts", "b.ts", "c.ts", "d.ts"];
383 const read = yield* readSlice({ paths, lines: 8_000, width: 30, created: ["new.ts"] });
384​
385 expect(patchedPaths(read.slice.patch)).toEqual(["new.ts"]);
386 expect(read.slice.patch).toContain("--- /dev/null");
387 expect(read.slice.patch).toContain("@@ -0,0 +1,8000 @@");
388 expect(read.slice.patch.length).toBeGreaterThan(MAX_DIFF_SLICE_BYTES);
389 expect(read.slice.nextCursor).toBe(`${ITERATION.id}:1`);
390 }),
391 );
392​
393 it.effect("carries on from where the last slice stopped", () =>
394 Effect.gen(function* () {
395 const paths = ["a.ts", "b.ts", "c.ts", "d.ts", "e.ts", "f.ts"];
396 const read = yield* readSlice({
397 paths,
398 lines: 100,
399 width: 900,
400 cursor: `${ITERATION.id}:2`,
401 });
402​
403 expect(patchedPaths(read.slice.patch)).toEqual(["c.ts", "d.ts"]);
404 expect(read.slice.nextCursor).toBe(`${ITERATION.id}:4`);
405 }),
406 );
407 it.effect("keeps the pull request being read, not the one looked up first", () =>
408 Effect.gen(function* () {
409 const HOT = 7;
410 const readsOf = new Map<number, number>();
411​
412 const provider = yield* make.pipe(
413 Effect.provide(
414 Layer.mock(AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli)({
415 getPullRequest: (input) =>
416 Effect.sync(() => {
417 readsOf.set(input.number, (readsOf.get(input.number) ?? 0) + 1);
418 return { ...PULL_REQUEST, number: input.number };
419 }),
420 listIterations: () => Effect.succeed([ITERATION]),
421 listIterationChanges: () =>
422 Effect.succeed({ changes: [change("a.ts")], truncated: false }),
423 readItemContent: () => Effect.succeed({ contents: side("new", 2, 4), isBinary: false }),
424 }),
425 ),
426 );
427​
428 const readDiff = (number: number) =>
429 provider.getDiff({ cwd: "/w", repository: "acme/web", host: "dev.azure.com", number });
430​
431 yield* readDiff(HOT);
432 for (let filled = 0; filled < LOCATION_CACHE_CAPACITY; filled += 1) {
433 yield* readDiff(HOT + 1 + filled);
434 yield* readDiff(HOT);
435 }
436​
437 expect(readsOf.get(HOT)).toBe(1);
438 }),
439 );
440});