Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ComposerPrimaryActions.test.tsx

270 lines8.1 KB

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 );
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 );
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 );
89​
90function renderSendButton(sendDisabledReason: string | null = null) {
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,
100 isConnecting: false,
101 isEnvironmentUnavailable: false,
102 isPreparingWorktree: false,
103 hasSendableContent: true,
104 onPreviousPendingQuestion: () => {},
105 onInterrupt: () => {},
106 onImplementPlanInNewThread: () => {},
107 }),
108 );
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("disables and labels the send button while feedback is uploading", () => {
208 const markup = renderSendButton("Sending feedback");
209​
210 expect(markup).toContain("disabled");
211 expect(markup).toContain('aria-label="Sending feedback"');
212 });
213​
214 it("offers Stop generation while a running turn is waiting for user input", () => {
215 expect(renderPendingActions(true)).toContain('aria-label="Stop generation"');
216 });
217​
218 it("does not offer Stop generation for a pending request without a running turn", () => {
219 expect(renderPendingActions(false)).not.toContain('aria-label="Stop generation"');
220 });
221​
222 it("matches the small pending action size without changing the standalone size", () => {
223 expect(renderPendingActions(true)).toContain("size-8 sm:size-7");
224 expect(renderStandaloneStop()).toContain("size-8 sm:h-8 sm:w-8");
225 expect(renderStandaloneStop()).not.toContain("sm:size-7");
226 });
227​
228 it("renders stage artwork inside the send button when artwork identification is active", () => {
229 stageArtworkState.mode = "artwork";
230 stageArtworkState.variant = "nightly";
231​
232 const markup = renderSendButton();
233​
234 expect(markup).toContain("stage-nightly");
235 expect(markup).toContain("bg-transparent text-white");
236 expect(markup).not.toContain("bg-message-action text-message-action-foreground");
237 });
238​
239 it("keeps the normal send-button fill when artwork identification is inactive", () => {
240 stageArtworkState.variant = "nightly";
241​
242 const markup = renderSendButton();
243​
244 expect(markup).not.toContain("stage-nightly");
245 expect(markup).toContain("bg-message-action text-message-action-foreground");
246 });
247​
248 it("only renders stop while running when Enter-to-send is available", () => {
249 const markup = renderRunningActions(false, true);
250​
251 expect(markup).toContain('aria-label="Stop generation"');
252 expect(markup).not.toContain('aria-label="Send message"');
253 });
254​
255 it("renders send alongside stop while running when Enter-to-send is unavailable", () => {
256 const markup = renderRunningActions(true, true);
257​
258 expect(markup).toContain('aria-label="Stop generation"');
259 expect(markup).toContain('aria-label="Send message"');
260 expect(markup).toContain('type="submit"');
261 expect(markup).toContain("size-9 sm:size-8");
262 });
263​
264 it("keeps stop as the only action while running with an empty composer", () => {
265 const markup = renderRunningActions(true, false);
266​
267 expect(markup).toContain('aria-label="Stop generation"');
268 expect(markup).not.toContain('aria-label="Send message"');
269 });
270});