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

ThreadStatusIndicators.test.ts

484 lines14.4 KB

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 };
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​
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 provider = {
105 kind: "github" as const,
106 name: "GitHub",
107 baseUrl: "https://github.com",
108 };
109​
110 it("returns the live merged PR when the checkout matches the feature branch", () => {
111 const gitStatus = status({
112 refName: featureBranch,
113 pr: mergedPr,
114 sourceControlProvider: provider,
115 });
116​
117 expect(
118 resolveDisplayedThreadPr({
119 threadBranch: featureBranch,
120 gitStatus,
121 snapshot: undefined,
122 retainTerminalOnBranchMismatch: true,
123 }),
124 ).toBe(mergedPr);
125 expect(
126 resolveDisplayedThreadPrProvider({
127 threadBranch: featureBranch,
128 gitStatus,
129 snapshot: undefined,
130 retainTerminalOnBranchMismatch: true,
131 }),
132 ).toEqual(provider);
133 });
134​
135 it("after caching a merged PR, resolves main status back to the cached feature PR", () => {
136 const matchingStatus = status({
137 refName: featureBranch,
138 pr: mergedPr,
139 sourceControlProvider: provider,
140 });
141 const cached = nextThreadChangeRequestSnapshot({
142 threadBranch: featureBranch,
143 gitStatus: matchingStatus,
144 snapshot: undefined,
145 retainTerminalOnBranchMismatch: true,
146 });
147 expect(cached).toEqual(snapshotFor(featureBranch, mergedPr, provider));
148​
149 const mainStatus = status({
150 refName: "main",
151 isDefaultRef: true,
152 pr: {
153 number: 99,
154 title: "Unrelated main PR",
155 url: "https://github.com/pingdotgg/t3code/pull/99",
156 baseRef: "main",
157 headRef: "main",
158 state: "open",
159 },
160 sourceControlProvider: provider,
161 });
162​
163 expect(
164 resolveDisplayedThreadPr({
165 threadBranch: featureBranch,
166 gitStatus: mainStatus,
167 snapshot: cached as ThreadChangeRequestSnapshot,
168 retainTerminalOnBranchMismatch: true,
169 }),
170 ).toEqual(mergedPr);
171 expect(
172 resolveDisplayedThreadPrProvider({
173 threadBranch: featureBranch,
174 gitStatus: mainStatus,
175 snapshot: cached as ThreadChangeRequestSnapshot,
176 retainTerminalOnBranchMismatch: true,
177 }),
178 ).toEqual(provider);
179 });
180​
181 it("never attaches a PR reported by main to the feature thread", () => {
182 const mainPr = {
183 number: 99,
184 title: "Unrelated main PR",
185 url: "https://github.com/pingdotgg/t3code/pull/99",
186 baseRef: "develop",
187 headRef: "main",
188 state: "merged" as const,
189 };
190 expect(
191 resolveDisplayedThreadPr({
192 threadBranch: featureBranch,
193 gitStatus: status({ refName: "main", pr: mainPr }),
194 snapshot: undefined,
195 retainTerminalOnBranchMismatch: true,
196 }),
197 ).toBeNull();
198 expect(
199 nextThreadChangeRequestSnapshot({
200 threadBranch: featureBranch,
201 gitStatus: status({ refName: "main", pr: mainPr }),
202 snapshot: undefined,
203 retainTerminalOnBranchMismatch: true,
204 }),
205 ).toBeNull();
206 });
207​
208 it("does not show a cached open PR across a branch mismatch", () => {
209 const openSnapshot = snapshotFor(featureBranch, {
210 ...mergedPr,
211 state: "open",
212 title: "Still open",
213 });
214​
215 expect(
216 resolveDisplayedThreadPr({
217 threadBranch: featureBranch,
218 gitStatus: status({ refName: "main", pr: null }),
219 snapshot: openSnapshot,
220 retainTerminalOnBranchMismatch: true,
221 }),
222 ).toBeNull();
223 });
224​
225 it("retains a cached closed PR across a branch mismatch", () => {
226 const closedPr = { ...mergedPr, state: "closed" as const, title: "Closed feature" };
227 const closedSnapshot = snapshotFor(featureBranch, closedPr, provider);
228​
229 expect(
230 resolveDisplayedThreadPr({
231 threadBranch: featureBranch,
232 gitStatus: status({ refName: "main", pr: null }),
233 snapshot: closedSnapshot,
234 retainTerminalOnBranchMismatch: true,
235 }),
236 ).toEqual(closedPr);
237 });
238​
239 it("does not retain or display a terminal PR when a worktree switches branches", () => {
240 const terminalSnapshot = snapshotFor(featureBranch, mergedPr, provider);
241 const mismatchedStatus = status({ refName: "feature/other", pr: null });
242​
243 expect(
244 resolveDisplayedThreadPr({
245 threadBranch: featureBranch,
246 gitStatus: mismatchedStatus,
247 snapshot: terminalSnapshot,
248 retainTerminalOnBranchMismatch: false,
249 }),
250 ).toBeNull();
251 expect(
252 resolveDisplayedThreadPrProvider({
253 threadBranch: featureBranch,
254 gitStatus: mismatchedStatus,
255 snapshot: terminalSnapshot,
256 retainTerminalOnBranchMismatch: false,
257 }),
258 ).toBeUndefined();
259 expect(
260 nextThreadChangeRequestSnapshot({
261 threadBranch: featureBranch,
262 gitStatus: mismatchedStatus,
263 snapshot: terminalSnapshot,
264 retainTerminalOnBranchMismatch: false,
265 }),
266 ).toBeNull();
267 });
268​
269 it("retains a local terminal snapshot when thread metadata follows the new branch", () => {
270 const otherBranchSnapshot = snapshotFor("feature/other", mergedPr, provider);
271​
272 expect(
273 resolveDisplayedThreadPr({
274 threadBranch: featureBranch,
275 gitStatus: status({ refName: "main", pr: null }),
276 snapshot: otherBranchSnapshot,
277 retainTerminalOnBranchMismatch: true,
278 }),
279 ).toEqual(mergedPr);
280 });
281​
282 it("retains a terminal snapshot when a local thread and status move to a branch with no PR", () => {
283 const terminalSnapshot = snapshotFor(featureBranch, mergedPr, provider);
284​
285 expect(
286 nextThreadChangeRequestSnapshot({
287 threadBranch: "main",
288 gitStatus: status({ refName: "main", pr: null }),
289 snapshot: terminalSnapshot,
290 retainTerminalOnBranchMismatch: true,
291 }),
292 ).toBeUndefined();
293 expect(
294 resolveDisplayedThreadPr({
295 threadBranch: "main",
296 gitStatus: status({ refName: "main", pr: null }),
297 snapshot: terminalSnapshot,
298 retainTerminalOnBranchMismatch: true,
299 }),
300 ).toEqual(mergedPr);
301 });
302​
303 it("clears an open snapshot when a local thread moves to a branch with no PR", () => {
304 const openSnapshot = snapshotFor(featureBranch, { ...mergedPr, state: "open" });
305​
306 expect(
307 nextThreadChangeRequestSnapshot({
308 threadBranch: "main",
309 gitStatus: status({ refName: "main", pr: null }),
310 snapshot: openSnapshot,
311 retainTerminalOnBranchMismatch: true,
312 }),
313 ).toBeNull();
314 });
315​
316 it("clears an open snapshot when a local checkout moves to a different branch", () => {
317 const openSnapshot = snapshotFor(featureBranch, { ...mergedPr, state: "open" });
318​
319 expect(
320 nextThreadChangeRequestSnapshot({
321 threadBranch: featureBranch,
322 gitStatus: status({ refName: "main", pr: null }),
323 snapshot: openSnapshot,
324 retainTerminalOnBranchMismatch: true,
325 }),
326 ).toBeNull();
327 });
328​
329 it("clears a retained snapshot when the thread branch is cleared", () => {
330 const terminalSnapshot = snapshotFor(featureBranch, mergedPr, provider);
331​
332 expect(
333 nextThreadChangeRequestSnapshot({
334 threadBranch: null,
335 gitStatus: status({ refName: "main", pr: null }),
336 snapshot: terminalSnapshot,
337 retainTerminalOnBranchMismatch: true,
338 }),
339 ).toBeNull();
340 expect(
341 resolveDisplayedThreadPr({
342 threadBranch: null,
343 gitStatus: status({ refName: "main", pr: null }),
344 snapshot: terminalSnapshot,
345 retainTerminalOnBranchMismatch: true,
346 }),
347 ).toBeNull();
348 expect(
349 resolveDisplayedThreadPrProvider({
350 threadBranch: null,
351 gitStatus: status({ refName: "main", pr: null }),
352 snapshot: terminalSnapshot,
353 retainTerminalOnBranchMismatch: true,
354 }),
355 ).toBeUndefined();
356 });
357​
358 it("does not erase a terminal snapshot when VCS data is missing", () => {
359 const terminalSnapshot = snapshotFor(featureBranch, mergedPr, provider);
360​
361 expect(
362 nextThreadChangeRequestSnapshot({
363 threadBranch: featureBranch,
364 gitStatus: null,
365 snapshot: terminalSnapshot,
366 retainTerminalOnBranchMismatch: true,
367 }),
368 ).toBeUndefined();
369 expect(
370 resolveDisplayedThreadPr({
371 threadBranch: featureBranch,
372 gitStatus: null,
373 snapshot: terminalSnapshot,
374 retainTerminalOnBranchMismatch: true,
375 }),
376 ).toEqual(mergedPr);
377 });
378​
379 it("keeps effectiveSettled true for a retained merged PR after a main checkout", () => {
380 const matchingStatus = status({
381 refName: featureBranch,
382 pr: mergedPr,
383 sourceControlProvider: provider,
384 });
385 const cached = nextThreadChangeRequestSnapshot({
386 threadBranch: featureBranch,
387 gitStatus: matchingStatus,
388 snapshot: undefined,
389 retainTerminalOnBranchMismatch: true,
390 });
391 expect(cached).not.toBeNull();
392 expect(cached).not.toBeUndefined();
393​
394 const mainStatus = status({ refName: "main", pr: null, isDefaultRef: true });
395 const displayed = resolveDisplayedThreadPr({
396 threadBranch: "main",
397 gitStatus: mainStatus,
398 snapshot: cached as ThreadChangeRequestSnapshot,
399 retainTerminalOnBranchMismatch: true,
400 });
401 expect(displayed?.state).toBe("merged");
402​
403 const shell = {
404 id: ThreadId.make("thread-1"),
405 projectId: ProjectId.make("project-1"),
406 title: "Feature thread",
407 modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5.4" },
408 runtimeMode: "full-access",
409 interactionMode: "default",
410 branch: "main",
411 worktreePath: null,
412 latestTurn: null,
413 session: null,
414 createdAt: "2026-04-09T00:00:00.000Z",
415 updatedAt: "2026-04-09T00:00:00.000Z",
416 archivedAt: null,
417 settledAt: null,
418 settledOverride: null,
419 latestUserMessageAt: "2026-04-09T00:00:00.000Z",
420 hasPendingApprovals: false,
421 hasPendingUserInput: false,
422 hasActionableProposedPlan: false,
423 } as OrchestrationThreadShell;
424​
425 expect(
426 effectiveSettled(shell, {
427 now: "2026-04-10T00:00:00.000Z",
428 autoSettleAfterDays: null,
429 changeRequestState: displayed?.state ?? null,
430 }),
431 ).toBe(true);
432 });
433});
434​
435describe("threadChangeRequestSnapshotsAtom", () => {
436 it.effect("retains snapshots while sidebar and chat consumers are unmounted", () =>
437 Effect.gen(function* () {
438 const registry = AtomRegistry.make();
439 const threadKey = "environment-1:thread-1";
440 const snapshot = snapshotFor("feature/current", mergedFeaturePr());
441​
442 const unmount = registry.mount(threadChangeRequestSnapshotsAtom);
443 registry.set(threadChangeRequestSnapshotsAtom, new Map([[threadKey, snapshot]]));
444 unmount();
445​
446 yield* Effect.yieldNow;
447​
448 const remount = registry.mount(threadChangeRequestSnapshotsAtom);
449 expect(registry.get(threadChangeRequestSnapshotsAtom).get(threadKey)).toEqual(snapshot);
450​
451 remount();
452 registry.dispose();
453 }),
454 );
455});
456​
457describe("prStatusIndicator", () => {
458 it("formats PR tooltips with number, uppercase status, and title", () => {
459 expect(prStatusIndicator(status().pr, undefined)).toMatchObject({
460 tooltip: "PR #42 - Open: PR branch",
461 tooltipLead: "PR #42 - Open",
462 tooltipTitle: "PR branch",
463 });
464 });
465​
466 it("uses red for closed pull requests", () => {
467 const closedPr = status().pr;
468 if (!closedPr) throw new Error("Expected pull request fixture");
469​
470 expect(prStatusIndicator({ ...closedPr, state: "closed" }, undefined)?.colorClass).toContain(
471 "text-red-600",
472 );
473 });
474});
475​
476describe("settledPrHoverColorClass", () => {
477 it.each([
478 ["open", "text-emerald-600"],
479 ["merged", "text-violet-600"],
480 ["closed", "text-red-600"],
481 ] as const)("restores the %s pull request color on row hover", (state, colorClass) => {
482 expect(settledPrHoverColorClass(state)).toContain(`group-hover/v2-row:${colorClass}`);
483 });
484});