The best way to control Claude Code, Codex and more from any computer or phone

main
ts 679 lines 21 KB
Raw
1 import * as Result from "effect/Result";
2 import { describe, expect, it } from "vite-plus/test";
3
4 import {
5 decodeItemContentJson,
6 decodeIterationChangesJson,
7 decodeIterationsJson,
8 decodePolicyChecksJson,
9 decodePullRequestJson,
10 decodePullRequestListJson,
11 decodeThreadsJson,
12 } from "./azureDevOpsPullRequestJson.ts";
13
14 const REST_URL =
15 "https://dev.azure.com/acme/_apis/git/repositories/6f9c9b7f-0000-0000-0000-000000000000/pullRequests/42";
16
17 function 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 };
33 }
34
35 function 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;
39 }
40
41 const asJson = (value: unknown) => JSON.stringify(value);
42
43 describe("decodePullRequestListJson", () => {
44 it("reads a pull request as a change request", () => {
45 const batch = expectSuccess(
46 decodePullRequestListJson(
47 asJson([
48 pullRequest({
49 labels: [{ id: "4f1c", name: "release-1.0", active: true, url: null }],
50 }),
51 ]),
52 ),
53 );
54
55 expect(batch.items).toHaveLength(1);
56 expect(batch.items[0]).toMatchObject({
57 number: 42,
58 title: "Add the change requests page",
59 author: { login: "bilal@acme.dev", name: "Bilal Hassan" },
60 headBranch: "feat/page",
61 baseBranch: "main",
62 state: "open",
63 isDraft: false,
64 mergeability: "mergeable",
65 labels: [{ name: "release-1.0" }],
66 });
67 });
68
69 it("assembles a browser url when Azure reports no web link", () => {
70 const batch = expectSuccess(decodePullRequestListJson(asJson([pullRequest()])));
71
72 expect(batch.items[0]?.url).toBe("https://dev.azure.com/acme/platform/_git/web/pullrequest/42");
73 });
74
75 it.each([
76 ["completed", "merged"],
77 ["something new", "open"],
78 ])("reads the %s status as %s", (status, expected) => {
79 const batch = expectSuccess(decodePullRequestListJson(asJson([pullRequest({ status })])));
80
81 expect(batch.items[0]?.state).toBe(expected);
82 });
83
84 it.each([
85 ["succeeded", "mergeable"],
86 ["conflicts", "conflicting"],
87 ["queued", "unknown"],
88 ])("reads the %s merge status as %s", (mergeStatus, expected) => {
89 const batch = expectSuccess(decodePullRequestListJson(asJson([pullRequest({ mergeStatus })])));
90
91 expect(batch.items[0]?.mergeability).toBe(expected);
92 });
93
94 it("stands the closing time in for a last-touched time Azure does not keep", () => {
95 const batch = expectSuccess(
96 decodePullRequestListJson(
97 asJson([pullRequest({ status: "completed", closedDate: "2026-07-05T00:00:00Z" })]),
98 ),
99 );
100
101 expect(batch.items[0]).toMatchObject({
102 createdAt: "2026-07-01T00:00:00Z",
103 updatedAt: "2026-07-05T00:00:00Z",
104 });
105 });
106
107 it("skips a malformed row but still counts it, so paging does not stop early", () => {
108 const batch = expectSuccess(
109 decodePullRequestListJson(asJson([{ pullRequestId: "nope" }, pullRequest()])),
110 );
111
112 expect(batch.items).toHaveLength(1);
113 expect(batch.rawCount).toBe(2);
114 expect(batch.rawIndexes).toEqual([1]);
115 });
116 });
117
118 describe("decodePullRequestJson", () => {
119 it("keeps the strategy stored with auto-complete", () => {
120 const armed = expectSuccess(
121 decodePullRequestJson(
122 asJson(
123 pullRequest({
124 autoCompleteSetBy: { displayName: "Bilal Hassan" },
125 completionOptions: { mergeStrategy: "squash" },
126 }),
127 ),
128 ),
129 );
130
131 expect(armed).toMatchObject({ autoMergeEnabled: true, autoMergeMethod: "squash" });
132
133 const unspecified = expectSuccess(
134 decodePullRequestJson(
135 asJson(
136 pullRequest({
137 autoCompleteSetBy: { displayName: "Bilal Hassan" },
138 completionOptions: { squashMerge: false },
139 }),
140 ),
141 ),
142 );
143 expect(unspecified?.autoMergeEnabled).toBe(true);
144 expect(unspecified?.autoMergeMethod).toBeUndefined();
145 });
146
147 it("works out where the repository lives from what Azure returned", () => {
148 const detail = expectSuccess(decodePullRequestJson(asJson(pullRequest())));
149
150 expect(detail?.location).toEqual({ project: "platform", repository: "web" });
151 });
152
153 it("reports no repository location when Azure said too little to name one", () => {
154 const detail = expectSuccess(
155 decodePullRequestJson(
156 asJson(
157 pullRequest({
158 url: null,
159 repository: null,
160 _links: {
161 web: { href: "https://dev.azure.com/acme/platform/_git/web/pullrequest/42" },
162 },
163 }),
164 ),
165 ),
166 );
167
168 expect(detail?.location).toBeNull();
169 });
170
171 it("returns nothing when Azure gave no way to place the pull request at all", () => {
172 const detail = expectSuccess(
173 decodePullRequestJson(asJson(pullRequest({ url: null, repository: null }))),
174 );
175
176 expect(detail).toBeNull();
177 });
178 });
179
180 describe("decodeThreadsJson", () => {
181 it("takes every real comment of every thread, oldest first", () => {
182 const comments = expectSuccess(
183 decodeThreadsJson(
184 asJson({
185 value: [
186 {
187 id: 2,
188 comments: [
189 {
190 id: 1,
191 content: "Second remark.",
192 author: { displayName: "Julius", uniqueName: "julius@acme.dev" },
193 publishedDate: "2026-07-03T00:00:00Z",
194 },
195 ],
196 },
197 {
198 id: 1,
199 comments: [
200 { id: 1, content: "Bilal voted", commentType: "system", publishedDate: "x" },
201 {
202 id: 2,
203 content: "First remark.",
204 author: { displayName: "Bilal", uniqueName: "bilal@acme.dev" },
205 publishedDate: "2026-07-02T00:00:00Z",
206 },
207 ],
208 },
209 ],
210 }),
211 ),
212 );
213
214 expect(comments.map((comment) => comment.body)).toEqual(["First remark.", "Second remark."]);
215 expect(comments[0]).toMatchObject({
216 kind: "issue-comment",
217 author: { login: "bilal@acme.dev" },
218 });
219 });
220
221 it("reads a thread pinned to a file as a review comment on the path its diff uses", () => {
222 const comments = expectSuccess(
223 decodeThreadsJson(
224 asJson({
225 value: [
226 {
227 id: 3,
228 threadContext: { filePath: "/src/app.ts" },
229 comments: [{ id: 1, content: "Rename this.", publishedDate: "2026-07-02T00:00:00Z" }],
230 },
231 ],
232 }),
233 ),
234 );
235
236 expect(comments[0]).toMatchObject({ kind: "review-comment", path: "src/app.ts" });
237 });
238
239 it("keeps the replies under a thread, which are as much of the conversation", () => {
240 const comments = expectSuccess(
241 decodeThreadsJson(
242 asJson({
243 value: [
244 {
245 id: 4,
246 threadContext: { filePath: "/src/app.ts" },
247 comments: [
248 { id: 1, content: "Rename this.", publishedDate: "2026-07-02T00:00:00Z" },
249 { id: 2, content: "Renamed.", publishedDate: "2026-07-02T01:00:00Z" },
250 { id: 3, content: "Thanks.", publishedDate: "2026-07-02T02:00:00Z" },
251 ],
252 },
253 ],
254 }),
255 ),
256 );
257
258 expect(comments.map((comment) => comment.id)).toEqual(["4:1", "4:2", "4:3"]);
259 });
260
261 it("drops deleted threads and threads with nothing to show", () => {
262 const comments = expectSuccess(
263 decodeThreadsJson(
264 asJson({
265 value: [
266 {
267 id: 1,
268 isDeleted: true,
269 comments: [{ id: 1, content: "gone", publishedDate: "2026-07-02T00:00:00Z" }],
270 },
271 { id: 2, comments: [] },
272 {
273 id: 3,
274 comments: [{ id: 1, content: " ", publishedDate: "2026-07-02T00:00:00Z" }],
275 },
276 ],
277 }),
278 ),
279 );
280
281 expect(comments).toEqual([]);
282 });
283 });
284
285 describe("decodeIterationsJson", () => {
286 const iteration = (id: number, head: string, base: string) => ({
287 id,
288 sourceRefCommit: { commitId: head },
289 commonRefCommit: { commitId: base },
290 targetRefCommit: { commitId: base },
291 });
292
293 it("reads every push in order, oldest first", () => {
294 const iterations = expectSuccess(
295 decodeIterationsJson(
296 asJson({ value: [iteration(2, "bbb", "base"), iteration(1, "aaa", "base")] }),
297 ),
298 );
299
300 expect(iterations.map((entry) => entry.id)).toEqual([1, 2]);
301 expect(iterations.at(-1)).toEqual({ id: 2, headCommit: "bbb", mergeBaseCommit: "base" });
302 });
303
304 it("skips a push Azure could not place both ends of", () => {
305 const iterations = expectSuccess(
306 decodeIterationsJson(
307 asJson({
308 value: [{ id: 1, sourceRefCommit: { commitId: "aaa" } }, iteration(2, "bbb", "base")],
309 }),
310 ),
311 );
312
313 expect(iterations.map((entry) => entry.id)).toEqual([2]);
314 });
315 });
316
317 describe("decodeIterationChangesJson", () => {
318 it("names each changed file without the slash Azure leads its paths with", () => {
319 const page = expectSuccess(
320 decodeIterationChangesJson(
321 asJson({
322 changeEntries: [
323 { changeType: "add", item: { path: "/DEMO.md", objectId: "ec00" } },
324 {
325 changeType: "edit",
326 item: { path: "/README.md", objectId: "8f80", originalObjectId: "0ca4" },
327 },
328 { changeType: "delete", item: { path: "/OLD.md", originalObjectId: "1111" } },
329 // Azure names a deleted file only where it came from.
330 {
331 changeType: "delete",
332 originalPath: "/GONE.md",
333 item: { path: null, objectId: "2222", originalObjectId: "2222" },
334 },
335 { changeType: "delete", sourceServerItem: "/src/gone.ts", item: { objectId: null } },
336 { changeType: "delete", item: { originalObjectId: "3333" } },
337 { changeType: "edit", originalPath: "/LOST.md", item: { objectId: "4444" } },
338 ],
339 }),
340 ),
341 );
342
343 expect(
344 page.changes.map((change) => [
345 change.path,
346 change.oldPath,
347 change.changeKind,
348 change.objectId,
349 ]),
350 ).toEqual([
351 ["DEMO.md", "DEMO.md", "new", "ec00"],
352 ["README.md", "README.md", "change", "8f80"],
353 ["OLD.md", "OLD.md", "deleted", null],
354 ["GONE.md", "GONE.md", "deleted", null],
355 ["src/gone.ts", "src/gone.ts", "deleted", null],
356 ]);
357 });
358
359 it("keeps a space at the end of a file's name, which belongs to the name", () => {
360 const page = expectSuccess(
361 decodeIterationChangesJson(
362 asJson({
363 changeEntries: [
364 { changeType: "edit", item: { path: "/docs/readme.md ", objectId: "ec00" } },
365 {
366 changeType: "rename",
367 sourceServerItem: "/docs/old.md ",
368 item: { path: "/docs/moved.md", objectId: "aaaa", originalObjectId: "aaaa" },
369 },
370 ],
371 }),
372 ),
373 );
374
375 expect(page.changes.map((change) => [change.path, change.oldPath])).toEqual([
376 ["docs/readme.md ", "docs/readme.md "],
377 ["docs/moved.md", "docs/old.md "],
378 ]);
379 });
380
381 it("reads a rename as one file that moved, and says whether it also changed", () => {
382 const page = expectSuccess(
383 decodeIterationChangesJson(
384 asJson({
385 changeEntries: [
386 {
387 changeType: "rename",
388 sourceServerItem: "/docs/old.md",
389 item: { path: "/docs/new.md", objectId: "aaaa", originalObjectId: "aaaa" },
390 },
391 {
392 changeType: "edit, rename",
393 sourceServerItem: "/src/old.ts",
394 item: { path: "/src/new.ts", objectId: "bbbb", originalObjectId: "cccc" },
395 },
396 ],
397 }),
398 ),
399 );
400
401 expect(page.changes).toEqual([
402 {
403 path: "docs/new.md",
404 oldPath: "docs/old.md",
405 changeKind: "rename-pure",
406 objectId: "aaaa",
407 originalObjectId: "aaaa",
408 },
409 {
410 path: "src/new.ts",
411 oldPath: "src/old.ts",
412 changeKind: "rename-changed",
413 objectId: "bbbb",
414 originalObjectId: "cccc",
415 },
416 ]);
417 });
418
419 it("drops the folders Azure lists alongside the files that changed", () => {
420 const page = expectSuccess(
421 decodeIterationChangesJson(
422 asJson({
423 changeEntries: [
424 { changeType: "add", item: { path: "/docs", isFolder: true, gitObjectType: "tree" } },
425 { changeType: "add", item: { path: "/docs/page.md", objectId: "dddd" } },
426 ],
427 }),
428 ),
429 );
430
431 expect(page.changes.map((change) => change.path)).toEqual(["docs/page.md"]);
432 });
433
434 it("reads where a rename came from out of either of the two places Azure names it", () => {
435 const page = expectSuccess(
436 decodeIterationChangesJson(
437 asJson({
438 changeEntries: [
439 {
440 changeType: "rename",
441 originalPath: "/docs/old.md",
442 item: { path: "/docs/new.md", objectId: "aaaa", originalObjectId: "aaaa" },
443 },
444 ],
445 }),
446 ),
447 );
448
449 expect(page.changes.at(0)?.oldPath).toBe("docs/old.md");
450 });
451 });
452
453 describe("decodeItemContentJson", () => {
454 it("reads an empty file as empty rather than as a failure to look", () => {
455 expect(expectSuccess(decodeItemContentJson(asJson({ path: "/a.md" })))).toEqual({
456 contents: "",
457 isBinary: false,
458 });
459 });
460 });
461
462 describe("what az devops invoke answers with", () => {
463 it("reads the envelope the extension adds its own continuation token to", () => {
464 expect(
465 expectSuccess(decodeThreadsJson(asJson({ value: [], count: 0, continuation_token: null }))),
466 ).toEqual([]);
467
468 expect(
469 expectSuccess(
470 decodeIterationsJson(
471 asJson({
472 count: 1,
473 continuation_token: null,
474 value: [
475 {
476 id: 1,
477 sourceRefCommit: { commitId: "f4031105213c68f197cd46ea303bb5e72acc889a" },
478 commonRefCommit: { commitId: "acbc805382edd6c38b8d6de6ba9f9bba9da4ad35" },
479 },
480 ],
481 }),
482 ),
483 ),
484 ).toEqual([
485 {
486 id: 1,
487 headCommit: "f4031105213c68f197cd46ea303bb5e72acc889a",
488 mergeBaseCommit: "acbc805382edd6c38b8d6de6ba9f9bba9da4ad35",
489 },
490 ]);
491 });
492
493 it("keeps the files of a change that names neither their object type nor their page after it", () => {
494 const page = expectSuccess(
495 decodeIterationChangesJson(
496 asJson({
497 continuation_token: null,
498 changeEntries: [
499 { changeType: "add", item: { path: "/DEMO.md", objectId: "EC005DB24" } },
500 { changeType: "edit", item: { path: "/README.md", objectId: "8F8047A49" } },
501 ],
502 }),
503 ),
504 );
505
506 expect(page.nextSkip).toBeNull();
507 expect(page.changes).toEqual([
508 {
509 path: "DEMO.md",
510 oldPath: "DEMO.md",
511 changeKind: "new",
512 objectId: "EC005DB24",
513 originalObjectId: null,
514 },
515 {
516 path: "README.md",
517 oldPath: "README.md",
518 changeKind: "change",
519 objectId: "8F8047A49",
520 originalObjectId: null,
521 },
522 ]);
523 });
524
525 it("reads a text file whose content type Azure calls a stream", () => {
526 expect(
527 expectSuccess(
528 decodeItemContentJson(
529 asJson({
530 path: "/README.md",
531 objectId: "8f8047a49",
532 gitObjectType: "blob",
533 content: "# T3Demo\n",
534 contentMetadata: {
535 contentType: "application/octet-stream",
536 encoding: 65001,
537 extension: "md",
538 fileName: "README.md",
539 },
540 continuation_token: null,
541 }),
542 ),
543 ),
544 ).toEqual({ contents: "# T3Demo\n", isBinary: false });
545 });
546 });
547
548 describe("decodePolicyChecksJson", () => {
549 const BUILD_TYPE = "0609b952-1397-4640-95ec-e00a01b2c241";
550 const STATUS_TYPE = "cbdc66da-9728-4af8-aada-9a5a32e4a226";
551 const CONFIGURATION_URL =
552 "https://dev.azure.com/acme/6f9c9b7f-0000-0000-0000-000000000000/_apis/policy/configurations/54";
553
554 const evaluation = (
555 status: string,
556 context: Record<string, unknown> | null,
557 settings: Record<string, unknown> = { displayName: "PR validation" },
558 configuration: Record<string, unknown> = {},
559 ) => ({
560 status,
561 context,
562 configuration: {
563 url: CONFIGURATION_URL,
564 isBlocking: true,
565 type: { id: BUILD_TYPE, displayName: "Build" },
566 settings,
567 ...configuration,
568 },
569 });
570
571 const decodeOne = (value: unknown, isDraft = false) =>
572 expectSuccess(decodePolicyChecksJson(asJson([value]), { isDraft })).checks[0];
573
574 it.each([
575 ["approved", "success"],
576 ["running", "pending"],
577 ["rejected", "failure"],
578 ["broken", "failure"],
579 ["notApplicable", "skipped"],
580 ["someNewStatus", "pending"],
581 ])("reads a build evaluation %s as %s", (status, expected) => {
582 expect(decodeOne(evaluation(status, { buildId: 21959 }))?.status).toBe(expected);
583 });
584
585 it("links a build to its results page, in the project the policy belongs to", () => {
586 expect(decodeOne(evaluation("running", { buildId: 21959 }))).toEqual({
587 name: "PR validation",
588 status: "pending",
589 description: null,
590 url: "https://dev.azure.com/acme/6f9c9b7f-0000-0000-0000-000000000000/_build/results?buildId=21959",
591 required: true,
592 });
593 });
594
595 it("names a build policy without its own name after the pipeline it runs", () => {
596 const check = decodeOne(evaluation("approved", { buildDefinitionName: "PR - Validation" }, {}));
597 expect(check?.name).toBe("PR - Validation");
598 expect(check?.url).toBeNull();
599 });
600
601 it("reads a status policy, which reports an external service's verdict", () => {
602 expect(
603 decodeOne(
604 evaluation(
605 "rejected",
606 null,
607 { defaultDisplayName: "SonarCloud", statusName: "gate" },
608 {
609 type: { id: STATUS_TYPE, displayName: "Status" },
610 },
611 ),
612 ),
613 ).toMatchObject({ name: "SonarCloud", status: "failure", url: null });
614 });
615
616 // Both arrive as `queued`; a spinner would never stop on either.
617 it.each([
618 {
619 name: "an expired build",
620 value: evaluation("queued", { buildId: 21919, isExpired: true }),
621 },
622 {
623 name: "a build only a person can queue",
624 value: evaluation("queued", null, { displayName: "Nightly", manualQueueOnly: true }),
625 },
626 ])("asks for action on $name", ({ value }) => {
627 const check = decodeOne(value);
628 expect(check?.status).toBe("action-required");
629 expect(check?.description).not.toBeNull();
630 });
631
632 // A watch waits on every check that asks for action, and nobody has to queue an optional one.
633 it.each([
634 { name: "expired", value: evaluation("queued", { buildId: 21919, isExpired: true }) },
635 {
636 name: "manual-only",
637 value: evaluation("queued", null, { displayName: "Nightly", manualQueueOnly: true }),
638 },
639 ])("skips an optional $name build that nobody queued", ({ value }) => {
640 const check = decodeOne({
641 ...value,
642 configuration: { ...value.configuration, isBlocking: false },
643 });
644 expect(check?.status).toBe("skipped");
645 expect(check?.description).not.toBeNull();
646 });
647
648 it("reports nothing for a build Azure holds back on a draft", () => {
649 expect(decodeOne(evaluation("queued", null), true)).toBeUndefined();
650 expect(decodeOne(evaluation("running", { buildId: 21960 }), true)?.status).toBe("pending");
651 });
652
653 it("keeps a build that is really waiting for an agent as running", () => {
654 expect(decodeOne(evaluation("queued", { buildId: 21960, isExpired: false }))?.status).toBe(
655 "pending",
656 );
657 expect(decodeOne(evaluation("queued", null))?.status).toBe("pending");
658 });
659
660 it.each([
661 {
662 name: "a review gate, which waits on people",
663 configuration: { type: { id: "fa4e907d-c16b-4a4c-9dfa-4906e5d171dd" } },
664 },
665 { name: "a disabled policy", configuration: { isEnabled: false } },
666 { name: "a deleted policy", configuration: { isDeleted: true } },
667 ])("leaves out $name but still counts the row for paging", ({ configuration }) => {
668 const row = evaluation("queued", null, {}, configuration);
669 expect(expectSuccess(decodePolicyChecksJson(asJson([row]), { isDraft: false }))).toEqual({
670 checks: [],
671 rawCount: 1,
672 });
673 });
674
675 it("fails the page on a row it cannot read, which could be a required check", () => {
676 const rows = asJson([evaluation("rejected", null), { status: 7 }]);
677 expect(Result.isFailure(decodePolicyChecksJson(rows, { isDraft: false }))).toBe(true);
678 });
679 });