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

threadPullRequests.test.ts

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