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
47describe("resolveThreadPrSource compatibility", () => {
48 const legacyRef = {
49 projectId: ProjectId.make("project"),
50 repository: "t3tools/t3code",
51 number: 1,
52 url: "https://github.com/t3tools/t3code/pull/1",
53 };
54 const branchRef = { ...legacyRef, number: 2, url: "https://github.com/t3tools/t3code/pull/2" };
55
56 it("polls the legacy reference when only the older linking capability exists", () => {
57 expect(
58 resolveThreadPrSource(
59 { pullRequests: [], linkedPullRequest: legacyRef, branchPullRequest: branchRef },
60 { threadPullRequestLinking: true },
61 ),
62 ).toEqual({ linkedPresentation: null, pullRequestRef: legacyRef });
63 });
64
65 it("ignores stale snapshots after reconnecting to a server without array support", () => {
66 expect(
67 resolveThreadPrSource(
68 { pullRequests: [linkedPr(3)], linkedPullRequest: legacyRef },
69 { threadPullRequestLinking: true, threadPullRequests: false },
70 ),
71 ).toEqual({ linkedPresentation: null, pullRequestRef: legacyRef });
72 });
73
74 it("uses snapshots without polling when both capabilities are advertised", () => {
75 expect(
76 resolveThreadPrSource(
77 { pullRequests: [linkedPr(3)], linkedPullRequest: legacyRef, branchPullRequest: branchRef },
78 { threadPullRequestLinking: true, threadPullRequests: true },
79 ),
80 ).toMatchObject({ linkedPresentation: { number: 3 }, pullRequestRef: null });
81 });
82
83 it("does not revive a removed modern link from the compatibility reference", () => {
84 expect(
85 resolveThreadPrSource(
86 { pullRequests: [], linkedPullRequest: legacyRef, branchPullRequest: branchRef },
87 { threadPullRequests: true },
88 ),
89 ).toEqual({ linkedPresentation: null, pullRequestRef: branchRef });
90 });
91
92 it("does not poll when a modern link is waiting for its first snapshot", () => {
93 expect(
94 resolveThreadPrSource(
95 {
96 pullRequests: [linkedPr(3, { snapshot: null })],
97 linkedPullRequest: legacyRef,
98 branchPullRequest: branchRef,
99 },
100 { threadPullRequests: true },
101 ),
102 ).toMatchObject({ linkedPresentation: { number: 3, state: null }, pullRequestRef: null });
103 });
104});