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

ProviderModelPicker.browser.test.tsx

171 lines6.0 KB

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