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