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

azureDevOpsPullRequestJson.test.ts

315 lines9.8 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("works out where the conversation lives from what Azure returned", () => {
163 const detail = expectSuccess(decodePullRequestJson(asJson(pullRequest())));
164​
165 expect(detail?.threadsUrl).toBe(
166 "https://dev.azure.com/acme/platform/_apis/git/repositories/web/pullRequests/42/threads",
167 );
168 });
169​
170 it("reports no conversation url when Azure said too little to build one", () => {
171 // A web link places the pull request, but without the REST url and repository there is
172 // nothing to hang a threads collection off.
173 const detail = expectSuccess(
174 decodePullRequestJson(
175 asJson(
176 pullRequest({
177 url: null,
178 repository: null,
179 _links: {
180 web: { href: "https://dev.azure.com/acme/platform/_git/web/pullrequest/42" },
181 },
182 }),
183 ),
184 ),
185 );
186​
187 expect(detail?.threadsUrl).toBeNull();
188 });
189​
190 it("returns nothing when Azure gave no way to place the pull request at all", () => {
191 const detail = expectSuccess(
192 decodePullRequestJson(asJson(pullRequest({ url: null, repository: null }))),
193 );
194​
195 expect(detail).toBeNull();
196 });
197});
198​
199describe("decodeViewerJson", () => {
200 it("reads the signed-in account name", () => {
201 expect(expectSuccess(decodeViewerJson(asJson({ user: { name: "bilal@acme.dev" } })))).toBe(
202 "bilal@acme.dev",
203 );
204 });
205​
206 it("returns nothing when nobody is signed in", () => {
207 expect(expectSuccess(decodeViewerJson(asJson({ user: null })))).toBeNull();
208 });
209});
210​
211describe("decodeThreadsJson", () => {
212 it("takes every real comment of every thread, oldest first", () => {
213 const comments = expectSuccess(
214 decodeThreadsJson(
215 asJson({
216 value: [
217 {
218 id: 2,
219 comments: [
220 {
221 id: 1,
222 content: "Second remark.",
223 author: { displayName: "Julius", uniqueName: "julius@acme.dev" },
224 publishedDate: "2026-07-03T00:00:00Z",
225 },
226 ],
227 },
228 {
229 id: 1,
230 comments: [
231 // Azure's own activity notes are events rather than remarks.
232 { id: 1, content: "Bilal voted", commentType: "system", publishedDate: "x" },
233 {
234 id: 2,
235 content: "First remark.",
236 author: { displayName: "Bilal", uniqueName: "bilal@acme.dev" },
237 publishedDate: "2026-07-02T00:00:00Z",
238 },
239 ],
240 },
241 ],
242 }),
243 ),
244 );
245​
246 expect(comments.map((comment) => comment.body)).toEqual(["First remark.", "Second remark."]);
247 expect(comments[0]).toMatchObject({
248 kind: "issue-comment",
249 author: { login: "bilal@acme.dev" },
250 });
251 });
252​
253 it("reads a thread pinned to a file as a review comment", () => {
254 const comments = expectSuccess(
255 decodeThreadsJson(
256 asJson({
257 value: [
258 {
259 id: 3,
260 threadContext: { filePath: "/src/app.ts" },
261 comments: [{ id: 1, content: "Rename this.", publishedDate: "2026-07-02T00:00:00Z" }],
262 },
263 ],
264 }),
265 ),
266 );
267​
268 expect(comments[0]).toMatchObject({ kind: "review-comment", path: "/src/app.ts" });
269 });
270​
271 it("keeps the replies under a thread, which are as much of the conversation", () => {
272 const comments = expectSuccess(
273 decodeThreadsJson(
274 asJson({
275 value: [
276 {
277 id: 4,
278 threadContext: { filePath: "/src/app.ts" },
279 comments: [
280 { id: 1, content: "Rename this.", publishedDate: "2026-07-02T00:00:00Z" },
281 { id: 2, content: "Renamed.", publishedDate: "2026-07-02T01:00:00Z" },
282 { id: 3, content: "Thanks.", publishedDate: "2026-07-02T02:00:00Z" },
283 ],
284 },
285 ],
286 }),
287 ),
288 );
289​
290 expect(comments.map((comment) => comment.id)).toEqual(["4:1", "4:2", "4:3"]);
291 });
292​
293 it("drops deleted threads and threads with nothing to show", () => {
294 const comments = expectSuccess(
295 decodeThreadsJson(
296 asJson({
297 value: [
298 {
299 id: 1,
300 isDeleted: true,
301 comments: [{ id: 1, content: "gone", publishedDate: "2026-07-02T00:00:00Z" }],
302 },
303 { id: 2, comments: [] },
304 {
305 id: 3,
306 comments: [{ id: 1, content: " ", publishedDate: "2026-07-02T00:00:00Z" }],
307 },
308 ],
309 }),
310 ),
311 );
312​
313 expect(comments).toEqual([]);
314 });
315});