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

AzureDevOpsPullRequestProvider.test.ts

351 lines12.7 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("getChangeRequest", () => {
127 it.effect("preserves the newest head when file counting fails", () =>
128 Effect.gen(function* () {
129 const provider = yield* make.pipe(
130 Effect.provide(
131 Layer.mock(AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli)({
132 getPullRequest: () => Effect.succeed(PULL_REQUEST),
133 listIterations: () =>
134 Effect.succeed([ITERATION, { ...ITERATION, id: 4, headCommit: "new-head" }]),
135 listIterationChanges: () =>
136 Effect.fail(
137 new AzureDevOpsPullRequestCli.AzureDevOpsPullRequestReadError({
138 command: "az",
139 cwd: "/w",
140 operation: "listIterationChanges",
141 cause: "unavailable",
142 }),
143 ),
144 }),
145 ),
146 );
147​
148 const detail = yield* provider.getChangeRequest({
149 cwd: "/w",
150 repository: "acme/web",
151 host: "dev.azure.com",
152 number: 7,
153 });
154 expect(detail.headSha).toBe("new-head");
155 expect(detail.changedFiles).toBe(0);
156 }),
157 );
158​
159 it.effect.each(["missing", "failed"] as const)(
160 "leaves the head unavailable when the iteration read is %s",
161 (state) =>
162 Effect.gen(function* () {
163 const provider = yield* make.pipe(
164 Effect.provide(
165 Layer.mock(AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli)({
166 getPullRequest: () => Effect.succeed(PULL_REQUEST),
167 listIterations: () =>
168 state === "missing"
169 ? Effect.succeed([])
170 : Effect.fail(
171 new AzureDevOpsPullRequestCli.AzureDevOpsPullRequestReadError({
172 command: "az",
173 cwd: "/w",
174 operation: "listIterations",
175 cause: "unavailable",
176 }),
177 ),
178 }),
179 ),
180 );
181 const detail = yield* provider.getChangeRequest({
182 cwd: "/w",
183 repository: "acme/web",
184 host: "dev.azure.com",
185 number: 7,
186 });
187 expect(detail.headSha).toBeUndefined();
188 expect(detail.changedFiles).toBe(0);
189 }),
190 );
191});
192​
193describe("getDiff reads", () => {
194 it.effect("holds every reader together to one request's worth of processes", () =>
195 Effect.gen(function* () {
196 const paths = ["a.ts", "b.ts", "c.ts", "d.ts", "e.ts", "f.ts"];
197 let inFlight = 0;
198 let peakInFlight = 0;
199​
200 const provider = yield* make.pipe(
201 Effect.provide(
202 Layer.mock(AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli)({
203 getPullRequest: () => Effect.succeed(PULL_REQUEST),
204 listIterations: () => Effect.succeed([ITERATION]),
205 listIterationChanges: () =>
206 Effect.succeed({ changes: paths.map((path) => change(path)), truncated: false }),
207 readItemContent: () =>
208 Effect.gen(function* () {
209 inFlight += 1;
210 peakInFlight = Math.max(peakInFlight, inFlight);
211 // Suspends before answering, as a subprocess would, so what is out at once is the
212 // scheduler's answer rather than an artefact of resolving inline.
213 yield* Effect.yieldNow;
214 yield* Effect.yieldNow;
215 inFlight -= 1;
216 return { contents: side("new", 2, 4), isBinary: false };
217 }),
218 }),
219 ),
220 );
221​
222 const readDiff = (number: number) =>
223 provider.getDiff({ cwd: "/w", repository: "acme/web", host: "dev.azure.com", number });
224​
225 yield* Effect.all([readDiff(7), readDiff(8)], { concurrency: 2 });
226​
227 expect(peakInFlight).toBeLessThanOrEqual(MAX_DIFF_SPAWNS);
228 }),
229 );
230​
231 it.effect("leaves a run of files it could not diff at all for the next slice", () =>
232 Effect.gen(function* () {
233 const paths = Array.from({ length: MAX_DIFF_SLICE_FILES + 20 }, (_, at) => `gen/a${at}.bin`);
234 const read = yield* readSlice({ paths, lines: 2, width: 4, refused: paths });
235​
236 expect(patchedPaths(read.slice.patch)).toHaveLength(MAX_DIFF_SLICE_FILES);
237 expect(byteLength(read.slice.patch)).toBeLessThan(MAX_DIFF_SLICE_BYTES);
238 expect(parseAzureDevOpsDiffCursor(read.slice.nextCursor)?.fileIndex).toBe(
239 MAX_DIFF_SLICE_FILES,
240 );
241 }),
242 );
243​
244 it.effect("keeps the patch in the order the change was listed, whoever answered first", () =>
245 Effect.gen(function* () {
246 const paths = ["a.ts", "b.ts", "c.ts", "d.ts", "e.ts"];
247 const read = yield* readSlice({ paths, lines: 2, width: 4 });
248​
249 expect(patchedPaths(read.slice.patch)).toEqual(paths);
250 expect(read.slice.nextCursor).toBeNull();
251 }),
252 );
253​
254 it.effect("leaves a file the host refused listed without its hunks, in its place", () =>
255 Effect.gen(function* () {
256 const paths = ["a.ts", "b.ts", "c.ts"];
257 const read = yield* readSlice({ paths, lines: 2, width: 4, refused: ["b.ts"] });
258​
259 expect(patchedPaths(read.slice.patch)).toEqual(paths);
260 expect(read.slice.truncated).toBe(true);
261 expect(read.slice.patch).toContain("+++ b/b.ts\ndiff --git a/c.ts");
262 expect(read.slice.patch.match(/^@@ /gmu)).toHaveLength(2);
263 }),
264 );
265});
266​
267describe("what one diff slice spends", () => {
268 it.effect("stops on the byte ceiling without carrying what it read past it", () =>
269 Effect.gen(function* () {
270 const paths = ["a.ts", "b.ts", "c.ts", "d.ts", "e.ts", "f.ts"];
271 const read = yield* readSlice({ paths, lines: 100, width: 900 });
272​
273 expect(read.slice.patch.length).toBeGreaterThan(MAX_DIFF_SLICE_BYTES);
274 expect(patchedPaths(read.slice.patch)).toEqual(["a.ts", "b.ts"]);
275 expect(read.slice.nextCursor).toBe(`${ITERATION.id}:2`);
276 expect(new Set(read.reads)).toEqual(new Set(["a.ts", "b.ts", "c.ts", "d.ts"]));
277 }),
278 );
279​
280 it.effect("stops once the diff work one request may do is spent", () =>
281 Effect.gen(function* () {
282 const paths = Array.from({ length: 12 }, (_, file) => `file-${file}.ts`);
283 const read = yield* readSlice({ paths, lines: MAX_FILE_DIFF_EDITS / 4, width: 1 });
284​
285 expect(read.slice.patch.length).toBeLessThan(MAX_DIFF_SLICE_BYTES);
286 expect(patchedPaths(read.slice.patch)).toEqual(paths.slice(0, 5));
287 expect(read.slice.nextCursor).toBe(`${ITERATION.id}:5`);
288 }),
289 );
290​
291 it.effect("carries a whole new file and stops the slice on what it weighed", () =>
292 Effect.gen(function* () {
293 const paths = ["new.ts", "b.ts", "c.ts", "d.ts"];
294 const read = yield* readSlice({ paths, lines: 8_000, width: 30, created: ["new.ts"] });
295​
296 expect(patchedPaths(read.slice.patch)).toEqual(["new.ts"]);
297 expect(read.slice.patch).toContain("--- /dev/null");
298 expect(read.slice.patch).toContain("@@ -0,0 +1,8000 @@");
299 expect(read.slice.patch.length).toBeGreaterThan(MAX_DIFF_SLICE_BYTES);
300 expect(read.slice.nextCursor).toBe(`${ITERATION.id}:1`);
301 }),
302 );
303​
304 it.effect("carries on from where the last slice stopped", () =>
305 Effect.gen(function* () {
306 const paths = ["a.ts", "b.ts", "c.ts", "d.ts", "e.ts", "f.ts"];
307 const read = yield* readSlice({
308 paths,
309 lines: 100,
310 width: 900,
311 cursor: `${ITERATION.id}:2`,
312 });
313​
314 expect(patchedPaths(read.slice.patch)).toEqual(["c.ts", "d.ts"]);
315 expect(read.slice.nextCursor).toBe(`${ITERATION.id}:4`);
316 }),
317 );
318 it.effect("keeps the pull request being read, not the one looked up first", () =>
319 Effect.gen(function* () {
320 const HOT = 7;
321 const readsOf = new Map<number, number>();
322​
323 const provider = yield* make.pipe(
324 Effect.provide(
325 Layer.mock(AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli)({
326 getPullRequest: (input) =>
327 Effect.sync(() => {
328 readsOf.set(input.number, (readsOf.get(input.number) ?? 0) + 1);
329 return { ...PULL_REQUEST, number: input.number };
330 }),
331 listIterations: () => Effect.succeed([ITERATION]),
332 listIterationChanges: () =>
333 Effect.succeed({ changes: [change("a.ts")], truncated: false }),
334 readItemContent: () => Effect.succeed({ contents: side("new", 2, 4), isBinary: false }),
335 }),
336 ),
337 );
338​
339 const readDiff = (number: number) =>
340 provider.getDiff({ cwd: "/w", repository: "acme/web", host: "dev.azure.com", number });
341​
342 yield* readDiff(HOT);
343 for (let filled = 0; filled < LOCATION_CACHE_CAPACITY; filled += 1) {
344 yield* readDiff(HOT + 1 + filled);
345 yield* readDiff(HOT);
346 }
347​
348 expect(readsOf.get(HOT)).toBe(1);
349 }),
350 );
351});