Open History to see the last commit
1import {
2 DEFAULT_CLIENT_SETTINGS,
3 ProviderDriverKind,
4 ProviderInstanceId,
5 type ModelSelection,
6} from "@t3tools/contracts";
7import { useState } from "react";
8import { expect, it, vi } from "vite-plus/test";
9import { page, userEvent } from "vite-plus/test/browser";
10
11import "~/index.css";
12
13import { render } from "../../../test/render";
14import { __setClientSettingsForTests } from "../../hooks/useSettings";
15import { deriveProviderInstanceEntries } from "../../providerInstances";
16import { ProviderModelPicker } from "./ProviderModelPicker";
17
18it("keeps each registry image when adding a model from another ACP agent", async () => {
19 const icons = [
20 "https://cdn.agentclientprotocol.com/registry/icons/picker-explicit.svg",
21 "https://cdn.agentclientprotocol.com/registry/v1/latest/picker-by-id.svg",
22 ];
23 const cache = await caches.open("t3-acp-registry-icons-v1");
24 await using _cachedIcons = {
25 async [Symbol.asyncDispose]() {
26 await Promise.all(icons.map((url) => cache.delete(url)));
27 },
28 };
29 for (const [index, url] of icons.entries()) {
30 await cache.put(
31 url,
32 new Response(
33 `<svg xmlns="http://www.w3.org/2000/svg" width="${11 + index * 6}" height="10"><path d="M0 0h10v10H0z"/></svg>`,
34 { headers: { "content-type": "image/svg+xml" } },
35 ),
36 );
37 }
38 const entries = deriveProviderInstanceEntries(
39 ["First", "Second"].map((name) => ({
40 instanceId: ProviderInstanceId.make(name.toLowerCase()),
41 driver: ProviderDriverKind.make("acpRegistry"),
42 displayName: `${name} agent`,
43 enabled: true,
44 installed: true,
45 version: null,
46 status: "ready" as const,
47 auth: { status: "authenticated" as const },
48 checkedAt: "2026-01-01T00:00:00.000Z",
49 models: [
50 { slug: name.toLowerCase(), name: `${name} model`, isCustom: false, capabilities: {} },
51 ],
52 slashCommands: [],
53 skills: [],
54 })),
55 ).map((entry, index) => ({
56 ...entry,
57 ...(index === 0 ? { acpRegistryIconUrl: icons[0] } : { acpRegistryAgentId: "picker-by-id" }),
58 }));
59 const first = { instanceId: entries[0]!.instanceId, model: "first" };
60 const options = new Map(entries.map((entry) => [entry.instanceId, entry.models]));
61
62 function Picker() {
63 const [selection, setSelection] = useState<ReadonlyArray<ModelSelection>>([first]);
64 return (
65 <ProviderModelPicker
66 environmentId={null}
67 activeInstanceId={first.instanceId}
68 model={first.model}
69 {...(selection.length > 1 ? { selectedModels: selection } : {})}
70 lockedProvider={null}
71 instanceEntries={entries}
72 modelOptionsByInstance={options}
73 onToggleModel={(instanceId, model) => setSelection([...selection, { instanceId, model }])}
74 onInstanceModelChange={(instanceId, model) => setSelection([{ instanceId, model }])}
75 />
76 );
77 }
78
79 await using screen = await render(<Picker />);
80 const imageWidths = () =>
81 Array.from(
82 screen.container.querySelectorAll<HTMLImageElement>("[data-chat-provider-model-picker] img"),
83 (image) => image.naturalWidth,
84 );
85 await vi.waitFor(() => expect(imageWidths()).toEqual([11]));
86 await screen.getByRole("button", { name: "First model", exact: true }).click();
87 await page.getByRole("button", { name: "Second agent", exact: true }).click();
88 await userEvent.keyboard("{Shift>}");
89 try {
90 await page.getByRole("option", { name: /Second model/ }).click();
91 } finally {
92 await userEvent.keyboard("{/Shift}");
93 }
94 await userEvent.keyboard("{Escape}");
95 await vi.waitFor(() => expect(imageWidths()).toEqual([11, 17]));
96});
97
98it("keeps a docked picker open after a pick and hands focus to that model's effort", async () => {
99 const instanceId = ProviderInstanceId.make("codex");
100 const entries = deriveProviderInstanceEntries([
101 {
102 instanceId,
103 driver: ProviderDriverKind.make("codex"),
104 enabled: true,
105 installed: true,
106 version: null,
107 status: "ready",
108 auth: { status: "authenticated" },
109 checkedAt: "2026-01-01T00:00:00.000Z",
110 models: [],
111 slashCommands: [],
112 skills: [],
113 },
114 ]);
115 const options = new Map([
116 [
117 instanceId,
118 [
119 { slug: "original", name: "Original model" },
120 { slug: "second", name: "Second model" },
121 { slug: "plain", name: "Plain model" },
122 ],
123 ],
124 ]);
125 // The last level is the selected one; "plain" has no effort to pick.
126 const levels: Record<string, ReadonlyArray<string>> = {
127 original: ["Low", "High"],
128 second: ["Medium"],
129 plain: [],
130 };
131
132 function Picker() {
133 const [model, setModel] = useState("original");
134 return (
135 <ProviderModelPicker
136 environmentId={null}
137 activeInstanceId={instanceId}
138 model={model}
139 lockedProvider={null}
140 instanceEntries={entries}
141 modelOptionsByInstance={options}
142 triggerAriaLabel="Choose model"
143 onInstanceModelChange={(_, selected) => setModel(selected)}
144 effortColumn={(levels[model] ?? []).map((level, index, all) => (
145 <button
146 key={level}
147 type="button"
148 role="radio"
149 aria-checked={index === all.length - 1}
150 data-effort-row
151 data-effort-level
152 >
153 {level}
154 </button>
155 ))}
156 />
157 );
158 }
159
160 await using screen = await render(<Picker />);
161 await screen.getByRole("button", { name: "Choose model" }).click();
162 const search = page.getByPlaceholder("Search models...");
163 await expect.element(search).toHaveFocus();
164 await userEvent.keyboard("{ArrowRight}");
165 await expect.element(page.getByRole("radio", { name: "High" })).toHaveFocus();
166 await userEvent.keyboard("{ArrowLeft}");
167 await expect.element(search).toHaveFocus();
168
169 await page.getByRole("option", { name: /Second model/ }).click();
170 await expect.element(page.getByRole("radio", { name: "Medium" })).toHaveFocus();
171
172 await page.getByRole("option", { name: /Plain model/ }).click();
173 await page.getByRole("option", { name: /Second model/ }).click();
174 await expect.element(page.getByRole("radio", { name: "Medium" })).toHaveFocus();
175 await userEvent.keyboard("{Escape}");
176 await expect.element(search).not.toBeInTheDocument();
177});
178
179it("opens on the active provider when every favorite belongs to a disabled provider", async () => {
180 const entries = deriveProviderInstanceEntries(
181 ["Codex", "Claude"].map((name) => ({
182 instanceId: ProviderInstanceId.make(name.toLowerCase()),
183 driver: ProviderDriverKind.make(name.toLowerCase()),
184 enabled: name === "Codex",
185 installed: true,
186 version: null,
187 status: "ready" as const,
188 auth: { status: "authenticated" as const },
189 checkedAt: "2026-01-01T00:00:00.000Z",
190 models: [
191 { slug: name.toLowerCase(), name: `${name} model`, isCustom: false, capabilities: {} },
192 ],
193 slashCommands: [],
194 skills: [],
195 })),
196 );
197 const [codex, claude] = entries;
198 __setClientSettingsForTests({
199 ...DEFAULT_CLIENT_SETTINGS,
200 favorites: [{ provider: claude!.instanceId, model: "claude" }],
201 });
202 using _settings = {
203 [Symbol.dispose]: () => __setClientSettingsForTests(DEFAULT_CLIENT_SETTINGS),
204 };
205
206 await using screen = await render(
207 <ProviderModelPicker
208 environmentId={null}
209 activeInstanceId={codex!.instanceId}
210 model="codex"
211 lockedProvider={null}
212 instanceEntries={entries}
213 modelOptionsByInstance={new Map(entries.map((entry) => [entry.instanceId, entry.models]))}
214 triggerAriaLabel="Choose model"
215 onInstanceModelChange={() => {}}
216 />,
217 );
218 await screen.getByRole("button", { name: "Choose model" }).click();
219 await expect.element(page.getByRole("option", { name: /Codex model/ })).toBeVisible();
220});