Open History to see the last commit
1import { ProjectId, type ThreadPullRequestLink } from "@t3tools/contracts";
2import { describe, expect, it } from "vite-plus/test";
3
4import { presentThreadLinkedPullRequests, resolveThreadPrSource } from "./thread-pr-presentation";
5
6function linkedPr(
7 number: number,
8 overrides: Partial<ThreadPullRequestLink> = {},
9): ThreadPullRequestLink {
10 return {
11 host: "github.com",
12 repository: "t3tools/t3code",
13 number,
14 url: `https://github.com/t3tools/t3code/pull/${number}`,
15 source: "manual",
16 linkedAt: "2026-09-08T00:00:00.000Z",
17 stack: null,
18 snapshot: {
19 state: "open",
20 title: `Change ${number}`,
21 headBranch: `change-${number}`,
22 baseBranch: "main",
23 isDraft: false,
24 updatedAt: null,
25 syncedAt: "2026-09-08T00:00:00.000Z",
26 },
27 ...overrides,
28 };
29}
30
31describe("presentThreadLinkedPullRequests", () => {
32 it("uses the top of a derived stack even when its bottom was linked later", () => {
33 const bottom = linkedPr(1, { linkedAt: "2026-09-09T00:00:00.000Z" });
34 const top = linkedPr(2);
35 expect(
36 presentThreadLinkedPullRequests([
37 bottom,
38 {
39 ...top,
40 snapshot: { ...top.snapshot!, baseBranch: "change-1" },
41 },
42 ]),
43 ).toMatchObject({ kind: "stack", label: "2", number: 2, url: top.url });
44 });
45
46 it("names a single link with its host's term", () => {
47 expect(presentThreadLinkedPullRequests([linkedPr(7)])?.accessibilityLabel).toBe(
48 "#7 pull request open",
49 );
50 expect(
51 presentThreadLinkedPullRequests([
52 linkedPr(7, {
53 host: "gitlab.com",
54 repository: "t3tools/t3code",
55 url: "https://gitlab.com/t3tools/t3code/-/merge_requests/7",
56 }),
57 ])?.accessibilityLabel,
58 ).toBe("#7 merge request open");
59 });
60});
61
62describe("resolveThreadPrSource compatibility", () => {
63 const legacyRef = {
64 projectId: ProjectId.make("project"),
65 repository: "t3tools/t3code",
66 number: 1,
67 url: "https://github.com/t3tools/t3code/pull/1",
68 };
69 const branchRef = { ...legacyRef, number: 2, url: "https://github.com/t3tools/t3code/pull/2" };
70
71 it("polls the legacy reference when only the older linking capability exists", () => {
72 expect(
73 resolveThreadPrSource(
74 { pullRequests: [], linkedPullRequest: legacyRef, branchPullRequest: branchRef },
75 { threadPullRequestLinking: true },
76 ),
77 ).toEqual({ linkedPresentation: null, pullRequestRef: legacyRef });
78 });
79
80 it("ignores stale snapshots after reconnecting to a server without array support", () => {
81 expect(
82 resolveThreadPrSource(
83 { pullRequests: [linkedPr(3)], linkedPullRequest: legacyRef },
84 { threadPullRequestLinking: true, threadPullRequests: false },
85 ),
86 ).toEqual({ linkedPresentation: null, pullRequestRef: legacyRef });
87 });
88
89 it("uses snapshots without polling when both capabilities are advertised", () => {
90 expect(
91 resolveThreadPrSource(
92 { pullRequests: [linkedPr(3)], linkedPullRequest: legacyRef, branchPullRequest: branchRef },
93 { threadPullRequestLinking: true, threadPullRequests: true },
94 ),
95 ).toMatchObject({ linkedPresentation: { number: 3 }, pullRequestRef: null });
96 });
97
98 it("does not revive a removed modern link from the compatibility reference", () => {
99 expect(
100 resolveThreadPrSource(
101 { pullRequests: [], linkedPullRequest: legacyRef, branchPullRequest: branchRef },
102 { threadPullRequests: true },
103 ),
104 ).toEqual({ linkedPresentation: null, pullRequestRef: branchRef });
105 });
106
107 it("does not poll when a modern link is waiting for its first snapshot", () => {
108 expect(
109 resolveThreadPrSource(
110 {
111 pullRequests: [linkedPr(3, { snapshot: null })],
112 linkedPullRequest: legacyRef,
113 branchPullRequest: branchRef,
114 },
115 { threadPullRequests: true },
116 ),
117 ).toMatchObject({ linkedPresentation: { number: 3, state: null }, pullRequestRef: null });
118 });
119});