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

threadPullRequests.test.ts

426 lines14.8 KB

1import {
2 ProjectId,
3 type ThreadPullRequestLink,
4 type ThreadPullRequestSnapshot,
5} from "@t3tools/contracts";
6import { describe, expect, it } from "vite-plus/test";
7​
8import {
9 legacyLinkedPullRequestOf,
10 legacyThreadPullRequestKey,
11 threadPullRequestSearchTerms,
12 resolveThreadCurrentPullRequest,
13 resolveThreadPullRequestChains,
14 resolveThreadPullRequestBadge,
15 threadPullRequestKeysEqual,
16} from "./threadPullRequests.ts";
17​
18function snapshot(input: Partial<ThreadPullRequestSnapshot> = {}): ThreadPullRequestSnapshot {
19 return {
20 state: "open",
21 title: "Change",
22 headBranch: "feature",
23 baseBranch: "main",
24 isDraft: false,
25 updatedAt: "2026-01-01T00:00:00.000Z",
26 syncedAt: "2026-01-01T00:00:00.000Z",
27 ...input,
28 };
30​
31function link(
32 number: number,
33 input: Partial<Omit<ThreadPullRequestLink, "number">> = {},
34): ThreadPullRequestLink {
35 return {
36 host: "github.com",
37 repository: "pingdotgg/t3code",
38 number,
39 url: `https://github.com/pingdotgg/t3code/pull/${number}`,
40 source: "manual",
41 linkedAt: `2026-01-01T00:00:${String(number).padStart(2, "0")}.000Z`,
42 snapshot: null,
43 stack: null,
44 ...input,
45 };
47​
48describe("threadPullRequestKeysEqual", () => {
49 it("ignores host and repository case", () => {
50 expect(
51 threadPullRequestKeysEqual(
52 { host: "GitHub.com", repository: "PingDotGG/t3code", number: 1 },
53 { host: "github.com", repository: "pingdotgg/t3code", number: 1 },
54 ),
55 ).toBe(true);
56 expect(
57 threadPullRequestKeysEqual(
58 { host: "github.com", repository: "pingdotgg/t3code", number: 1 },
59 { host: "gitlab.com", repository: "pingdotgg/t3code", number: 1 },
60 ),
61 ).toBe(false);
62 });
63});
64​
65describe("resolveThreadCurrentPullRequest", () => {
66 it("returns null with no visible links", () => {
67 expect(resolveThreadCurrentPullRequest([])).toBeNull();
68 expect(resolveThreadCurrentPullRequest([link(1, { source: "stack-dismissed" })])).toBeNull();
69 });
70​
71 it("treats an unsynced link as open", () => {
72 expect(resolveThreadCurrentPullRequest([link(1)])).toMatchObject({
73 kind: "single",
74 link: { number: 1 },
75 });
76 });
77​
78 it("prefers the single open link over terminal ones", () => {
79 const current = resolveThreadCurrentPullRequest([
80 link(1, { snapshot: snapshot({ state: "merged" }) }),
81 link(2, { snapshot: snapshot({ state: "open" }) }),
82 link(3, { snapshot: snapshot({ state: "closed" }) }),
83 ]);
84 expect(current).toMatchObject({ kind: "single", link: { number: 2 } });
85 });
86​
87 it("reports a stack when several links are open and puts the highest layer on top", () => {
88 const stack = {
89 kind: "native" as const,
90 id: "s1",
91 number: 1,
92 url: "https://github.com/pingdotgg/t3code/stacks/1",
93 base: "main",
94 layers: [
95 { number: 10, headBranch: "a", state: "open" as const },
96 { number: 11, headBranch: "b", state: "open" as const },
97 ],
98 };
99 const current = resolveThreadCurrentPullRequest([
100 link(11, { snapshot: snapshot(), stack }),
101 link(10, { snapshot: snapshot(), stack }),
102 ]);
103 expect(current).toMatchObject({ kind: "stack", top: { number: 11 } });
104 if (current?.kind === "stack") {
105 expect(current.open.map((entry) => entry.number)).toEqual([11, 10]);
106 }
107 });
108​
109 it("orders an open set without stack data by most recent link", () => {
110 const current = resolveThreadCurrentPullRequest([link(1), link(2)]);
111 expect(current).toMatchObject({ kind: "stack", top: { number: 2 } });
112 });
113​
114 it("falls back to the most recently updated terminal link", () => {
115 const current = resolveThreadCurrentPullRequest([
116 link(1, { snapshot: snapshot({ state: "merged", updatedAt: "2026-01-03T00:00:00.000Z" }) }),
117 link(2, { snapshot: snapshot({ state: "closed", updatedAt: "2026-01-02T00:00:00.000Z" }) }),
118 ]);
119 expect(current).toMatchObject({ kind: "single", link: { number: 1 } });
120 });
121});
122​
123describe("legacyLinkedPullRequestOf", () => {
124 const identity = {
125 canonicalKey: "github.com/pingdotgg/t3code",
126 provider: "github",
127 displayName: "pingdotgg/t3code",
128 locator: {
129 source: "git-remote" as const,
130 remoteName: "origin",
131 remoteUrl: "https://github.com/pingdotgg/t3code.git",
132 },
133 };
134 it("projects only links the owning project can route without a host", () => {
135 expect(legacyLinkedPullRequestOf([link(7)], "project-1" as never, identity)).toEqual({
136 projectId: "project-1",
137 repository: "pingdotgg/t3code",
138 number: 7,
139 url: "https://github.com/pingdotgg/t3code/pull/7",
140 });
141 expect(legacyLinkedPullRequestOf([], "project-1" as never, identity)).toBeNull();
142 });
143 it.each([
144 {
145 host: "github.enterprise.test",
146 url: "https://github.enterprise.test/pingdotgg/t3code/pull/7",
147 },
148 { repository: "acme/other", url: "https://github.com/acme/other/pull/7" },
149 ])("omits an unsafe legacy route %j", (foreign) => {
150 expect(
151 legacyLinkedPullRequestOf([link(7, foreign)], "project-1" as never, identity),
152 ).toBeNull();
153 expect(
154 legacyLinkedPullRequestOf([link(7, foreign), link(8)], "project-1" as never, identity)
155 ?.number,
156 ).toBe(8);
157 });
158 it.each([
159 "dev.azure.com/org-a/project/_git/web",
160 "ssh.dev.azure.com/v3/org-a/project/web",
161 "org-a.visualstudio.com/DefaultCollection/project/_git/web",
162 ])("projects Azure links through the legacy selector for %s", (canonicalKey) => {
163 const azureIdentity = {
164 ...identity,
165 provider: "azure-devops",
166 canonicalKey,
167 displayName: canonicalKey.slice(canonicalKey.indexOf("/") + 1),
168 name: "web",
169 };
170 const own = link(7, {
171 host: "dev.azure.com",
172 repository: "org-a/project/_git/web",
173 url: "https://dev.azure.com/org-a/project/_git/web/pullrequest/7",
174 });
175 const foreign = link(7, {
176 host: "dev.azure.com",
177 repository: "web",
178 url: "https://dev.azure.com/org-b/project/_git/web/pullrequest/7",
179 linkedAt: "2026-01-02T00:00:00.000Z",
180 });
181 for (const repository of ["web", "org-a/project/_git/web"]) {
182 expect(
183 legacyLinkedPullRequestOf(
184 [{ ...own, repository }, foreign],
185 "project-1" as never,
186 azureIdentity,
187 ),
188 ).toEqual({ projectId: "project-1", repository: "web", number: 7, url: own.url });
189 }
190 expect(legacyLinkedPullRequestOf([foreign], "project-1" as never, azureIdentity)).toBeNull();
191 expect(legacyThreadPullRequestKey({ ...own, repository: "web" })).toEqual({
192 host: "dev.azure.com",
193 repository: "org-a/project/_git/web",
194 number: 7,
195 });
196 expect(
197 threadPullRequestKeysEqual(own, {
198 host: "org-a.visualstudio.com",
199 repository: "DefaultCollection/project/_git/web",
200 number: 7,
201 }),
202 ).toBe(true);
203 });
204 it("does not guess when the project identity is unavailable", () => {
205 expect(legacyLinkedPullRequestOf([link(7)], "project-1" as never, null)).toBeNull();
206 });
207});
208​
209describe("resolveThreadPullRequestChains", () => {
210 it("chains links by base → head within a repository, bottom to top", () => {
211 const chains = resolveThreadPullRequestChains([
212 link(3, { snapshot: snapshot({ headBranch: "c", baseBranch: "b" }) }),
213 link(1, { snapshot: snapshot({ headBranch: "a", baseBranch: "main" }) }),
214 link(2, { snapshot: snapshot({ headBranch: "b", baseBranch: "a" }) }),
215 link(9, { snapshot: snapshot({ headBranch: "solo", baseBranch: "main" }) }),
216 ]);
217 expect(chains.map((chain) => [chain.kind, chain.layers.map((layer) => layer.number)])).toEqual([
218 ["derived", [1, 2, 3]],
219 ["derived", [9]],
220 ]);
221 });
222​
223 it("uses the native stack order when the host provides one", () => {
224 const stack = {
225 kind: "native" as const,
226 id: "s1",
227 number: 1,
228 url: "https://github.com/pingdotgg/t3code/stacks/1",
229 base: "main",
230 layers: [
231 { number: 5, headBranch: "a", state: "merged" as const },
232 { number: 6, headBranch: "b", state: "open" as const },
233 ],
234 };
235 const chains = resolveThreadPullRequestChains([
236 link(6, { snapshot: snapshot({ headBranch: "b", baseBranch: "main" }), stack }),
237 link(5, {
238 snapshot: snapshot({ state: "merged", headBranch: "a", baseBranch: "main" }),
239 stack,
240 }),
241 link(8),
242 ]);
243 expect(chains.map((chain) => [chain.kind, chain.layers.map((layer) => layer.number)])).toEqual([
244 ["native", [5, 6]],
245 ["derived", [8]],
246 ]);
247 });
248});
249​
250describe("chain selection and badge state", () => {
251 it.each([
252 ["open", false, "open", false, "open"],
253 ["closed", false, "closed", false, "closed"],
254 ["open", true, "open", true, "draft"],
255 ["open", false, "open", true, "open"],
256 ["closed", false, "open", true, "open"],
257 ["merged", false, "merged", false, "merged"],
258 ["merged", false, "closed", true, "closed"],
259 ] as const)(
260 "aggregates %s (draft %s) and %s (draft %s) as %s",
261 (firstState, firstDraft, secondState, secondDraft, state) => {
262 for (const stacked of [false, true]) {
263 const links = [
264 link(1, {
265 snapshot: snapshot({ state: firstState, isDraft: firstDraft, headBranch: "base" }),
266 }),
267 link(2, {
268 snapshot: snapshot({
269 state: secondState,
270 isDraft: secondDraft,
271 baseBranch: stacked ? "base" : "main",
272 }),
273 }),
274 link(3, { source: "stack-dismissed" }),
275 ];
276 expect(resolveThreadPullRequestBadge(links)).toEqual(
277 stacked
278 ? { kind: "stack", layers: 2, state }
279 : { kind: "pull-request", others: 1, state },
280 );
281 }
282 },
283 );
284​
285 it.each(["open", "merged", "closed"] as const)(
286 "targets the top of a derived %s chain despite a later bottom update and link",
287 (state) => {
288 const bottom = link(2, {
289 snapshot: snapshot({ state, headBranch: "base", updatedAt: "2026-02-01T00:00:00.000Z" }),
290 });
291 const top = link(1, {
292 snapshot: snapshot({ state, headBranch: "top", baseBranch: "base" }),
293 });
294 expect(resolveThreadCurrentPullRequest([bottom, top])).toMatchObject(
295 state === "open"
296 ? { kind: "stack", top: { number: 1 }, open: [top, bottom] }
297 : { kind: "single", link: { number: 1 } },
298 );
299 expect(resolveThreadPullRequestBadge([bottom, top])).toEqual({
300 kind: "stack",
301 layers: 2,
302 state,
303 });
304 },
305 );
306​
307 it("keeps the top native layer after completion and treats an unsynced layer as open", () => {
308 const stack = {
309 kind: "native" as const,
310 id: "native-1",
311 number: 1,
312 url: "https://github.com/pingdotgg/t3code/stacks/1",
313 base: "main",
314 layers: [
315 { number: 2, headBranch: "base", state: "merged" as const },
316 { number: 1, headBranch: "top", state: "merged" as const },
317 ],
318 };
319 const bottom = link(2, {
320 stack,
321 snapshot: snapshot({ state: "merged", updatedAt: "2026-02-01T00:00:00.000Z" }),
322 });
323 const top = link(1, { stack, snapshot: snapshot({ state: "merged" }) });
324 expect(resolveThreadCurrentPullRequest([bottom, top])).toMatchObject({
325 kind: "single",
326 link: { number: 1 },
327 });
328 expect(resolveThreadPullRequestBadge([bottom, { ...top, snapshot: null }])).toEqual({
329 kind: "stack",
330 layers: 2,
331 state: "open",
332 });
333 });
334​
335 it("uses aggregate state and distinguishes unrelated work from a stack", () => {
336 const bottom = link(1, { snapshot: snapshot({ state: "merged", headBranch: "base" }) });
337 const top = link(2, { snapshot: snapshot({ state: "closed", baseBranch: "base" }) });
338 expect(resolveThreadPullRequestBadge([bottom, top])).toEqual({
339 kind: "stack",
340 layers: 2,
341 state: "closed",
342 });
343 expect(resolveThreadPullRequestBadge([bottom, top, link(3)])).toEqual({
344 kind: "pull-request",
345 others: 2,
346 state: "open",
347 });
348 expect(resolveThreadPullRequestBadge([link(3, { source: "stack-dismissed" })])).toBeNull();
349 });
350​
351 it("keeps branch matching case-sensitive while ignoring repository case", () => {
352 const bottom = link(1, {
353 repository: "PingDotGG/T3code",
354 snapshot: snapshot({ headBranch: "Base" }),
355 });
356 const top = link(2, { snapshot: snapshot({ headBranch: "top", baseBranch: "base" }) });
357 expect(resolveThreadPullRequestChains([bottom, top])).toHaveLength(2);
358 expect(
359 resolveThreadPullRequestChains([
360 bottom,
361 { ...top, snapshot: snapshot({ headBranch: "top", baseBranch: "Base" }) },
362 ])[0]?.layers,
363 ).toEqual([bottom, { ...top, snapshot: snapshot({ headBranch: "top", baseBranch: "Base" }) }]);
364 });
365​
366 it("preserves cyclic links without presenting a guessed stack order", () => {
367 const links = [
368 link(1, { snapshot: snapshot({ headBranch: "a", baseBranch: "b" }) }),
369 link(2, { snapshot: snapshot({ headBranch: "b", baseBranch: "a" }) }),
370 ];
371 expect(resolveThreadPullRequestChains(links).map((chain) => chain.layers)).toEqual(
372 links.map((entry) => [entry]),
373 );
374 expect(resolveThreadCurrentPullRequest(links)).toMatchObject({
375 kind: "stack",
376 top: { number: 2 },
377 });
378 expect(resolveThreadPullRequestBadge(links)).toEqual({
379 kind: "pull-request",
380 others: 1,
381 state: "open",
382 });
383 });
384​
385 it("does not guess a parent when a head branch was reused", () => {
386 const links = [
387 link(1, { snapshot: snapshot({ state: "merged", headBranch: "reused" }) }),
388 link(2, { snapshot: snapshot({ headBranch: "reused" }) }),
389 link(3, { snapshot: snapshot({ headBranch: "top", baseBranch: "reused" }) }),
390 ];
391 expect(resolveThreadPullRequestChains(links).map((chain) => chain.layers)).toEqual(
392 links.map((entry) => [entry]),
393 );
394 });
395});
396​
397describe("threadPullRequestSearchTerms", () => {
398 it("includes completed and unsynced links but excludes dismissed links", () => {
399 const terms = threadPullRequestSearchTerms({
400 pullRequests: [
401 link(12, { snapshot: snapshot({ title: "Fix login", state: "merged" }) }),
402 link(34),
403 link(56, { source: "stack-dismissed" }),
404 ],
405 });
406 expect(terms).toContain("#12");
407 expect(terms).toContain("pingdotgg/t3code#12");
408 expect(terms).toContain("https://github.com/pingdotgg/t3code/pull/12");
409 expect(terms).toContain("Fix login");
410 expect(terms).toContain("#34");
411 expect(terms.join(" ")).not.toContain("56");
412 });
413});
414​
415it("searches the legacy projection when old environments decode to an empty links list", () => {
416 const linkedPullRequest = {
417 projectId: ProjectId.make("project"),
418 repository: "pingdotgg/t3code",
419 number: 12,
420 url: "https://github.com/pingdotgg/t3code/pull/12",
421 };
422 expect(threadPullRequestSearchTerms({ pullRequests: [], linkedPullRequest })).toContain("#12");
423 expect(
424 threadPullRequestSearchTerms({ pullRequests: [link(34)], linkedPullRequest }),
425 ).not.toContain("#12");
426});