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

CompactComposerControlsMenu.browser.tsx

328 lines9.6 KB

1import {
2 DEFAULT_MODEL,
3 DEFAULT_MODEL_BY_PROVIDER,
4 EnvironmentId,
5 ModelSelection,
6 ProviderInstanceId,
7 ProviderDriverKind,
8 ThreadId,
9} from "@t3tools/contracts";
10import { scopedThreadKey, scopeThreadRef } from "@t3tools/client-runtime";
11import "../../index.css";
12​
13import { page } from "vitest/browser";
14import { afterEach, describe, expect, it, vi } from "vitest";
15import { render } from "vitest-browser-react";
16import { createModelCapabilities, createModelSelection } from "@t3tools/shared/model";
17​
18import { CompactComposerControlsMenu } from "./CompactComposerControlsMenu";
19import { TraitsMenuContent } from "./TraitsPicker";
20import { useComposerDraftStore } from "../../composerDraftStore";
21​
22const LOCAL_ENVIRONMENT_ID = EnvironmentId.make("environment-local");
23​
24function selectDescriptor(
25 id: string,
26 label: string,
27 options: ReadonlyArray<{ id: string; label: string; isDefault?: boolean }>,
28 promptInjectedValues?: ReadonlyArray<string>,
29) {
30 return {
31 id,
32 label,
33 type: "select" as const,
34 options: [...options],
35 ...(options.find((option) => option.isDefault)?.id
36 ? { currentValue: options.find((option) => option.isDefault)?.id }
37 : {}),
38 ...(promptInjectedValues && promptInjectedValues.length > 0
39 ? { promptInjectedValues: [...promptInjectedValues] }
40 : {}),
41 };
43​
44function booleanDescriptor(id: string, label: string) {
45 return {
46 id,
47 label,
48 type: "boolean" as const,
49 };
51​
52async function mountMenu(props?: { modelSelection?: ModelSelection; prompt?: string }) {
53 const threadId = ThreadId.make("thread-compact-menu");
54 const threadRef = scopeThreadRef(LOCAL_ENVIRONMENT_ID, threadId);
55 const threadKey = scopedThreadKey(threadRef);
56 const provider = ProviderDriverKind.make("claudeAgent");
57 const instanceId = ProviderInstanceId.make(props?.modelSelection?.instanceId ?? provider);
58 const model =
59 props?.modelSelection?.model ?? DEFAULT_MODEL_BY_PROVIDER[provider] ?? DEFAULT_MODEL;
60​
61 useComposerDraftStore.setState({
62 draftsByThreadKey: {
63 [threadKey]: {
64 prompt: props?.prompt ?? "",
65 images: [],
66 nonPersistedImageIds: [],
67 persistedAttachments: [],
68 terminalContexts: [],
69 modelSelectionByProvider: {
70 [instanceId]: createModelSelection(instanceId, model, props?.modelSelection?.options),
71 },
72 activeProvider: instanceId,
73 runtimeMode: null,
74 interactionMode: null,
75 },
76 },
77 draftThreadsByThreadKey: {},
78 logicalProjectDraftThreadKeyByLogicalProjectKey: {},
79 });
80 const host = document.createElement("div");
81 document.body.append(host);
82 const onPromptChange = vi.fn();
83 const providerOptions = props?.modelSelection?.options;
84 const models = [
85 {
86 slug: "claude-opus-4-6",
87 name: "Claude Opus 4.6",
88 isCustom: false,
89 capabilities: createModelCapabilities({
90 optionDescriptors: [
91 selectDescriptor(
92 "effort",
93 "Reasoning",
94 [
95 { id: "low", label: "Low" },
96 { id: "medium", label: "Medium" },
97 { id: "high", label: "High", isDefault: true },
98 { id: "max", label: "Max" },
99 { id: "ultrathink", label: "Ultrathink" },
100 ],
101 ["ultrathink"],
102 ),
103 booleanDescriptor("fastMode", "Fast Mode"),
104 ],
105 }),
106 },
107 {
108 slug: "claude-haiku-4-5",
109 name: "Claude Haiku 4.5",
110 isCustom: false,
111 capabilities: createModelCapabilities({
112 optionDescriptors: [booleanDescriptor("thinking", "Thinking")],
113 }),
114 },
115 {
116 slug: "claude-sonnet-4-6",
117 name: "Claude Sonnet 4.6",
118 isCustom: false,
119 capabilities: createModelCapabilities({
120 optionDescriptors: [
121 selectDescriptor(
122 "effort",
123 "Reasoning",
124 [
125 { id: "low", label: "Low" },
126 { id: "medium", label: "Medium" },
127 { id: "high", label: "High", isDefault: true },
128 { id: "ultrathink", label: "Ultrathink" },
129 ],
130 ["ultrathink"],
131 ),
132 ],
133 }),
134 },
135 ];
136 const screen = await render(
137 <CompactComposerControlsMenu
138 activePlan={false}
139 interactionMode="default"
140 planSidebarLabel="Plan"
141 planSidebarOpen={false}
142 runtimeMode="approval-required"
143 showInteractionModeToggle
144 traitsMenuContent={
145 <TraitsMenuContent
146 provider={provider}
147 models={models}
148 threadRef={threadRef}
149 model={model}
150 prompt={props?.prompt ?? ""}
151 modelOptions={providerOptions}
152 onPromptChange={onPromptChange}
153 />
154 }
155 onToggleInteractionMode={vi.fn()}
156 onTogglePlanSidebar={vi.fn()}
157 onRuntimeModeChange={vi.fn()}
158 />,
159 { container: host },
160 );
161​
162 const cleanup = async () => {
163 await screen.unmount();
164 host.remove();
165 };
166​
167 return {
168 [Symbol.asyncDispose]: cleanup,
169 cleanup,
170 };
172​
173describe("CompactComposerControlsMenu", () => {
174 afterEach(() => {
175 document.body.innerHTML = "";
176 useComposerDraftStore.setState({
177 draftsByThreadKey: {},
178 draftThreadsByThreadKey: {},
179 logicalProjectDraftThreadKeyByLogicalProjectKey: {},
180 stickyModelSelectionByProvider: {},
181 });
182 });
183​
184 it("shows fast mode controls for Opus", async () => {
185 await using _ = await mountMenu({
186 modelSelection: createModelSelection(
187 ProviderInstanceId.make("claudeAgent"),
188 "claude-opus-4-6",
189 ),
190 });
191​
192 await page.getByLabelText("More composer controls").click();
193​
194 await vi.waitFor(() => {
195 const text = document.body.textContent ?? "";
196 expect(text).toContain("Fast Mode");
197 expect(text).toContain("On");
198 expect(text).toContain("Off");
199 });
200 });
201​
202 it("hides fast mode controls for non-Opus Claude models", async () => {
203 await using _ = await mountMenu({
204 modelSelection: createModelSelection(
205 ProviderInstanceId.make("claudeAgent"),
206 "claude-sonnet-4-6",
207 ),
208 });
209​
210 await page.getByLabelText("More composer controls").click();
211​
212 await vi.waitFor(() => {
213 expect(document.body.textContent ?? "").not.toContain("Fast Mode");
214 });
215 });
216​
217 it("shows only the provided effort options", async () => {
218 await using _ = await mountMenu({
219 modelSelection: createModelSelection(
220 ProviderInstanceId.make("claudeAgent"),
221 "claude-sonnet-4-6",
222 ),
223 });
224​
225 await page.getByLabelText("More composer controls").click();
226​
227 await vi.waitFor(() => {
228 const text = document.body.textContent ?? "";
229 expect(text).toContain("Low");
230 expect(text).toContain("Medium");
231 expect(text).toContain("High");
232 expect(text).not.toContain("Max");
233 expect(text).toContain("Ultrathink");
234 });
235 });
236​
237 it("shows a Claude thinking on/off section for Haiku", async () => {
238 await using _ = await mountMenu({
239 modelSelection: createModelSelection(
240 ProviderInstanceId.make("claudeAgent"),
241 "claude-haiku-4-5",
242 [{ id: "thinking", value: true }],
243 ),
244 });
245​
246 await page.getByLabelText("More composer controls").click();
247​
248 await vi.waitFor(() => {
249 const text = document.body.textContent ?? "";
250 expect(text).toContain("Thinking");
251 expect(text).toContain("On");
252 expect(text).toContain("Off");
253 });
254 });
255​
256 it("shows prompt-controlled Ultrathink state with selectable effort controls", async () => {
257 await using _ = await mountMenu({
258 modelSelection: createModelSelection(
259 ProviderInstanceId.make("claudeAgent"),
260 "claude-opus-4-6",
261 [{ id: "effort", value: "high" }],
262 ),
263 prompt: "Ultrathink:\nInvestigate this",
264 });
265​
266 await page.getByLabelText("More composer controls").click();
267​
268 await vi.waitFor(() => {
269 const text = document.body.textContent ?? "";
270 expect(text).toContain("Reasoning");
271 expect(text).not.toContain("ultrathink");
272 });
273 });
274​
275 it("warns when ultrathink appears in prompt body text", async () => {
276 await using _ = await mountMenu({
277 modelSelection: createModelSelection(
278 ProviderInstanceId.make("claudeAgent"),
279 "claude-opus-4-6",
280 [{ id: "effort", value: "high" }],
281 ),
282 prompt: "Ultrathink:\nplease ultrathink about this problem",
283 });
284​
285 await page.getByLabelText("More composer controls").click();
286​
287 await vi.waitFor(() => {
288 const text = document.body.textContent ?? "";
289 expect(text).toContain(
290 'Your prompt contains "ultrathink" in the text. Remove it to change this option.',
291 );
292 });
293 });
294​
295 it("can hide the interaction mode section", async () => {
296 const host = document.createElement("div");
297 document.body.append(host);
298 const screen = await render(
299 <CompactComposerControlsMenu
300 activePlan={false}
301 interactionMode="default"
302 planSidebarLabel="Plan"
303 planSidebarOpen={false}
304 runtimeMode="approval-required"
305 showInteractionModeToggle={false}
306 onToggleInteractionMode={vi.fn()}
307 onTogglePlanSidebar={vi.fn()}
308 onRuntimeModeChange={vi.fn()}
309 />,
310 { container: host },
311 );
312​
313 await page.getByLabelText("More composer controls").click();
314​
315 await vi.waitFor(() => {
316 const text = document.body.textContent ?? "";
317 expect(text).not.toContain("Mode");
318 expect(text).not.toContain("Chat");
319 expect(text).not.toContain("Plan");
320 expect(text).toContain("Access");
321 expect(text).toContain("Supervised");
322 expect(text).toContain("Full access");
323 });
324​
325 await screen.unmount();
326 host.remove();
327 });
328});