The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { ThreadDetailsIssueRows } from "./ThreadDetailsIssueRows"; |
| 2 | import type { EnvironmentProject } from "@t3tools/client-runtime/state/shell"; |
| 3 | import { |
| 4 | EnvironmentId, |
| 5 | IssueTrackerKind, |
| 6 | ProjectId, |
| 7 | SourceControlProviderKind, |
| 8 | ThreadId, |
| 9 | type PullRequestCheck, |
| 10 | type ScopedThreadRef, |
| 11 | type ThreadIssueLink, |
| 12 | } from "@t3tools/contracts"; |
| 13 | import { DraftId } from "~/composerDraftStore"; |
| 14 | import { AsyncResult } from "effect/reactivity"; |
| 15 | import { expect, it, vi } from "vite-plus/test"; |
| 16 | |
| 17 | const state = vi.hoisted(() => ({ |
| 18 | issuesSupported: false, |
| 19 | isDraft: false, |
| 20 | status: "success" as PullRequestCheck["status"], |
| 21 | extraStatus: null as PullRequestCheck["status"] | null, |
| 22 | mergeMethods: ["merge"] as Array<"merge" | "squash" | "rebase">, |
| 23 | perform: vi.fn(), |
| 24 | newThread: vi.fn(), |
| 25 | prepareThread: vi.fn(), |
| 26 | checkoutError: null as Error | null, |
| 27 | addToast: vi.fn(), |
| 28 | updateToast: vi.fn(), |
| 29 | projects: [] as unknown[], |
| 30 | pullRequests: [] as unknown[], |
| 31 | projectMergeMethods: {} as Record<string, "merge" | "squash" | "rebase">, |
| 32 | })); |
| 33 | |
| 34 | vi.mock(import("~/state/use-atom-command"), () => ({ |
| 35 | useAtomCommand: (() => state.perform) as never, |
| 36 | })); |
| 37 | |
| 38 | vi.mock(import("~/state/entities"), async (importOriginal) => ({ |
| 39 | ...(await importOriginal()), |
| 40 | // Rows render a pull request only with a project, so this does not reach tests that pass none. |
| 41 | useServerConfigs: (() => |
| 42 | new Map([ |
| 43 | [ |
| 44 | EnvironmentId.make("environment"), |
| 45 | { environment: { capabilities: { pullRequests: true, issues: state.issuesSupported } } }, |
| 46 | ], |
| 47 | ])) as never, |
| 48 | useProjects: (() => state.projects) as never, |
| 49 | useQuietThreadShell: ((ref: unknown) => |
| 50 | ref === null ? null : { branch: null, pullRequests: state.pullRequests }) as never, |
| 51 | })); |
| 52 | vi.mock(import("~/state/issues"), () => ({ useLinkedIssueDetail: () => null })); |
| 53 | |
| 54 | const queryData = () => ({ |
| 55 | projectId: ProjectId.make("project"), |
| 56 | workspaceRoot: "/workspace", |
| 57 | url: "https://github.com/owner/repo/pull/1", |
| 58 | number: 1, |
| 59 | title: "Test PR", |
| 60 | state: "open", |
| 61 | isDraft: state.isDraft, |
| 62 | mergeability: "mergeable", |
| 63 | headBranch: "feature", |
| 64 | baseBranch: "main", |
| 65 | changedFiles: 1, |
| 66 | additions: 1, |
| 67 | deletions: 0, |
| 68 | checks: [state.status, ...(state.extraStatus ? [state.extraStatus] : [])].map((status) => ({ |
| 69 | name: `CI-${status}`, |
| 70 | status, |
| 71 | description: null, |
| 72 | url: null, |
| 73 | })), |
| 74 | capabilities: { actions: ["merge", "ready"], mergeMethods: state.mergeMethods }, |
| 75 | viewerPermissions: { actions: ["merge", "ready"] }, |
| 76 | mergeCapabilities: { |
| 77 | merge: state.mergeMethods.includes("merge"), |
| 78 | squash: state.mergeMethods.includes("squash"), |
| 79 | rebase: state.mergeMethods.includes("rebase"), |
| 80 | }, |
| 81 | }); |
| 82 | vi.mock(import("~/state/query"), async (importOriginal) => ({ |
| 83 | ...(await importOriginal()), |
| 84 | useEnvironmentQuerySelector: ((_atom: unknown, select: (result: unknown) => unknown) => |
| 85 | select(AsyncResult.success(queryData()))) as never, |
| 86 | useEnvironmentQueryIsNewer: () => true, |
| 87 | useEnvironmentQueryRefresh: () => ({ refresh: vi.fn(), isPending: () => false }), |
| 88 | useEnvironmentQuery: (() => ({ |
| 89 | data: { |
| 90 | code: { additions: 1, deletions: 0 }, |
| 91 | tests: { additions: 0, deletions: 0 }, |
| 92 | mocks: { additions: 0, deletions: 0 }, |
| 93 | generated: { additions: 0, deletions: 0 }, |
| 94 | docs: { additions: 7, deletions: 3 }, |
| 95 | }, |
| 96 | isPending: false, |
| 97 | })) as never, |
| 98 | })); |
| 99 | vi.mock(import("../pullRequest/usePullRequestActions"), async (importOriginal) => ({ |
| 100 | ...(await importOriginal()), |
| 101 | usePullRequestDefaultMergeMethodResolver: ((_environmentId: unknown, projectId: unknown) => () => |
| 102 | state.projectMergeMethods[String(projectId)]) as never, |
| 103 | })); |
| 104 | |
| 105 | vi.mock(import("~/hooks/useHandleNewThread"), async (original) => ({ |
| 106 | ...(await original()), |
| 107 | useNewThreadHandler: (() => state.newThread) as never, |
| 108 | })); |
| 109 | vi.mock(import("~/state/sourceControlActions"), async (original) => ({ |
| 110 | ...(await original()), |
| 111 | usePreparePullRequestThreadAction: (() => ({ |
| 112 | run: state.prepareThread, |
| 113 | isAllowed: true, |
| 114 | error: state.checkoutError, |
| 115 | })) as never, |
| 116 | })); |
| 117 | vi.mock(import("../ui/toast"), async (original) => { |
| 118 | const actual = await original(); |
| 119 | return { |
| 120 | ...actual, |
| 121 | toastManager: { |
| 122 | ...actual.toastManager, |
| 123 | add: state.addToast, |
| 124 | update: state.updateToast, |
| 125 | } as never, |
| 126 | }; |
| 127 | }); |
| 128 | |
| 129 | import { act, render as renderUi } from "../../../test/render"; |
| 130 | import { useComposerDraftStore } from "~/composerDraftStore"; |
| 131 | import { useUiStateStore } from "~/uiStateStore"; |
| 132 | import { ThreadDetailsPrRow } from "./ThreadDetailsPrRow"; |
| 133 | import { ThreadDetailsThreadPrRows } from "./ThreadDetailsPrRows"; |
| 134 | |
| 135 | const threadRef: ScopedThreadRef = { |
| 136 | environmentId: EnvironmentId.make("environment"), |
| 137 | threadId: ThreadId.make("thread"), |
| 138 | }; |
| 139 | const scratchProject = { |
| 140 | id: ProjectId.make("scratch"), |
| 141 | environmentId: EnvironmentId.make("environment"), |
| 142 | title: "No project", |
| 143 | workspaceRoot: "/home/user/.t3/scratch", |
| 144 | scripts: [], |
| 145 | }; |
| 146 | const repositoryProject = { |
| 147 | id: ProjectId.make("repository"), |
| 148 | environmentId: EnvironmentId.make("environment"), |
| 149 | title: "repo", |
| 150 | workspaceRoot: "/work/repo", |
| 151 | defaultModelSelection: null, |
| 152 | createdAt: "2026-11-18T00:00:00.000Z", |
| 153 | updatedAt: "2026-11-18T00:00:00.000Z", |
| 154 | scripts: [], |
| 155 | repositoryIdentity: { |
| 156 | canonicalKey: "github.com/owner/repo", |
| 157 | locator: { |
| 158 | source: "git-remote", |
| 159 | remoteName: "origin", |
| 160 | remoteUrl: "https://github.com/owner/repo.git", |
| 161 | }, |
| 162 | provider: SourceControlProviderKind.make("github"), |
| 163 | owner: "owner", |
| 164 | name: "repo", |
| 165 | displayName: "owner/repo", |
| 166 | }, |
| 167 | } satisfies EnvironmentProject; |
| 168 | |
| 169 | const clickButton = async (scope: ParentNode, label: string) => |
| 170 | await act(async () => { |
| 171 | [...scope.querySelectorAll("button")] |
| 172 | .find((button) => button.textContent?.trim() === label)! |
| 173 | .click(); |
| 174 | }); |
| 175 | |
| 176 | it("merges with the last selected merge method", async () => { |
| 177 | await using screen = await setup(); |
| 178 | const { container } = screen; |
| 179 | state.mergeMethods = ["merge", "squash"]; |
| 180 | useUiStateStore.getState().setPullRequestMergeMethod("squash"); |
| 181 | await act(async () => { |
| 182 | await screen.rerender( |
| 183 | <ThreadDetailsPrRow |
| 184 | environmentId={EnvironmentId.make("environment")} |
| 185 | pr={null} |
| 186 | number={1} |
| 187 | status={null} |
| 188 | project={repositoryProject} |
| 189 | label="Test PR" |
| 190 | onOpen={vi.fn()} |
| 191 | />, |
| 192 | ); |
| 193 | }); |
| 194 | const clickMerge = async (scope: ParentNode) => |
| 195 | await act(async () => { |
| 196 | [...scope.querySelectorAll("button")] |
| 197 | .find((button) => button.textContent?.trim() === "Merge")! |
| 198 | .click(); |
| 199 | }); |
| 200 | await clickMerge(container); |
| 201 | const dialog = document.querySelector('[role="alertdialog"]')!; |
| 202 | expect(dialog.textContent).toContain("using squash"); |
| 203 | await clickMerge(dialog); |
| 204 | expect(state.perform).toHaveBeenCalledWith( |
| 205 | expect.objectContaining({ |
| 206 | input: expect.objectContaining({ action: "merge", mergeMethod: "squash" }), |
| 207 | }), |
| 208 | ); |
| 209 | }); |
| 210 | |
| 211 | it("requires a new merge click after passing checks become pending and pass again", async () => { |
| 212 | await using screen = await setup(); |
| 213 | const { container } = screen; |
| 214 | const render = () => ( |
| 215 | <ThreadDetailsPrRow |
| 216 | environmentId={EnvironmentId.make("environment")} |
| 217 | pr={null} |
| 218 | number={1} |
| 219 | status={null} |
| 220 | project={repositoryProject} |
| 221 | label="Test PR" |
| 222 | onOpen={vi.fn()} |
| 223 | /> |
| 224 | ); |
| 225 | const clickMerge = async () => |
| 226 | await act(async () => { |
| 227 | [...container.querySelectorAll("button")] |
| 228 | .find((button) => button.textContent?.trim() === "Merge")! |
| 229 | .click(); |
| 230 | }); |
| 231 | const dialogs = () => [...document.querySelectorAll('[role="alertdialog"]')]; |
| 232 | |
| 233 | await act(async () => await screen.rerender(render())); |
| 234 | await clickMerge(); |
| 235 | expect(dialogs()).toHaveLength(1); |
| 236 | await act(async () => { |
| 237 | state.status = "pending"; |
| 238 | await screen.rerender(render()); |
| 239 | }); |
| 240 | expect(dialogs()).toHaveLength(0); |
| 241 | await act(async () => { |
| 242 | state.status = "success"; |
| 243 | await screen.rerender(render()); |
| 244 | }); |
| 245 | expect(dialogs()).toHaveLength(0); |
| 246 | await clickMerge(); |
| 247 | expect(dialogs()).toHaveLength(1); |
| 248 | expect(state.perform).not.toHaveBeenCalled(); |
| 249 | }); |
| 250 | |
| 251 | async function setup() { |
| 252 | state.perform.mockReset().mockResolvedValue({ _tag: "Success", value: undefined }); |
| 253 | state.checkoutError = null; |
| 254 | state.newThread.mockReset().mockResolvedValue({ |
| 255 | draftId: DraftId.make("new-draft"), |
| 256 | threadId: ThreadId.make("new-thread"), |
| 257 | }); |
| 258 | state.prepareThread.mockReset(); |
| 259 | state.addToast.mockReset().mockReturnValue("checkout-toast"); |
| 260 | state.updateToast.mockReset(); |
| 261 | |
| 262 | state.issuesSupported = false; |
| 263 | state.isDraft = false; |
| 264 | state.status = "success"; |
| 265 | state.extraStatus = null; |
| 266 | state.mergeMethods = ["merge"]; |
| 267 | state.projects = []; |
| 268 | state.pullRequests = []; |
| 269 | state.projectMergeMethods = {}; |
| 270 | useComposerDraftStore.getState().clearComposerContent(threadRef); |
| 271 | useUiStateStore.getState().setPullRequestMergeMethod("merge"); |
| 272 | return renderUi(null); |
| 273 | } |
| 274 | |
| 275 | it("adds the failing checks to a given composer instead of opening a new thread", async () => { |
| 276 | await using screen = await setup(); |
| 277 | state.status = "failure"; |
| 278 | await act(async () => { |
| 279 | await screen.rerender( |
| 280 | <ThreadDetailsPrRow |
| 281 | environmentId={EnvironmentId.make("environment")} |
| 282 | pr={null} |
| 283 | number={1} |
| 284 | status={null} |
| 285 | project={repositoryProject} |
| 286 | label="Test PR" |
| 287 | onOpen={vi.fn()} |
| 288 | composerTarget={threadRef} |
| 289 | />, |
| 290 | ); |
| 291 | }); |
| 292 | await clickButton(screen.container, "Fix"); |
| 293 | expect(state.newThread).not.toHaveBeenCalled(); |
| 294 | expect(state.prepareThread).not.toHaveBeenCalled(); |
| 295 | expect(useComposerDraftStore.getState().getComposerDraft(threadRef)?.prompt).toContain( |
| 296 | "https://github.com/owner/repo/pull/1", |
| 297 | ); |
| 298 | }); |
| 299 | |
| 300 | const repositoryClone = { |
| 301 | ...repositoryProject, |
| 302 | id: ProjectId.make("clone"), |
| 303 | workspaceRoot: "/work/clone", |
| 304 | }; |
| 305 | |
| 306 | it.each([ |
| 307 | { |
| 308 | name: "the project holding its repository, not the No project thread's", |
| 309 | thread: scratchProject, |
| 310 | repository: "owner/repo", |
| 311 | expected: "rebase", |
| 312 | }, |
| 313 | { |
| 314 | name: "the thread's own project when it holds the repository too", |
| 315 | thread: repositoryClone, |
| 316 | repository: "owner/repo", |
| 317 | expected: "merge", |
| 318 | }, |
| 319 | { |
| 320 | name: "the thread's project when no project holds the repository", |
| 321 | thread: scratchProject, |
| 322 | repository: "owner/other", |
| 323 | expected: "squash", |
| 324 | }, |
| 325 | ])("merges a linked pull request with $name", async ({ thread, repository, expected }) => { |
| 326 | await using screen = await setup(); |
| 327 | state.mergeMethods = ["merge", "squash", "rebase"]; |
| 328 | state.projects = [scratchProject, repositoryProject, repositoryClone]; |
| 329 | state.projectMergeMethods = { scratch: "squash", repository: "rebase", clone: "merge" }; |
| 330 | useUiStateStore.getState().setPullRequestMergeMethod("squash"); |
| 331 | state.pullRequests = [ |
| 332 | { |
| 333 | host: "github.com", |
| 334 | repository, |
| 335 | number: 1, |
| 336 | url: `https://github.com/${repository}/pull/1`, |
| 337 | source: "manual", |
| 338 | linkedAt: "2026-10-30T00:00:00.000Z", |
| 339 | snapshot: null, |
| 340 | stack: null, |
| 341 | }, |
| 342 | ]; |
| 343 | await act(async () => { |
| 344 | await screen.rerender( |
| 345 | <ThreadDetailsThreadPrRows threadRef={threadRef} project={thread as never} />, |
| 346 | ); |
| 347 | }); |
| 348 | await clickButton(screen.container, "Merge"); |
| 349 | expect(document.querySelector('[role="alertdialog"]')!.textContent).toContain( |
| 350 | `using ${expected}`, |
| 351 | ); |
| 352 | }); |
| 353 | |
| 354 | it("names a linked pull request row by its kind, number and title, not its URL", async () => { |
| 355 | await using screen = await setup(); |
| 356 | state.projects = [repositoryProject]; |
| 357 | state.pullRequests = [ |
| 358 | { |
| 359 | host: "github.com", |
| 360 | repository: "owner/repo", |
| 361 | number: 1, |
| 362 | url: "https://github.com/owner/repo/pull/1", |
| 363 | source: "manual", |
| 364 | linkedAt: "2026-10-30T00:00:00.000Z", |
| 365 | snapshot: null, |
| 366 | stack: null, |
| 367 | }, |
| 368 | ]; |
| 369 | await act(async () => { |
| 370 | await screen.rerender( |
| 371 | <ThreadDetailsThreadPrRows threadRef={threadRef} project={repositoryProject as never} />, |
| 372 | ); |
| 373 | }); |
| 374 | const row = [...screen.container.querySelectorAll("button")].find((button) => |
| 375 | button.textContent?.includes("Test PR"), |
| 376 | )!; |
| 377 | const name = row.getAttribute("aria-label") ?? row.textContent ?? ""; |
| 378 | expect(name).not.toContain("https://"); |
| 379 | expect(name).toMatch(/pull request/i); |
| 380 | expect(name).toContain("#1"); |
| 381 | expect(name).toContain("Test PR"); |
| 382 | const exposedNumbers = [...screen.container.querySelectorAll("span")].filter( |
| 383 | (span) => |
| 384 | span.textContent?.trim() === "#1" && |
| 385 | span.closest("button") === null && |
| 386 | span.closest('[aria-hidden="true"]') === null, |
| 387 | ); |
| 388 | expect(exposedNumbers).toHaveLength(0); |
| 389 | }); |
| 390 | |
| 391 | it("names the first linked issue and reveals more issues on demand", async () => { |
| 392 | await using screen = await setup(); |
| 393 | const issue: ThreadIssueLink = { |
| 394 | tracker: IssueTrackerKind.make("github"), |
| 395 | host: "github.com", |
| 396 | scope: "owner/repo", |
| 397 | key: "123", |
| 398 | projectId: ProjectId.make("repository"), |
| 399 | url: "https://github.com/owner/repo/issues/123", |
| 400 | source: "manual", |
| 401 | linkedAt: "2026-10-30T00:00:00.000Z", |
| 402 | displayKey: "#123", |
| 403 | title: "Sidebar flickers on resize", |
| 404 | }; |
| 405 | await act(async () => { |
| 406 | await screen.rerender( |
| 407 | <ThreadDetailsIssueRows |
| 408 | links={[ |
| 409 | issue, |
| 410 | { |
| 411 | ...issue, |
| 412 | key: "124", |
| 413 | displayKey: "#124", |
| 414 | url: "https://github.com/owner/repo/issues/124", |
| 415 | title: "Tab list jumps", |
| 416 | }, |
| 417 | ]} |
| 418 | threadRef={threadRef} |
| 419 | />, |
| 420 | ); |
| 421 | }); |
| 422 | const row = [...screen.container.querySelectorAll("a")].find((anchor) => |
| 423 | anchor.textContent?.includes("Sidebar flickers on resize"), |
| 424 | )!; |
| 425 | const name = row.getAttribute("aria-label") ?? row.textContent ?? ""; |
| 426 | expect(name).not.toContain("https://"); |
| 427 | expect(name).toMatch(/issue/i); |
| 428 | expect(name).toContain("#123"); |
| 429 | expect(name).toContain("Sidebar flickers on resize"); |
| 430 | const exposedKeys = [...screen.container.querySelectorAll("span")].filter( |
| 431 | (span) => |
| 432 | span.textContent?.trim() === "#123" && |
| 433 | span.closest("a") === null && |
| 434 | span.closest('[aria-hidden="true"]') === null, |
| 435 | ); |
| 436 | expect(exposedKeys).toHaveLength(0); |
| 437 | const externalClick = new MouseEvent("click", { bubbles: true, cancelable: true }); |
| 438 | await act(async () => { |
| 439 | row.dispatchEvent(externalClick); |
| 440 | }); |
| 441 | expect(externalClick.defaultPrevented).toBe(false); |
| 442 | const extra = screen.getByRole("link", { name: "Issue #124: Tab list jumps" }); |
| 443 | await expect.element(extra).not.toBeInTheDocument(); |
| 444 | await screen.getByRole("button", { name: "Show 1 more" }).click(); |
| 445 | await expect.element(extra).toBeVisible(); |
| 446 | await screen.getByRole("button", { name: "Show less" }).click(); |
| 447 | await expect.element(extra).not.toBeInTheDocument(); |
| 448 | }); |
| 449 | |
| 450 | it("keeps Ready pending when refreshed detail becomes mergeable before the action settles", async () => { |
| 451 | await using screen = await setup(); |
| 452 | const row = () => ( |
| 453 | <ThreadDetailsPrRow |
| 454 | environmentId={EnvironmentId.make("environment")} |
| 455 | pr={null} |
| 456 | number={1} |
| 457 | status={null} |
| 458 | project={repositoryProject} |
| 459 | label="Test PR" |
| 460 | onOpen={vi.fn()} |
| 461 | /> |
| 462 | ); |
| 463 | let finish!: (result: { _tag: "Success"; value: undefined }) => void; |
| 464 | const result = new Promise<{ _tag: "Success"; value: undefined }>((resolve) => { |
| 465 | finish = resolve; |
| 466 | }); |
| 467 | state.perform.mockReturnValueOnce(result); |
| 468 | state.isDraft = true; |
| 469 | await screen.rerender(row()); |
| 470 | await clickButton(screen.container, "Ready"); |
| 471 | expect(state.perform).toHaveBeenCalledWith( |
| 472 | expect.objectContaining({ |
| 473 | input: expect.objectContaining({ |
| 474 | projectId: repositoryProject.id, |
| 475 | repository: "owner/repo", |
| 476 | number: 1, |
| 477 | action: "ready", |
| 478 | }), |
| 479 | }), |
| 480 | ); |
| 481 | await expect.element(screen.getByRole("button", { name: "Marking..." })).toBeDisabled(); |
| 482 | |
| 483 | state.isDraft = false; |
| 484 | await screen.rerender(row()); |
| 485 | await expect.element(screen.getByRole("button", { name: "Marking..." })).toBeDisabled(); |
| 486 | await act(async () => { |
| 487 | finish({ _tag: "Success", value: undefined }); |
| 488 | await result; |
| 489 | }); |
| 490 | await expect.element(screen.getByRole("button", { name: "Merge", exact: true })).toBeEnabled(); |
| 491 | }); |