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

ProviderModelPicker.browser.test.tsx

196 lines6.1 KB

1import {
2 ANTIGRAVITY_DEFAULT_MODEL,
3 ProviderDriverKind,
4 ProviderInstanceId,
5 type ServerProvider,
6} from "@t3tools/contracts";
7import { act, type ReactNode } from "react";
8import { createRoot } from "react-dom/client";
9import { afterEach, describe, expect, it, vi } from "vite-plus/test";
10​
11import { deriveProviderInstanceEntries } from "../../providerInstances";
12import { ProviderModelPicker } from "./ProviderModelPicker";
13import type { ModelEsque } from "./providerIconUtils";
14​
15function providerEntry(instanceId: string, driver: string) {
16 const provider: ServerProvider = {
17 instanceId: ProviderInstanceId.make(instanceId),
18 driver: ProviderDriverKind.make(driver),
19 enabled: true,
20 installed: true,
21 version: null,
22 status: "ready",
23 auth: { status: "authenticated" },
24 checkedAt: "2026-08-28T00:00:00.000Z",
25 models: [],
26 slashCommands: [],
27 skills: [],
28 };
29 return deriveProviderInstanceEntries([provider])[0]!;
31​
32afterEach(() => {
33 document.body.replaceChildren();
34 vi.unstubAllGlobals();
35});
36​
37// Returns the trigger's visible text, which is what the user reads before opening the picker.
38async function triggerText(node: ReactNode) {
39 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
40 const container = document.createElement("div");
41 document.body.append(container);
42 const root = createRoot(container);
43 await act(async () => root.render(node));
44 const text = container.querySelector("button")?.textContent ?? "";
45 await act(async () => root.unmount());
46 return text;
48​
49function renderPicker(input: {
50 instanceId: string;
51 driver: string;
52 model: string;
53 options: ReadonlyArray<ModelEsque>;
54 includeEntry?: boolean;
55 triggerLabel?: string;
56}) {
57 const instanceId = ProviderInstanceId.make(input.instanceId);
58 const entry = providerEntry(input.instanceId, input.driver);
59 return triggerText(
60 <ProviderModelPicker
61 environmentId={null}
62 activeInstanceId={instanceId}
63 model={input.model}
64 lockedProvider={null}
65 instanceEntries={input.includeEntry === false ? [] : [entry]}
66 modelOptionsByInstance={new Map([[instanceId, input.options]])}
67 onInstanceModelChange={() => {}}
68 {...(input.triggerLabel ? { triggerLabel: input.triggerLabel } : {})}
69 />,
70 );
72​
73describe("ProviderModelPicker", () => {
74 it("shows a neutral aggregate value without a representative model or availability badge", async () => {
75 const label = await renderPicker({
76 instanceId: "codex_personal",
77 driver: "codex",
78 model: "gpt-5",
79 options: [{ slug: "gpt-5", name: "GPT 5", isUnavailable: true }],
80 triggerLabel: "Mixed values",
81 });
82 expect(label).toContain("Mixed values");
83 expect(label).not.toContain("GPT 5");
84 expect(label).not.toContain("Unavailable");
85 });
86​
87 it.each(["", ANTIGRAVITY_DEFAULT_MODEL])(
88 "shows a choice prompt before Antigravity has an account catalog for %s",
89 async (model) => {
90 const label = await renderPicker({
91 instanceId: "antigravity",
92 driver: "antigravity",
93 model,
94 options: [],
95 });
96​
97 expect(label).toContain("Choose model");
98 expect(label).not.toContain(ANTIGRAVITY_DEFAULT_MODEL);
99 },
100 );
101​
102 it.each([{ aliases: [ANTIGRAVITY_DEFAULT_MODEL] }, { isDefault: true }])(
103 "shows the actual default model for an Antigravity marker with %j",
104 async (defaultMetadata) => {
105 const label = await renderPicker({
106 instanceId: "google_work",
107 driver: "antigravity",
108 model: ANTIGRAVITY_DEFAULT_MODEL,
109 options: [
110 { slug: "gemini-fast", name: "Gemini Fast" },
111 { slug: "gemini-pro", name: "Gemini Pro", ...defaultMetadata },
112 ],
113 });
114​
115 expect(label).toContain("Gemini Pro");
116 expect(label).not.toContain("Gemini Fast");
117 expect(label).not.toContain(ANTIGRAVITY_DEFAULT_MODEL);
118 },
119 );
120​
121 it.each(["opencode", "antigravity"])(
122 "keeps the selected model label when the %s account catalog does not contain it",
123 async (driver) => {
124 const label = await renderPicker({
125 instanceId: "team_runtime",
126 driver,
127 model: "missing-model",
128 options: [{ slug: "fallback", name: "Fallback model" }],
129 });
130​
131 expect(label).toContain("missing-model");
132 expect(label).not.toContain("Fallback model");
133 },
134 );
135​
136 it.each(["codex", "claudeAgent", "cursor", "grok"])(
137 "uses the first option label for a missing %s model",
138 async (driver) => {
139 const label = await renderPicker({
140 instanceId: `${driver}_work`,
141 driver,
142 model: "missing-model",
143 options: [{ slug: "fallback-model", name: "Fallback model" }],
144 });
145​
146 expect(label).toContain("Fallback model");
147 expect(label).not.toContain("missing-model");
148 },
149 );
150​
151 it("prefers a matching model for OpenCode", async () => {
152 const label = await renderPicker({
153 instanceId: "custom_runtime",
154 driver: "opencode",
155 model: "openrouter/selected",
156 options: [
157 { slug: "openrouter/fallback", name: "Fallback model" },
158 { slug: "openrouter/selected", name: "Selected model" },
159 ],
160 });
161​
162 expect(label).toContain("Selected model");
163 expect(label).not.toContain("Fallback model");
164 });
165​
166 it("uses the first option when the active instance entry is missing", async () => {
167 const label = await renderPicker({
168 instanceId: "missing_instance",
169 driver: "opencode",
170 model: "missing-model",
171 options: [{ slug: "fallback-model", name: "Fallback model" }],
172 includeEntry: false,
173 });
174​
175 expect(label).toContain("Fallback model");
176 expect(label).not.toContain("missing-model");
177 });
178​
179 it("keeps instance initials visible in the resting trigger", async () => {
180 const activeEntry = providerEntry("codex_personal", "codex");
181 const label = await triggerText(
182 <ProviderModelPicker
183 environmentId={null}
184 activeInstanceId={activeEntry.instanceId}
185 model="gpt-5"
186 lockedProvider={null}
187 instanceEntries={[providerEntry("codex", "codex"), activeEntry]}
188 modelOptionsByInstance={new Map()}
189 size="xs"
190 onInstanceModelChange={() => {}}
191 />,
192 );
193​
194 expect(label).toContain("CP");
195 });
196});