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

azureDevOpsPullRequestJson.test.ts

343 lines10.6 KB

1import * as Result from "effect/Result";
2import { describe, expect, it } from "vite-plus/test";
3​
4import {
5 decodePullRequestJson,
6 decodePullRequestListJson,
7 decodeThreadsJson,
8 decodeViewerJson,
9} from "./azureDevOpsPullRequestJson.ts";
10​
11const REST_URL =
12 "https://dev.azure.com/acme/_apis/git/repositories/6f9c9b7f-0000-0000-0000-000000000000/pullRequests/42";
13​
14/** Shaped after Azure's `GitPullRequest`, trimmed to the fields that are read. */
15function pullRequest(overrides: Record<string, unknown> = {}): Record<string, unknown> {
16 return {
17 pullRequestId: 42,
18 title: "Add the change requests page",
19 description: "Ships the page.",
20 status: "active",
21 isDraft: false,
22 mergeStatus: "succeeded",
23 createdBy: { displayName: "Bilal Hassan", uniqueName: "bilal@acme.dev" },
24 sourceRefName: "refs/heads/feat/page",
25 targetRefName: "refs/heads/main",
26 creationDate: "2026-07-01T00:00:00Z",
27 url: REST_URL,
28 repository: { name: "web", project: { name: "platform" } },
29 ...overrides,
30 };
32​
33function expectSuccess<A>(result: Result.Result<A, unknown>): A {
34 expect(Result.isSuccess(result)).toBe(true);
35 if (!Result.isSuccess(result)) throw new Error("expected a successful decode");
36 return result.success;
38​
39const asJson = (value: unknown) => JSON.stringify(value);
40​
41describe("decodePullRequestListJson", () => {
42 it("reads a pull request as a change request", () => {
43 const batch = expectSuccess(decodePullRequestListJson(asJson([pullRequest()])));
44​
45 expect(batch.items).toHaveLength(1);
46 expect(batch.items[0]).toMatchObject({
47 number: 42,
48 title: "Add the change requests page",
49 // The login is an email, because that is what `az account show` reports to compare with.
50 author: { login: "bilal@acme.dev", name: "Bilal Hassan" },
51 // Azure prefixes its refs, which no other host does.
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 // Azure leaves the field out entirely rather than sending it empty, so its absence is the
156 // whole of what it says about auto-complete being off.
157 expect(expectSuccess(decodePullRequestJson(asJson(pullRequest())))?.autoMergeEnabled).toBe(
158 false,
159 );
160 });
161​
162 it("keeps the strategy stored with auto-complete", () => {
163 const armed = expectSuccess(
164 decodePullRequestJson(
165 asJson(
166 pullRequest({
167 autoCompleteSetBy: { displayName: "Bilal Hassan" },
168 completionOptions: { mergeStrategy: "squash" },
169 }),
170 ),
171 ),
172 );
173​
174 expect(armed).toMatchObject({ autoMergeEnabled: true, autoMergeMethod: "squash" });
175​
176 const unspecified = expectSuccess(
177 decodePullRequestJson(
178 asJson(
179 pullRequest({
180 autoCompleteSetBy: { displayName: "Bilal Hassan" },
181 completionOptions: { squashMerge: false },
182 }),
183 ),
184 ),
185 );
186 expect(unspecified?.autoMergeEnabled).toBe(true);
187 expect(unspecified?.autoMergeMethod).toBeUndefined();
188 });
189​
190 it("works out where the conversation lives from what Azure returned", () => {
191 const detail = expectSuccess(decodePullRequestJson(asJson(pullRequest())));
192​
193 expect(detail?.threadsUrl).toBe(
194 "https://dev.azure.com/acme/platform/_apis/git/repositories/web/pullRequests/42/threads",
195 );
196 });
197​
198 it("reports no conversation url when Azure said too little to build one", () => {
199 // A web link places the pull request, but without the REST url and repository there is
200 // nothing to hang a threads collection off.
201 const detail = expectSuccess(
202 decodePullRequestJson(
203 asJson(
204 pullRequest({
205 url: null,
206 repository: null,
207 _links: {
208 web: { href: "https://dev.azure.com/acme/platform/_git/web/pullrequest/42" },
209 },
210 }),
211 ),
212 ),
213 );
214​
215 expect(detail?.threadsUrl).toBeNull();
216 });
217​
218 it("returns nothing when Azure gave no way to place the pull request at all", () => {
219 const detail = expectSuccess(
220 decodePullRequestJson(asJson(pullRequest({ url: null, repository: null }))),
221 );
222​
223 expect(detail).toBeNull();
224 });
225});
226​
227describe("decodeViewerJson", () => {
228 it("reads the signed-in account name", () => {
229 expect(expectSuccess(decodeViewerJson(asJson({ user: { name: "bilal@acme.dev" } })))).toBe(
230 "bilal@acme.dev",
231 );
232 });
233​
234 it("returns nothing when nobody is signed in", () => {
235 expect(expectSuccess(decodeViewerJson(asJson({ user: null })))).toBeNull();
236 });
237});
238​
239describe("decodeThreadsJson", () => {
240 it("takes every real comment of every thread, oldest first", () => {
241 const comments = expectSuccess(
242 decodeThreadsJson(
243 asJson({
244 value: [
245 {
246 id: 2,
247 comments: [
248 {
249 id: 1,
250 content: "Second remark.",
251 author: { displayName: "Julius", uniqueName: "julius@acme.dev" },
252 publishedDate: "2026-07-03T00:00:00Z",
253 },
254 ],
255 },
256 {
257 id: 1,
258 comments: [
259 // Azure's own activity notes are events rather than remarks.
260 { id: 1, content: "Bilal voted", commentType: "system", publishedDate: "x" },
261 {
262 id: 2,
263 content: "First remark.",
264 author: { displayName: "Bilal", uniqueName: "bilal@acme.dev" },
265 publishedDate: "2026-07-02T00:00:00Z",
266 },
267 ],
268 },
269 ],
270 }),
271 ),
272 );
273​
274 expect(comments.map((comment) => comment.body)).toEqual(["First remark.", "Second remark."]);
275 expect(comments[0]).toMatchObject({
276 kind: "issue-comment",
277 author: { login: "bilal@acme.dev" },
278 });
279 });
280​
281 it("reads a thread pinned to a file as a review comment", () => {
282 const comments = expectSuccess(
283 decodeThreadsJson(
284 asJson({
285 value: [
286 {
287 id: 3,
288 threadContext: { filePath: "/src/app.ts" },
289 comments: [{ id: 1, content: "Rename this.", publishedDate: "2026-07-02T00:00:00Z" }],
290 },
291 ],
292 }),
293 ),
294 );
295​
296 expect(comments[0]).toMatchObject({ kind: "review-comment", path: "/src/app.ts" });
297 });
298​
299 it("keeps the replies under a thread, which are as much of the conversation", () => {
300 const comments = expectSuccess(
301 decodeThreadsJson(
302 asJson({
303 value: [
304 {
305 id: 4,
306 threadContext: { filePath: "/src/app.ts" },
307 comments: [
308 { id: 1, content: "Rename this.", publishedDate: "2026-07-02T00:00:00Z" },
309 { id: 2, content: "Renamed.", publishedDate: "2026-07-02T01:00:00Z" },
310 { id: 3, content: "Thanks.", publishedDate: "2026-07-02T02:00:00Z" },
311 ],
312 },
313 ],
314 }),
315 ),
316 );
317​
318 expect(comments.map((comment) => comment.id)).toEqual(["4:1", "4:2", "4:3"]);
319 });
320​
321 it("drops deleted threads and threads with nothing to show", () => {
322 const comments = expectSuccess(
323 decodeThreadsJson(
324 asJson({
325 value: [
326 {
327 id: 1,
328 isDeleted: true,
329 comments: [{ id: 1, content: "gone", publishedDate: "2026-07-02T00:00:00Z" }],
330 },
331 { id: 2, comments: [] },
332 {
333 id: 3,
334 comments: [{ id: 1, content: " ", publishedDate: "2026-07-02T00:00:00Z" }],
335 },
336 ],
337 }),
338 ),
339 );
340​
341 expect(comments).toEqual([]);
342 });
343});