Open History to see the last commit
1import "~/index.css";
2
3import type { ComponentProps } from "react";
4import { expect, it, vi } from "vite-plus/test";
5import { act, fakeTimers, render } from "../../../test/render";
6
7vi.mock("~/hooks/useSettings", () => ({
8 useEnvironmentIdentificationMode: () => "none",
9 useClientSettings: () => false,
10}));
11vi.mock("../SidebarStageBackdrop", () => ({
12 StageBackdropButtonArt: () => null,
13 useSidebarStageBackdropVariant: () => null,
14}));
15
16import { ComposerPrimaryActions } from "./ComposerPrimaryActions";
17
18function 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
38it.each([false, true])(
39 "keeps repeat clicks away from Resume and Send (draft: %s)",
40 async (draft) => {
41 const props = setup();
42 const container = document.createElement("div");
43 Object.assign(container.style, { display: "flex", justifyContent: "flex-end", width: "400px" });
44 await using screen = await render(<ComposerPrimaryActions {...props} />, { container });
45 const stop = screen
46 .getByRole("button", { name: "Stop generation" })
47 .element() as HTMLButtonElement;
48 const { x, y, width, height } = stop.getBoundingClientRect();
49 const target = () =>
50 document.elementFromPoint(x + width / 2, y + height / 2)?.closest("button");
51 await act(() => stop.click());
52 await act(() =>
53 (
54 screen.getByRole("button", { name: "Stopping", exact: true }).element() as HTMLButtonElement
55 ).click(),
56 );
57 await expect
58 .element(screen.getByRole("button", { name: "Stopping", exact: true }))
59 .toBeDisabled();
60 expect(props.onInterrupt).toHaveBeenCalledTimes(1);
61 await screen.rerender(
62 <ComposerPrimaryActions
63 {...props}
64 isRunning={false}
65 canInterrupt={false}
66 canResume
67 hasSendableContent={draft}
68 />,
69 );
70 await expect
71 .element(screen.getByRole("button", { name: "Stopped", exact: true }))
72 .toBeDisabled();
73 expect(target()).toHaveAttribute("aria-label", "Stopped");
74 await act(() => {
75 for (let i = 0; i < 20; i++) target()?.click();
76 });
77 expect(props.onResume).not.toHaveBeenCalled();
78 expect(props.onSubmitMessage).not.toHaveBeenCalled();
79 const action = screen
80 .getByRole("button", { name: draft ? "Submit message" : "Resume thread" })
81 .element() as HTMLButtonElement;
82 await act(() => action.click());
83 expect(draft ? props.onSubmitMessage : props.onResume).toHaveBeenCalledTimes(1);
84 await screen.rerender(<ComposerPrimaryActions {...props} />);
85 await expect.element(screen.getByRole("button", { name: "Stop generation" })).toBeEnabled();
86 },
87);
88
89it("allows retry after ten seconds and protects a late stop", async () => {
90 const props = setup();
91 await using screen = await render(<ComposerPrimaryActions {...props} />);
92 using _timers = fakeTimers();
93 await act(() =>
94 (
95 screen.getByRole("button", { name: "Stop generation" }).element() as HTMLButtonElement
96 ).click(),
97 );
98 await act(() => vi.advanceTimersByTime(9_999));
99 expect(screen.getByRole("button", { name: "Stopping", exact: true }).element()).toBeDisabled();
100 await act(() => vi.advanceTimersByTime(1));
101 await act(() =>
102 (
103 screen.getByRole("button", { name: "Stop generation" }).element() as HTMLButtonElement
104 ).click(),
105 );
106 await screen.rerender(
107 <ComposerPrimaryActions {...props} isRunning={false} canInterrupt={false} canResume />,
108 );
109 expect(screen.getByRole("button", { name: "Stopped", exact: true }).element()).toBeDisabled();
110 expect(props.onInterrupt).toHaveBeenCalledTimes(2);
111 expect(props.onResume).not.toHaveBeenCalled();
112});
113
114it("keeps Stop pending across a permission failure", async () => {
115 const props = setup();
116 await using screen = await render(<ComposerPrimaryActions {...props} />);
117 await act(() =>
118 (
119 screen.getByRole("button", { name: "Stop generation" }).element() as HTMLButtonElement
120 ).click(),
121 );
122 await screen.rerender(
123 <ComposerPrimaryActions {...props} canOperateThread={false} canInterrupt={false} />,
124 );
125 await screen.rerender(<ComposerPrimaryActions {...props} />);
126 await expect
127 .element(screen.getByRole("button", { name: "Stopping", exact: true }))
128 .toBeDisabled();
129 expect(props.onInterrupt).toHaveBeenCalledTimes(1);
130});
131
132it("keeps a pending stop when the run id clears and resets it for a different run", async () => {
133 const props = setup();
134 await using screen = await render(<ComposerPrimaryActions {...props} runId="first" />);
135 await act(() =>
136 (
137 screen.getByRole("button", { name: "Stop generation" }).element() as HTMLButtonElement
138 ).click(),
139 );
140 await screen.rerender(<ComposerPrimaryActions {...props} runId={null} />);
141 expect(screen.getByRole("button", { name: "Stopping", exact: true }).element()).toBeDisabled();
142 await screen.rerender(<ComposerPrimaryActions {...props} runId="second" />);
143 expect(screen.getByRole("button", { name: "Stop generation" }).element()).toBeEnabled();
144});