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

TraitsPicker.browser.tsx

503 lines14.7 KB

1import "../../index.css";
2​
3import {
4 type ModelSelection,
5 ClaudeModelOptions,
6 CodexModelOptions,
7 DEFAULT_MODEL_BY_PROVIDER,
8 DEFAULT_SERVER_SETTINGS,
9 EnvironmentId,
10 type ServerProvider,
11 ThreadId,
12} from "@t3tools/contracts";
13import { scopedThreadKey, scopeThreadRef } from "@t3tools/client-runtime";
14import { page } from "vitest/browser";
15import { useCallback } from "react";
16import { afterEach, describe, expect, it, vi } from "vitest";
17import { render } from "vitest-browser-react";
18​
19import { TraitsPicker } from "./TraitsPicker";
20import {
21 COMPOSER_DRAFT_STORAGE_KEY,
22 ComposerThreadDraftState,
23 useComposerDraftStore,
24 useComposerThreadDraft,
25 useEffectiveComposerModelState,
26} from "../../composerDraftStore";
27import { DEFAULT_CLIENT_SETTINGS } from "@t3tools/contracts/settings";
28​
29// ── Claude TraitsPicker tests ─────────────────────────────────────────
30​
31const LOCAL_ENVIRONMENT_ID = EnvironmentId.make("environment-local");
32const CLAUDE_THREAD_ID = ThreadId.make("thread-claude-traits");
33const CLAUDE_THREAD_REF = scopeThreadRef(LOCAL_ENVIRONMENT_ID, CLAUDE_THREAD_ID);
34const CLAUDE_THREAD_KEY = scopedThreadKey(CLAUDE_THREAD_REF);
35const CODEX_THREAD_ID = ThreadId.make("thread-codex-traits");
36const CODEX_THREAD_REF = scopeThreadRef(LOCAL_ENVIRONMENT_ID, CODEX_THREAD_ID);
37const CODEX_THREAD_KEY = scopedThreadKey(CODEX_THREAD_REF);
38const TEST_PROVIDERS: ReadonlyArray<ServerProvider> = [
39 {
40 provider: "codex",
41 enabled: true,
42 installed: true,
43 version: "0.1.0",
44 status: "ready",
45 auth: { status: "authenticated" },
46 checkedAt: "2026-01-01T00:00:00.000Z",
47 slashCommands: [],
48 skills: [],
49 models: [
50 {
51 slug: "gpt-5.4",
52 name: "GPT-5.4",
53 isCustom: false,
54 capabilities: {
55 reasoningEffortLevels: [
56 { value: "xhigh", label: "Extra High" },
57 { value: "high", label: "High", isDefault: true },
58 ],
59 supportsFastMode: true,
60 supportsThinkingToggle: false,
61 contextWindowOptions: [],
62 promptInjectedEffortLevels: [],
63 },
64 },
65 ],
66 },
67 {
68 provider: "claudeAgent",
69 enabled: true,
70 installed: true,
71 version: "0.1.0",
72 status: "ready",
73 auth: { status: "authenticated" },
74 checkedAt: "2026-01-01T00:00:00.000Z",
75 slashCommands: [],
76 skills: [],
77 models: [
78 {
79 slug: "claude-opus-4-6",
80 name: "Claude Opus 4.6",
81 isCustom: false,
82 capabilities: {
83 reasoningEffortLevels: [
84 { value: "low", label: "Low" },
85 { value: "medium", label: "Medium" },
86 { value: "high", label: "High", isDefault: true },
87 { value: "max", label: "Max" },
88 { value: "ultrathink", label: "Ultrathink" },
89 ],
90 supportsFastMode: true,
91 supportsThinkingToggle: false,
92 contextWindowOptions: [],
93 promptInjectedEffortLevels: ["ultrathink"],
94 },
95 },
96 {
97 slug: "claude-sonnet-4-6",
98 name: "Claude Sonnet 4.6",
99 isCustom: false,
100 capabilities: {
101 reasoningEffortLevels: [
102 { value: "low", label: "Low" },
103 { value: "medium", label: "Medium" },
104 { value: "high", label: "High", isDefault: true },
105 { value: "ultrathink", label: "Ultrathink" },
106 ],
107 supportsFastMode: false,
108 supportsThinkingToggle: false,
109 contextWindowOptions: [],
110 promptInjectedEffortLevels: ["ultrathink"],
111 },
112 },
113 {
114 slug: "claude-haiku-4-5",
115 name: "Claude Haiku 4.5",
116 isCustom: false,
117 capabilities: {
118 reasoningEffortLevels: [],
119 supportsFastMode: false,
120 supportsThinkingToggle: true,
121 contextWindowOptions: [],
122 promptInjectedEffortLevels: [],
123 },
124 },
125 ],
126 },
127];
128​
129function ClaudeTraitsPickerHarness(props: {
130 model: string;
131 fallbackModelSelection: ModelSelection | null;
132 triggerVariant?: "ghost" | "outline";
133}) {
134 const prompt = useComposerThreadDraft(CLAUDE_THREAD_REF).prompt;
135 const setPrompt = useComposerDraftStore((store) => store.setPrompt);
136 const { modelOptions, selectedModel } = useEffectiveComposerModelState({
137 threadRef: CLAUDE_THREAD_REF,
138 providers: TEST_PROVIDERS,
139 selectedProvider: "claudeAgent",
140 threadModelSelection: props.fallbackModelSelection,
141 projectModelSelection: null,
142 settings: {
143 ...DEFAULT_SERVER_SETTINGS,
144 ...DEFAULT_CLIENT_SETTINGS,
145 },
146 });
147 const handlePromptChange = useCallback(
148 (nextPrompt: string) => {
149 setPrompt(CLAUDE_THREAD_REF, nextPrompt);
150 },
151 [setPrompt],
152 );
153​
154 return (
155 <TraitsPicker
156 provider="claudeAgent"
157 models={TEST_PROVIDERS[1]!.models}
158 threadRef={CLAUDE_THREAD_REF}
159 model={selectedModel ?? props.model}
160 prompt={prompt}
161 modelOptions={modelOptions?.claudeAgent}
162 onPromptChange={handlePromptChange}
163 triggerVariant={props.triggerVariant}
164 />
165 );
167​
168async function mountClaudePicker(props?: {
169 model?: string;
170 prompt?: string;
171 options?: ClaudeModelOptions;
172 fallbackModelOptions?: {
173 effort?: "low" | "medium" | "high" | "max" | "ultrathink";
174 thinking?: boolean;
175 fastMode?: boolean;
176 } | null;
177 skipDraftModelOptions?: boolean;
178 triggerVariant?: "ghost" | "outline";
179}) {
180 const model = props?.model ?? "claude-opus-4-6";
181 const claudeOptions = !props?.skipDraftModelOptions ? props?.options : undefined;
182 const draftsByThreadKey: Record<string, ComposerThreadDraftState> = {
183 [CLAUDE_THREAD_KEY]: {
184 prompt: props?.prompt ?? "",
185 images: [],
186 nonPersistedImageIds: [],
187 persistedAttachments: [],
188 terminalContexts: [],
189 modelSelectionByProvider: props?.skipDraftModelOptions
190 ? {}
191 : {
192 claudeAgent: {
193 provider: "claudeAgent",
194 model,
195 ...(claudeOptions && Object.keys(claudeOptions).length > 0
196 ? { options: claudeOptions }
197 : {}),
198 },
199 },
200 activeProvider: "claudeAgent",
201 runtimeMode: null,
202 interactionMode: null,
203 },
204 };
205 useComposerDraftStore.setState({
206 draftsByThreadKey,
207 draftThreadsByThreadKey: {},
208 logicalProjectDraftThreadKeyByLogicalProjectKey: {},
209 });
210 const host = document.createElement("div");
211 document.body.append(host);
212 const fallbackModelSelection =
213 props?.fallbackModelOptions !== undefined
214 ? ({
215 provider: "claudeAgent",
216 model,
217 ...(props.fallbackModelOptions ? { options: props.fallbackModelOptions } : {}),
218 } satisfies ModelSelection)
219 : null;
220 const screen = await render(
221 <ClaudeTraitsPickerHarness
222 model={model}
223 fallbackModelSelection={fallbackModelSelection}
224 {...(props?.triggerVariant ? { triggerVariant: props.triggerVariant } : {})}
225 />,
226 { container: host },
227 );
228​
229 const cleanup = async () => {
230 await screen.unmount();
231 host.remove();
232 };
233​
234 return {
235 [Symbol.asyncDispose]: cleanup,
236 cleanup,
237 };
239​
240describe("TraitsPicker (Claude)", () => {
241 afterEach(() => {
242 document.body.innerHTML = "";
243 useComposerDraftStore.setState({
244 draftsByThreadKey: {},
245 draftThreadsByThreadKey: {},
246 logicalProjectDraftThreadKeyByLogicalProjectKey: {},
247 stickyModelSelectionByProvider: {},
248 });
249 });
250​
251 it("shows fast mode controls for Opus", async () => {
252 await using _ = await mountClaudePicker();
253​
254 await page.getByRole("button").click();
255​
256 await vi.waitFor(() => {
257 const text = document.body.textContent ?? "";
258 expect(text).toContain("Fast Mode");
259 expect(text).toContain("off");
260 expect(text).toContain("on");
261 });
262 });
263​
264 it("hides fast mode controls for non-Opus models", async () => {
265 await using _ = await mountClaudePicker({ model: "claude-sonnet-4-6" });
266​
267 await page.getByRole("button").click();
268​
269 await vi.waitFor(() => {
270 expect(document.body.textContent ?? "").not.toContain("Fast Mode");
271 });
272 });
273​
274 it("shows only the provided effort options", async () => {
275 await using _ = await mountClaudePicker({
276 model: "claude-sonnet-4-6",
277 });
278​
279 await page.getByRole("button").click();
280​
281 await vi.waitFor(() => {
282 const text = document.body.textContent ?? "";
283 expect(text).toContain("Low");
284 expect(text).toContain("Medium");
285 expect(text).toContain("High");
286 expect(text).not.toContain("Max");
287 expect(text).toContain("Ultrathink");
288 });
289 });
290​
291 it("shows a th inking on/off dropdown for Haiku", async () => {
292 await using _ = await mountClaudePicker({
293 model: "claude-haiku-4-5",
294 options: { thinking: true },
295 });
296​
297 await vi.waitFor(() => {
298 expect(document.body.textContent ?? "").toContain("Thinking On");
299 });
300 await page.getByRole("button").click();
301​
302 await vi.waitFor(() => {
303 const text = document.body.textContent ?? "";
304 expect(text).toContain("Thinking");
305 expect(text).toContain("On (default)");
306 expect(text).toContain("Off");
307 });
308 });
309​
310 it("shows prompt-controlled Ultrathink state with selectable effort controls", async () => {
311 await using _ = await mountClaudePicker({
312 model: "claude-opus-4-6",
313 options: { effort: "high" },
314 prompt: "Ultrathink:\nInvestigate this",
315 });
316​
317 await vi.waitFor(() => {
318 expect(document.body.textContent ?? "").toContain("Ultrathink");
319 expect(document.body.textContent ?? "").not.toContain("Ultrathink · Prompt");
320 });
321 await page.getByRole("button").click();
322​
323 await vi.waitFor(() => {
324 const text = document.body.textContent ?? "";
325 expect(text).toContain("Effort");
326 expect(text).not.toContain("ultrathink");
327 });
328 });
329​
330 it("warns when ultrathink appears in prompt body text", async () => {
331 await using _ = await mountClaudePicker({
332 model: "claude-opus-4-6",
333 options: { effort: "high" },
334 prompt: "Ultrathink:\nplease ultrathink about this problem",
335 });
336​
337 await page.getByRole("button").click();
338​
339 await vi.waitFor(() => {
340 const text = document.body.textContent ?? "";
341 expect(text).toContain(
342 'Your prompt contains "ultrathink" in the text. Remove it to change effort.',
343 );
344 });
345 });
346​
347 it("persists sticky claude model options when traits change", async () => {
348 await using _ = await mountClaudePicker({
349 model: "claude-opus-4-6",
350 options: { effort: "medium", fastMode: false },
351 });
352​
353 await page.getByRole("button").click();
354 await page.getByRole("menuitemradio", { name: "Max" }).click();
355​
356 expect(
357 useComposerDraftStore.getState().stickyModelSelectionByProvider.claudeAgent,
358 ).toMatchObject({
359 provider: "claudeAgent",
360 options: {
361 effort: "max",
362 },
363 });
364 });
365​
366 it("accepts outline trigger styling", async () => {
367 await using _ = await mountClaudePicker({
368 triggerVariant: "outline",
369 });
370​
371 const button = document.querySelector("button");
372 if (!(button instanceof HTMLButtonElement)) {
373 throw new Error("Expected traits trigger button to be rendered.");
374 }
375 expect(button.className).toContain("border-input");
376 expect(button.className).toContain("bg-popover");
377 });
378});
379​
380// ── Codex TraitsPicker tests ──────────────────────────────────────────
381​
382async function mountCodexPicker(props: { model?: string; options?: CodexModelOptions }) {
383 const model = props.model ?? DEFAULT_MODEL_BY_PROVIDER.codex;
384 const draftsByThreadKey: Record<string, ComposerThreadDraftState> = {
385 [CODEX_THREAD_KEY]: {
386 prompt: "",
387 images: [],
388 nonPersistedImageIds: [],
389 persistedAttachments: [],
390 terminalContexts: [],
391 modelSelectionByProvider: {
392 codex: {
393 provider: "codex",
394 model,
395 ...(props.options ? { options: props.options } : {}),
396 },
397 },
398 activeProvider: "codex",
399 runtimeMode: null,
400 interactionMode: null,
401 },
402 };
403​
404 useComposerDraftStore.setState({
405 draftsByThreadKey,
406 draftThreadsByThreadKey: {},
407 logicalProjectDraftThreadKeyByLogicalProjectKey: {
408 "environment-local:project-codex-traits": CODEX_THREAD_KEY,
409 },
410 });
411 const host = document.createElement("div");
412 document.body.append(host);
413 const screen = await render(
414 <TraitsPicker
415 provider="codex"
416 models={TEST_PROVIDERS[0]!.models}
417 threadRef={CODEX_THREAD_REF}
418 model={props.model ?? DEFAULT_MODEL_BY_PROVIDER.codex}
419 prompt=""
420 modelOptions={props.options}
421 onPromptChange={() => {}}
422 />,
423 { container: host },
424 );
425​
426 const cleanup = async () => {
427 await screen.unmount();
428 host.remove();
429 };
430​
431 return {
432 [Symbol.asyncDispose]: cleanup,
433 cleanup,
434 };
436​
437describe("TraitsPicker (Codex)", () => {
438 afterEach(() => {
439 document.body.innerHTML = "";
440 localStorage.removeItem(COMPOSER_DRAFT_STORAGE_KEY);
441 useComposerDraftStore.setState({
442 draftsByThreadKey: {},
443 draftThreadsByThreadKey: {},
444 logicalProjectDraftThreadKeyByLogicalProjectKey: {},
445 stickyModelSelectionByProvider: {},
446 });
447 });
448​
449 it("shows fast mode controls", async () => {
450 await using _ = await mountCodexPicker({
451 options: { fastMode: false },
452 });
453​
454 await page.getByRole("button").click();
455​
456 await vi.waitFor(() => {
457 const text = document.body.textContent ?? "";
458 expect(text).toContain("Fast Mode");
459 expect(text).toContain("off");
460 expect(text).toContain("on");
461 });
462 });
463​
464 it("shows Fast in the trigger label when fast mode is active", async () => {
465 await using _ = await mountCodexPicker({
466 options: { fastMode: true },
467 });
468​
469 await vi.waitFor(() => {
470 expect(document.body.textContent ?? "").toContain("High · Fast");
471 });
472 });
473​
474 it("shows only the provided effort options", async () => {
475 await using _ = await mountCodexPicker({
476 options: { fastMode: false },
477 });
478​
479 await page.getByRole("button").click();
480​
481 await vi.waitFor(() => {
482 const text = document.body.textContent ?? "";
483 expect(text).toContain("Extra High");
484 expect(text).toContain("High");
485 expect(text).not.toContain("Low");
486 expect(text).not.toContain("Medium");
487 });
488 });
489​
490 it("persists sticky codex model options when traits change", async () => {
491 await using _ = await mountCodexPicker({
492 options: { fastMode: false },
493 });
494​
495 await page.getByRole("button").click();
496 await page.getByRole("menuitemradio", { name: "on" }).click();
497​
498 expect(useComposerDraftStore.getState().stickyModelSelectionByProvider.codex).toMatchObject({
499 provider: "codex",
500 options: { fastMode: true },
501 });
502 });
503});