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

use-thread-pr.test.ts

254 lines8.5 KB

1import { SourceControlProviderKind } from "@t3tools/contracts";
2import { ProjectId, type ThreadPullRequestLink, type VcsStatusResult } from "@t3tools/contracts";
3import { describe, expect, it } from "vite-plus/test";
4​
5import {
6 presentThreadLinkedPullRequests,
7 presentThreadPr,
8 resolveThreadPrBadgeAction,
9 resolveThreadPrSource,
10} from "./thread-pr-presentation";
11​
12const pullRequest: NonNullable<VcsStatusResult["pr"]> = {
13 number: 3774,
14 title: "Desktop-style pull request indicator",
15 url: "https://github.com/t3tools/t3code/pull/3774",
16 baseRef: "main",
17 headRef: "codex/desktop-style-pr-indicator",
18 state: "merged",
19};
20​
21describe("presentThreadPr", () => {
22 it("uses the compact pull request number label without a hash prefix", () => {
23 expect(presentThreadPr(pullRequest, undefined)).toMatchObject({
24 label: "3774",
25 accessibilityLabel: "#3774 pull request merged",
26 textClassName: "text-adaptive-violet-600-400",
27 });
28 });
29​
30 it("uses merge-request terminology for GitLab", () => {
31 expect(
32 presentThreadPr(pullRequest, {
33 kind: SourceControlProviderKind.make("gitlab"),
34 name: "GitLab",
35 baseUrl: "https://gitlab.com",
36 }),
37 ).toMatchObject({
38 label: "3774",
39 accessibilityLabel: "#3774 merge request merged",
40 });
41 });
42​
43 it("uses gray for draft pull requests", () => {
44 expect(
45 presentThreadPr({ ...pullRequest, state: "open", isDraft: true }, undefined),
46 ).toMatchObject({
47 accessibilityLabel: "#3774 pull request draft",
48 textClassName: "text-foreground-muted",
49 });
50 });
51});
52​
53function linkedPr(
54 number: number,
55 overrides: Partial<ThreadPullRequestLink> = {},
56): ThreadPullRequestLink {
57 return {
58 host: "github.com",
59 repository: "t3tools/t3code",
60 number,
61 url: `https://github.com/t3tools/t3code/pull/${number}`,
62 source: "manual",
63 linkedAt: "2026-09-08T00:00:00.000Z",
64 stack: null,
65 snapshot: {
66 state: "open",
67 title: `Change ${number}`,
68 headBranch: `change-${number}`,
69 baseBranch: "main",
70 isDraft: false,
71 updatedAt: null,
72 syncedAt: "2026-09-08T00:00:00.000Z",
73 },
74 ...overrides,
75 };
77​
78describe("presentThreadLinkedPullRequests", () => {
79 it("renders unsynced links with neutral pending status", () => {
80 expect(presentThreadLinkedPullRequests([linkedPr(1, { snapshot: null })])).toMatchObject({
81 number: 1,
82 label: "1",
83 state: null,
84 textClassName: "text-foreground-muted",
85 accessibilityLabel: "#1 pull request status pending",
86 });
87 });
88​
89 it("counts unrelated links without labelling them a stack", () => {
90 expect(presentThreadLinkedPullRequests([linkedPr(1), linkedPr(2)])).toMatchObject({
91 kind: "pull-request",
92 label: "+2",
93 others: 1,
94 state: "open",
95 isDraft: false,
96 textClassName: "text-adaptive-emerald-600-400",
97 });
98 });
99​
100 it.each([
101 ["closed", false, "closed", false, "closed", false, "text-adaptive-rose-600-400"],
102 ["open", true, "open", true, "open", true, "text-foreground-muted"],
103 ["open", true, "open", false, "open", false, "text-adaptive-emerald-600-400"],
104 ["closed", false, "open", false, "open", false, "text-adaptive-emerald-600-400"],
105 ["merged", false, "merged", false, "merged", false, "text-adaptive-violet-600-400"],
106 ["closed", false, "merged", false, "closed", false, "text-adaptive-rose-600-400"],
107 ] as const)(
108 "colors linked %s (draft %s) and %s (draft %s) by their aggregate state",
109 (firstState, firstDraft, secondState, secondDraft, state, isDraft, textClassName) => {
110 const first = linkedPr(1);
111 const second = linkedPr(2);
112 expect(
113 presentThreadLinkedPullRequests([
114 { ...first, snapshot: { ...first.snapshot!, state: firstState, isDraft: firstDraft } },
115 {
116 ...second,
117 snapshot: { ...second.snapshot!, state: secondState, isDraft: secondDraft },
118 },
119 ]),
120 ).toMatchObject({
121 label: "+2",
122 state,
123 isDraft,
124 textClassName,
125 accessibilityLabel: `2 linked pull requests, overall ${isDraft ? "draft" : state}`,
126 });
127 },
128 );
129​
130 it("uses the top of a derived stack even when its bottom was linked later", () => {
131 const bottom = linkedPr(1, { linkedAt: "2026-09-09T00:00:00.000Z" });
132 const top = linkedPr(2);
133 expect(
134 presentThreadLinkedPullRequests([
135 bottom,
136 {
137 ...top,
138 snapshot: { ...top.snapshot!, baseBranch: "change-1" },
139 },
140 ]),
141 ).toMatchObject({ kind: "stack", label: "2", number: 2, url: top.url });
142 });
143​
144 it("hides dismissed stack members", () => {
145 expect(
146 presentThreadLinkedPullRequests([linkedPr(1, { source: "stack-dismissed" })]),
147 ).toBeNull();
148 });
149​
150 it("retains merged state from the persisted snapshot", () => {
151 const link = linkedPr(1);
152 expect(
153 presentThreadLinkedPullRequests([
154 { ...link, snapshot: { ...link.snapshot!, state: "merged" } },
155 ]),
156 ).toMatchObject({ state: "merged", textClassName: "text-adaptive-violet-600-400" });
157 });
158});
159​
160describe("resolveThreadPrBadgeAction", () => {
161 it("opens a single linked or branch pull request directly", () => {
162 expect(resolveThreadPrBadgeAction(presentThreadLinkedPullRequests([linkedPr(1)])!)).toEqual({
163 kind: "open",
164 accessibilityLabel: "Open #1 pull request open",
165 });
166 expect(resolveThreadPrBadgeAction(presentThreadPr(pullRequest, null))).toMatchObject({
167 kind: "open",
168 });
169 });
170​
171 it("lists unrelated linked pull requests", () => {
172 expect(
173 resolveThreadPrBadgeAction(presentThreadLinkedPullRequests([linkedPr(1), linkedPr(2)])!),
174 ).toEqual({
175 kind: "list",
176 accessibilityLabel: "Show 2 linked pull requests, overall open",
177 });
178 });
179​
180 it("lists a stack even though the badge shows its top pull request", () => {
181 const top = linkedPr(2);
182 const stack = presentThreadLinkedPullRequests([
183 linkedPr(1),
184 { ...top, snapshot: { ...top.snapshot!, baseBranch: "change-1" } },
185 ])!;
186 expect(resolveThreadPrBadgeAction(stack)).toMatchObject({ kind: "list" });
187 });
188});
189​
190describe("resolveThreadPrSource compatibility", () => {
191 const legacyRef = {
192 projectId: ProjectId.make("project"),
193 repository: "t3tools/t3code",
194 number: 1,
195 url: "https://github.com/t3tools/t3code/pull/1",
196 };
197 const branchRef = { ...legacyRef, number: 2, url: "https://github.com/t3tools/t3code/pull/2" };
198​
199 it("polls the legacy reference when only the older linking capability exists", () => {
200 expect(
201 resolveThreadPrSource(
202 { pullRequests: [], linkedPullRequest: legacyRef, branchPullRequest: branchRef },
203 { threadPullRequestLinking: true },
204 ),
205 ).toEqual({ linkedPresentation: null, pullRequestRef: legacyRef });
206 });
207​
208 it("keeps the legacy reference for a server without either capability", () => {
209 expect(resolveThreadPrSource({ pullRequests: [], linkedPullRequest: legacyRef }, {})).toEqual({
210 linkedPresentation: null,
211 pullRequestRef: legacyRef,
212 });
213 });
214​
215 it("ignores stale snapshots after reconnecting to a server without array support", () => {
216 expect(
217 resolveThreadPrSource(
218 { pullRequests: [linkedPr(3)], linkedPullRequest: legacyRef },
219 { threadPullRequestLinking: true, threadPullRequests: false },
220 ),
221 ).toEqual({ linkedPresentation: null, pullRequestRef: legacyRef });
222 });
223​
224 it("uses snapshots without polling when both capabilities are advertised", () => {
225 expect(
226 resolveThreadPrSource(
227 { pullRequests: [linkedPr(3)], linkedPullRequest: legacyRef, branchPullRequest: branchRef },
228 { threadPullRequestLinking: true, threadPullRequests: true },
229 ),
230 ).toMatchObject({ linkedPresentation: { number: 3 }, pullRequestRef: null });
231 });
232​
233 it("does not revive a removed modern link from the compatibility reference", () => {
234 expect(
235 resolveThreadPrSource(
236 { pullRequests: [], linkedPullRequest: legacyRef, branchPullRequest: branchRef },
237 { threadPullRequests: true },
238 ),
239 ).toEqual({ linkedPresentation: null, pullRequestRef: branchRef });
240 });
241​
242 it("does not poll when a modern link is waiting for its first snapshot", () => {
243 expect(
244 resolveThreadPrSource(
245 {
246 pullRequests: [linkedPr(3, { snapshot: null })],
247 linkedPullRequest: legacyRef,
248 branchPullRequest: branchRef,
249 },
250 { threadPullRequests: true },
251 ),
252 ).toMatchObject({ linkedPresentation: { number: 3, state: null }, pullRequestRef: null });
253 });
254});