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

ThreadStatusIndicators.test.ts

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