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

ThreadStatusIndicators.test.ts

571 lines16.6 KB

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