| 1 | import { ProjectId, ThreadId, type VcsStatusResult } from "@t3tools/contracts"; |
| 2 | import { act, type ComponentProps, type ReactNode } from "react"; |
| 3 | import { create, type ReactTestRenderer } from "react-test-renderer"; |
| 4 | import { afterEach, expect, it, vi } from "vite-plus/test"; |
| 5 | |
| 6 | import { AppAtomRegistryProvider } from "../rpc/atomRegistry"; |
| 7 | import { makeThreadFixture } from "../test-fixtures"; |
| 8 | |
| 9 | const state = vi.hoisted(() => ({ queries: [] as string[] })); |
| 10 | |
| 11 | vi.mock("../state/entities", () => ({ useProject: () => null, useServerConfigs: () => new Map() })); |
| 12 | vi.mock("../uiStateStore", () => ({ useUiStateStore: () => undefined })); |
| 13 | vi.mock("../state/vcs", async () => { |
| 14 | const { AsyncResult, Atom } = await import("effect/reactivity"); |
| 15 | const statuses = Atom.family((cwd: string) => |
| 16 | Atom.make(() => { |
| 17 | state.queries.push(`vcs:${cwd}`); |
| 18 | return AsyncResult.success({ |
| 19 | isRepo: true, |
| 20 | hasPrimaryRemote: true, |
| 21 | isDefaultRef: false, |
| 22 | refName: "feature/test", |
| 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: "Observed worktree PR", |
| 31 | url: "https://github.com/example/repo/pull/42", |
| 32 | baseRef: "main", |
| 33 | headRef: "feature/test", |
| 34 | state: "open", |
| 35 | }, |
| 36 | } satisfies VcsStatusResult); |
| 37 | }), |
| 38 | ); |
| 39 | return { |
| 40 | vcsEnvironment: { status: ({ input }: { input: { cwd: string } }) => statuses(input.cwd) }, |
| 41 | }; |
| 42 | }); |
| 43 | vi.mock("../state/pullRequests", async (importOriginal) => { |
| 44 | const original = await importOriginal<typeof import("../state/pullRequests")>(); |
| 45 | const { AsyncResult, Atom } = await import("effect/reactivity"); |
| 46 | const { ProjectId } = await import("@t3tools/contracts"); |
| 47 | const summaries = Atom.family((number: number) => |
| 48 | Atom.make(() => { |
| 49 | state.queries.push(`pr:${number}`); |
| 50 | return AsyncResult.success({ |
| 51 | provider: "github" as const, |
| 52 | projectId: ProjectId.make("project-test"), |
| 53 | repository: "example/repo", |
| 54 | number, |
| 55 | title: "Observed linked PR", |
| 56 | url: `https://github.com/example/repo/pull/${number}`, |
| 57 | state: "open" as const, |
| 58 | headBranch: "feature/test", |
| 59 | baseBranch: "main", |
| 60 | updatedAt: "2026-09-04T00:00:00.000Z", |
| 61 | }); |
| 62 | }), |
| 63 | ); |
| 64 | return { |
| 65 | ...original, |
| 66 | linkedPullRequestDetailAtom: ({ input }: { input: { number: number } }) => |
| 67 | summaries(input.number), |
| 68 | }; |
| 69 | }); |
| 70 | vi.mock("./ui/tooltip", async () => { |
| 71 | const { cloneElement, isValidElement } = await import("react"); |
| 72 | return { |
| 73 | Tooltip: ({ children }: { children: ReactNode }) => <>{children}</>, |
| 74 | TooltipTrigger({ |
| 75 | render, |
| 76 | children, |
| 77 | }: ComponentProps<typeof import("./ui/tooltip").TooltipTrigger>) { |
| 78 | return isValidElement(render) ? cloneElement(render, undefined, children) : children; |
| 79 | }, |
| 80 | TooltipPopup: () => null, |
| 81 | }; |
| 82 | }); |
| 83 | |
| 84 | import { ThreadRowLeadingStatus } from "./ThreadStatusIndicators"; |
| 85 | |
| 86 | afterEach(() => { |
| 87 | vi.unstubAllGlobals(); |
| 88 | }); |
| 89 | |
| 90 | it("leases only visible palette results while retaining observed and cached PR badges", async () => { |
| 91 | const observers: Array<{ setVisible: (visible: boolean) => void }> = []; |
| 92 | const scrollRoot = {}; |
| 93 | vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true); |
| 94 | vi.stubGlobal( |
| 95 | "IntersectionObserver", |
| 96 | class { |
| 97 | constructor(callback: (entries: ReadonlyArray<{ isIntersecting: boolean }>) => void) { |
| 98 | observers.push({ setVisible: (visible) => callback([{ isIntersecting: visible }]) }); |
| 99 | } |
| 100 | observe() {} |
| 101 | disconnect() {} |
| 102 | }, |
| 103 | ); |
| 104 | const threads = Array.from({ length: 128 }, (_, index) => |
| 105 | makeThreadFixture({ |
| 106 | id: ThreadId.make(`palette-thread-${index}`), |
| 107 | branch: "feature/test", |
| 108 | worktreePath: `/test/worktree-${index}`, |
| 109 | linkedPullRequest: |
| 110 | index % 2 === 0 |
| 111 | ? null |
| 112 | : { |
| 113 | projectId: ProjectId.make("project-test"), |
| 114 | repository: "example/repo", |
| 115 | number: index, |
| 116 | url: `https://github.com/example/repo/pull/${index}`, |
| 117 | }, |
| 118 | }), |
| 119 | ); |
| 120 | let renderer: ReactTestRenderer | undefined; |
| 121 | state.queries.length = 0; |
| 122 | try { |
| 123 | await act(async () => { |
| 124 | renderer = create( |
| 125 | <AppAtomRegistryProvider> |
| 126 | {threads.map((thread, index) => ( |
| 127 | <span key={thread.id}> |
| 128 | <ThreadRowLeadingStatus |
| 129 | thread={thread} |
| 130 | snapshot={ |
| 131 | index === 126 |
| 132 | ? { |
| 133 | branch: "feature/test", |
| 134 | sourceControlProvider: undefined, |
| 135 | pr: { |
| 136 | number: 126, |
| 137 | title: "Cached badge", |
| 138 | url: "https://github.com/example/repo/pull/126", |
| 139 | baseRef: "main", |
| 140 | headRef: "feature/test", |
| 141 | state: "open", |
| 142 | }, |
| 143 | } |
| 144 | : undefined |
| 145 | } |
| 146 | /> |
| 147 | {thread.title} |
| 148 | </span> |
| 149 | ))} |
| 150 | </AppAtomRegistryProvider>, |
| 151 | { createNodeMock: () => ({ parentElement: { closest: () => scrollRoot } }) }, |
| 152 | ); |
| 153 | }); |
| 154 | const mounted = renderer!; |
| 155 | const badgeCount = () => |
| 156 | mounted.root.findAll( |
| 157 | (node) => node.type === "span" && String(node.props["aria-label"] ?? "").startsWith("PR #"), |
| 158 | ).length; |
| 159 | expect(observers).toHaveLength(128); |
| 160 | expect(state.queries).toEqual([]); |
| 161 | expect(badgeCount()).toBe(1); |
| 162 | |
| 163 | await act(async () => { |
| 164 | for (const observer of observers.slice(0, 8)) observer.setVisible(true); |
| 165 | }); |
| 166 | expect(state.queries).toHaveLength(8); |
| 167 | expect(state.queries.filter((key) => key.startsWith("vcs:"))).toHaveLength(4); |
| 168 | expect(state.queries.filter((key) => key.startsWith("pr:"))).toHaveLength(4); |
| 169 | expect(badgeCount()).toBe(9); |
| 170 | |
| 171 | await act(async () => { |
| 172 | for (const observer of observers.slice(0, 8)) observer.setVisible(false); |
| 173 | for (const observer of observers.slice(8, 16)) observer.setVisible(true); |
| 174 | }); |
| 175 | expect(state.queries).toHaveLength(16); |
| 176 | expect(badgeCount()).toBe(17); |
| 177 | } finally { |
| 178 | await act(async () => renderer?.unmount()); |
| 179 | } |
| 180 | }); |