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

ComposerPrimaryActions.test.tsx

252 lines7.4 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 renderRunningActions(
48 showSendWhileRunning: boolean,
49 hasSendableContent: boolean,
50 isStopping = false,
51) {
52 return renderToStaticMarkup(
53 createElement(ComposerPrimaryActions, {
54 compact: true,
55 pendingAction: null,
56 isRunning: true,
57 isStopping,
58 showPlanFollowUpPrompt: false,
59 promptHasText: hasSendableContent,
60 isSendBusy: false,
61 sendDisabledReason: null,
62 isConnecting: false,
63 isEnvironmentUnavailable: false,
64 isPreparingWorktree: false,
65 hasSendableContent,
66 showSendWhileRunning,
67 onPreviousPendingQuestion: () => {},
68 onInterrupt: () => {},
69 onImplementPlanInNewThread: () => {},
70 }),
71 );
73​
74function renderSendButton(sendDisabledReason: string | null = null) {
75 return renderToStaticMarkup(
76 createElement(ComposerPrimaryActions, {
77 compact: true,
78 pendingAction: null,
79 isRunning: false,
80 showPlanFollowUpPrompt: false,
81 promptHasText: true,
82 isSendBusy: false,
83 sendDisabledReason,
84 isConnecting: false,
85 isEnvironmentUnavailable: false,
86 isPreparingWorktree: false,
87 hasSendableContent: true,
88 onPreviousPendingQuestion: () => {},
89 onInterrupt: () => {},
90 onImplementPlanInNewThread: () => {},
91 }),
92 );
94​
95afterEach(() => {
96 stageArtworkState.mode = "none";
97 stageArtworkState.variant = null;
98});
99​
100describe("formatPendingPrimaryActionLabel", () => {
101 it("returns 'Submitting...' while responding", () => {
102 expect(
103 formatPendingPrimaryActionLabel({
104 compact: false,
105 isLastQuestion: false,
106 isResponding: true,
107 questionIndex: 0,
108 }),
109 ).toBe("Submitting...");
110 });
111​
112 it("returns 'Submitting...' while responding regardless of other flags", () => {
113 expect(
114 formatPendingPrimaryActionLabel({
115 compact: true,
116 isLastQuestion: true,
117 isResponding: true,
118 questionIndex: 3,
119 }),
120 ).toBe("Submitting...");
121 });
122​
123 it("returns 'Submit' in compact mode on the last question", () => {
124 expect(
125 formatPendingPrimaryActionLabel({
126 compact: true,
127 isLastQuestion: true,
128 isResponding: false,
129 questionIndex: 0,
130 }),
131 ).toBe("Submit");
132 });
133​
134 it("returns 'Next' in compact mode when not the last question", () => {
135 expect(
136 formatPendingPrimaryActionLabel({
137 compact: true,
138 isLastQuestion: false,
139 isResponding: false,
140 questionIndex: 1,
141 }),
142 ).toBe("Next");
143 });
144​
145 it("returns 'Next question' when not the last question", () => {
146 expect(
147 formatPendingPrimaryActionLabel({
148 compact: false,
149 isLastQuestion: false,
150 isResponding: false,
151 questionIndex: 0,
152 }),
153 ).toBe("Next question");
154 });
155​
156 it("returns singular 'Submit answer' on the last question when it is the only question", () => {
157 expect(
158 formatPendingPrimaryActionLabel({
159 compact: false,
160 isLastQuestion: true,
161 isResponding: false,
162 questionIndex: 0,
163 }),
164 ).toBe("Submit answer");
165 });
166​
167 it("returns plural 'Submit answers' on the last question when there are multiple questions", () => {
168 expect(
169 formatPendingPrimaryActionLabel({
170 compact: false,
171 isLastQuestion: true,
172 isResponding: false,
173 questionIndex: 1,
174 }),
175 ).toBe("Submit answers");
176 });
177​
178 it("returns plural 'Submit answers' for higher question indices", () => {
179 expect(
180 formatPendingPrimaryActionLabel({
181 compact: false,
182 isLastQuestion: true,
183 isResponding: false,
184 questionIndex: 5,
185 }),
186 ).toBe("Submit answers");
187 });
188});
189​
190describe("ComposerPrimaryActions", () => {
191 it("disables and labels the send button while feedback is uploading", () => {
192 const markup = renderSendButton("Sending feedback");
193​
194 expect(markup).toContain("disabled");
195 expect(markup).toContain('aria-label="Sending feedback"');
196 });
197​
198 it("offers Stop generation while a running turn is waiting for user input", () => {
199 expect(renderPendingActions(true)).toContain('aria-label="Stop generation"');
200 });
201​
202 it("does not offer Stop generation for a pending request without a running turn", () => {
203 expect(renderPendingActions(false)).not.toContain('aria-label="Stop generation"');
204 });
205​
206 it("renders stage artwork inside the send button when artwork identification is active", () => {
207 stageArtworkState.mode = "artwork";
208 stageArtworkState.variant = "nightly";
209​
210 const markup = renderSendButton();
211​
212 expect(markup).toContain("stage-nightly");
213 });
214​
215 it("hides stage artwork when artwork identification is inactive", () => {
216 stageArtworkState.variant = "nightly";
217​
218 const markup = renderSendButton();
219​
220 expect(markup).not.toContain("stage-nightly");
221 });
222​
223 it("only renders stop while running when Enter-to-send is available", () => {
224 const markup = renderRunningActions(false, true);
225​
226 expect(markup).toContain('aria-label="Stop generation"');
227 expect(markup).not.toContain('aria-label="Send message"');
228 });
229​
230 it("renders send alongside stop while running when Enter-to-send is unavailable", () => {
231 const markup = renderRunningActions(true, true);
232​
233 expect(markup).toContain('aria-label="Stop generation"');
234 expect(markup).toContain('aria-label="Send message"');
235 expect(markup).toContain('type="submit"');
236 });
237​
238 it("keeps stop as the only action while running with an empty composer", () => {
239 const markup = renderRunningActions(true, false);
240​
241 expect(markup).toContain('aria-label="Stop generation"');
242 expect(markup).not.toContain('aria-label="Send message"');
243 });
244​
245 it("disables stop and announces Stopping generation while the interrupt settles", () => {
246 const markup = renderRunningActions(false, false, true);
247​
248 expect(markup).toContain('aria-label="Stopping generation"');
249 expect(markup).toContain("disabled");
250 expect(markup).not.toContain('aria-label="Stop generation"');
251 });
252});