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