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

ThreadStatusIndicators.test.ts

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