The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { EnvironmentId, ProjectId, ThreadId, type PullRequestRef } from "@t3tools/contracts"; |
| 2 | import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients"; |
| 3 | import { expect, it, vi } from "vite-plus/test"; |
| 4 | import { userEvent } from "vite-plus/test/browser"; |
| 5 | |
| 6 | import { act, render } from "../../../test/render"; |
| 7 | import { useComposerDraftStore } from "~/composerDraftStore"; |
| 8 | import { ToastProvider } from "../ui/toast"; |
| 9 | import { usePullRequestActionRunner, usePullRequestHandoffs } from "./usePullRequestActions"; |
| 10 | |
| 11 | const state = vi.hoisted(() => ({ |
| 12 | runAction: vi.fn(), |
| 13 | newThread: vi.fn(), |
| 14 | prepareThread: vi.fn(), |
| 15 | })); |
| 16 | vi.mock(import("~/state/use-atom-command"), () => ({ |
| 17 | useAtomCommand: (() => state.runAction) as never, |
| 18 | })); |
| 19 | vi.mock(import("~/hooks/useHandleNewThread"), () => ({ |
| 20 | useNewThreadHandler: (() => state.newThread) as never, |
| 21 | })); |
| 22 | vi.mock(import("~/state/sourceControlActions"), () => ({ |
| 23 | usePreparePullRequestThreadAction: (() => ({ run: state.prepareThread })) as never, |
| 24 | })); |
| 25 | |
| 26 | function deferred<T>() { |
| 27 | let resolve!: (value: T) => void; |
| 28 | const promise = new Promise<T>((done) => { |
| 29 | resolve = done; |
| 30 | }); |
| 31 | return { promise, resolve }; |
| 32 | } |
| 33 | |
| 34 | function MergeButton({ reference }: { reference: PullRequestRef }) { |
| 35 | const { actionPending, pendingAction, perform } = usePullRequestActionRunner({ |
| 36 | environmentId: EnvironmentId.make("environment"), |
| 37 | reference, |
| 38 | terminology: sourceControlClients.get(undefined).changeRequest, |
| 39 | }); |
| 40 | return ( |
| 41 | <button type="button" disabled={actionPending} onClick={() => void perform("merge")}> |
| 42 | {pendingAction === "merge" ? "Merging..." : "Merge"} |
| 43 | </button> |
| 44 | ); |
| 45 | } |
| 46 | |
| 47 | function setup() { |
| 48 | state.runAction.mockReset(); |
| 49 | state.newThread.mockReset(); |
| 50 | state.prepareThread.mockReset(); |
| 51 | } |
| 52 | |
| 53 | it("keeps pending merges with their pull request when navigating between threads", async () => { |
| 54 | setup(); |
| 55 | const first: PullRequestRef = { |
| 56 | projectId: ProjectId.make("project"), |
| 57 | repository: "owner/repo", |
| 58 | number: 1, |
| 59 | }; |
| 60 | const second = { ...first, number: 2 }; |
| 61 | const firstResult = deferred<{ _tag: "Success"; value: void }>(); |
| 62 | const secondResult = deferred<{ _tag: "Success"; value: void }>(); |
| 63 | state.runAction |
| 64 | .mockReturnValueOnce(firstResult.promise) |
| 65 | .mockReturnValueOnce(secondResult.promise); |
| 66 | await using screen = await render(<MergeButton reference={first} />); |
| 67 | const button = screen.getByRole("button"); |
| 68 | |
| 69 | await userEvent.click(button); |
| 70 | await expect.element(button).toHaveTextContent("Merging..."); |
| 71 | await screen.rerender( |
| 72 | <MergeButton reference={{ ...first, projectId: ProjectId.make("other-project") }} />, |
| 73 | ); |
| 74 | await expect.element(button).toHaveTextContent("Merge"); |
| 75 | await expect.element(button).toBeEnabled(); |
| 76 | await screen.rerender(<MergeButton reference={first} />); |
| 77 | await expect.element(button).toHaveTextContent("Merging..."); |
| 78 | await expect.element(button).toBeDisabled(); |
| 79 | await screen.rerender(<MergeButton reference={second} />); |
| 80 | await expect.element(button).toHaveTextContent("Merge"); |
| 81 | await expect.element(button).toBeEnabled(); |
| 82 | await userEvent.click(button); |
| 83 | expect(state.runAction).toHaveBeenCalledTimes(2); |
| 84 | |
| 85 | await screen.rerender(<MergeButton reference={first} />); |
| 86 | await expect.element(button).toHaveTextContent("Merging..."); |
| 87 | await expect.element(button).toBeDisabled(); |
| 88 | await act(async () => { |
| 89 | firstResult.resolve({ _tag: "Success", value: undefined }); |
| 90 | await firstResult.promise; |
| 91 | }); |
| 92 | await expect.element(button).toHaveTextContent("Merge"); |
| 93 | await screen.rerender(<MergeButton reference={second} />); |
| 94 | await expect.element(button).toHaveTextContent("Merging..."); |
| 95 | await act(async () => { |
| 96 | secondResult.resolve({ _tag: "Success", value: undefined }); |
| 97 | await secondResult.promise; |
| 98 | }); |
| 99 | await expect.element(button).toHaveTextContent("Merge"); |
| 100 | }); |
| 101 | |
| 102 | function FixChecksButton() { |
| 103 | const { startHandoff } = usePullRequestHandoffs({ |
| 104 | environmentId: EnvironmentId.make("environment"), |
| 105 | detail: { |
| 106 | projectId: ProjectId.make("project"), |
| 107 | workspaceRoot: "/repo", |
| 108 | url: "https://github.com/owner/repo/pull/1", |
| 109 | }, |
| 110 | }); |
| 111 | return ( |
| 112 | <button type="button" onClick={() => void startHandoff("fix", { prompt: "Fix the checks" })}> |
| 113 | Fix |
| 114 | </button> |
| 115 | ); |
| 116 | } |
| 117 | |
| 118 | it("stops a pull request handoff when its loading toast is dismissed", async () => { |
| 119 | setup(); |
| 120 | const prepared = deferred<{ |
| 121 | _tag: "Success"; |
| 122 | value: { |
| 123 | branch: string; |
| 124 | worktreePath: string; |
| 125 | isOnPullRequestHead: boolean; |
| 126 | pullRequest: { url: string }; |
| 127 | }; |
| 128 | }>(); |
| 129 | state.newThread |
| 130 | .mockResolvedValueOnce({ draftId: "draft-1", threadId: ThreadId.make("thread-1") }) |
| 131 | .mockResolvedValueOnce({ draftId: "draft-2", threadId: ThreadId.make("thread-1") }); |
| 132 | state.prepareThread.mockReturnValueOnce(prepared.promise); |
| 133 | await using screen = await render( |
| 134 | <ToastProvider> |
| 135 | <FixChecksButton /> |
| 136 | </ToastProvider>, |
| 137 | ); |
| 138 | |
| 139 | await userEvent.click(screen.getByRole("button", { name: "Fix" })); |
| 140 | const toast = screen.getByRole("dialog", { name: "Preparing the pull request checkout..." }); |
| 141 | await expect.element(toast).toBeVisible(); |
| 142 | await userEvent.click(toast.getByRole("button", { name: "Dismiss notification" })); |
| 143 | await expect.element(toast).not.toBeInTheDocument(); |
| 144 | await act(async () => { |
| 145 | prepared.resolve({ |
| 146 | _tag: "Success", |
| 147 | value: { |
| 148 | branch: "pr-1", |
| 149 | worktreePath: "/repo-worktrees/pr-1", |
| 150 | isOnPullRequestHead: true, |
| 151 | pullRequest: { url: "https://github.com/owner/repo/pull/1" }, |
| 152 | }, |
| 153 | }); |
| 154 | await prepared.promise; |
| 155 | }); |
| 156 | |
| 157 | expect(state.newThread).toHaveBeenCalledTimes(1); |
| 158 | expect(useComposerDraftStore.getState().getComposerDraft("draft-2" as never)?.prompt ?? "").toBe( |
| 159 | "", |
| 160 | ); |
| 161 | }); |