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