Open History to see the last commit
1import { createElement } from "react";
2import { renderToStaticMarkup } from "react-dom/server";
3import { afterEach, describe, expect, it, vi } from "vite-plus/test";
4
5const stageArtworkState = vi.hoisted(() => ({
6 mode: "none" as "artwork" | "none",
7 variant: null as "nightly" | "dev" | null,
8}));
9
10vi.mock("~/hooks/useSettings", () => ({
11 useEnvironmentIdentificationMode: () => stageArtworkState.mode,
12}));
13vi.mock("../SidebarStageBackdrop", () => ({
14 StageBackdropButtonArt: ({ variant }: { variant: string }) => `stage-${variant}`,
15 useSidebarStageBackdropVariant: (enabled = true) => (enabled ? stageArtworkState.variant : null),
16}));
17
18import { ComposerPrimaryActions, formatPendingPrimaryActionLabel } from "./ComposerPrimaryActions";
19
20function renderPendingActions(isRunning: boolean) {
21 return renderToStaticMarkup(
22 createElement(ComposerPrimaryActions, {
23 compact: true,
24 pendingAction: {
25 questionIndex: 0,
26 isLastQuestion: true,
27 canAdvance: true,
28 isResponding: false,
29 isComplete: true,
30 },
31 isRunning,
32 showPlanFollowUpPrompt: false,
33 promptHasText: false,
34 isSendBusy: false,
35 sendDisabledReason: null,
36 isConnecting: false,
37 isEnvironmentUnavailable: false,
38 isPreparingWorktree: false,
39 hasSendableContent: false,
40 onPreviousPendingQuestion: () => {},
41 onInterrupt: () => {},
42 onImplementPlanInNewThread: () => {},
43 }),
44 );
45}
46
47function renderStandaloneStop() {
48 return renderToStaticMarkup(
49 createElement(ComposerPrimaryActions, {
50 compact: true,
51 pendingAction: null,
52 isRunning: true,
53 showPlanFollowUpPrompt: false,
54 promptHasText: false,
55 isSendBusy: false,
56 sendDisabledReason: null,
57 isConnecting: false,
58 isEnvironmentUnavailable: false,
59 isPreparingWorktree: false,
60 hasSendableContent: false,
61 onPreviousPendingQuestion: () => {},
62 onInterrupt: () => {},
63 onImplementPlanInNewThread: () => {},
64 }),
65 );
66}
67
68function renderRunningActions(showSendWhileRunning: boolean, hasSendableContent: boolean) {
69 return renderToStaticMarkup(
70 createElement(ComposerPrimaryActions, {
71 compact: true,
72 pendingAction: null,
73 isRunning: true,
74 showPlanFollowUpPrompt: false,
75 promptHasText: hasSendableContent,
76 isSendBusy: false,
77 sendDisabledReason: null,
78 isConnecting: false,
79 isEnvironmentUnavailable: false,
80 isPreparingWorktree: false,
81 hasSendableContent,
82 showSendWhileRunning,
83 onPreviousPendingQuestion: () => {},
84 onInterrupt: () => {},
85 onImplementPlanInNewThread: () => {},
86 }),
87 );
88}
89
90function renderSendButton() {
91 return renderToStaticMarkup(
92 createElement(ComposerPrimaryActions, {
93 compact: true,
94 pendingAction: null,
95 isRunning: false,
96 showPlanFollowUpPrompt: false,
97 promptHasText: true,
98 isSendBusy: false,
99 sendDisabledReason: null,
100 isConnecting: false,
101 isEnvironmentUnavailable: false,
102 isPreparingWorktree: false,
103 hasSendableContent: true,
104 onPreviousPendingQuestion: () => {},
105 onInterrupt: () => {},
106 onImplementPlanInNewThread: () => {},
107 }),
108 );
109}
110
111afterEach(() => {
112 stageArtworkState.mode = "none";
113 stageArtworkState.variant = null;
114});
115
116describe("formatPendingPrimaryActionLabel", () => {
117 it("returns 'Submitting...' while responding", () => {
118 expect(
119 formatPendingPrimaryActionLabel({
120 compact: false,
121 isLastQuestion: false,
122 isResponding: true,
123 questionIndex: 0,
124 }),
125 ).toBe("Submitting...");
126 });
127
128 it("returns 'Submitting...' while responding regardless of other flags", () => {
129 expect(
130 formatPendingPrimaryActionLabel({
131 compact: true,
132 isLastQuestion: true,
133 isResponding: true,
134 questionIndex: 3,
135 }),
136 ).toBe("Submitting...");
137 });
138
139 it("returns 'Submit' in compact mode on the last question", () => {
140 expect(
141 formatPendingPrimaryActionLabel({
142 compact: true,
143 isLastQuestion: true,
144 isResponding: false,
145 questionIndex: 0,
146 }),
147 ).toBe("Submit");
148 });
149
150 it("returns 'Next' in compact mode when not the last question", () => {
151 expect(
152 formatPendingPrimaryActionLabel({
153 compact: true,
154 isLastQuestion: false,
155 isResponding: false,
156 questionIndex: 1,
157 }),
158 ).toBe("Next");
159 });
160
161 it("returns 'Next question' when not the last question", () => {
162 expect(
163 formatPendingPrimaryActionLabel({
164 compact: false,
165 isLastQuestion: false,
166 isResponding: false,
167 questionIndex: 0,
168 }),
169 ).toBe("Next question");
170 });
171
172 it("returns singular 'Submit answer' on the last question when it is the only question", () => {
173 expect(
174 formatPendingPrimaryActionLabel({
175 compact: false,
176 isLastQuestion: true,
177 isResponding: false,
178 questionIndex: 0,
179 }),
180 ).toBe("Submit answer");
181 });
182
183 it("returns plural 'Submit answers' on the last question when there are multiple questions", () => {
184 expect(
185 formatPendingPrimaryActionLabel({
186 compact: false,
187 isLastQuestion: true,
188 isResponding: false,
189 questionIndex: 1,
190 }),
191 ).toBe("Submit answers");
192 });
193
194 it("returns plural 'Submit answers' for higher question indices", () => {
195 expect(
196 formatPendingPrimaryActionLabel({
197 compact: false,
198 isLastQuestion: true,
199 isResponding: false,
200 questionIndex: 5,
201 }),
202 ).toBe("Submit answers");
203 });
204});
205
206describe("ComposerPrimaryActions", () => {
207 it("offers Stop generation while a running turn is waiting for user input", () => {
208 expect(renderPendingActions(true)).toContain('aria-label="Stop generation"');
209 });
210
211 it("does not offer Stop generation for a pending request without a running turn", () => {
212 expect(renderPendingActions(false)).not.toContain('aria-label="Stop generation"');
213 });
214
215 it("matches the small pending action size without changing the standalone size", () => {
216 expect(renderPendingActions(true)).toContain("size-8 sm:size-7");
217 expect(renderStandaloneStop()).toContain("size-8 sm:h-8 sm:w-8");
218 expect(renderStandaloneStop()).not.toContain("sm:size-7");
219 });
220
221 it("renders stage artwork inside the send button when artwork identification is active", () => {
222 stageArtworkState.mode = "artwork";
223 stageArtworkState.variant = "nightly";
224
225 const markup = renderSendButton();
226
227 expect(markup).toContain("stage-nightly");
228 expect(markup).toContain("bg-transparent text-white");
229 expect(markup).not.toContain("bg-message-action text-message-action-foreground");
230 });
231
232 it("keeps the normal send-button fill when artwork identification is inactive", () => {
233 stageArtworkState.variant = "nightly";
234
235 const markup = renderSendButton();
236
237 expect(markup).not.toContain("stage-nightly");
238 expect(markup).toContain("bg-message-action text-message-action-foreground");
239 });
240
241 it("only renders stop while running when Enter-to-send is available", () => {
242 const markup = renderRunningActions(false, true);
243
244 expect(markup).toContain('aria-label="Stop generation"');
245 expect(markup).not.toContain('aria-label="Send message"');
246 });
247
248 it("renders send alongside stop while running when Enter-to-send is unavailable", () => {
249 const markup = renderRunningActions(true, true);
250
251 expect(markup).toContain('aria-label="Stop generation"');
252 expect(markup).toContain('aria-label="Send message"');
253 expect(markup).toContain('type="submit"');
254 expect(markup).toContain("size-9 sm:size-8");
255 });
256
257 it("keeps stop as the only action while running with an empty composer", () => {
258 const markup = renderRunningActions(true, false);
259
260 expect(markup).toContain('aria-label="Stop generation"');
261 expect(markup).not.toContain('aria-label="Send message"');
262 });
263});