|
| 1 | +/* @vitest-environment jsdom */ |
| 2 | +import { render } from "lit"; |
| 3 | +import { describe, expect, it, vi } from "vitest"; |
| 4 | +import { renderModelPicker } from "./model-picker.ts"; |
| 5 | + |
| 6 | +describe("renderModelPicker", () => { |
| 7 | + it("renders provider details and caller sentinels while preserving an unknown current model", () => { |
| 8 | + const container = document.createElement("div"); |
| 9 | + render( |
| 10 | + renderModelPicker({ |
| 11 | + label: "Model", |
| 12 | + value: "legacy/model", |
| 13 | + options: [ |
| 14 | + { value: "", label: "Automatic" }, |
| 15 | + { |
| 16 | + value: "openai/gpt-5.6-luna", |
| 17 | + label: "GPT-5.6 Luna", |
| 18 | + provider: "openai", |
| 19 | + detail: "Fast · 128k", |
| 20 | + disabled: true, |
| 21 | + }, |
| 22 | + ], |
| 23 | + onChange: vi.fn(), |
| 24 | + }), |
| 25 | + container, |
| 26 | + ); |
| 27 | + |
| 28 | + expect(container.querySelector('wa-option[value=""] [slot="start"]')).toBeNull(); |
| 29 | + const openai = container.querySelector('wa-option[value="openai/gpt-5.6-luna"]'); |
| 30 | + expect(openai?.querySelector('[data-provider-icon="codex"]')).not.toBeNull(); |
| 31 | + expect(openai?.textContent).toContain("Fast · 128k"); |
| 32 | + expect(openai?.hasAttribute("disabled")).toBe(true); |
| 33 | + expect(container.querySelector('wa-option[value="legacy/model"]')?.textContent).toContain( |
| 34 | + "legacy/model", |
| 35 | + ); |
| 36 | + }); |
| 37 | + |
| 38 | + it("reveals free-form entry without leaking its internal option value", () => { |
| 39 | + const container = document.createElement("div"); |
| 40 | + const onChange = vi.fn(); |
| 41 | + render( |
| 42 | + renderModelPicker({ |
| 43 | + label: "Model", |
| 44 | + value: "openai/gpt-5.6-luna", |
| 45 | + options: [ |
| 46 | + { value: "", label: "Default" }, |
| 47 | + { value: "openai/gpt-5.6-luna", label: "GPT-5.6 Luna", provider: "openai" }, |
| 48 | + ], |
| 49 | + custom: { label: "Custom model…", placeholder: "provider/model" }, |
| 50 | + onChange, |
| 51 | + }), |
| 52 | + container, |
| 53 | + ); |
| 54 | + |
| 55 | + const customOption = Array.from(container.querySelectorAll("wa-option")).find( |
| 56 | + (option) => option.textContent?.trim() === "Custom model…", |
| 57 | + ); |
| 58 | + const picker = container.querySelector<HTMLElement & { value: string }>("wa-select"); |
| 59 | + const input = container.querySelector<HTMLInputElement>("input"); |
| 60 | + expect(customOption).not.toBeNull(); |
| 61 | + expect(input?.hidden).toBe(true); |
| 62 | + if (!customOption || !picker || !input) { |
| 63 | + return; |
| 64 | + } |
| 65 | + Object.defineProperty(picker, "value", { |
| 66 | + configurable: true, |
| 67 | + value: customOption.getAttribute("value"), |
| 68 | + }); |
| 69 | + picker.dispatchEvent(new Event("change", { bubbles: true })); |
| 70 | + Reflect.deleteProperty(picker, "value"); |
| 71 | + expect(input.hidden).toBe(false); |
| 72 | + |
| 73 | + input.value = "vendor/model with spaces"; |
| 74 | + input.dispatchEvent(new Event("input", { bubbles: true })); |
| 75 | + expect(onChange).toHaveBeenCalledWith("vendor/model with spaces"); |
| 76 | + expect(onChange).not.toHaveBeenCalledWith(customOption.getAttribute("value")); |
| 77 | + }); |
| 78 | +}); |
0 commit comments