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

ComposerPrimaryActions.test.tsx

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