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

threadPullRequests.test.ts

557 lines19.6 KB

1import {
2 ProjectId,
3 type ThreadPullRequestLink,
4 type ThreadPullRequestSnapshot,
5} from "@t3tools/contracts";
6import { beforeEach, 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​
18// Match Hermes: these ES2023 array methods are absent on mobile, and this module runs in
19// the home thread list on every launch.
20beforeEach(() => {
21 const methods = ["toSorted", "toReversed", "toSpliced"] as const;
22 const descriptors = methods.map((method) =>
23 Object.getOwnPropertyDescriptor(Array.prototype, method),
24 );
25 for (const method of methods) Reflect.deleteProperty(Array.prototype, method);
26 return () => {
27 for (const [index, method] of methods.entries()) {
28 const descriptor = descriptors[index];
29 if (descriptor) Reflect.defineProperty(Array.prototype, method, descriptor);
30 }
31 };
32});
33​
34function snapshot(input: Partial<ThreadPullRequestSnapshot> = {}): ThreadPullRequestSnapshot {
35 return {
36 state: "open",
37 title: "Change",
38 headBranch: "feature",
39 baseBranch: "main",
40 isDraft: false,
41 updatedAt: "2026-01-01T00:00:00.000Z",
42 syncedAt: "2026-01-01T00:00:00.000Z",
43 ...input,
44 };
46​
47function link(
48 number: number,
49 input: Partial<Omit<ThreadPullRequestLink, "number">> = {},
50): ThreadPullRequestLink {
51 return {
52 host: "github.com",
53 repository: "pingdotgg/t3code",
54 number,
55 url: `https://github.com/pingdotgg/t3code/pull/${number}`,
56 source: "manual",
57 linkedAt: `2026-01-01T00:00:${String(number).padStart(2, "0")}.000Z`,
58 snapshot: null,
59 stack: null,
60 ...input,
61 };
63​
64describe("threadPullRequestKeysEqual", () => {
65 it("recovers Forgejo ports from old stored URLs and keeps separate servers distinct", () => {
66 const old = link(1, {
67 host: "forge.example",
68 repository: "team/repo",
69 url: "http://forge.example:3000/team/repo/pulls/1",
70 });
71 expect(threadPullRequestKeysEqual(old, { ...old, host: "forge.example:3000" })).toBe(true);
72 expect(
73 threadPullRequestKeysEqual(old, {
74 host: "forge.example:3000",
75 repository: "team/repo",
76 number: 1,
77 }),
78 ).toBe(true);
79 expect(
80 threadPullRequestKeysEqual(old, {
81 ...old,
82 url: "http://forge.example:4000/team/repo/pulls/1",
83 }),
84 ).toBe(false);
85 });
86​
87 it("ignores host and repository case", () => {
88 expect(
89 threadPullRequestKeysEqual(
90 { host: "GitHub.com", repository: "PingDotGG/t3code", number: 1 },
91 { host: "github.com", repository: "pingdotgg/t3code", number: 1 },
92 ),
93 ).toBe(true);
94 expect(
95 threadPullRequestKeysEqual(
96 { host: "github.com", repository: "pingdotgg/t3code", number: 1 },
97 { host: "gitlab.com", repository: "pingdotgg/t3code", number: 1 },
98 ),
99 ).toBe(false);
100 });
101});
102​
103describe("resolveThreadCurrentPullRequest", () => {
104 it("returns null with no visible links", () => {
105 expect(resolveThreadCurrentPullRequest([])).toBeNull();
106 expect(resolveThreadCurrentPullRequest([link(1, { source: "stack-dismissed" })])).toBeNull();
107 });
108​
109 it("treats an unsynced link as open", () => {
110 expect(resolveThreadCurrentPullRequest([link(1)])).toMatchObject({
111 kind: "single",
112 link: { number: 1 },
113 });
114 });
115​
116 it("prefers the single open link over terminal ones", () => {
117 const current = resolveThreadCurrentPullRequest([
118 link(1, { snapshot: snapshot({ state: "merged" }) }),
119 link(2, { snapshot: snapshot({ state: "open" }) }),
120 link(3, { snapshot: snapshot({ state: "closed" }) }),
121 ]);
122 expect(current).toMatchObject({ kind: "single", link: { number: 2 } });
123 });
124​
125 it("reports a stack when several links are open and puts the highest layer on top", () => {
126 const stack = {
127 kind: "native" as const,
128 id: "s1",
129 number: 1,
130 url: "https://github.com/pingdotgg/t3code/stacks/1",
131 base: "main",
132 layers: [
133 { number: 10, headBranch: "a", state: "open" as const },
134 { number: 11, headBranch: "b", state: "open" as const },
135 ],
136 };
137 const current = resolveThreadCurrentPullRequest([
138 link(11, { snapshot: snapshot(), stack }),
139 link(10, { snapshot: snapshot(), stack }),
140 ]);
141 expect(current).toMatchObject({ kind: "stack", top: { number: 11 } });
142 if (current?.kind === "stack") {
143 expect(current.open.map((entry) => entry.number)).toEqual([11, 10]);
144 }
145 });
146​
147 it("orders an open set without stack data by most recent link", () => {
148 const current = resolveThreadCurrentPullRequest([link(1), link(2)]);
149 expect(current).toMatchObject({ kind: "stack", top: { number: 2 } });
150 });
151​
152 it("falls back to the most recently updated terminal link", () => {
153 const current = resolveThreadCurrentPullRequest([
154 link(1, { snapshot: snapshot({ state: "merged", updatedAt: "2026-01-03T00:00:00.000Z" }) }),
155 link(2, { snapshot: snapshot({ state: "closed", updatedAt: "2026-01-02T00:00:00.000Z" }) }),
156 ]);
157 expect(current).toMatchObject({ kind: "single", link: { number: 1 } });
158 });
159});
160​
161describe("legacyLinkedPullRequestOf", () => {
162 const identity = {
163 canonicalKey: "github.com/pingdotgg/t3code",
164 provider: "github",
165 displayName: "pingdotgg/t3code",
166 locator: {
167 source: "git-remote" as const,
168 remoteName: "origin",
169 remoteUrl: "https://github.com/pingdotgg/t3code.git",
170 },
171 };
172 it("projects only links the owning project can route without a host", () => {
173 expect(legacyLinkedPullRequestOf([link(7)], "project-1" as never, identity)).toEqual({
174 projectId: "project-1",
175 repository: "pingdotgg/t3code",
176 number: 7,
177 url: "https://github.com/pingdotgg/t3code/pull/7",
178 });
179 expect(legacyLinkedPullRequestOf([], "project-1" as never, identity)).toBeNull();
180 });
181 it.each([
182 {
183 host: "github.enterprise.test",
184 url: "https://github.enterprise.test/pingdotgg/t3code/pull/7",
185 },
186 { repository: "acme/other", url: "https://github.com/acme/other/pull/7" },
187 ])("omits an unsafe legacy route %j", (foreign) => {
188 expect(
189 legacyLinkedPullRequestOf([link(7, foreign)], "project-1" as never, identity),
190 ).toBeNull();
191 expect(
192 legacyLinkedPullRequestOf([link(7, foreign), link(8)], "project-1" as never, identity)
193 ?.number,
194 ).toBe(8);
195 });
196 it.each([
197 "dev.azure.com/org-a/project/_git/web",
198 "ssh.dev.azure.com/v3/org-a/project/web",
199 "org-a.visualstudio.com/DefaultCollection/project/_git/web",
200 ])("projects Azure links through the legacy selector for %s", (canonicalKey) => {
201 const azureIdentity = {
202 ...identity,
203 provider: "azure-devops",
204 canonicalKey,
205 displayName: canonicalKey.slice(canonicalKey.indexOf("/") + 1),
206 name: "web",
207 };
208 const own = link(7, {
209 host: "dev.azure.com",
210 repository: "org-a/project/_git/web",
211 url: "https://dev.azure.com/org-a/project/_git/web/pullrequest/7",
212 });
213 const foreign = link(7, {
214 host: "dev.azure.com",
215 repository: "web",
216 url: "https://dev.azure.com/org-b/project/_git/web/pullrequest/7",
217 linkedAt: "2026-01-02T00:00:00.000Z",
218 });
219 for (const repository of ["web", "org-a/project/_git/web"]) {
220 expect(
221 legacyLinkedPullRequestOf(
222 [{ ...own, repository }, foreign],
223 "project-1" as never,
224 azureIdentity,
225 ),
226 ).toEqual({ projectId: "project-1", repository: "web", number: 7, url: own.url });
227 }
228 expect(legacyLinkedPullRequestOf([foreign], "project-1" as never, azureIdentity)).toBeNull();
229 expect(legacyThreadPullRequestKey({ ...own, repository: "web" })).toEqual({
230 host: "dev.azure.com",
231 repository: "org-a/project/_git/web",
232 number: 7,
233 });
234 expect(
235 threadPullRequestKeysEqual(own, {
236 host: "org-a.visualstudio.com",
237 repository: "DefaultCollection/project/_git/web",
238 number: 7,
239 }),
240 ).toBe(true);
241 });
242 it("does not guess when the project identity is unavailable", () => {
243 expect(legacyLinkedPullRequestOf([link(7)], "project-1" as never, null)).toBeNull();
244 });
245 it("does not route links for a local-path remote with no host or provider", () => {
246 const localIdentity = {
247 canonicalKey: "/tmp/r/remote",
248 displayName: "remote",
249 locator: {
250 source: "git-remote" as const,
251 remoteName: "origin",
252 remoteUrl: "/tmp/r/remote.git",
253 },
254 };
255 expect(legacyLinkedPullRequestOf([link(7)], "project-1" as never, localIdentity)).toBeNull();
256 });
257});
258​
259describe("resolveThreadPullRequestChains", () => {
260 it("chains links by base → head within a repository, bottom to top", () => {
261 const chains = resolveThreadPullRequestChains([
262 link(3, { snapshot: snapshot({ headBranch: "c", baseBranch: "b" }) }),
263 link(1, { snapshot: snapshot({ headBranch: "a", baseBranch: "main" }) }),
264 link(2, { snapshot: snapshot({ headBranch: "b", baseBranch: "a" }) }),
265 link(9, { snapshot: snapshot({ headBranch: "solo", baseBranch: "main" }) }),
266 ]);
267 expect(chains.map((chain) => [chain.kind, chain.layers.map((layer) => layer.number)])).toEqual([
268 ["derived", [1, 2, 3]],
269 ["derived", [9]],
270 ]);
271 });
272​
273 it("keeps every link built on the same parent under it", () => {
274 const links = [
275 link(3, { snapshot: snapshot({ headBranch: "c", baseBranch: "a" }) }),
276 link(5, { snapshot: snapshot({ headBranch: "p", baseBranch: "main" }) }),
277 link(6, { snapshot: snapshot({ headBranch: "q", baseBranch: "p" }) }),
278 link(1, { snapshot: snapshot({ headBranch: "a", baseBranch: "p" }) }),
279 link(2, { snapshot: snapshot({ headBranch: "b", baseBranch: "a" }) }),
280 link(9, { snapshot: snapshot({ headBranch: "solo", baseBranch: "main" }) }),
281 ];
282 const chains = resolveThreadPullRequestChains(links);
283 expect(
284 chains.map((chain) => [chain.layers.map((layer) => layer.number), chain.depths]),
285 ).toEqual([
286 // #6 and #1 both build on #5; #3 and #2 both build on #1.
287 [
288 [5, 6, 1, 3, 2],
289 [0, 1, 1, 2, 2],
290 ],
291 [[9], [0]],
292 ]);
293 expect(resolveThreadPullRequestBadge(links.slice(0, 5))).toEqual({
294 kind: "stack",
295 layers: 5,
296 state: "open",
297 });
298 });
299​
300 it("points at the highest layer of a branched stack, not the last sibling listed", () => {
301 // #1 → #2 → #3, and #4 also builds on #1.
302 const branched = (state: ThreadPullRequestSnapshot["state"]) => [
303 link(1, { snapshot: snapshot({ state, headBranch: "a" }) }),
304 link(2, { snapshot: snapshot({ state, headBranch: "b", baseBranch: "a" }) }),
305 link(3, { snapshot: snapshot({ state, headBranch: "c", baseBranch: "b" }) }),
306 link(4, { snapshot: snapshot({ state, headBranch: "d", baseBranch: "a" }) }),
307 ];
308 const open = resolveThreadCurrentPullRequest(branched("open"));
309 expect(open?.kind === "stack" ? open.open.map((entry) => entry.number) : null).toEqual([
310 3, 4, 2, 1,
311 ]);
312 expect(resolveThreadCurrentPullRequest(branched("merged"))).toMatchObject({
313 kind: "single",
314 link: { number: 3 },
315 });
316 });
317​
318 it("uses the native stack order when the host provides one", () => {
319 const stack = {
320 kind: "native" as const,
321 id: "s1",
322 number: 1,
323 url: "https://github.com/pingdotgg/t3code/stacks/1",
324 base: "main",
325 layers: [
326 { number: 5, headBranch: "a", state: "merged" as const },
327 { number: 6, headBranch: "b", state: "open" as const },
328 ],
329 };
330 const chains = resolveThreadPullRequestChains([
331 link(6, { snapshot: snapshot({ headBranch: "b", baseBranch: "main" }), stack }),
332 link(5, {
333 snapshot: snapshot({ state: "merged", headBranch: "a", baseBranch: "main" }),
334 stack,
335 }),
336 link(8),
337 ]);
338 expect(chains.map((chain) => [chain.kind, chain.layers.map((layer) => layer.number)])).toEqual([
339 ["native", [5, 6]],
340 ["derived", [8]],
341 ]);
342 });
343});
344​
345describe("chain selection and badge state", () => {
346 it.each([
347 ["open", false, "open", false, "open"],
348 ["closed", false, "closed", false, "closed"],
349 ["open", true, "open", true, "draft"],
350 ["open", false, "open", true, "open"],
351 ["closed", false, "open", true, "open"],
352 ["merged", false, "merged", false, "merged"],
353 ["merged", false, "closed", true, "closed"],
354 ] as const)(
355 "aggregates %s (draft %s) and %s (draft %s) as %s",
356 (firstState, firstDraft, secondState, secondDraft, state) => {
357 for (const stacked of [false, true]) {
358 const links = [
359 link(1, {
360 snapshot: snapshot({ state: firstState, isDraft: firstDraft, headBranch: "base" }),
361 }),
362 link(2, {
363 snapshot: snapshot({
364 state: secondState,
365 isDraft: secondDraft,
366 baseBranch: stacked ? "base" : "main",
367 }),
368 }),
369 link(3, { source: "stack-dismissed" }),
370 ];
371 expect(resolveThreadPullRequestBadge(links)).toEqual(
372 stacked
373 ? { kind: "stack", layers: 2, state }
374 : { kind: "pull-request", others: 1, state },
375 );
376 }
377 },
378 );
379​
380 it.each(["open", "merged", "closed"] as const)(
381 "targets the top of a derived %s chain despite a later bottom update and link",
382 (state) => {
383 const bottom = link(2, {
384 snapshot: snapshot({ state, headBranch: "base", updatedAt: "2026-02-01T00:00:00.000Z" }),
385 });
386 const top = link(1, {
387 snapshot: snapshot({ state, headBranch: "top", baseBranch: "base" }),
388 });
389 expect(resolveThreadCurrentPullRequest([bottom, top])).toMatchObject(
390 state === "open"
391 ? { kind: "stack", top: { number: 1 }, open: [top, bottom] }
392 : { kind: "single", link: { number: 1 } },
393 );
394 expect(resolveThreadPullRequestBadge([bottom, top])).toEqual({
395 kind: "stack",
396 layers: 2,
397 state,
398 });
399 },
400 );
401​
402 it("keeps the top native layer after completion and treats an unsynced layer as open", () => {
403 const stack = {
404 kind: "native" as const,
405 id: "native-1",
406 number: 1,
407 url: "https://github.com/pingdotgg/t3code/stacks/1",
408 base: "main",
409 layers: [
410 { number: 2, headBranch: "base", state: "merged" as const },
411 { number: 1, headBranch: "top", state: "merged" as const },
412 ],
413 };
414 const bottom = link(2, {
415 stack,
416 snapshot: snapshot({ state: "merged", updatedAt: "2026-02-01T00:00:00.000Z" }),
417 });
418 const top = link(1, { stack, snapshot: snapshot({ state: "merged" }) });
419 expect(resolveThreadCurrentPullRequest([bottom, top])).toMatchObject({
420 kind: "single",
421 link: { number: 1 },
422 });
423 expect(resolveThreadPullRequestBadge([bottom, { ...top, snapshot: null }])).toEqual({
424 kind: "stack",
425 layers: 2,
426 state: "open",
427 });
428 });
429​
430 it("uses aggregate state and distinguishes unrelated work from a stack", () => {
431 const bottom = link(1, { snapshot: snapshot({ state: "merged", headBranch: "base" }) });
432 const top = link(2, { snapshot: snapshot({ state: "closed", baseBranch: "base" }) });
433 expect(resolveThreadPullRequestBadge([bottom, top])).toEqual({
434 kind: "stack",
435 layers: 2,
436 state: "closed",
437 });
438 expect(resolveThreadPullRequestBadge([bottom, top, link(3)])).toEqual({
439 kind: "pull-request",
440 others: 2,
441 state: "open",
442 });
443 expect(resolveThreadPullRequestBadge([link(3, { source: "stack-dismissed" })])).toBeNull();
444 });
445​
446 it("keeps branch matching case-sensitive while ignoring repository case", () => {
447 const bottom = link(1, {
448 repository: "PingDotGG/T3code",
449 snapshot: snapshot({ headBranch: "Base" }),
450 });
451 const top = link(2, { snapshot: snapshot({ headBranch: "top", baseBranch: "base" }) });
452 expect(resolveThreadPullRequestChains([bottom, top])).toHaveLength(2);
453 expect(
454 resolveThreadPullRequestChains([
455 bottom,
456 { ...top, snapshot: snapshot({ headBranch: "top", baseBranch: "Base" }) },
457 ])[0]?.layers,
458 ).toEqual([bottom, { ...top, snapshot: snapshot({ headBranch: "top", baseBranch: "Base" }) }]);
459 });
460​
461 it("preserves cyclic links without presenting a guessed stack order", () => {
462 const links = [
463 link(1, { snapshot: snapshot({ headBranch: "a", baseBranch: "b" }) }),
464 link(2, { snapshot: snapshot({ headBranch: "b", baseBranch: "a" }) }),
465 ];
466 expect(resolveThreadPullRequestChains(links).map((chain) => chain.layers)).toEqual(
467 links.map((entry) => [entry]),
468 );
469 expect(resolveThreadCurrentPullRequest(links)).toMatchObject({
470 kind: "stack",
471 top: { number: 2 },
472 });
473 expect(resolveThreadPullRequestBadge(links)).toEqual({
474 kind: "pull-request",
475 others: 1,
476 state: "open",
477 });
478 });
479​
480 it("lists a pull request once when two links share its identity", () => {
481 const links = [
482 link(1, { snapshot: snapshot({ headBranch: "a" }) }),
483 link(1, { snapshot: snapshot({ headBranch: "b", baseBranch: "a" }) }),
484 link(1, { snapshot: snapshot({ headBranch: "c" }) }),
485 ];
486 expect(resolveThreadPullRequestChains(links).map((chain) => chain.layers)).toEqual([
487 [links[0]],
488 ]);
489 });
490​
491 it("does not guess a parent when a head branch was reused", () => {
492 const links = [
493 link(1, { snapshot: snapshot({ state: "merged", headBranch: "reused" }) }),
494 link(2, { snapshot: snapshot({ headBranch: "reused" }) }),
495 link(3, { snapshot: snapshot({ headBranch: "top", baseBranch: "reused" }) }),
496 ];
497 expect(resolveThreadPullRequestChains(links).map((chain) => chain.layers)).toEqual(
498 links.map((entry) => [entry]),
499 );
500 });
501});
502​
503describe("threadPullRequestSearchTerms", () => {
504 it.each([undefined, [], [link(34)]])(
505 "searches the branch PR alongside existing links (%j)",
506 (pullRequests) => {
507 const branchPullRequest = {
508 projectId: ProjectId.make("project"),
509 repository: "pingdotgg/t3code",
510 number: 11809,
511 url: "https://github.com/pingdotgg/t3code/pull/11809",
512 };
513 const terms = threadPullRequestSearchTerms({
514 pullRequests,
515 branchPullRequest,
516 linkedPullRequest: {
517 ...branchPullRequest,
518 number: 12,
519 url: "https://github.com/pingdotgg/t3code/pull/12",
520 },
521 });
522 for (const query of ["11809", "#11809", "pingdotgg/t3code#11809", branchPullRequest.url]) {
523 expect(terms.some((term) => term.includes(query))).toBe(true);
524 }
525 expect(terms).toContain(pullRequests?.length ? "#34" : "#12");
526 },
527 );
528​
529 it("includes completed and unsynced links but excludes dismissed links", () => {
530 const terms = threadPullRequestSearchTerms({
531 pullRequests: [
532 link(12, { snapshot: snapshot({ title: "Fix login", state: "merged" }) }),
533 link(34),
534 link(56, { source: "stack-dismissed" }),
535 ],
536 });
537 expect(terms).toContain("#12");
538 expect(terms).toContain("pingdotgg/t3code#12");
539 expect(terms).toContain("https://github.com/pingdotgg/t3code/pull/12");
540 expect(terms).toContain("Fix login");
541 expect(terms).toContain("#34");
542 expect(terms.join(" ")).not.toContain("56");
543 });
544});
545​
546it("searches the legacy projection when old environments decode to an empty links list", () => {
547 const linkedPullRequest = {
548 projectId: ProjectId.make("project"),
549 repository: "pingdotgg/t3code",
550 number: 12,
551 url: "https://github.com/pingdotgg/t3code/pull/12",
552 };
553 expect(threadPullRequestSearchTerms({ pullRequests: [], linkedPullRequest })).toContain("#12");
554 expect(
555 threadPullRequestSearchTerms({ pullRequests: [link(34)], linkedPullRequest }),
556 ).not.toContain("#12");
557});