Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

use-thread-pr.test.ts

190 lines6.0 KB

1import { ProjectId, type ThreadPullRequestLink, type VcsStatusResult } from "@t3tools/contracts";
2import { describe, expect, it } from "vite-plus/test";
3​
4import {
5 presentThreadLinkedPullRequests,
6 presentThreadPr,
7 resolveThreadPrSource,
8} from "./thread-pr-presentation";
9​
10const pullRequest: NonNullable<VcsStatusResult["pr"]> = {
11 number: 3774,
12 title: "Desktop-style pull request indicator",
13 url: "https://github.com/t3tools/t3code/pull/3774",
14 baseRef: "main",
15 headRef: "codex/desktop-style-pr-indicator",
16 state: "merged",
17};
18​
19describe("presentThreadPr", () => {
20 it("uses the compact pull request number label without a hash prefix", () => {
21 expect(presentThreadPr(pullRequest, undefined)).toMatchObject({
22 label: "3774",
23 accessibilityLabel: "#3774 pull request merged",
24 textClassName: "text-adaptive-violet-600-400",
25 });
26 });
27​
28 it("uses merge-request terminology for GitLab", () => {
29 expect(
30 presentThreadPr(pullRequest, {
31 kind: "gitlab",
32 name: "GitLab",
33 baseUrl: "https://gitlab.com",
34 }),
35 ).toMatchObject({
36 label: "3774",
37 accessibilityLabel: "#3774 merge request merged",
38 });
39 });
40​
41 it("uses gray for draft pull requests", () => {
42 expect(
43 presentThreadPr({ ...pullRequest, state: "open", isDraft: true }, undefined),
44 ).toMatchObject({
45 accessibilityLabel: "#3774 pull request draft",
46 textClassName: "text-foreground-muted",
47 });
48 });
49});
50​
51function linkedPr(
52 number: number,
53 overrides: Partial<ThreadPullRequestLink> = {},
54): ThreadPullRequestLink {
55 return {
56 host: "github.com",
57 repository: "t3tools/t3code",
58 number,
59 url: `https://github.com/t3tools/t3code/pull/${number}`,
60 source: "manual",
61 linkedAt: "2026-09-08T00:00:00.000Z",
62 stack: null,
63 snapshot: {
64 state: "open",
65 title: `Change ${number}`,
66 headBranch: `change-${number}`,
67 baseBranch: "main",
68 isDraft: false,
69 updatedAt: null,
70 syncedAt: "2026-09-08T00:00:00.000Z",
71 },
72 ...overrides,
73 };
75​
76describe("presentThreadLinkedPullRequests", () => {
77 it("renders unsynced links with neutral pending status", () => {
78 expect(presentThreadLinkedPullRequests([linkedPr(1, { snapshot: null })])).toMatchObject({
79 number: 1,
80 label: "1",
81 state: null,
82 textClassName: "text-foreground-muted",
83 accessibilityLabel: "#1 pull request status pending",
84 });
85 });
86​
87 it("counts unrelated links without labelling them a stack", () => {
88 expect(presentThreadLinkedPullRequests([linkedPr(1), linkedPr(2)])).toMatchObject({
89 kind: "pull-request",
90 label: "+2",
91 others: 1,
92 textClassName: "text-foreground-muted",
93 });
94 });
95​
96 it("uses the top of a derived stack even when its bottom was linked later", () => {
97 const bottom = linkedPr(1, { linkedAt: "2026-09-09T00:00:00.000Z" });
98 const top = linkedPr(2);
99 expect(
100 presentThreadLinkedPullRequests([
101 bottom,
102 {
103 ...top,
104 snapshot: { ...top.snapshot!, baseBranch: "change-1" },
105 },
106 ]),
107 ).toMatchObject({ kind: "stack", label: "2", number: 2, url: top.url });
108 });
109​
110 it("hides dismissed stack members", () => {
111 expect(
112 presentThreadLinkedPullRequests([linkedPr(1, { source: "stack-dismissed" })]),
113 ).toBeNull();
114 });
115​
116 it("retains merged state from the persisted snapshot", () => {
117 const link = linkedPr(1);
118 expect(
119 presentThreadLinkedPullRequests([
120 { ...link, snapshot: { ...link.snapshot!, state: "merged" } },
121 ]),
122 ).toMatchObject({ state: "merged", textClassName: "text-adaptive-violet-600-400" });
123 });
124});
125​
126describe("resolveThreadPrSource compatibility", () => {
127 const legacyRef = {
128 projectId: ProjectId.make("project"),
129 repository: "t3tools/t3code",
130 number: 1,
131 url: "https://github.com/t3tools/t3code/pull/1",
132 };
133 const branchRef = { ...legacyRef, number: 2, url: "https://github.com/t3tools/t3code/pull/2" };
134​
135 it("polls the legacy reference when only the older linking capability exists", () => {
136 expect(
137 resolveThreadPrSource(
138 { pullRequests: [], linkedPullRequest: legacyRef, branchPullRequest: branchRef },
139 { threadPullRequestLinking: true },
140 ),
141 ).toEqual({ linkedPresentation: null, pullRequestRef: legacyRef });
142 });
143​
144 it("keeps the legacy reference for a server without either capability", () => {
145 expect(resolveThreadPrSource({ pullRequests: [], linkedPullRequest: legacyRef }, {})).toEqual({
146 linkedPresentation: null,
147 pullRequestRef: legacyRef,
148 });
149 });
150​
151 it("ignores stale snapshots after reconnecting to a server without array support", () => {
152 expect(
153 resolveThreadPrSource(
154 { pullRequests: [linkedPr(3)], linkedPullRequest: legacyRef },
155 { threadPullRequestLinking: true, threadPullRequests: false },
156 ),
157 ).toEqual({ linkedPresentation: null, pullRequestRef: legacyRef });
158 });
159​
160 it("uses snapshots without polling when both capabilities are advertised", () => {
161 expect(
162 resolveThreadPrSource(
163 { pullRequests: [linkedPr(3)], linkedPullRequest: legacyRef, branchPullRequest: branchRef },
164 { threadPullRequestLinking: true, threadPullRequests: true },
165 ),
166 ).toMatchObject({ linkedPresentation: { number: 3 }, pullRequestRef: null });
167 });
168​
169 it("does not revive a removed modern link from the compatibility reference", () => {
170 expect(
171 resolveThreadPrSource(
172 { pullRequests: [], linkedPullRequest: legacyRef, branchPullRequest: branchRef },
173 { threadPullRequests: true },
174 ),
175 ).toEqual({ linkedPresentation: null, pullRequestRef: branchRef });
176 });
177​
178 it("does not poll when a modern link is waiting for its first snapshot", () => {
179 expect(
180 resolveThreadPrSource(
181 {
182 pullRequests: [linkedPr(3, { snapshot: null })],
183 linkedPullRequest: legacyRef,
184 branchPullRequest: branchRef,
185 },
186 { threadPullRequests: true },
187 ),
188 ).toMatchObject({ linkedPresentation: { number: 3, state: null }, pullRequestRef: null });
189 });
190});