|
| 1 | +import { ProviderDriverKind, ProviderInstanceId, type ModelSelection } from "@t3tools/contracts"; |
| 2 | +import { useState } from "react"; |
| 3 | +import { expect, it, vi } from "vite-plus/test"; |
| 4 | +import { page, userEvent } from "vite-plus/test/browser"; |
| 5 | + |
| 6 | +import "~/index.css"; |
| 7 | + |
| 8 | +import { render } from "../../../test/render"; |
| 9 | +import { deriveProviderInstanceEntries } from "../../providerInstances"; |
| 10 | +import { ProviderModelPicker } from "./ProviderModelPicker"; |
| 11 | + |
| 12 | +it("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 | +}); |
0 commit comments