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

ThreadStatusIndicators.test.ts

641 lines19.0 KB

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