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

AzureDevOpsPullRequestProvider.test.ts

374 lines13.3 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 }),
181 );
182});
183​
184describe("getDiff reads", () => {
185 it.effect("holds every reader together to one request's worth of processes", () =>
186 Effect.gen(function* () {
187 const paths = ["a.ts", "b.ts", "c.ts", "d.ts", "e.ts", "f.ts"];
188 let inFlight = 0;
189 let peakInFlight = 0;
190​
191 const provider = yield* make.pipe(
192 Effect.provide(
193 Layer.mock(AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli)({
194 getPullRequest: () => Effect.succeed(PULL_REQUEST),
195 listIterations: () => Effect.succeed([ITERATION]),
196 listIterationChanges: () =>
197 Effect.succeed({ changes: paths.map((path) => change(path)), truncated: false }),
198 readItemContent: () =>
199 Effect.gen(function* () {
200 inFlight += 1;
201 peakInFlight = Math.max(peakInFlight, inFlight);
202 // Suspends before answering, as a subprocess would, so what is out at once is the
203 // scheduler's answer rather than an artefact of resolving inline.
204 yield* Effect.yieldNow;
205 yield* Effect.yieldNow;
206 inFlight -= 1;
207 return { contents: side("new", 2, 4), isBinary: false };
208 }),
209 }),
210 ),
211 );
212​
213 const readDiff = (number: number) =>
214 provider.getDiff({ cwd: "/w", repository: "acme/web", host: "dev.azure.com", number });
215​
216 yield* Effect.all([readDiff(7), readDiff(8)], { concurrency: 2 });
217​
218 expect(peakInFlight).toBeLessThanOrEqual(MAX_DIFF_SPAWNS);
219 }),
220 );
221​
222 it.effect("leaves a run of files it could not diff at all for the next slice", () =>
223 Effect.gen(function* () {
224 const paths = Array.from({ length: MAX_DIFF_SLICE_FILES + 20 }, (_, at) => `gen/a${at}.bin`);
225 const read = yield* readSlice({ paths, lines: 2, width: 4, refused: paths });
226​
227 expect(patchedPaths(read.slice.patch)).toHaveLength(MAX_DIFF_SLICE_FILES);
228 expect(byteLength(read.slice.patch)).toBeLessThan(MAX_DIFF_SLICE_BYTES);
229 expect(parseAzureDevOpsDiffCursor(read.slice.nextCursor)?.fileIndex).toBe(
230 MAX_DIFF_SLICE_FILES,
231 );
232 }),
233 );
234​
235 it.effect("asks for both sides of several files at once rather than one side at a time", () =>
236 Effect.gen(function* () {
237 const read = yield* readSlice({
238 paths: ["a.ts", "b.ts", "c.ts", "d.ts"],
239 lines: 2,
240 width: 4,
241 });
242​
243 expect(read.peakInFlight).toBeGreaterThan(2);
244 }),
245 );
246​
247 it.effect("holds the number of files it reads at once down", () =>
248 Effect.gen(function* () {
249 const paths = Array.from({ length: 24 }, (_, file) => `file-${file}.ts`);
250 const read = yield* readSlice({ paths, lines: 2, width: 4 });
251​
252 expect(read.reads).toHaveLength(paths.length * 2);
253 expect(read.peakInFlight).toBeLessThanOrEqual(8);
254 }),
255 );
256​
257 it.effect("keeps the patch in the order the change was listed, whoever answered first", () =>
258 Effect.gen(function* () {
259 const paths = ["a.ts", "b.ts", "c.ts", "d.ts", "e.ts"];
260 const read = yield* readSlice({ paths, lines: 2, width: 4 });
261​
262 expect(patchedPaths(read.slice.patch)).toEqual(paths);
263 expect(read.slice.nextCursor).toBeNull();
264 }),
265 );
266​
267 it.effect("leaves a file the host refused listed without its hunks, in its place", () =>
268 Effect.gen(function* () {
269 const paths = ["a.ts", "b.ts", "c.ts"];
270 const read = yield* readSlice({ paths, lines: 2, width: 4, refused: ["b.ts"] });
271​
272 expect(patchedPaths(read.slice.patch)).toEqual(paths);
273 expect(read.slice.truncated).toBe(true);
274 expect(read.slice.patch).toContain("+++ b/b.ts\ndiff --git a/c.ts");
275 expect(read.slice.patch.match(/^@@ /gmu)).toHaveLength(2);
276 }),
277 );
278});
279​
280describe("what one diff slice spends", () => {
281 it.effect("stops on the byte ceiling without carrying what it read past it", () =>
282 Effect.gen(function* () {
283 const paths = ["a.ts", "b.ts", "c.ts", "d.ts", "e.ts", "f.ts"];
284 const read = yield* readSlice({ paths, lines: 100, width: 900 });
285​
286 expect(read.slice.patch.length).toBeGreaterThan(MAX_DIFF_SLICE_BYTES);
287 expect(patchedPaths(read.slice.patch)).toEqual(["a.ts", "b.ts"]);
288 expect(read.slice.nextCursor).toBe(`${ITERATION.id}:2`);
289 expect(new Set(read.reads)).toEqual(new Set(["a.ts", "b.ts", "c.ts", "d.ts"]));
290 }),
291 );
292​
293 it.effect("narrows what it reads at once as the slice fills", () =>
294 Effect.gen(function* () {
295 const paths = ["a.ts", "b.ts", "c.ts", "d.ts", "e.ts", "f.ts", "g.ts", "h.ts"];
296 const read = yield* readSlice({ paths, lines: 50, width: 450 });
297​
298 expect(read.slice.nextCursor).not.toBeNull();
299 expect(new Set(read.reads)).toEqual(new Set(patchedPaths(read.slice.patch)));
300 }),
301 );
302​
303 it.effect("stops once the diff work one request may do is spent", () =>
304 Effect.gen(function* () {
305 const paths = Array.from({ length: 12 }, (_, file) => `file-${file}.ts`);
306 const read = yield* readSlice({ paths, lines: MAX_FILE_DIFF_EDITS / 4, width: 1 });
307​
308 expect(read.slice.patch.length).toBeLessThan(MAX_DIFF_SLICE_BYTES);
309 expect(patchedPaths(read.slice.patch)).toEqual(paths.slice(0, 5));
310 expect(read.slice.nextCursor).toBe(`${ITERATION.id}:5`);
311 }),
312 );
313​
314 it.effect("carries a whole new file and stops the slice on what it weighed", () =>
315 Effect.gen(function* () {
316 const paths = ["new.ts", "b.ts", "c.ts", "d.ts"];
317 const read = yield* readSlice({ paths, lines: 8_000, width: 30, created: ["new.ts"] });
318​
319 expect(patchedPaths(read.slice.patch)).toEqual(["new.ts"]);
320 expect(read.slice.patch).toContain("--- /dev/null");
321 expect(read.slice.patch).toContain("@@ -0,0 +1,8000 @@");
322 expect(read.slice.patch.length).toBeGreaterThan(MAX_DIFF_SLICE_BYTES);
323 expect(read.slice.nextCursor).toBe(`${ITERATION.id}:1`);
324 }),
325 );
326​
327 it.effect("carries on from where the last slice stopped", () =>
328 Effect.gen(function* () {
329 const paths = ["a.ts", "b.ts", "c.ts", "d.ts", "e.ts", "f.ts"];
330 const read = yield* readSlice({
331 paths,
332 lines: 100,
333 width: 900,
334 cursor: `${ITERATION.id}:2`,
335 });
336​
337 expect(patchedPaths(read.slice.patch)).toEqual(["c.ts", "d.ts"]);
338 expect(read.slice.nextCursor).toBe(`${ITERATION.id}:4`);
339 }),
340 );
341 it.effect("keeps the pull request being read, not the one looked up first", () =>
342 Effect.gen(function* () {
343 const HOT = 7;
344 const readsOf = new Map<number, number>();
345​
346 const provider = yield* make.pipe(
347 Effect.provide(
348 Layer.mock(AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli)({
349 getPullRequest: (input) =>
350 Effect.sync(() => {
351 readsOf.set(input.number, (readsOf.get(input.number) ?? 0) + 1);
352 return { ...PULL_REQUEST, number: input.number };
353 }),
354 listIterations: () => Effect.succeed([ITERATION]),
355 listIterationChanges: () =>
356 Effect.succeed({ changes: [change("a.ts")], truncated: false }),
357 readItemContent: () => Effect.succeed({ contents: side("new", 2, 4), isBinary: false }),
358 }),
359 ),
360 );
361​
362 const readDiff = (number: number) =>
363 provider.getDiff({ cwd: "/w", repository: "acme/web", host: "dev.azure.com", number });
364​
365 yield* readDiff(HOT);
366 for (let filled = 0; filled < LOCATION_CACHE_CAPACITY; filled += 1) {
367 yield* readDiff(HOT + 1 + filled);
368 yield* readDiff(HOT);
369 }
370​
371 expect(readsOf.get(HOT)).toBe(1);
372 }),
373 );
374});