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 stored reference before the server config loads", () => {
76 expect(
77 resolveThreadPrSource(
78 { pullRequests: [], linkedPullRequest: legacyRef, branchPullRequest: branchRef },
79 false,
80 ),
81 ).toEqual({ linkedPresentation: null, pullRequestRef: legacyRef });
82 });
83
84 it("uses snapshots without polling once the server config loads", () => {
85 expect(
86 resolveThreadPrSource(
87 { pullRequests: [linkedPr(3)], linkedPullRequest: legacyRef, branchPullRequest: branchRef },
88 true,
89 ),
90 ).toMatchObject({ linkedPresentation: { number: 3 }, pullRequestRef: null });
91 });
92
93 it("does not revive a removed modern link from the compatibility reference", () => {
94 expect(
95 resolveThreadPrSource(
96 { pullRequests: [], linkedPullRequest: legacyRef, branchPullRequest: branchRef },
97 true,
98 ),
99 ).toEqual({ linkedPresentation: null, pullRequestRef: branchRef });
100 });
101
102 it("does not poll when a modern link is waiting for its first snapshot", () => {
103 expect(
104 resolveThreadPrSource(
105 {
106 pullRequests: [linkedPr(3, { snapshot: null })],
107 linkedPullRequest: legacyRef,
108 branchPullRequest: branchRef,
109 },
110 true,
111 ),
112 ).toMatchObject({ linkedPresentation: { number: 3, state: null }, pullRequestRef: null });
113 });
114});
115
116describe("rememberBounded", () => {
117 it("moves a re-answered entry to the newest slot and evicts the oldest past the limit", () => {
118 let snapshots: ReadonlyMap<string, number> = new Map();
119 snapshots = rememberBounded(snapshots, "a", 1, 2);
120 snapshots = rememberBounded(snapshots, "b", 2, 2);
121 snapshots = rememberBounded(snapshots, "a", 3, 2);
122 snapshots = rememberBounded(snapshots, "c", 4, 2);
123 expect([...snapshots]).toEqual([
124 ["a", 3],
125 ["c", 4],
126 ]);
127 });
128});