Open History to see the last commit
1import { ProjectId, type PullRequestSummary, type VcsStatusResult } from "@t3tools/contracts";
2import { describe, expect, it } from "@effect/vitest";
3import * as Effect from "effect/Effect";
4import { AtomRegistry } from "effect/unstable/reactivity";
5import {
6 GitMergeIcon,
7 GitPullRequestClosedIcon,
8 GitPullRequestDraftIcon,
9 GitPullRequestIcon,
10} from "lucide-react";
11
12import {
13 ChangeRequestStatusIcon,
14 nextThreadChangeRequestSnapshot,
15 prStatusIndicator,
16 resolveDisplayedThreadPr,
17 resolveDisplayedThreadPrProvider,
18 resolveThreadPr,
19 settledPrHoverColorClass,
20 threadChangeRequestSnapshotsEqual,
21 threadChangeRequestSnapshotsAtom,
22 type ThreadChangeRequestSnapshot,
23} from "./ThreadStatusIndicators";
24import { newestPullRequestSummary } from "../state/pullRequests";
25
26describe("ChangeRequestStatusIcon", () => {
27 it.each([
28 ["open", "open", false, GitPullRequestIcon],
29 ["draft", "open", true, GitPullRequestDraftIcon],
30 ["closed", "closed", false, GitPullRequestClosedIcon],
31 ["merged", "merged", false, GitMergeIcon],
32 ] as const)("uses the %s pull request glyph", (_label, state, isDraft, expectedIcon) => {
33 expect(ChangeRequestStatusIcon({ state, isDraft }).type).toBe(expectedIcon);
34 });
35});
36
37function status(overrides: Partial<VcsStatusResult> = {}): VcsStatusResult {
38 return {
39 isRepo: true,
40 hasPrimaryRemote: true,
41 isDefaultRef: false,
42 refName: "feature/current",
43 hasWorkingTreeChanges: false,
44 workingTree: { files: [], insertions: 0, deletions: 0 },
45 hasUpstream: true,
46 aheadCount: 0,
47 behindCount: 0,
48 pr: {
49 number: 42,
50 title: "PR branch",
51 url: "https://github.com/pingdotgg/t3code/pull/42",
52 baseRef: "main",
53 headRef: "feature/current",
54 state: "open",
55 },
56 ...overrides,
57 };
58}
59
60function mergedFeaturePr(): NonNullable<VcsStatusResult["pr"]> {
61 return {
62 number: 42,
63 title: "Feature PR",
64 url: "https://github.com/pingdotgg/t3code/pull/42",
65 baseRef: "main",
66 headRef: "feature/current",
67 state: "merged",
68 };
69}
70
71function snapshotFor(
72 branch: string,
73 pr: NonNullable<VcsStatusResult["pr"]>,
74 sourceControlProvider?: VcsStatusResult["sourceControlProvider"],
75): ThreadChangeRequestSnapshot {
76 return { branch, pr, sourceControlProvider };
77}
78
79function pullRequestSummary(
80 state: PullRequestSummary["state"],
81 updatedAt: string,
82): PullRequestSummary {
83 return {
84 provider: "github",
85 projectId: ProjectId.make("project-1"),
86 repository: "pingdotgg/t3code",
87 number: 42,
88 title: "Feature PR",
89 url: "https://github.com/pingdotgg/t3code/pull/42",
90 state,
91 headBranch: "feature/current",
92 baseBranch: "main",
93 updatedAt,
94 };
95}
96
97describe("shared pull request state", () => {
98 it("shows a panel-observed merge instead of an older sidebar summary", () => {
99 const open = pullRequestSummary("open", "2026-09-03T01:00:00.000Z");
100 const merged = pullRequestSummary("merged", "2026-09-03T01:01:00.000Z");
101
102 expect(newestPullRequestSummary(open, merged)).toBe(merged);
103 });
104
105 it("never lets a stale open response regress a merged observation", () => {
106 const merged = pullRequestSummary("merged", "2026-09-03T01:01:00.000Z");
107 const staleOpen = pullRequestSummary("open", "2026-09-03T01:00:00.000Z");
108
109 expect(newestPullRequestSummary(merged, staleOpen)).toBe(merged);
110 });
111
112 it("accepts a newer open state after a closed pull request is reopened", () => {
113 const closed = pullRequestSummary("closed", "2026-09-03T01:00:00.000Z");
114 const reopened = pullRequestSummary("open", "2026-09-03T01:01:00.000Z");
115
116 expect(newestPullRequestSummary(closed, reopened)).toBe(reopened);
117 });
118});
119
120describe("resolveThreadPr", () => {
121 it("keeps local-checkout PR indicators scoped to the stored thread branch", () => {
122 expect(
123 resolveThreadPr({
124 threadBranch: "feature/other",
125 gitStatus: status(),
126 }),
127 ).toBeNull();
128 });
129
130 it("hides PR indicators when a dedicated worktree has switched away from the thread branch", () => {
131 expect(
132 resolveThreadPr({
133 threadBranch: "stack/base",
134 gitStatus: status(),
135 }),
136 ).toBeNull();
137 });
138
139 it("hides PR indicators when thread branch metadata is missing", () => {
140 expect(
141 resolveThreadPr({
142 threadBranch: null,
143 gitStatus: status(),
144 }),
145 ).toBeNull();
146 });
147
148 it("shows the PR when the live checkout matches the stored thread branch", () => {
149 const gitStatus = status();
150
151 expect(
152 resolveThreadPr({
153 threadBranch: "feature/current",
154 gitStatus,
155 }),
156 ).toBe(gitStatus.pr);
157 });
158});
159
160describe("resolveDisplayedThreadPr + nextThreadChangeRequestSnapshot", () => {
161 const featureBranch = "feature/current";
162 const mergedPr = mergedFeaturePr();
163 const linkedPullRequest = {
164 projectId: ProjectId.make("project-1"),
165 repository: "pingdotgg/t3code",
166 number: 42,
167 url: "https://github.com/pingdotgg/t3code/pull/42",
168 };
169 const provider = {
170 kind: "github" as const,
171 name: "GitHub",
172 baseUrl: "https://github.com",
173 };
174
175 it("returns the live merged PR when the checkout matches the feature branch", () => {
176 const gitStatus = status({
177 refName: featureBranch,
178 pr: mergedPr,
179 sourceControlProvider: provider,
180 });
181
182 expect(
183 resolveDisplayedThreadPr({
184 threadBranch: featureBranch,
185 gitStatus,
186 snapshot: undefined,
187 retainTerminalOnBranchMismatch: true,
188 }),
189 ).toBe(mergedPr);
190 expect(
191 resolveDisplayedThreadPrProvider({
192 threadBranch: featureBranch,
193 gitStatus,
194 snapshot: undefined,
195 retainTerminalOnBranchMismatch: true,
196 }),
197 ).toEqual(provider);
198 });
199
200 it("shows a linked pull request when the checkout has a different branch", () => {
201 const linkedPullRequestStatus = {
202 pr: mergedPr,
203 sourceControlProvider: provider,
204 };
205
206 expect(
207 resolveDisplayedThreadPr({
208 threadBranch: "feature/other",
209 gitStatus: status({ refName: "feature/other", pr: null }),
210 snapshot: undefined,
211 retainTerminalOnBranchMismatch: false,
212 linkedPullRequest,
213 linkedPullRequestStatus,
214 }),
215 ).toEqual(mergedPr);
216 expect(
217 resolveDisplayedThreadPrProvider({
218 threadBranch: "feature/other",
219 gitStatus: status({ refName: "feature/other", pr: null }),
220 snapshot: undefined,
221 retainTerminalOnBranchMismatch: false,
222 linkedPullRequest,
223 linkedPullRequestStatus,
224 }),
225 ).toEqual(provider);
226 expect(
227 nextThreadChangeRequestSnapshot({
228 threadBranch: "feature/other",
229 gitStatus: status({ refName: "feature/other", pr: null }),
230 snapshot: undefined,
231 retainTerminalOnBranchMismatch: false,
232 linkedPullRequest,
233 linkedPullRequestStatus,
234 }),
235 ).toEqual({
236 branch: "feature/other",
237 pr: mergedPr,
238 sourceControlProvider: provider,
239 linkedPullRequest,
240 });
241 });
242
243 it("keeps a matching linked pull request snapshot while its status reloads", () => {
244 const snapshot = {
245 ...snapshotFor(featureBranch, mergedPr, provider),
246 linkedPullRequest,
247 };
248
249 expect(
250 resolveDisplayedThreadPr({
251 threadBranch: null,
252 gitStatus: null,
253 snapshot,
254 retainTerminalOnBranchMismatch: false,
255 linkedPullRequest,
256 linkedPullRequestStatus: null,
257 }),
258 ).toEqual(mergedPr);
259 expect(
260 nextThreadChangeRequestSnapshot({
261 threadBranch: null,
262 gitStatus: null,
263 snapshot,
264 retainTerminalOnBranchMismatch: false,
265 linkedPullRequest,
266 linkedPullRequestStatus: null,
267 }),
268 ).toBeUndefined();
269 });
270
271 it("clears an old snapshot when a different pull request is linked", () => {
272 const snapshot = {
273 ...snapshotFor(featureBranch, mergedPr, provider),
274 linkedPullRequest: { ...linkedPullRequest, number: 41 },
275 };
276
277 expect(
278 nextThreadChangeRequestSnapshot({
279 threadBranch: featureBranch,
280 gitStatus: null,
281 snapshot,
282 retainTerminalOnBranchMismatch: true,
283 linkedPullRequest,
284 linkedPullRequestStatus: null,
285 }),
286 ).toBeNull();
287 });
288
289 it("removes a linked pull request snapshot after the link is cleared", () => {
290 const snapshot = {
291 ...snapshotFor(featureBranch, mergedPr, provider),
292 linkedPullRequest,
293 };
294
295 expect(
296 resolveDisplayedThreadPr({
297 threadBranch: featureBranch,
298 gitStatus: status({ refName: "main", pr: null }),
299 snapshot,
300 retainTerminalOnBranchMismatch: true,
301 }),
302 ).toBeNull();
303 expect(
304 nextThreadChangeRequestSnapshot({
305 threadBranch: featureBranch,
306 gitStatus: null,
307 snapshot,
308 retainTerminalOnBranchMismatch: true,
309 }),
310 ).toBeNull();
311 });
312
313 it("after caching a merged PR, resolves main status back to the cached feature PR", () => {
314 const matchingStatus = status({
315 refName: featureBranch,
316 pr: mergedPr,
317 sourceControlProvider: provider,
318 });
319 const cached = nextThreadChangeRequestSnapshot({
320 threadBranch: featureBranch,
321 gitStatus: matchingStatus,
322 snapshot: undefined,
323 retainTerminalOnBranchMismatch: true,
324 });
325 expect(cached).toEqual(snapshotFor(featureBranch, mergedPr, provider));
326
327 const mainStatus = status({
328 refName: "main",
329 isDefaultRef: true,
330 pr: {
331 number: 99,
332 title: "Unrelated main PR",
333 url: "https://github.com/pingdotgg/t3code/pull/99",
334 baseRef: "main",
335 headRef: "main",
336 state: "open",
337 },
338 sourceControlProvider: provider,
339 });
340
341 expect(
342 resolveDisplayedThreadPr({
343 threadBranch: featureBranch,
344 gitStatus: mainStatus,
345 snapshot: cached as ThreadChangeRequestSnapshot,
346 retainTerminalOnBranchMismatch: true,
347 }),
348 ).toEqual(mergedPr);
349 expect(
350 resolveDisplayedThreadPrProvider({
351 threadBranch: featureBranch,
352 gitStatus: mainStatus,
353 snapshot: cached as ThreadChangeRequestSnapshot,
354 retainTerminalOnBranchMismatch: true,
355 }),
356 ).toEqual(provider);
357 });
358
359 it("never attaches a PR reported by main to the feature thread", () => {
360 const mainPr = {
361 number: 99,
362 title: "Unrelated main PR",
363 url: "https://github.com/pingdotgg/t3code/pull/99",
364 baseRef: "develop",
365 headRef: "main",
366 state: "merged" as const,
367 };
368 expect(
369 resolveDisplayedThreadPr({
370 threadBranch: featureBranch,
371 gitStatus: status({ refName: "main", pr: mainPr }),
372 snapshot: undefined,
373 retainTerminalOnBranchMismatch: true,
374 }),
375 ).toBeNull();
376 expect(
377 nextThreadChangeRequestSnapshot({
378 threadBranch: featureBranch,
379 gitStatus: status({ refName: "main", pr: mainPr }),
380 snapshot: undefined,
381 retainTerminalOnBranchMismatch: true,
382 }),
383 ).toBeNull();
384 });
385
386 it("does not show a cached open PR across a branch mismatch", () => {
387 const openSnapshot = snapshotFor(featureBranch, {
388 ...mergedPr,
389 state: "open",
390 title: "Still open",
391 });
392
393 expect(
394 resolveDisplayedThreadPr({
395 threadBranch: featureBranch,
396 gitStatus: status({ refName: "main", pr: null }),
397 snapshot: openSnapshot,
398 retainTerminalOnBranchMismatch: true,
399 }),
400 ).toBeNull();
401 });
402
403 it("retains a cached closed PR across a branch mismatch", () => {
404 const closedPr = { ...mergedPr, state: "closed" as const, title: "Closed feature" };
405 const closedSnapshot = snapshotFor(featureBranch, closedPr, provider);
406
407 expect(
408 resolveDisplayedThreadPr({
409 threadBranch: featureBranch,
410 gitStatus: status({ refName: "main", pr: null }),
411 snapshot: closedSnapshot,
412 retainTerminalOnBranchMismatch: true,
413 }),
414 ).toEqual(closedPr);
415 });
416
417 it("does not retain or display a terminal PR when a worktree switches branches", () => {
418 const terminalSnapshot = snapshotFor(featureBranch, mergedPr, provider);
419 const mismatchedStatus = status({ refName: "feature/other", pr: null });
420
421 expect(
422 resolveDisplayedThreadPr({
423 threadBranch: featureBranch,
424 gitStatus: mismatchedStatus,
425 snapshot: terminalSnapshot,
426 retainTerminalOnBranchMismatch: false,
427 }),
428 ).toBeNull();
429 expect(
430 resolveDisplayedThreadPrProvider({
431 threadBranch: featureBranch,
432 gitStatus: mismatchedStatus,
433 snapshot: terminalSnapshot,
434 retainTerminalOnBranchMismatch: false,
435 }),
436 ).toBeUndefined();
437 expect(
438 nextThreadChangeRequestSnapshot({
439 threadBranch: featureBranch,
440 gitStatus: mismatchedStatus,
441 snapshot: terminalSnapshot,
442 retainTerminalOnBranchMismatch: false,
443 }),
444 ).toBeNull();
445 });
446
447 it("retains a local terminal snapshot when thread metadata follows the new branch", () => {
448 const otherBranchSnapshot = snapshotFor("feature/other", mergedPr, provider);
449
450 expect(
451 resolveDisplayedThreadPr({
452 threadBranch: featureBranch,
453 gitStatus: status({ refName: "main", pr: null }),
454 snapshot: otherBranchSnapshot,
455 retainTerminalOnBranchMismatch: true,
456 }),
457 ).toEqual(mergedPr);
458 });
459
460 it("retains a terminal snapshot when a local thread and status move to a branch with no PR", () => {
461 const terminalSnapshot = snapshotFor(featureBranch, mergedPr, provider);
462
463 expect(
464 nextThreadChangeRequestSnapshot({
465 threadBranch: "main",
466 gitStatus: status({ refName: "main", pr: null }),
467 snapshot: terminalSnapshot,
468 retainTerminalOnBranchMismatch: true,
469 }),
470 ).toBeUndefined();
471 expect(
472 resolveDisplayedThreadPr({
473 threadBranch: "main",
474 gitStatus: status({ refName: "main", pr: null }),
475 snapshot: terminalSnapshot,
476 retainTerminalOnBranchMismatch: true,
477 }),
478 ).toEqual(mergedPr);
479 });
480
481 it("clears an open snapshot when a local thread moves to a branch with no PR", () => {
482 const openSnapshot = snapshotFor(featureBranch, { ...mergedPr, state: "open" });
483
484 expect(
485 nextThreadChangeRequestSnapshot({
486 threadBranch: "main",
487 gitStatus: status({ refName: "main", pr: null }),
488 snapshot: openSnapshot,
489 retainTerminalOnBranchMismatch: true,
490 }),
491 ).toBeNull();
492 });
493
494 it("clears an open snapshot when a local checkout moves to a different branch", () => {
495 const openSnapshot = snapshotFor(featureBranch, { ...mergedPr, state: "open" });
496
497 expect(
498 nextThreadChangeRequestSnapshot({
499 threadBranch: featureBranch,
500 gitStatus: status({ refName: "main", pr: null }),
501 snapshot: openSnapshot,
502 retainTerminalOnBranchMismatch: true,
503 }),
504 ).toBeNull();
505 });
506
507 it("clears a retained snapshot when the thread branch is cleared", () => {
508 const terminalSnapshot = snapshotFor(featureBranch, mergedPr, provider);
509
510 expect(
511 nextThreadChangeRequestSnapshot({
512 threadBranch: null,
513 gitStatus: status({ refName: "main", pr: null }),
514 snapshot: terminalSnapshot,
515 retainTerminalOnBranchMismatch: true,
516 }),
517 ).toBeNull();
518 expect(
519 resolveDisplayedThreadPr({
520 threadBranch: null,
521 gitStatus: status({ refName: "main", pr: null }),
522 snapshot: terminalSnapshot,
523 retainTerminalOnBranchMismatch: true,
524 }),
525 ).toBeNull();
526 expect(
527 resolveDisplayedThreadPrProvider({
528 threadBranch: null,
529 gitStatus: status({ refName: "main", pr: null }),
530 snapshot: terminalSnapshot,
531 retainTerminalOnBranchMismatch: true,
532 }),
533 ).toBeUndefined();
534 });
535
536 it("does not erase a terminal snapshot when VCS data is missing", () => {
537 const terminalSnapshot = snapshotFor(featureBranch, mergedPr, provider);
538
539 expect(
540 nextThreadChangeRequestSnapshot({
541 threadBranch: featureBranch,
542 gitStatus: null,
543 snapshot: terminalSnapshot,
544 retainTerminalOnBranchMismatch: true,
545 }),
546 ).toBeUndefined();
547 expect(
548 resolveDisplayedThreadPr({
549 threadBranch: featureBranch,
550 gitStatus: null,
551 snapshot: terminalSnapshot,
552 retainTerminalOnBranchMismatch: true,
553 }),
554 ).toEqual(mergedPr);
555 });
556
557 it("shows the last matching PR snapshot while its live status lease is released", () => {
558 const openPr = { ...mergedPr, state: "open" as const };
559 const input = {
560 threadBranch: featureBranch,
561 gitStatus: null,
562 snapshot: snapshotFor(featureBranch, openPr, provider),
563 retainTerminalOnBranchMismatch: false,
564 };
565 expect(resolveDisplayedThreadPr(input)).toEqual(openPr);
566 expect(resolveDisplayedThreadPrProvider(input)).toEqual(provider);
567 expect(resolveDisplayedThreadPr({ ...input, threadBranch: "feature/other" })).toBeNull();
568 expect(
569 resolveDisplayedThreadPr({
570 ...input,
571 snapshot: { ...input.snapshot, linkedPullRequest },
572 }),
573 ).toBeNull();
574 });
575
576 it("retains a merged PR after a main checkout", () => {
577 const matchingStatus = status({
578 refName: featureBranch,
579 pr: mergedPr,
580 sourceControlProvider: provider,
581 });
582 const cached = nextThreadChangeRequestSnapshot({
583 threadBranch: featureBranch,
584 gitStatus: matchingStatus,
585 snapshot: undefined,
586 retainTerminalOnBranchMismatch: true,
587 });
588 expect(cached).not.toBeNull();
589 expect(cached).not.toBeUndefined();
590
591 const mainStatus = status({ refName: "main", pr: null, isDefaultRef: true });
592 const displayed = resolveDisplayedThreadPr({
593 threadBranch: "main",
594 gitStatus: mainStatus,
595 snapshot: cached as ThreadChangeRequestSnapshot,
596 retainTerminalOnBranchMismatch: true,
597 });
598 expect(displayed?.state).toBe("merged");
599 });
600
601 it("refreshes a cached snapshot when a pull request becomes ready", () => {
602 const readyPr = { ...mergedPr, state: "open" as const };
603 const draftPr = { ...readyPr, isDraft: true };
604
605 expect(
606 threadChangeRequestSnapshotsEqual(
607 snapshotFor(featureBranch, draftPr),
608 snapshotFor(featureBranch, readyPr),
609 ),
610 ).toBe(false);
611 });
612});
613
614describe("threadChangeRequestSnapshotsAtom", () => {
615 it.effect("retains snapshots while sidebar and chat consumers are unmounted", () =>
616 Effect.gen(function* () {
617 const registry = AtomRegistry.make();
618 const threadKey = "environment-1:thread-1";
619 const snapshot = snapshotFor("feature/current", mergedFeaturePr());
620
621 const unmount = registry.mount(threadChangeRequestSnapshotsAtom);
622 registry.set(threadChangeRequestSnapshotsAtom, new Map([[threadKey, snapshot]]));
623 unmount();
624
625 yield* Effect.yieldNow;
626
627 const remount = registry.mount(threadChangeRequestSnapshotsAtom);
628 expect(registry.get(threadChangeRequestSnapshotsAtom).get(threadKey)).toEqual(snapshot);
629
630 remount();
631 registry.dispose();
632 }),
633 );
634});
635
636describe("prStatusIndicator", () => {
637 it("formats PR tooltips with number, uppercase status, and title", () => {
638 expect(prStatusIndicator(status().pr, undefined)).toMatchObject({
639 tooltip: "PR #42 - Open: PR branch",
640 tooltipLead: "PR #42 - Open",
641 tooltipTitle: "PR branch",
642 });
643 });
644
645 it("uses red for closed pull requests", () => {
646 const closedPr = status().pr;
647 if (!closedPr) throw new Error("Expected pull request fixture");
648
649 expect(prStatusIndicator({ ...closedPr, state: "closed" }, undefined)?.colorClass).toContain(
650 "text-red-600",
651 );
652 });
653
654 it("uses gray and draft wording for draft pull requests", () => {
655 const draftPr = status().pr;
656 if (!draftPr) throw new Error("Expected pull request fixture");
657
658 expect(prStatusIndicator({ ...draftPr, isDraft: true }, undefined)).toMatchObject({
659 label: "PR draft",
660 colorClass: "text-zinc-500 dark:text-zinc-400/80",
661 tooltipLead: "PR #42 - Draft",
662 });
663 });
664});
665
666describe("settledPrHoverColorClass", () => {
667 it.each([
668 ["open", "text-emerald-600"],
669 ["merged", "text-violet-600"],
670 ["closed", "text-red-600"],
671 ] as const)("restores the %s pull request color on row hover", (state, colorClass) => {
672 expect(settledPrHoverColorClass(state)).toContain(`group-hover/sidebar-row:${colorClass}`);
673 });
674
675 it("keeps draft pull requests gray on row hover", () => {
676 expect(settledPrHoverColorClass("open", true)).toContain(
677 "group-hover/sidebar-row:text-zinc-500",
678 );
679 });
680});