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

ThreadStatusIndicators.test.ts

503 lines15.0 KB

1import { SourceControlProviderKind } from "@t3tools/contracts";
2import { ProjectId, type PullRequestSummary, type VcsStatusResult } from "@t3tools/contracts";
3import { describe, expect, it } from "@effect/vitest";
4import * as Effect from "effect/Effect";
5import { AtomRegistry } from "effect/reactivity";
6​
7import {
8 nextThreadChangeRequestSnapshot,
9 resolveDisplayedThreadPr,
10 resolveDisplayedThreadPrProvider,
11 resolveThreadPr,
12 threadChangeRequestSnapshotsEqual,
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: SourceControlProviderKind.make("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("never lets a stale open response regress a merged observation", () => {
80 const merged = pullRequestSummary("merged", "2026-09-03T01:01:00.000Z");
81 const staleOpen = pullRequestSummary("open", "2026-09-03T01:00:00.000Z");
82​
83 expect(newestPullRequestSummary(merged, staleOpen)).toBe(merged);
84 });
85​
86 it("accepts a newer open state after a closed pull request is reopened", () => {
87 const closed = pullRequestSummary("closed", "2026-09-03T01:00:00.000Z");
88 const reopened = pullRequestSummary("open", "2026-09-03T01:01:00.000Z");
89​
90 expect(newestPullRequestSummary(closed, reopened)).toBe(reopened);
91 });
92});
93​
94describe("resolveThreadPr", () => {
95 it("hides PR indicators when a dedicated worktree has switched away from the thread branch", () => {
96 expect(
97 resolveThreadPr({
98 threadBranch: "stack/base",
99 gitStatus: status(),
100 }),
101 ).toBeNull();
102 });
103});
104​
105describe("resolveDisplayedThreadPr + nextThreadChangeRequestSnapshot", () => {
106 const featureBranch = "feature/current";
107 const mergedPr = mergedFeaturePr();
108 const linkedPullRequest = {
109 projectId: ProjectId.make("project-1"),
110 repository: "pingdotgg/t3code",
111 number: 42,
112 url: "https://github.com/pingdotgg/t3code/pull/42",
113 };
114 const provider = {
115 kind: SourceControlProviderKind.make("github"),
116 name: "GitHub",
117 baseUrl: "https://github.com",
118 };
119​
120 it("shows a linked pull request when the checkout has a different branch", () => {
121 const linkedPullRequestStatus = {
122 pr: mergedPr,
123 sourceControlProvider: provider,
124 };
125​
126 expect(
127 resolveDisplayedThreadPr({
128 threadBranch: "feature/other",
129 gitStatus: status({ refName: "feature/other", pr: null }),
130 snapshot: undefined,
131 retainTerminalOnBranchMismatch: false,
132 linkedPullRequest,
133 linkedPullRequestStatus,
134 }),
135 ).toEqual(mergedPr);
136 expect(
137 resolveDisplayedThreadPrProvider({
138 threadBranch: "feature/other",
139 gitStatus: status({ refName: "feature/other", pr: null }),
140 snapshot: undefined,
141 retainTerminalOnBranchMismatch: false,
142 linkedPullRequest,
143 linkedPullRequestStatus,
144 }),
145 ).toEqual(provider);
146 expect(
147 nextThreadChangeRequestSnapshot({
148 threadBranch: "feature/other",
149 gitStatus: status({ refName: "feature/other", pr: null }),
150 snapshot: undefined,
151 retainTerminalOnBranchMismatch: false,
152 linkedPullRequest,
153 linkedPullRequestStatus,
154 }),
155 ).toEqual({
156 branch: "feature/other",
157 pr: mergedPr,
158 sourceControlProvider: provider,
159 linkedPullRequest,
160 });
161 });
162​
163 it("keeps a matching linked pull request snapshot while its status reloads", () => {
164 const snapshot = {
165 ...snapshotFor(featureBranch, mergedPr, provider),
166 linkedPullRequest,
167 };
168​
169 expect(
170 resolveDisplayedThreadPr({
171 threadBranch: null,
172 gitStatus: null,
173 snapshot,
174 retainTerminalOnBranchMismatch: false,
175 linkedPullRequest,
176 linkedPullRequestStatus: null,
177 }),
178 ).toEqual(mergedPr);
179 expect(
180 nextThreadChangeRequestSnapshot({
181 threadBranch: null,
182 gitStatus: null,
183 snapshot,
184 retainTerminalOnBranchMismatch: false,
185 linkedPullRequest,
186 linkedPullRequestStatus: null,
187 }),
188 ).toBeUndefined();
189 });
190​
191 it("clears an old snapshot when a different pull request is linked", () => {
192 const snapshot = {
193 ...snapshotFor(featureBranch, mergedPr, provider),
194 linkedPullRequest: { ...linkedPullRequest, number: 41 },
195 };
196​
197 expect(
198 nextThreadChangeRequestSnapshot({
199 threadBranch: featureBranch,
200 gitStatus: null,
201 snapshot,
202 retainTerminalOnBranchMismatch: true,
203 linkedPullRequest,
204 linkedPullRequestStatus: null,
205 }),
206 ).toBeNull();
207 });
208​
209 it("removes a linked pull request snapshot after the link is cleared", () => {
210 const snapshot = {
211 ...snapshotFor(featureBranch, mergedPr, provider),
212 linkedPullRequest,
213 };
214​
215 expect(
216 resolveDisplayedThreadPr({
217 threadBranch: featureBranch,
218 gitStatus: status({ refName: "main", pr: null }),
219 snapshot,
220 retainTerminalOnBranchMismatch: true,
221 }),
222 ).toBeNull();
223 expect(
224 nextThreadChangeRequestSnapshot({
225 threadBranch: featureBranch,
226 gitStatus: null,
227 snapshot,
228 retainTerminalOnBranchMismatch: true,
229 }),
230 ).toBeNull();
231 });
232​
233 it("after caching a merged PR, resolves main status back to the cached feature PR", () => {
234 const matchingStatus = status({
235 refName: featureBranch,
236 pr: mergedPr,
237 sourceControlProvider: provider,
238 });
239 const cached = nextThreadChangeRequestSnapshot({
240 threadBranch: featureBranch,
241 gitStatus: matchingStatus,
242 snapshot: undefined,
243 retainTerminalOnBranchMismatch: true,
244 });
245 expect(cached).toEqual(snapshotFor(featureBranch, mergedPr, provider));
246​
247 const mainStatus = status({
248 refName: "main",
249 isDefaultRef: true,
250 pr: {
251 number: 99,
252 title: "Unrelated main PR",
253 url: "https://github.com/pingdotgg/t3code/pull/99",
254 baseRef: "main",
255 headRef: "main",
256 state: "open",
257 },
258 sourceControlProvider: provider,
259 });
260​
261 expect(
262 resolveDisplayedThreadPr({
263 threadBranch: featureBranch,
264 gitStatus: mainStatus,
265 snapshot: cached as ThreadChangeRequestSnapshot,
266 retainTerminalOnBranchMismatch: true,
267 }),
268 ).toEqual(mergedPr);
269 expect(
270 resolveDisplayedThreadPrProvider({
271 threadBranch: featureBranch,
272 gitStatus: mainStatus,
273 snapshot: cached as ThreadChangeRequestSnapshot,
274 retainTerminalOnBranchMismatch: true,
275 }),
276 ).toEqual(provider);
277 });
278​
279 it("never attaches a PR reported by main to the feature thread", () => {
280 const mainPr = {
281 number: 99,
282 title: "Unrelated main PR",
283 url: "https://github.com/pingdotgg/t3code/pull/99",
284 baseRef: "develop",
285 headRef: "main",
286 state: "merged" as const,
287 };
288 expect(
289 resolveDisplayedThreadPr({
290 threadBranch: featureBranch,
291 gitStatus: status({ refName: "main", pr: mainPr }),
292 snapshot: undefined,
293 retainTerminalOnBranchMismatch: true,
294 }),
295 ).toBeNull();
296 expect(
297 nextThreadChangeRequestSnapshot({
298 threadBranch: featureBranch,
299 gitStatus: status({ refName: "main", pr: mainPr }),
300 snapshot: undefined,
301 retainTerminalOnBranchMismatch: true,
302 }),
303 ).toBeNull();
304 });
305​
306 it("does not show a cached open PR across a branch mismatch", () => {
307 const openSnapshot = snapshotFor(featureBranch, {
308 ...mergedPr,
309 state: "open",
310 title: "Still open",
311 });
312​
313 expect(
314 resolveDisplayedThreadPr({
315 threadBranch: featureBranch,
316 gitStatus: status({ refName: "main", pr: null }),
317 snapshot: openSnapshot,
318 retainTerminalOnBranchMismatch: true,
319 }),
320 ).toBeNull();
321 });
322​
323 it("retains a cached closed PR across a branch mismatch", () => {
324 const closedPr = { ...mergedPr, state: "closed" as const, title: "Closed feature" };
325 const closedSnapshot = snapshotFor(featureBranch, closedPr, provider);
326​
327 expect(
328 resolveDisplayedThreadPr({
329 threadBranch: featureBranch,
330 gitStatus: status({ refName: "main", pr: null }),
331 snapshot: closedSnapshot,
332 retainTerminalOnBranchMismatch: true,
333 }),
334 ).toEqual(closedPr);
335 });
336​
337 it("does not retain or display a terminal PR when a worktree switches branches", () => {
338 const terminalSnapshot = snapshotFor(featureBranch, mergedPr, provider);
339 const mismatchedStatus = status({ refName: "feature/other", pr: null });
340​
341 expect(
342 resolveDisplayedThreadPr({
343 threadBranch: featureBranch,
344 gitStatus: mismatchedStatus,
345 snapshot: terminalSnapshot,
346 retainTerminalOnBranchMismatch: false,
347 }),
348 ).toBeNull();
349 expect(
350 resolveDisplayedThreadPrProvider({
351 threadBranch: featureBranch,
352 gitStatus: mismatchedStatus,
353 snapshot: terminalSnapshot,
354 retainTerminalOnBranchMismatch: false,
355 }),
356 ).toBeUndefined();
357 expect(
358 nextThreadChangeRequestSnapshot({
359 threadBranch: featureBranch,
360 gitStatus: mismatchedStatus,
361 snapshot: terminalSnapshot,
362 retainTerminalOnBranchMismatch: false,
363 }),
364 ).toBeNull();
365 });
366​
367 it("retains a terminal snapshot when a local thread and status move to a branch with no PR", () => {
368 const terminalSnapshot = snapshotFor(featureBranch, mergedPr, provider);
369​
370 expect(
371 nextThreadChangeRequestSnapshot({
372 threadBranch: "main",
373 gitStatus: status({ refName: "main", pr: null }),
374 snapshot: terminalSnapshot,
375 retainTerminalOnBranchMismatch: true,
376 }),
377 ).toBeUndefined();
378 expect(
379 resolveDisplayedThreadPr({
380 threadBranch: "main",
381 gitStatus: status({ refName: "main", pr: null }),
382 snapshot: terminalSnapshot,
383 retainTerminalOnBranchMismatch: true,
384 }),
385 ).toEqual(mergedPr);
386 });
387​
388 it("clears an open snapshot when a local thread moves to a branch with no PR", () => {
389 const openSnapshot = snapshotFor(featureBranch, { ...mergedPr, state: "open" });
390​
391 expect(
392 nextThreadChangeRequestSnapshot({
393 threadBranch: "main",
394 gitStatus: status({ refName: "main", pr: null }),
395 snapshot: openSnapshot,
396 retainTerminalOnBranchMismatch: true,
397 }),
398 ).toBeNull();
399 });
400​
401 it("clears a retained snapshot when the thread branch is cleared", () => {
402 const terminalSnapshot = snapshotFor(featureBranch, mergedPr, provider);
403​
404 expect(
405 nextThreadChangeRequestSnapshot({
406 threadBranch: null,
407 gitStatus: status({ refName: "main", pr: null }),
408 snapshot: terminalSnapshot,
409 retainTerminalOnBranchMismatch: true,
410 }),
411 ).toBeNull();
412 expect(
413 resolveDisplayedThreadPr({
414 threadBranch: null,
415 gitStatus: status({ refName: "main", pr: null }),
416 snapshot: terminalSnapshot,
417 retainTerminalOnBranchMismatch: true,
418 }),
419 ).toBeNull();
420 expect(
421 resolveDisplayedThreadPrProvider({
422 threadBranch: null,
423 gitStatus: status({ refName: "main", pr: null }),
424 snapshot: terminalSnapshot,
425 retainTerminalOnBranchMismatch: true,
426 }),
427 ).toBeUndefined();
428 });
429​
430 it("does not erase a terminal snapshot when VCS data is missing", () => {
431 const terminalSnapshot = snapshotFor(featureBranch, mergedPr, provider);
432​
433 expect(
434 nextThreadChangeRequestSnapshot({
435 threadBranch: featureBranch,
436 gitStatus: null,
437 snapshot: terminalSnapshot,
438 retainTerminalOnBranchMismatch: true,
439 }),
440 ).toBeUndefined();
441 expect(
442 resolveDisplayedThreadPr({
443 threadBranch: featureBranch,
444 gitStatus: null,
445 snapshot: terminalSnapshot,
446 retainTerminalOnBranchMismatch: true,
447 }),
448 ).toEqual(mergedPr);
449 });
450​
451 it("shows the last matching PR snapshot while its live status lease is released", () => {
452 const openPr = { ...mergedPr, state: "open" as const };
453 const input = {
454 threadBranch: featureBranch,
455 gitStatus: null,
456 snapshot: snapshotFor(featureBranch, openPr, provider),
457 retainTerminalOnBranchMismatch: false,
458 };
459 expect(resolveDisplayedThreadPr(input)).toEqual(openPr);
460 expect(resolveDisplayedThreadPrProvider(input)).toEqual(provider);
461 expect(resolveDisplayedThreadPr({ ...input, threadBranch: "feature/other" })).toBeNull();
462 expect(
463 resolveDisplayedThreadPr({
464 ...input,
465 snapshot: { ...input.snapshot, linkedPullRequest },
466 }),
467 ).toBeNull();
468 });
469​
470 it("refreshes a cached snapshot when a pull request becomes ready", () => {
471 const readyPr = { ...mergedPr, state: "open" as const };
472 const draftPr = { ...readyPr, isDraft: true };
473​
474 expect(
475 threadChangeRequestSnapshotsEqual(
476 snapshotFor(featureBranch, draftPr),
477 snapshotFor(featureBranch, readyPr),
478 ),
479 ).toBe(false);
480 });
481});
482​
483describe("threadChangeRequestSnapshotsAtom", () => {
484 it.effect("retains snapshots while sidebar and chat consumers are unmounted", () =>
485 Effect.gen(function* () {
486 const registry = AtomRegistry.make();
487 const threadKey = "environment-1:thread-1";
488 const snapshot = snapshotFor("feature/current", mergedFeaturePr());
489​
490 const unmount = registry.mount(threadChangeRequestSnapshotsAtom);
491 registry.set(threadChangeRequestSnapshotsAtom, new Map([[threadKey, snapshot]]));
492 unmount();
493​
494 yield* Effect.yieldNow;
495​
496 const remount = registry.mount(threadChangeRequestSnapshotsAtom);
497 expect(registry.get(threadChangeRequestSnapshotsAtom).get(threadKey)).toEqual(snapshot);
498​
499 remount();
500 registry.dispose();
501 }),
502 );
503});