The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { sha256 } from "@noble/hashes/sha2"; |
| 2 | import { bytesToHex } from "@noble/hashes/utils"; |
| 3 | import { |
| 4 | EnvironmentId, |
| 5 | ProjectId, |
| 6 | SourceControlProviderKind, |
| 7 | type PullRequestDetailView, |
| 8 | type PullRequestDiffResult, |
| 9 | } from "@t3tools/contracts"; |
| 10 | import { expect, it, vi } from "vite-plus/test"; |
| 11 | import { page, userEvent } from "vite-plus/test/browser"; |
| 12 | |
| 13 | import "~/index.css"; |
| 14 | import { __setClientSettingsForTests } from "../../hooks/useSettings"; |
| 15 | import { setAppearanceMode } from "../../hooks/useTheme"; |
| 16 | import { DEFAULT_CLIENT_SETTINGS } from "@t3tools/contracts/settings"; |
| 17 | |
| 18 | const { diffAtom, testFileDiffAtom, pagedDiffAtom } = await vi.hoisted(async () => { |
| 19 | const { AsyncResult, Atom } = await import("effect/reactivity"); |
| 20 | const filePatch = (path: string, addition = "after()") => |
| 21 | [ |
| 22 | `diff --git a/${path} b/${path}`, |
| 23 | "index 1111111..2222222 100644", |
| 24 | `--- a/${path}`, |
| 25 | `+++ b/${path}`, |
| 26 | "@@ -1,6 +1,6 @@", |
| 27 | "-before()", |
| 28 | `+${addition}`, |
| 29 | " one()", |
| 30 | " two()", |
| 31 | " three()", |
| 32 | " four()", |
| 33 | " five()", |
| 34 | "", |
| 35 | ].join("\n"); |
| 36 | const firstPage: PullRequestDiffResult = { |
| 37 | patch: filePatch("a.ts"), |
| 38 | truncated: false, |
| 39 | nextCursor: null, |
| 40 | }; |
| 41 | const pagedDiffAtom = Atom.family((shortPages: boolean) => { |
| 42 | const pages: Record<string, PullRequestDiffResult> = { |
| 43 | first: { |
| 44 | patch: Array.from({ length: shortPages ? 2 : 40 }, (_, index) => |
| 45 | filePatch(index === 1 ? "early-1.test.ts" : `early-${index}.ts`), |
| 46 | ).join(""), |
| 47 | truncated: true, |
| 48 | nextCursor: "page-2", |
| 49 | }, |
| 50 | "page-2": { |
| 51 | patch: (shortPages ? filePatch("late.ts") : "") + filePatch("late.test.ts"), |
| 52 | truncated: false, |
| 53 | nextCursor: null, |
| 54 | }, |
| 55 | }; |
| 56 | return Atom.family((cursor: string) => Atom.make(AsyncResult.success(pages[cursor]!))); |
| 57 | }); |
| 58 | return { |
| 59 | // An environment query whose refetch returns equal content hands back the previous value |
| 60 | // itself, so a refresh publishes a new result around the very same page. |
| 61 | diffAtom: Atom.make(() => AsyncResult.success(firstPage)), |
| 62 | testFileDiffAtom: Atom.make(() => |
| 63 | AsyncResult.success({ |
| 64 | patch: |
| 65 | filePatch("early-0.ts") + |
| 66 | filePatch("early-1.test.ts", "testOnly()") + |
| 67 | filePatch("early-2.generated.ts") + |
| 68 | (mocks.refreshed ? filePatch("early-3.ts") : ""), |
| 69 | truncated: false, |
| 70 | nextCursor: null, |
| 71 | }), |
| 72 | ), |
| 73 | pagedDiffAtom, |
| 74 | }; |
| 75 | }); |
| 76 | |
| 77 | const mocks = vi.hoisted(() => ({ |
| 78 | paged: false, |
| 79 | shortPages: false, |
| 80 | withTestFile: false, |
| 81 | refreshed: false, |
| 82 | diffCommits: [] as Array<string | undefined>, |
| 83 | viewedPaths: null as ReadonlySet<string> | null, |
| 84 | viewedListeners: new Set<() => void>(), |
| 85 | })); |
| 86 | |
| 87 | function deliverViewedPaths(paths: ReadonlySet<string> | null) { |
| 88 | mocks.viewedPaths = paths; |
| 89 | for (const listener of mocks.viewedListeners) listener(); |
| 90 | } |
| 91 | |
| 92 | vi.mock(import("~/state/use-atom-command"), async (original) => ({ |
| 93 | ...(await original()), |
| 94 | useAtomCommand: (() => vi.fn()) as never, |
| 95 | })); |
| 96 | vi.mock(import("./usePullRequestFilesViewed"), async () => { |
| 97 | const { useSyncExternalStore } = await import("react"); |
| 98 | const subscribe = (listener: () => void) => { |
| 99 | mocks.viewedListeners.add(listener); |
| 100 | return () => mocks.viewedListeners.delete(listener); |
| 101 | }; |
| 102 | return { |
| 103 | usePullRequestFilesViewed: () => { |
| 104 | const viewedPaths = useSyncExternalStore(subscribe, () => mocks.viewedPaths); |
| 105 | return { |
| 106 | enabled: viewedPaths !== null, |
| 107 | isViewed: (path: string) => viewedPaths?.has(path) === true, |
| 108 | isStale: () => false, |
| 109 | setViewed: (path: string, viewed: boolean) => { |
| 110 | const next = new Set(mocks.viewedPaths); |
| 111 | if (viewed) next.add(path); |
| 112 | else next.delete(path); |
| 113 | deliverViewedPaths(next); |
| 114 | }, |
| 115 | truncated: false, |
| 116 | error: null, |
| 117 | refresh: () => {}, |
| 118 | }; |
| 119 | }, |
| 120 | }; |
| 121 | }); |
| 122 | vi.mock(import("~/state/pullRequests"), async (original) => { |
| 123 | const actual = await original(); |
| 124 | return { |
| 125 | ...actual, |
| 126 | pullRequestEnvironment: { |
| 127 | ...actual.pullRequestEnvironment, |
| 128 | diff: (({ input }: { input: { cursor?: string; commit?: string } }) => { |
| 129 | mocks.diffCommits.push(input.commit); |
| 130 | if (mocks.paged) return pagedDiffAtom(mocks.shortPages)(input.cursor ?? "first"); |
| 131 | return mocks.withTestFile ? testFileDiffAtom : diffAtom; |
| 132 | }) as never, |
| 133 | }, |
| 134 | }; |
| 135 | }); |
| 136 | |
| 137 | import { render, stubGlobal } from "../../../test/render"; |
| 138 | import PullRequestCodeTab from "./PullRequestCodeTab"; |
| 139 | |
| 140 | const detail: PullRequestDetailView = { |
| 141 | provider: SourceControlProviderKind.make("github"), |
| 142 | projectId: ProjectId.make("project"), |
| 143 | projectTitle: "Project", |
| 144 | workspaceRoot: "/workspace", |
| 145 | repository: "owner/repo", |
| 146 | number: 1, |
| 147 | title: "Test pull request", |
| 148 | body: "", |
| 149 | url: "https://github.com/owner/repo/pull/1", |
| 150 | author: { login: "author", name: null, avatarUrl: null }, |
| 151 | viewer: "author", |
| 152 | state: "open", |
| 153 | isDraft: false, |
| 154 | mergeability: "mergeable", |
| 155 | additions: 1, |
| 156 | deletions: 1, |
| 157 | changedFiles: 1, |
| 158 | headBranch: "feature", |
| 159 | baseBranch: "main", |
| 160 | createdAt: "2026-09-01T00:00:00Z", |
| 161 | updatedAt: "2026-09-01T00:00:00Z", |
| 162 | mergedAt: null, |
| 163 | closedAt: null, |
| 164 | reviewers: [], |
| 165 | labels: [], |
| 166 | checks: [], |
| 167 | comments: [], |
| 168 | commentCount: 0, |
| 169 | commentsTruncated: false, |
| 170 | reviewThreads: [], |
| 171 | commits: [], |
| 172 | mergeCapabilities: { merge: false, squash: false, rebase: false }, |
| 173 | capabilities: { |
| 174 | diff: true, |
| 175 | comment: false, |
| 176 | search: true, |
| 177 | actions: [], |
| 178 | mergeMethods: [], |
| 179 | review: { inlineComment: false, reply: false, resolve: false, verdicts: [] }, |
| 180 | reviewers: { request: false, listCandidates: false }, |
| 181 | edit: { changeRequest: false, comment: false }, |
| 182 | }, |
| 183 | viewerPermissions: { |
| 184 | actions: [], |
| 185 | comment: false, |
| 186 | resolve: false, |
| 187 | verdicts: [], |
| 188 | requestReviewers: false, |
| 189 | }, |
| 190 | }; |
| 191 | |
| 192 | function setup({ paged }: { paged: boolean }) { |
| 193 | window.localStorage.removeItem("t3code.diffHideTestFiles"); |
| 194 | __setClientSettingsForTests(DEFAULT_CLIENT_SETTINGS); |
| 195 | mocks.paged = paged; |
| 196 | mocks.shortPages = false; |
| 197 | mocks.withTestFile = false; |
| 198 | mocks.refreshed = false; |
| 199 | mocks.diffCommits = []; |
| 200 | mocks.viewedPaths = null; |
| 201 | mocks.viewedListeners.clear(); |
| 202 | } |
| 203 | |
| 204 | const tab = ( |
| 205 | refreshToken: number, |
| 206 | revealRequest: { readonly path: string; readonly nonce: number } | null = null, |
| 207 | revealUrl?: string, |
| 208 | onRevealSettled?: (url: string) => void, |
| 209 | ) => ( |
| 210 | <PullRequestCodeTab |
| 211 | scrollerRef={() => {}} |
| 212 | environmentId={EnvironmentId.make("environment")} |
| 213 | reference={detail} |
| 214 | detail={detail} |
| 215 | selectedCommitOid={null} |
| 216 | onSelectedCommitChange={() => {}} |
| 217 | onRefresh={() => {}} |
| 218 | refreshToken={refreshToken} |
| 219 | revealRequest={revealRequest} |
| 220 | revealUrl={revealUrl} |
| 221 | onRevealSettled={onRevealSettled} |
| 222 | /> |
| 223 | ); |
| 224 | |
| 225 | it("keeps the diff after a refresh that brings back the same page", async () => { |
| 226 | setup({ paged: false }); |
| 227 | const container = document.createElement("div"); |
| 228 | container.className = "flex h-[700px] w-[900px]"; |
| 229 | await using screen = await render(tab(0), { container }); |
| 230 | await expect.element(screen.getByText("1 file", { exact: true })).toBeVisible(); |
| 231 | |
| 232 | await screen.rerender(tab(1)); |
| 233 | |
| 234 | await expect |
| 235 | .element(screen.getByText("This pull request has no file changes.")) |
| 236 | .not.toBeInTheDocument(); |
| 237 | await expect.element(screen.getByText("1 file", { exact: true })).toBeVisible(); |
| 238 | }); |
| 239 | |
| 240 | it("restores saved Viewed folds, follows cleared marks, and allows explicit expansion", async () => { |
| 241 | setup({ paged: false }); |
| 242 | __setClientSettingsForTests({ ...DEFAULT_CLIENT_SETTINGS, diffFilesCollapsed: false }); |
| 243 | using _preferences = { |
| 244 | [Symbol.dispose]() { |
| 245 | __setClientSettingsForTests(DEFAULT_CLIENT_SETTINGS); |
| 246 | }, |
| 247 | }; |
| 248 | mocks.viewedPaths = new Set(["a.ts"]); |
| 249 | const container = document.createElement("div"); |
| 250 | container.className = "flex h-[700px] w-[900px]"; |
| 251 | await using screen = await render(tab(0), { container }); |
| 252 | await expect.element(screen.getByRole("checkbox", { name: "Viewed" })).toBeChecked(); |
| 253 | await expect |
| 254 | .element(screen.getByRole("button", { name: "Expand diff", exact: true })) |
| 255 | .toHaveAttribute("aria-expanded", "false"); |
| 256 | |
| 257 | await screen.getByRole("button", { name: "Expand diff", exact: true }).click(); |
| 258 | await expect.element(screen.getByText("three()", { exact: true })).toBeVisible(); |
| 259 | screen.getByRole("checkbox", { name: "Viewed" }).element().focus(); |
| 260 | await userEvent.keyboard(" "); |
| 261 | await expect.element(screen.getByRole("checkbox", { name: "Viewed" })).not.toBeChecked(); |
| 262 | await expect.element(screen.getByText("three()", { exact: true })).toBeVisible(); |
| 263 | screen.getByRole("checkbox", { name: "Viewed" }).element().focus(); |
| 264 | await userEvent.keyboard(" "); |
| 265 | await expect |
| 266 | .element(screen.getByRole("button", { name: "Expand diff", exact: true })) |
| 267 | .toHaveAttribute("aria-expanded", "false"); |
| 268 | |
| 269 | deliverViewedPaths(new Set()); |
| 270 | await expect.element(screen.getByRole("checkbox", { name: "Viewed" })).not.toBeChecked(); |
| 271 | await expect.element(screen.getByText("three()", { exact: true })).toBeVisible(); |
| 272 | deliverViewedPaths(new Set(["a.ts"])); |
| 273 | await expect |
| 274 | .element(screen.getByRole("button", { name: "Expand diff", exact: true })) |
| 275 | .toHaveAttribute("aria-expanded", "false"); |
| 276 | await screen.getByRole("button", { name: "Expand all files", exact: true }).click(); |
| 277 | await expect.element(screen.getByText("three()", { exact: true })).toBeVisible(); |
| 278 | |
| 279 | await screen.rerender(<div />); |
| 280 | await screen.rerender(tab(1)); |
| 281 | await expect |
| 282 | .element(screen.getByRole("button", { name: "Expand diff", exact: true })) |
| 283 | .toHaveAttribute("aria-expanded", "false"); |
| 284 | }); |
| 285 | |
| 286 | // regression: #13279 a Viewed answer that lands after a link reveals a file must not fold it |
| 287 | it("keeps a revealed file open when its Viewed mark loads afterwards", async () => { |
| 288 | setup({ paged: false }); |
| 289 | __setClientSettingsForTests({ ...DEFAULT_CLIENT_SETTINGS, diffFilesCollapsed: false }); |
| 290 | using _preferences = { |
| 291 | [Symbol.dispose]() { |
| 292 | __setClientSettingsForTests(DEFAULT_CLIENT_SETTINGS); |
| 293 | }, |
| 294 | }; |
| 295 | mocks.viewedPaths = new Set(); |
| 296 | const container = document.createElement("div"); |
| 297 | container.className = "flex h-[700px] w-[900px]"; |
| 298 | await using screen = await render(tab(0, { path: "a.ts", nonce: 1 }), { container }); |
| 299 | await expect.element(screen.getByText("three()", { exact: true })).toBeVisible(); |
| 300 | |
| 301 | deliverViewedPaths(new Set(["a.ts"])); |
| 302 | await expect.element(screen.getByRole("checkbox", { name: "Viewed" })).toBeChecked(); |
| 303 | |
| 304 | await expect |
| 305 | .element(screen.getByRole("button", { name: "Collapse diff" })) |
| 306 | .toHaveAttribute("aria-expanded", "true"); |
| 307 | await expect.element(screen.getByText("three()", { exact: true })).toBeVisible(); |
| 308 | }); |
| 309 | |
| 310 | function pagedContainer() { |
| 311 | const container = document.createElement("div"); |
| 312 | container.className = "flex h-[500px] w-[900px]"; |
| 313 | return container; |
| 314 | } |
| 315 | |
| 316 | async function expectFileHeaderShown( |
| 317 | screen: Awaited<ReturnType<typeof render>>, |
| 318 | container: HTMLElement, |
| 319 | path: string, |
| 320 | ) { |
| 321 | const header = screen.getByText(path, { exact: true }).first(); |
| 322 | await expect.element(header).toBeInTheDocument(); |
| 323 | await vi.waitFor(() => { |
| 324 | const bounds = container.querySelector(".diff-render-surface")!.getBoundingClientRect(); |
| 325 | const { top, bottom } = header.element().getBoundingClientRect(); |
| 326 | expect(top).toBeGreaterThanOrEqual(bounds.top); |
| 327 | expect(bottom).toBeLessThanOrEqual(bounds.bottom); |
| 328 | }); |
| 329 | } |
| 330 | |
| 331 | it("scrolls to a file asked for as the tab opens", async () => { |
| 332 | setup({ paged: true }); |
| 333 | const container = pagedContainer(); |
| 334 | await using screen = await render(tab(0, { path: "early-39.ts", nonce: 1 }), { container }); |
| 335 | |
| 336 | await expectFileHeaderShown(screen, container, "early-39.ts"); |
| 337 | }); |
| 338 | |
| 339 | it("pages forward to a file asked for that is not loaded yet, then scrolls to it", async () => { |
| 340 | setup({ paged: true }); |
| 341 | __setClientSettingsForTests({ ...DEFAULT_CLIENT_SETTINGS, diffFilesCollapsed: true }); |
| 342 | using _preferences = { |
| 343 | [Symbol.dispose]() { |
| 344 | window.localStorage.removeItem("t3code.diffHideTestFiles"); |
| 345 | __setClientSettingsForTests(DEFAULT_CLIENT_SETTINGS); |
| 346 | }, |
| 347 | }; |
| 348 | const container = pagedContainer(); |
| 349 | await using screen = await render(tab(0), { container }); |
| 350 | await expect.element(screen.getByText("early-0.ts", { exact: true }).first()).toBeVisible(); |
| 351 | await screen.getByRole("button", { name: "Hide test files" }).click(); |
| 352 | |
| 353 | const url = `${detail.url}/files#diff-${bytesToHex(sha256(new TextEncoder().encode("late.test.ts")))}R4-R5`; |
| 354 | const settled = vi.fn(); |
| 355 | await screen.rerender(tab(0, null, url, settled)); |
| 356 | |
| 357 | await expectFileHeaderShown(screen, container, "late.test.ts"); |
| 358 | await expect.element(screen.getByText("three()", { exact: true })).toBeVisible(); |
| 359 | await vi.waitFor(() => expect(settled).toHaveBeenCalledWith(url)); |
| 360 | // A settled link keeps its test file past the filter, and a later click reveals it again. |
| 361 | await screen.rerender(tab(0, { path: "early-0.ts", nonce: 1 })); |
| 362 | await expectFileHeaderShown(screen, container, "early-0.ts"); |
| 363 | await screen.rerender(tab(0, { path: "early-0.ts", nonce: 1 }, url, settled)); |
| 364 | await expectFileHeaderShown(screen, container, "late.test.ts"); |
| 365 | }); |
| 366 | |
| 367 | it("keeps a linked test file expanded when a refresh changes the rest of the diff", async () => { |
| 368 | setup({ paged: false }); |
| 369 | mocks.withTestFile = true; |
| 370 | __setClientSettingsForTests({ ...DEFAULT_CLIENT_SETTINGS, diffFilesCollapsed: true }); |
| 371 | using _preferences = { |
| 372 | [Symbol.dispose]() { |
| 373 | window.localStorage.removeItem("t3code.diffHideTestFiles"); |
| 374 | __setClientSettingsForTests(DEFAULT_CLIENT_SETTINGS); |
| 375 | }, |
| 376 | }; |
| 377 | const container = document.createElement("div"); |
| 378 | container.className = "flex h-[700px] w-[900px]"; |
| 379 | await using screen = await render(tab(0), { container }); |
| 380 | await screen.getByRole("button", { name: "Hide test files" }).click(); |
| 381 | |
| 382 | const path = "early-1.test.ts"; |
| 383 | const url = `${detail.url}/files#diff-${bytesToHex(sha256(new TextEncoder().encode(path)))}`; |
| 384 | const settled = vi.fn(); |
| 385 | await screen.rerender(tab(0, null, url, settled)); |
| 386 | await vi.waitFor(() => expect(settled).toHaveBeenCalledWith(url)); |
| 387 | await screen.rerender(tab(0)); |
| 388 | const testLine = screen.getByText("testOnly()", { exact: true }); |
| 389 | await expect.element(testLine).toBeVisible(); |
| 390 | |
| 391 | mocks.refreshed = true; |
| 392 | await screen.rerender(tab(1)); |
| 393 | await expect.element(screen.getByText("4 files", { exact: true })).toBeVisible(); |
| 394 | await expect.element(testLine).toBeVisible(); |
| 395 | }); |
| 396 | |
| 397 | // Two short pages of a source and a test file each; the next page loads only through the file tree. |
| 398 | async function renderShortPages(stack: AsyncDisposableStack) { |
| 399 | setup({ paged: true }); |
| 400 | mocks.shortPages = true; |
| 401 | __setClientSettingsForTests({ ...DEFAULT_CLIENT_SETTINGS, diffFilesCollapsed: false }); |
| 402 | stack.defer(() => { |
| 403 | window.localStorage.removeItem("t3code.diffHideTestFiles"); |
| 404 | window.localStorage.removeItem("t3code.pullRequestFileTreeOpen"); |
| 405 | __setClientSettingsForTests(DEFAULT_CLIENT_SETTINGS); |
| 406 | }); |
| 407 | stack.use( |
| 408 | stubGlobal( |
| 409 | "IntersectionObserver", |
| 410 | class extends IntersectionObserver { |
| 411 | constructor(callback: IntersectionObserverCallback, options?: IntersectionObserverInit) { |
| 412 | super(options?.rootMargin === "240px" ? () => {} : callback, options); |
| 413 | } |
| 414 | }, |
| 415 | ), |
| 416 | ); |
| 417 | const container = document.createElement("div"); |
| 418 | container.className = "flex h-[1000px] w-[900px]"; |
| 419 | const screen = stack.use(await render(tab(0), { container })); |
| 420 | const loadNextPage = async () => { |
| 421 | await screen.getByRole("button", { name: "Show file tree", exact: true }).click(); |
| 422 | const loadMore = screen.getByRole("button", { name: "Load more files", exact: true }); |
| 423 | await loadMore.click(); |
| 424 | await expect.element(loadMore).not.toBeInTheDocument(); |
| 425 | await screen.getByRole("button", { name: "Hide file tree", exact: true }).click(); |
| 426 | }; |
| 427 | // The later page sits at the bottom of the list, which grows as expanded files render. |
| 428 | const diffToggle = async (path: string) => { |
| 429 | const scroller = container.querySelector(".diff-render-surface")!; |
| 430 | const header = await vi.waitFor(() => { |
| 431 | scroller.scrollTop = scroller.scrollHeight; |
| 432 | return screen.getByText(path, { exact: true }).element(); |
| 433 | }); |
| 434 | const { host } = header.getRootNode() as ShadowRoot; |
| 435 | return page.elementLocator(host).getByRole("button", { name: /^(Expand|Collapse) diff$/ }); |
| 436 | }; |
| 437 | const filter = screen.getByRole("button", { name: "Hide test files" }); |
| 438 | return { screen, loadNextPage, diffToggle, filter }; |
| 439 | } |
| 440 | |
| 441 | it("keeps a later test file folded by hand through a refresh and bulk toggles while hidden", async () => { |
| 442 | await using stack = new AsyncDisposableStack(); |
| 443 | const { screen, loadNextPage, diffToggle, filter } = await renderShortPages(stack); |
| 444 | await loadNextPage(); |
| 445 | const lateTest = await diffToggle("late.test.ts"); |
| 446 | await lateTest.click(); |
| 447 | await expect.element(lateTest).toHaveAttribute("aria-expanded", "false"); |
| 448 | await filter.click(); |
| 449 | |
| 450 | // A refresh drops the loaded later page, so the bulk toggles below run without it. |
| 451 | await screen.rerender(tab(1)); |
| 452 | await expect.element(screen.getByText("1 test file hidden", { exact: true })).toBeVisible(); |
| 453 | await screen.getByRole("button", { name: "Collapse all files", exact: true }).click(); |
| 454 | await screen.getByRole("button", { name: "Expand all files", exact: true }).click(); |
| 455 | await loadNextPage(); |
| 456 | await filter.click(); |
| 457 | |
| 458 | await expect.element(screen.getByText("4 files", { exact: true })).toBeVisible(); |
| 459 | await expect.element(await diffToggle("late.test.ts")).toHaveAttribute("aria-expanded", "false"); |
| 460 | }); |
| 461 | |
| 462 | it("never requests the diff of a selected commit the pull request no longer has", async () => { |
| 463 | setup({ paged: false }); |
| 464 | const container = document.createElement("div"); |
| 465 | container.className = "flex h-[700px] w-[900px]"; |
| 466 | // The timeline can open a commit that a force-push removed from the pull request. |
| 467 | await using screen = await render( |
| 468 | <PullRequestCodeTab |
| 469 | scrollerRef={() => {}} |
| 470 | environmentId={EnvironmentId.make("environment")} |
| 471 | reference={detail} |
| 472 | detail={{ |
| 473 | ...detail, |
| 474 | commits: [ |
| 475 | { |
| 476 | oid: "kept", |
| 477 | messageHeadline: "Kept commit", |
| 478 | committedDate: "2026-09-01T00:00:00Z", |
| 479 | }, |
| 480 | ], |
| 481 | }} |
| 482 | selectedCommitOid="gone" |
| 483 | onSelectedCommitChange={() => {}} |
| 484 | onRefresh={() => {}} |
| 485 | />, |
| 486 | { container }, |
| 487 | ); |
| 488 | |
| 489 | await expect.element(screen.getByText("1 file", { exact: true })).toBeVisible(); |
| 490 | await expect |
| 491 | .element(screen.getByRole("button", { name: "Diff scope: All commits" })) |
| 492 | .toBeVisible(); |
| 493 | expect(mocks.diffCommits).not.toContain("gone"); |
| 494 | }); |
| 495 | |
| 496 | it("answers a commit's diff link only once that commit's diff is shown", async () => { |
| 497 | setup({ paged: false }); |
| 498 | const container = document.createElement("div"); |
| 499 | container.className = "flex h-[700px] w-[900px]"; |
| 500 | const oid = "c".repeat(40); |
| 501 | const withCommit = { |
| 502 | ...detail, |
| 503 | commits: [{ oid, messageHeadline: "Linked commit", committedDate: "2026-09-01T00:00:00Z" }], |
| 504 | }; |
| 505 | const url = `${detail.url}/commits/${oid}#diff-${bytesToHex(sha256(new TextEncoder().encode("a.ts")))}R2`; |
| 506 | const settled = vi.fn(); |
| 507 | const scoped = (selectedCommitOid: string | null, revealUrl?: string) => ( |
| 508 | <PullRequestCodeTab |
| 509 | scrollerRef={() => {}} |
| 510 | environmentId={EnvironmentId.make("environment")} |
| 511 | reference={detail} |
| 512 | detail={withCommit} |
| 513 | selectedCommitOid={selectedCommitOid} |
| 514 | onSelectedCommitChange={() => {}} |
| 515 | onRefresh={() => {}} |
| 516 | revealUrl={revealUrl} |
| 517 | onRevealSettled={settled} |
| 518 | /> |
| 519 | ); |
| 520 | await using screen = await render(scoped(null), { container }); |
| 521 | await expect.element(screen.getByText("1 file", { exact: true })).toBeVisible(); |
| 522 | |
| 523 | // The panel selects the link's commit after this tab has already seen the link. |
| 524 | await screen.rerender(scoped(null, url)); |
| 525 | expect(settled).not.toHaveBeenCalled(); |
| 526 | await screen.rerender(scoped(oid, url)); |
| 527 | await vi.waitFor(() => expect(settled).toHaveBeenCalledWith(url)); |
| 528 | }); |
| 529 | |
| 530 | it("hides a resolved conversation behind avatars on its line until they are clicked", async () => { |
| 531 | setup({ paged: false }); |
| 532 | __setClientSettingsForTests({ ...DEFAULT_CLIENT_SETTINGS, diffFilesCollapsed: false }); |
| 533 | using _preferences = { |
| 534 | [Symbol.dispose]() { |
| 535 | __setClientSettingsForTests(DEFAULT_CLIENT_SETTINGS); |
| 536 | }, |
| 537 | }; |
| 538 | const container = document.createElement("div"); |
| 539 | container.className = "flex h-[700px] w-[900px]"; |
| 540 | const comment = { |
| 541 | author: { login: "reviewer", name: null, avatarUrl: null }, |
| 542 | body: "Rename this.", |
| 543 | createdAt: "2026-09-01T00:00:00Z", |
| 544 | url: null, |
| 545 | }; |
| 546 | const withThread = (isResolved: boolean, oldSideToo = false) => ( |
| 547 | <PullRequestCodeTab |
| 548 | scrollerRef={() => {}} |
| 549 | environmentId={EnvironmentId.make("environment")} |
| 550 | reference={detail} |
| 551 | detail={{ |
| 552 | ...detail, |
| 553 | capabilities: { |
| 554 | ...detail.capabilities, |
| 555 | review: { ...detail.capabilities.review, reply: true }, |
| 556 | }, |
| 557 | viewerPermissions: { ...detail.viewerPermissions, comment: true }, |
| 558 | reviewThreads: [ |
| 559 | { |
| 560 | id: "thread", |
| 561 | path: "a.ts", |
| 562 | line: 3, |
| 563 | side: "right", |
| 564 | isResolved, |
| 565 | isOutdated: false, |
| 566 | comments: [{ id: "comment", ...comment }], |
| 567 | }, |
| 568 | ...(oldSideToo |
| 569 | ? [ |
| 570 | { |
| 571 | id: "old-side", |
| 572 | path: "a.ts", |
| 573 | line: 3, |
| 574 | side: "left" as const, |
| 575 | isResolved: true, |
| 576 | isOutdated: false, |
| 577 | comments: [{ id: "old-side-comment", ...comment }], |
| 578 | }, |
| 579 | ] |
| 580 | : []), |
| 581 | ], |
| 582 | }} |
| 583 | selectedCommitOid={null} |
| 584 | onSelectedCommitChange={() => {}} |
| 585 | onRefresh={() => {}} |
| 586 | /> |
| 587 | ); |
| 588 | await using screen = await render(withThread(true), { container }); |
| 589 | const marker = screen.getByRole("button", { name: "Resolved conversation", exact: true }); |
| 590 | const card = (state: string) => screen.getByRole("button", { name: `${state} · 1 comment` }); |
| 591 | const reply = () => screen.getByRole("textbox", { name: "Reply to this conversation" }); |
| 592 | |
| 593 | await expect.element(marker).toHaveAttribute("aria-expanded", "false"); |
| 594 | await expect.element(card("Resolved")).not.toBeInTheDocument(); |
| 595 | |
| 596 | await marker.click(); |
| 597 | await expect.element(marker).toHaveAttribute("aria-expanded", "true"); |
| 598 | await expect.element(card("Resolved")).toHaveAttribute("aria-expanded", "true"); |
| 599 | await screen.getByRole("button", { name: "Reply", exact: true }).click(); |
| 600 | await reply().fill("Unsent"); |
| 601 | |
| 602 | // Hidden conversations stay mounted, so the unsent reply is still there. |
| 603 | await marker.click(); |
| 604 | await expect.element(card("Resolved")).not.toBeInTheDocument(); |
| 605 | await marker.click(); |
| 606 | await expect.element(reply()).toHaveValue("Unsent"); |
| 607 | await marker.click(); |
| 608 | |
| 609 | // Seen open and then resolved, it stays until the avatars are clicked. |
| 610 | await screen.rerender(withThread(false)); |
| 611 | await expect.element(card("Open")).toBeVisible(); |
| 612 | await screen.rerender(withThread(true)); |
| 613 | await expect.element(card("Resolved")).toBeVisible(); |
| 614 | |
| 615 | // Stacked view draws a context row once, so both sides' conversations share the avatars. |
| 616 | await screen.rerender(withThread(true, true)); |
| 617 | await expect |
| 618 | .element(screen.getByRole("button", { name: "2 resolved conversations" })) |
| 619 | .toBeVisible(); |
| 620 | }); |
| 621 | |
| 622 | // regression: #37996 |
| 623 | it("keeps an unsent reply when the appearance switches between light and dark", async () => { |
| 624 | setup({ paged: false }); |
| 625 | __setClientSettingsForTests({ ...DEFAULT_CLIENT_SETTINGS, diffFilesCollapsed: false }); |
| 626 | setAppearanceMode("dark"); |
| 627 | using _preferences = { |
| 628 | [Symbol.dispose]() { |
| 629 | __setClientSettingsForTests(DEFAULT_CLIENT_SETTINGS); |
| 630 | setAppearanceMode("system"); |
| 631 | }, |
| 632 | }; |
| 633 | const container = document.createElement("div"); |
| 634 | container.className = "flex h-[700px] w-[900px]"; |
| 635 | await using screen = await render( |
| 636 | <PullRequestCodeTab |
| 637 | scrollerRef={() => {}} |
| 638 | environmentId={EnvironmentId.make("environment")} |
| 639 | reference={detail} |
| 640 | detail={{ |
| 641 | ...detail, |
| 642 | capabilities: { |
| 643 | ...detail.capabilities, |
| 644 | review: { ...detail.capabilities.review, reply: true }, |
| 645 | }, |
| 646 | viewerPermissions: { ...detail.viewerPermissions, comment: true }, |
| 647 | reviewThreads: [ |
| 648 | { |
| 649 | id: "thread", |
| 650 | path: "a.ts", |
| 651 | line: 3, |
| 652 | side: "right", |
| 653 | isResolved: false, |
| 654 | isOutdated: false, |
| 655 | comments: [ |
| 656 | { |
| 657 | id: "comment", |
| 658 | author: { login: "reviewer", name: null, avatarUrl: null }, |
| 659 | body: "Rename this.", |
| 660 | createdAt: "2026-09-01T00:00:00Z", |
| 661 | url: null, |
| 662 | }, |
| 663 | ], |
| 664 | }, |
| 665 | ], |
| 666 | }} |
| 667 | selectedCommitOid={null} |
| 668 | onSelectedCommitChange={() => {}} |
| 669 | onRefresh={() => {}} |
| 670 | />, |
| 671 | { container }, |
| 672 | ); |
| 673 | const reply = () => screen.getByRole("textbox", { name: "Reply to this conversation" }); |
| 674 | await screen.getByRole("button", { name: "Reply", exact: true }).click(); |
| 675 | await reply().fill("Unsent"); |
| 676 | |
| 677 | setAppearanceMode("light"); |
| 678 | |
| 679 | await expect.element(reply()).toHaveValue("Unsent"); |
| 680 | }); |