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 "~/index.css"; |
| 2 | |
| 3 | import type { ComponentProps } from "react"; |
| 4 | import { expect, it, vi } from "vite-plus/test"; |
| 5 | import { act, fakeTimers, render } from "../../../test/render"; |
| 6 | |
| 7 | vi.mock("~/hooks/useSettings", () => ({ |
| 8 | useEnvironmentIdentificationMode: () => "none", |
| 9 | useClientSettings: () => false, |
| 10 | })); |
| 11 | vi.mock("../SidebarStageBackdrop", () => ({ |
| 12 | StageBackdropButtonArt: () => null, |
| 13 | useSidebarStageBackdropVariant: () => null, |
| 14 | })); |
| 15 | |
| 16 | import { CollapsedComposerStopAction, ComposerPrimaryActions } from "./ComposerPrimaryActions"; |
| 17 | |
| 18 | function setup() { |
| 19 | return { |
| 20 | compact: true, |
| 21 | canOperateThread: true, |
| 22 | pendingAction: null, |
| 23 | isRunning: true, |
| 24 | canInterrupt: true, |
| 25 | canResume: false, |
| 26 | isSendBusy: false, |
| 27 | sendDisabledReason: null, |
| 28 | isEnvironmentUnavailable: false, |
| 29 | isPreparingWorktree: false, |
| 30 | hasSendableContent: false, |
| 31 | onPreviousPendingQuestion: vi.fn(), |
| 32 | onInterrupt: vi.fn(), |
| 33 | onResume: vi.fn(), |
| 34 | onSubmitMessage: vi.fn(), |
| 35 | } satisfies ComponentProps<typeof ComposerPrimaryActions>; |
| 36 | } |
| 37 | |
| 38 | it.each(["resume", "draft", "question"] as const)( |
| 39 | "keeps repeat Stop clicks from reaching the action that replaces it (%s)", |
| 40 | async (mode) => { |
| 41 | const props = setup(); |
| 42 | const submit = vi.fn(); |
| 43 | const pendingAction = |
| 44 | mode === "question" |
| 45 | ? { |
| 46 | questionIndex: 0, |
| 47 | isLastQuestion: true, |
| 48 | canAdvance: true, |
| 49 | isResponding: false, |
| 50 | isComplete: true, |
| 51 | } |
| 52 | : null; |
| 53 | const view = (overrides: Partial<ComponentProps<typeof ComposerPrimaryActions>> = {}) => ( |
| 54 | <form |
| 55 | style={{ display: "flex", justifyContent: "flex-end", width: "400px" }} |
| 56 | onSubmit={(event) => { |
| 57 | event.preventDefault(); |
| 58 | submit(); |
| 59 | }} |
| 60 | > |
| 61 | <ComposerPrimaryActions {...props} pendingAction={pendingAction} {...overrides} /> |
| 62 | </form> |
| 63 | ); |
| 64 | await using screen = await render(view()); |
| 65 | using _timers = fakeTimers(); |
| 66 | const stop = screen |
| 67 | .getByRole("button", { name: "Stop generation" }) |
| 68 | .element() as HTMLButtonElement; |
| 69 | const { x, y, width, height } = stop.getBoundingClientRect(); |
| 70 | const target = () => |
| 71 | document.elementFromPoint(x + width / 2, y + height / 2)?.closest("button"); |
| 72 | await act(() => stop.click()); |
| 73 | await act(() => |
| 74 | ( |
| 75 | screen.getByRole("button", { name: "Stopping", exact: true }).element() as HTMLButtonElement |
| 76 | ).click(), |
| 77 | ); |
| 78 | expect(screen.getByRole("button", { name: "Stopping", exact: true }).element()).toBeDisabled(); |
| 79 | expect(props.onInterrupt).toHaveBeenCalledTimes(1); |
| 80 | await screen.rerender( |
| 81 | view({ |
| 82 | isRunning: false, |
| 83 | canInterrupt: false, |
| 84 | canResume: true, |
| 85 | hasSendableContent: mode === "draft", |
| 86 | }), |
| 87 | ); |
| 88 | const action = { resume: "Resume thread", draft: "Submit message", question: "Submit" }[mode]; |
| 89 | expect(screen.getByRole("button", { name: "Stop generation" }).query()).toBeNull(); |
| 90 | expect(target()).toBe(screen.getByRole("button", { name: action, exact: true }).element()); |
| 91 | await act(() => { |
| 92 | for (let i = 0; i < 20; i++) target()?.click(); |
| 93 | }); |
| 94 | await act(() => vi.advanceTimersByTime(499)); |
| 95 | await act(() => target()?.click()); |
| 96 | expect(props.onResume).not.toHaveBeenCalled(); |
| 97 | expect(props.onSubmitMessage).not.toHaveBeenCalled(); |
| 98 | expect(submit).not.toHaveBeenCalled(); |
| 99 | await act(() => vi.advanceTimersByTime(1)); |
| 100 | await act(() => target()?.click()); |
| 101 | expect( |
| 102 | { resume: props.onResume, draft: props.onSubmitMessage, question: submit }[mode], |
| 103 | ).toHaveBeenCalledTimes(1); |
| 104 | await screen.rerender(view()); |
| 105 | expect(screen.getByRole("button", { name: "Stop generation" }).element()).toBeEnabled(); |
| 106 | }, |
| 107 | ); |
| 108 | |
| 109 | it("allows retry after ten seconds and protects a late stop", async () => { |
| 110 | const props = setup(); |
| 111 | await using screen = await render(<ComposerPrimaryActions {...props} />); |
| 112 | using _timers = fakeTimers(); |
| 113 | await act(() => |
| 114 | ( |
| 115 | screen.getByRole("button", { name: "Stop generation" }).element() as HTMLButtonElement |
| 116 | ).click(), |
| 117 | ); |
| 118 | await act(() => vi.advanceTimersByTime(9_999)); |
| 119 | expect(screen.getByRole("button", { name: "Stopping", exact: true }).element()).toBeDisabled(); |
| 120 | await act(() => vi.advanceTimersByTime(1)); |
| 121 | await act(() => |
| 122 | ( |
| 123 | screen.getByRole("button", { name: "Stop generation" }).element() as HTMLButtonElement |
| 124 | ).click(), |
| 125 | ); |
| 126 | await screen.rerender( |
| 127 | <ComposerPrimaryActions {...props} isRunning={false} canInterrupt={false} canResume />, |
| 128 | ); |
| 129 | await act(() => |
| 130 | (screen.getByRole("button", { name: "Resume thread" }).element() as HTMLButtonElement).click(), |
| 131 | ); |
| 132 | expect(props.onInterrupt).toHaveBeenCalledTimes(2); |
| 133 | expect(props.onResume).not.toHaveBeenCalled(); |
| 134 | }); |
| 135 | |
| 136 | it("keeps Stop pending across a permission failure", async () => { |
| 137 | const props = setup(); |
| 138 | await using screen = await render(<ComposerPrimaryActions {...props} />); |
| 139 | await act(() => |
| 140 | ( |
| 141 | screen.getByRole("button", { name: "Stop generation" }).element() as HTMLButtonElement |
| 142 | ).click(), |
| 143 | ); |
| 144 | await screen.rerender( |
| 145 | <ComposerPrimaryActions {...props} canOperateThread={false} canInterrupt={false} />, |
| 146 | ); |
| 147 | await screen.rerender(<ComposerPrimaryActions {...props} />); |
| 148 | await expect |
| 149 | .element(screen.getByRole("button", { name: "Stopping", exact: true })) |
| 150 | .toBeDisabled(); |
| 151 | expect(props.onInterrupt).toHaveBeenCalledTimes(1); |
| 152 | }); |
| 153 | |
| 154 | it("keeps a pending stop when the run id clears and resets it for a different run", async () => { |
| 155 | const props = setup(); |
| 156 | await using screen = await render(<ComposerPrimaryActions {...props} runId="first" />); |
| 157 | await act(() => |
| 158 | ( |
| 159 | screen.getByRole("button", { name: "Stop generation" }).element() as HTMLButtonElement |
| 160 | ).click(), |
| 161 | ); |
| 162 | await screen.rerender(<ComposerPrimaryActions {...props} runId={null} />); |
| 163 | expect(screen.getByRole("button", { name: "Stopping", exact: true }).element()).toBeDisabled(); |
| 164 | await screen.rerender(<ComposerPrimaryActions {...props} runId="second" />); |
| 165 | expect(screen.getByRole("button", { name: "Stop generation" }).element()).toBeEnabled(); |
| 166 | }); |
| 167 | |
| 168 | it("swaps the collapsed composer's send button for Stop while a turn can be stopped", async () => { |
| 169 | const onInterrupt = vi.fn(); |
| 170 | const collapsed = (overrides: { canInterrupt?: boolean; hasSendableContent?: boolean } = {}) => ( |
| 171 | <CollapsedComposerStopAction |
| 172 | canOperateThread |
| 173 | canInterrupt={overrides.canInterrupt ?? true} |
| 174 | runId="run" |
| 175 | hasSendableContent={overrides.hasSendableContent ?? false} |
| 176 | isEditingQueuedMessage={false} |
| 177 | onInterrupt={onInterrupt} |
| 178 | > |
| 179 | <button type="button">Send message</button> |
| 180 | </CollapsedComposerStopAction> |
| 181 | ); |
| 182 | await using screen = await render(collapsed({ hasSendableContent: true })); |
| 183 | await expect.element(screen.getByRole("button", { name: "Send message" })).toBeVisible(); |
| 184 | await screen.rerender(collapsed()); |
| 185 | await act(() => |
| 186 | ( |
| 187 | screen.getByRole("button", { name: "Stop generation" }).element() as HTMLButtonElement |
| 188 | ).click(), |
| 189 | ); |
| 190 | expect(onInterrupt).toHaveBeenCalledTimes(1); |
| 191 | await expect |
| 192 | .element(screen.getByRole("button", { name: "Stopping", exact: true })) |
| 193 | .toBeDisabled(); |
| 194 | await screen.rerender(collapsed({ canInterrupt: false })); |
| 195 | await expect.element(screen.getByRole("button", { name: "Send message" })).toBeVisible(); |
| 196 | }); |