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

ComposerPrimaryActions.test.ts

218 lines6.3 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 renderSendButton() {
69 return renderToStaticMarkup(
70 createElement(ComposerPrimaryActions, {
71 compact: true,
72 pendingAction: null,
73 isRunning: false,
74 showPlanFollowUpPrompt: false,
75 promptHasText: true,
76 isSendBusy: false,
77 sendDisabledReason: null,
78 isConnecting: false,
79 isEnvironmentUnavailable: false,
80 isPreparingWorktree: false,
81 hasSendableContent: true,
82 onPreviousPendingQuestion: () => {},
83 onInterrupt: () => {},
84 onImplementPlanInNewThread: () => {},
85 }),
86 );
88​
89afterEach(() => {
90 stageArtworkState.mode = "none";
91 stageArtworkState.variant = null;
92});
93​
94describe("formatPendingPrimaryActionLabel", () => {
95 it("returns 'Submitting...' while responding", () => {
96 expect(
97 formatPendingPrimaryActionLabel({
98 compact: false,
99 isLastQuestion: false,
100 isResponding: true,
101 questionIndex: 0,
102 }),
103 ).toBe("Submitting...");
104 });
105​
106 it("returns 'Submitting...' while responding regardless of other flags", () => {
107 expect(
108 formatPendingPrimaryActionLabel({
109 compact: true,
110 isLastQuestion: true,
111 isResponding: true,
112 questionIndex: 3,
113 }),
114 ).toBe("Submitting...");
115 });
116​
117 it("returns 'Submit' in compact mode on the last question", () => {
118 expect(
119 formatPendingPrimaryActionLabel({
120 compact: true,
121 isLastQuestion: true,
122 isResponding: false,
123 questionIndex: 0,
124 }),
125 ).toBe("Submit");
126 });
127​
128 it("returns 'Next' in compact mode when not the last question", () => {
129 expect(
130 formatPendingPrimaryActionLabel({
131 compact: true,
132 isLastQuestion: false,
133 isResponding: false,
134 questionIndex: 1,
135 }),
136 ).toBe("Next");
137 });
138​
139 it("returns 'Next question' when not the last question", () => {
140 expect(
141 formatPendingPrimaryActionLabel({
142 compact: false,
143 isLastQuestion: false,
144 isResponding: false,
145 questionIndex: 0,
146 }),
147 ).toBe("Next question");
148 });
149​
150 it("returns singular 'Submit answer' on the last question when it is the only question", () => {
151 expect(
152 formatPendingPrimaryActionLabel({
153 compact: false,
154 isLastQuestion: true,
155 isResponding: false,
156 questionIndex: 0,
157 }),
158 ).toBe("Submit answer");
159 });
160​
161 it("returns plural 'Submit answers' on the last question when there are multiple questions", () => {
162 expect(
163 formatPendingPrimaryActionLabel({
164 compact: false,
165 isLastQuestion: true,
166 isResponding: false,
167 questionIndex: 1,
168 }),
169 ).toBe("Submit answers");
170 });
171​
172 it("returns plural 'Submit answers' for higher question indices", () => {
173 expect(
174 formatPendingPrimaryActionLabel({
175 compact: false,
176 isLastQuestion: true,
177 isResponding: false,
178 questionIndex: 5,
179 }),
180 ).toBe("Submit answers");
181 });
182});
183​
184describe("ComposerPrimaryActions", () => {
185 it("offers Stop generation while a running turn is waiting for user input", () => {
186 expect(renderPendingActions(true)).toContain('aria-label="Stop generation"');
187 });
188​
189 it("does not offer Stop generation for a pending request without a running turn", () => {
190 expect(renderPendingActions(false)).not.toContain('aria-label="Stop generation"');
191 });
192​
193 it("matches the small pending action size without changing the standalone size", () => {
194 expect(renderPendingActions(true)).toContain("size-8 sm:size-7");
195 expect(renderStandaloneStop()).toContain("size-8 sm:h-8 sm:w-8");
196 expect(renderStandaloneStop()).not.toContain("sm:size-7");
197 });
198​
199 it("renders stage artwork inside the send button when artwork identification is active", () => {
200 stageArtworkState.mode = "artwork";
201 stageArtworkState.variant = "nightly";
202​
203 const markup = renderSendButton();
204​
205 expect(markup).toContain("stage-nightly");
206 expect(markup).toContain("bg-transparent text-white");
207 expect(markup).not.toContain("bg-message-action text-message-action-foreground");
208 });
209​
210 it("keeps the normal send-button fill when artwork identification is inactive", () => {
211 stageArtworkState.variant = "nightly";
212​
213 const markup = renderSendButton();
214​
215 expect(markup).not.toContain("stage-nightly");
216 expect(markup).toContain("bg-message-action text-message-action-foreground");
217 });
218});