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

azureDevOpsPullRequestJson.test.ts

634 lines19.1 KB

1import * as Result from "effect/Result";
2import { describe, expect, it } from "vite-plus/test";
3​
4import {
5 decodeItemContentJson,
6 decodeIterationChangesJson,
7 decodeIterationsJson,
8 decodePullRequestJson,
9 decodePullRequestListJson,
10 decodeThreadsJson,
11 decodeViewerJson,
12} from "./azureDevOpsPullRequestJson.ts";
13​
14const REST_URL =
15 "https://dev.azure.com/acme/_apis/git/repositories/6f9c9b7f-0000-0000-0000-000000000000/pullRequests/42";
16​
17function pullRequest(overrides: Record<string, unknown> = {}): Record<string, unknown> {
18 return {
19 pullRequestId: 42,
20 title: "Add the change requests page",
21 description: "Ships the page.",
22 status: "active",
23 isDraft: false,
24 mergeStatus: "succeeded",
25 createdBy: { displayName: "Bilal Hassan", uniqueName: "bilal@acme.dev" },
26 sourceRefName: "refs/heads/feat/page",
27 targetRefName: "refs/heads/main",
28 creationDate: "2026-07-01T00:00:00Z",
29 url: REST_URL,
30 repository: { name: "web", project: { name: "platform" } },
31 ...overrides,
32 };
34​
35function expectSuccess<A>(result: Result.Result<A, unknown>): A {
36 expect(Result.isSuccess(result)).toBe(true);
37 if (!Result.isSuccess(result)) throw new Error("expected a successful decode");
38 return result.success;
40​
41const asJson = (value: unknown) => JSON.stringify(value);
42​
43describe("decodePullRequestListJson", () => {
44 it("reads a pull request as a change request", () => {
45 const batch = expectSuccess(decodePullRequestListJson(asJson([pullRequest()])));
46​
47 expect(batch.items).toHaveLength(1);
48 expect(batch.items[0]).toMatchObject({
49 number: 42,
50 title: "Add the change requests page",
51 author: { login: "bilal@acme.dev", name: "Bilal Hassan" },
52 headBranch: "feat/page",
53 baseBranch: "main",
54 state: "open",
55 isDraft: false,
56 mergeability: "mergeable",
57 });
58 });
59​
60 it("assembles a browser url when Azure reports no web link", () => {
61 const batch = expectSuccess(decodePullRequestListJson(asJson([pullRequest()])));
62​
63 expect(batch.items[0]?.url).toBe("https://dev.azure.com/acme/platform/_git/web/pullrequest/42");
64 });
65​
66 it("prefers the web link Azure sends when asked for one", () => {
67 const batch = expectSuccess(
68 decodePullRequestListJson(
69 asJson([
70 pullRequest({
71 _links: {
72 web: { href: "https://dev.azure.com/acme/platform/_git/web/pullrequest/42" },
73 },
74 }),
75 ]),
76 ),
77 );
78​
79 expect(batch.items[0]?.url).toBe("https://dev.azure.com/acme/platform/_git/web/pullrequest/42");
80 });
81​
82 it.each([
83 ["active", "open"],
84 ["completed", "merged"],
85 ["abandoned", "closed"],
86 ["something new", "open"],
87 ])("reads the %s status as %s", (status, expected) => {
88 const batch = expectSuccess(decodePullRequestListJson(asJson([pullRequest({ status })])));
89​
90 expect(batch.items[0]?.state).toBe(expected);
91 });
92​
93 it.each([
94 ["succeeded", "mergeable"],
95 ["conflicts", "conflicting"],
96 ["rejectedByPolicy", "conflicting"],
97 ["queued", "unknown"],
98 ["notSet", "unknown"],
99 ])("reads the %s merge status as %s", (mergeStatus, expected) => {
100 const batch = expectSuccess(decodePullRequestListJson(asJson([pullRequest({ mergeStatus })])));
101​
102 expect(batch.items[0]?.mergeability).toBe(expected);
103 });
104​
105 it("stands the closing time in for a last-touched time Azure does not keep", () => {
106 const batch = expectSuccess(
107 decodePullRequestListJson(
108 asJson([pullRequest({ status: "completed", closedDate: "2026-07-05T00:00:00Z" })]),
109 ),
110 );
111​
112 expect(batch.items[0]).toMatchObject({
113 createdAt: "2026-07-01T00:00:00Z",
114 updatedAt: "2026-07-05T00:00:00Z",
115 });
116 });
117​
118 it("skips a malformed row but still counts it, so paging does not stop early", () => {
119 const batch = expectSuccess(
120 decodePullRequestListJson(asJson([{ pullRequestId: "nope" }, pullRequest()])),
121 );
122​
123 expect(batch.items).toHaveLength(1);
124 expect(batch.rawCount).toBe(2);
125 expect(batch.rawIndexes).toEqual([1]);
126 });
127});
128​
129describe("decodePullRequestJson", () => {
130 it("reads reviewers as review requests", () => {
131 const detail = expectSuccess(
132 decodePullRequestJson(
133 asJson(
134 pullRequest({
135 reviewers: [{ displayName: "Julius", uniqueName: "julius@acme.dev", vote: 10 }],
136 }),
137 ),
138 ),
139 );
140​
141 expect(detail?.reviewRequestLogins).toEqual(["julius@acme.dev"]);
142 expect(detail?.reviewers).toEqual([
143 { login: "julius@acme.dev", name: "Julius", avatarUrl: null },
144 ]);
145 });
146​
147 it("reads auto-complete from whoever armed it, and its absence as nobody", () => {
148 const armed = expectSuccess(
149 decodePullRequestJson(
150 asJson(pullRequest({ autoCompleteSetBy: { displayName: "Bilal Hassan" } })),
151 ),
152 );
153 expect(armed?.autoMergeEnabled).toBe(true);
154​
155 expect(expectSuccess(decodePullRequestJson(asJson(pullRequest())))?.autoMergeEnabled).toBe(
156 false,
157 );
158 });
159​
160 it("keeps the strategy stored with auto-complete", () => {
161 const armed = expectSuccess(
162 decodePullRequestJson(
163 asJson(
164 pullRequest({
165 autoCompleteSetBy: { displayName: "Bilal Hassan" },
166 completionOptions: { mergeStrategy: "squash" },
167 }),
168 ),
169 ),
170 );
171​
172 expect(armed).toMatchObject({ autoMergeEnabled: true, autoMergeMethod: "squash" });
173​
174 const unspecified = expectSuccess(
175 decodePullRequestJson(
176 asJson(
177 pullRequest({
178 autoCompleteSetBy: { displayName: "Bilal Hassan" },
179 completionOptions: { squashMerge: false },
180 }),
181 ),
182 ),
183 );
184 expect(unspecified?.autoMergeEnabled).toBe(true);
185 expect(unspecified?.autoMergeMethod).toBeUndefined();
186 });
187​
188 it("works out where the repository lives from what Azure returned", () => {
189 const detail = expectSuccess(decodePullRequestJson(asJson(pullRequest())));
190​
191 expect(detail?.location).toEqual({ project: "platform", repository: "web" });
192 });
193​
194 it("reports no repository location when Azure said too little to name one", () => {
195 const detail = expectSuccess(
196 decodePullRequestJson(
197 asJson(
198 pullRequest({
199 url: null,
200 repository: null,
201 _links: {
202 web: { href: "https://dev.azure.com/acme/platform/_git/web/pullrequest/42" },
203 },
204 }),
205 ),
206 ),
207 );
208​
209 expect(detail?.location).toBeNull();
210 });
211​
212 it("returns nothing when Azure gave no way to place the pull request at all", () => {
213 const detail = expectSuccess(
214 decodePullRequestJson(asJson(pullRequest({ url: null, repository: null }))),
215 );
216​
217 expect(detail).toBeNull();
218 });
219});
220​
221describe("decodeViewerJson", () => {
222 it("reads the signed-in account name", () => {
223 expect(expectSuccess(decodeViewerJson(asJson({ user: { name: "bilal@acme.dev" } })))).toBe(
224 "bilal@acme.dev",
225 );
226 });
227​
228 it("returns nothing when nobody is signed in", () => {
229 expect(expectSuccess(decodeViewerJson(asJson({ user: null })))).toBeNull();
230 });
231});
232​
233describe("decodeThreadsJson", () => {
234 it("takes every real comment of every thread, oldest first", () => {
235 const comments = expectSuccess(
236 decodeThreadsJson(
237 asJson({
238 value: [
239 {
240 id: 2,
241 comments: [
242 {
243 id: 1,
244 content: "Second remark.",
245 author: { displayName: "Julius", uniqueName: "julius@acme.dev" },
246 publishedDate: "2026-07-03T00:00:00Z",
247 },
248 ],
249 },
250 {
251 id: 1,
252 comments: [
253 { id: 1, content: "Bilal voted", commentType: "system", publishedDate: "x" },
254 {
255 id: 2,
256 content: "First remark.",
257 author: { displayName: "Bilal", uniqueName: "bilal@acme.dev" },
258 publishedDate: "2026-07-02T00:00:00Z",
259 },
260 ],
261 },
262 ],
263 }),
264 ),
265 );
266​
267 expect(comments.map((comment) => comment.body)).toEqual(["First remark.", "Second remark."]);
268 expect(comments[0]).toMatchObject({
269 kind: "issue-comment",
270 author: { login: "bilal@acme.dev" },
271 });
272 });
273​
274 it("reads a thread pinned to a file as a review comment", () => {
275 const comments = expectSuccess(
276 decodeThreadsJson(
277 asJson({
278 value: [
279 {
280 id: 3,
281 threadContext: { filePath: "/src/app.ts" },
282 comments: [{ id: 1, content: "Rename this.", publishedDate: "2026-07-02T00:00:00Z" }],
283 },
284 ],
285 }),
286 ),
287 );
288​
289 expect(comments[0]).toMatchObject({ kind: "review-comment", path: "/src/app.ts" });
290 });
291​
292 it("keeps the replies under a thread, which are as much of the conversation", () => {
293 const comments = expectSuccess(
294 decodeThreadsJson(
295 asJson({
296 value: [
297 {
298 id: 4,
299 threadContext: { filePath: "/src/app.ts" },
300 comments: [
301 { id: 1, content: "Rename this.", publishedDate: "2026-07-02T00:00:00Z" },
302 { id: 2, content: "Renamed.", publishedDate: "2026-07-02T01:00:00Z" },
303 { id: 3, content: "Thanks.", publishedDate: "2026-07-02T02:00:00Z" },
304 ],
305 },
306 ],
307 }),
308 ),
309 );
310​
311 expect(comments.map((comment) => comment.id)).toEqual(["4:1", "4:2", "4:3"]);
312 });
313​
314 it("drops deleted threads and threads with nothing to show", () => {
315 const comments = expectSuccess(
316 decodeThreadsJson(
317 asJson({
318 value: [
319 {
320 id: 1,
321 isDeleted: true,
322 comments: [{ id: 1, content: "gone", publishedDate: "2026-07-02T00:00:00Z" }],
323 },
324 { id: 2, comments: [] },
325 {
326 id: 3,
327 comments: [{ id: 1, content: " ", publishedDate: "2026-07-02T00:00:00Z" }],
328 },
329 ],
330 }),
331 ),
332 );
333​
334 expect(comments).toEqual([]);
335 });
336});
337​
338describe("decodeIterationsJson", () => {
339 const iteration = (id: number, head: string, base: string) => ({
340 id,
341 sourceRefCommit: { commitId: head },
342 commonRefCommit: { commitId: base },
343 targetRefCommit: { commitId: base },
344 });
345​
346 it("reads every push in order, oldest first", () => {
347 const iterations = expectSuccess(
348 decodeIterationsJson(
349 asJson({ value: [iteration(2, "bbb", "base"), iteration(1, "aaa", "base")] }),
350 ),
351 );
352​
353 expect(iterations.map((entry) => entry.id)).toEqual([1, 2]);
354 expect(iterations.at(-1)).toEqual({ id: 2, headCommit: "bbb", mergeBaseCommit: "base" });
355 });
356​
357 it("skips a push Azure could not place both ends of", () => {
358 const iterations = expectSuccess(
359 decodeIterationsJson(
360 asJson({
361 value: [{ id: 1, sourceRefCommit: { commitId: "aaa" } }, iteration(2, "bbb", "base")],
362 }),
363 ),
364 );
365​
366 expect(iterations.map((entry) => entry.id)).toEqual([2]);
367 });
368});
369​
370describe("decodeIterationChangesJson", () => {
371 it("names each changed file without the slash Azure leads its paths with", () => {
372 const page = expectSuccess(
373 decodeIterationChangesJson(
374 asJson({
375 changeEntries: [
376 { changeType: "add", item: { path: "/DEMO.md", objectId: "ec00" } },
377 {
378 changeType: "edit",
379 item: { path: "/README.md", objectId: "8f80", originalObjectId: "0ca4" },
380 },
381 { changeType: "delete", item: { path: "/OLD.md", originalObjectId: "1111" } },
382 ],
383 }),
384 ),
385 );
386​
387 expect(page.changes.map((change) => [change.path, change.changeKind])).toEqual([
388 ["DEMO.md", "new"],
389 ["README.md", "change"],
390 ["OLD.md", "deleted"],
391 ]);
392 });
393​
394 it("keeps a space at the end of a file's name, which belongs to the name", () => {
395 const page = expectSuccess(
396 decodeIterationChangesJson(
397 asJson({
398 changeEntries: [
399 { changeType: "edit", item: { path: "/docs/readme.md ", objectId: "ec00" } },
400 {
401 changeType: "rename",
402 sourceServerItem: "/docs/old.md ",
403 item: { path: "/docs/moved.md", objectId: "aaaa", originalObjectId: "aaaa" },
404 },
405 ],
406 }),
407 ),
408 );
409​
410 expect(page.changes.map((change) => [change.path, change.oldPath])).toEqual([
411 ["docs/readme.md ", "docs/readme.md "],
412 ["docs/moved.md", "docs/old.md "],
413 ]);
414 });
415​
416 it("reads a rename as one file that moved, and says whether it also changed", () => {
417 const page = expectSuccess(
418 decodeIterationChangesJson(
419 asJson({
420 changeEntries: [
421 {
422 changeType: "rename",
423 sourceServerItem: "/docs/old.md",
424 item: { path: "/docs/new.md", objectId: "aaaa", originalObjectId: "aaaa" },
425 },
426 {
427 changeType: "edit, rename",
428 sourceServerItem: "/src/old.ts",
429 item: { path: "/src/new.ts", objectId: "bbbb", originalObjectId: "cccc" },
430 },
431 ],
432 }),
433 ),
434 );
435​
436 expect(page.changes).toEqual([
437 {
438 path: "docs/new.md",
439 oldPath: "docs/old.md",
440 changeKind: "rename-pure",
441 objectId: "aaaa",
442 originalObjectId: "aaaa",
443 },
444 {
445 path: "src/new.ts",
446 oldPath: "src/old.ts",
447 changeKind: "rename-changed",
448 objectId: "bbbb",
449 originalObjectId: "cccc",
450 },
451 ]);
452 });
453​
454 it("drops the folders Azure lists alongside the files that changed", () => {
455 const page = expectSuccess(
456 decodeIterationChangesJson(
457 asJson({
458 changeEntries: [
459 { changeType: "add", item: { path: "/docs", isFolder: true, gitObjectType: "tree" } },
460 { changeType: "add", item: { path: "/docs/page.md", objectId: "dddd" } },
461 ],
462 }),
463 ),
464 );
465​
466 expect(page.changes.map((change) => change.path)).toEqual(["docs/page.md"]);
467 });
468​
469 it("carries where the next page of a long change starts", () => {
470 const page = expectSuccess(
471 decodeIterationChangesJson(
472 asJson({
473 changeEntries: [{ changeType: "add", item: { path: "/DEMO.md", objectId: "ec00" } }],
474 nextSkip: 2000,
475 }),
476 ),
477 );
478​
479 expect(page.nextSkip).toBe(2000);
480 });
481​
482 it("reads the last page, which names no page after it, as the end of the change", () => {
483 const page = expectSuccess(
484 decodeIterationChangesJson(
485 asJson({
486 changeEntries: [{ changeType: "add", item: { path: "/DEMO.md", objectId: "ec00" } }],
487 }),
488 ),
489 );
490​
491 expect(page.nextSkip).toBeNull();
492 });
493​
494 it("reads where a rename came from out of either of the two places Azure names it", () => {
495 const page = expectSuccess(
496 decodeIterationChangesJson(
497 asJson({
498 changeEntries: [
499 {
500 changeType: "rename",
501 originalPath: "/docs/old.md",
502 item: { path: "/docs/new.md", objectId: "aaaa", originalObjectId: "aaaa" },
503 },
504 ],
505 }),
506 ),
507 );
508​
509 expect(page.changes.at(0)?.oldPath).toBe("docs/old.md");
510 });
511});
512​
513describe("decodeItemContentJson", () => {
514 it("reads the file's text out of the envelope Azure wraps it in", () => {
515 expect(
516 expectSuccess(decodeItemContentJson(asJson({ path: "/a.md", content: "one\ntwo" }))),
517 ).toEqual({ contents: "one\ntwo", isBinary: false });
518 });
519​
520 it("reads an empty file as empty rather than as a failure to look", () => {
521 expect(expectSuccess(decodeItemContentJson(asJson({ path: "/a.md" })))).toEqual({
522 contents: "",
523 isBinary: false,
524 });
525 });
526​
527 it("keeps Azure's own word that a file is binary", () => {
528 expect(
529 expectSuccess(
530 decodeItemContentJson(
531 asJson({ path: "/logo.png", content: "b2xk", contentMetadata: { isBinary: true } }),
532 ),
533 ),
534 ).toEqual({ contents: "b2xk", isBinary: true });
535 });
536});
537​
538/**
539 * These are the shapes a real organisation answered with, read back through `az devops invoke`
540 * rather than `az rest`: the extension hands over the route's own body with one key of its own
541 * added, and a live repository leaves out fields the contract documents.
542 */
543describe("what az devops invoke answers with", () => {
544 it("reads the envelope the extension adds its own continuation token to", () => {
545 // Set on every JSON body it returns, from a response header these routes do not send, so it
546 // arrives as null rather than not at all.
547 expect(
548 expectSuccess(decodeThreadsJson(asJson({ value: [], count: 0, continuation_token: null }))),
549 ).toEqual([]);
550​
551 expect(
552 expectSuccess(
553 decodeIterationsJson(
554 asJson({
555 count: 1,
556 continuation_token: null,
557 value: [
558 {
559 id: 1,
560 sourceRefCommit: { commitId: "f4031105213c68f197cd46ea303bb5e72acc889a" },
561 commonRefCommit: { commitId: "acbc805382edd6c38b8d6de6ba9f9bba9da4ad35" },
562 },
563 ],
564 }),
565 ),
566 ),
567 ).toEqual([
568 {
569 id: 1,
570 headCommit: "f4031105213c68f197cd46ea303bb5e72acc889a",
571 mergeBaseCommit: "acbc805382edd6c38b8d6de6ba9f9bba9da4ad35",
572 },
573 ]);
574 });
575​
576 it("keeps the files of a change that names neither their object type nor their page after it", () => {
577 // A live iteration states `changeType`, `item.path` and `item.objectId` and nothing else: no
578 // `gitObjectType`, no `isFolder`, and no `nextSkip` on the only page.
579 const page = expectSuccess(
580 decodeIterationChangesJson(
581 asJson({
582 continuation_token: null,
583 changeEntries: [
584 { changeType: "add", item: { path: "/DEMO.md", objectId: "EC005DB24" } },
585 { changeType: "edit", item: { path: "/README.md", objectId: "8F8047A49" } },
586 ],
587 }),
588 ),
589 );
590​
591 expect(page.nextSkip).toBeNull();
592 expect(page.changes).toEqual([
593 {
594 path: "DEMO.md",
595 oldPath: "DEMO.md",
596 changeKind: "new",
597 objectId: "EC005DB24",
598 originalObjectId: null,
599 },
600 {
601 path: "README.md",
602 oldPath: "README.md",
603 changeKind: "change",
604 objectId: "8F8047A49",
605 originalObjectId: null,
606 },
607 ]);
608 });
609​
610 it("reads a text file whose content type Azure calls a stream", () => {
611 // `contentMetadata` comes back for a markdown file with `application/octet-stream` on it and
612 // no `isBinary` at all, so the content type is not the field to ask, and its absence is the
613 // answer that the text is text.
614 expect(
615 expectSuccess(
616 decodeItemContentJson(
617 asJson({
618 path: "/README.md",
619 objectId: "8f8047a49",
620 gitObjectType: "blob",
621 content: "# T3Demo\n",
622 contentMetadata: {
623 contentType: "application/octet-stream",
624 encoding: 65001,
625 extension: "md",
626 fileName: "README.md",
627 },
628 continuation_token: null,
629 }),
630 ),
631 ),
632 ).toEqual({ contents: "# T3Demo\n", isBinary: false });
633 });
634});