askrobots / dbbasic-browser
Public
A desktop browser with x402 built in: when a page asks for payment, it pays in stablecoins and shows the content.
danq9
site icons (favicons) in tabs, address bar suggestions and the bookmarks panel
7249579
15 hours, 50 minutes ago
History
chat-model-choice.test.ts · 3.1 KB · 47 lines
| 1 | import { describe, expect, it } from "vitest"; |
| 2 | import type { AiConfig } from "../src/ai-config.js"; |
| 3 | import { CHAT_DEFAULT_ANTHROPIC, chatAi, chatModelChoices, parseSavedChoice } from "../src/chat-model-choice.js"; |
| 4 | |
| 5 | const anthropic: AiConfig = { provider: "anthropic", model: "claude-haiku-5-5", keys: { anthropic: "k" }, available: ["anthropic"], sources: {} }; |
| 6 | const both: AiConfig = { ...anthropic, keys: { anthropic: "k", openai: "o" }, available: ["anthropic", "openai"] }; |
| 7 | const local: AiConfig = { provider: "openai", model: "llama-local", keys: {}, available: ["openai"], sources: {}, baseUrl: "http://192.168.1.20:11434/v1" }; |
| 8 | |
| 9 | describe("the assistant's model", () => { |
| 10 | it("offers the Anthropic models cheapest first, with what each costs next to Haiku", () => { |
| 11 | const c = chatModelChoices(anthropic); |
| 12 | expect(c.map((x) => x.id)).toEqual(["anthropic/claude-haiku-5-5", "anthropic/claude-sonnet-5-5", "anthropic/claude-opus-5-5", "anthropic/claude-fable-5-2"]); |
| 13 | expect(c.map((x) => x.label)).toEqual(["Haiku 5.5 · cheapest", "Sonnet 5.5 · 20× cost", "Opus 5.5 · 40× cost", "Fable 5.2 · 80× cost"]); |
| 14 | }); |
| 15 | |
| 16 | it("offers only providers that are set up; a local model shows its machine", () => { |
| 17 | expect(chatModelChoices(null)).toEqual([]); |
| 18 | expect(chatModelChoices(both).some((x) => x.provider === "openai")).toBe(true); |
| 19 | expect(chatModelChoices(local)).toEqual([{ id: "openai/llama-local", provider: "openai", model: "llama-local", label: "llama-local (192.168.1.20:11434)" }]); |
| 20 | }); |
| 21 | |
| 22 | it("a model the env file names that is not in the list still shows", () => { |
| 23 | const named = { ...anthropic, model: "claude-opus-5", modelExplicit: true }; |
| 24 | expect(chatModelChoices(named).map((x) => x.id)).toContain("anthropic/claude-opus-5"); |
| 25 | }); |
| 26 | |
| 27 | it("defaults to Sonnet for the chat, unless the env file named a model", () => { |
| 28 | expect(chatAi(anthropic, null)?.model).toBe(CHAT_DEFAULT_ANTHROPIC); |
| 29 | expect(chatAi({ ...anthropic, model: "claude-opus-5-5", modelExplicit: true }, null)?.model).toBe("claude-opus-5-5"); |
| 30 | expect(chatAi(local, null)?.model).toBe("llama-local"); |
| 31 | expect(chatAi(null, "anthropic/claude-opus-5-5")).toBeNull(); |
| 32 | }); |
| 33 | |
| 34 | it("uses the saved choice while it is on offer, and ignores it when it is not", () => { |
| 35 | expect(chatAi(anthropic, "anthropic/claude-opus-5-5")?.model).toBe("claude-opus-5-5"); |
| 36 | const o = chatAi(both, "openai/gpt-5.4-mini"); |
| 37 | expect([o?.provider, o?.model]).toEqual(["openai", "gpt-5.4-mini"]); |
| 38 | expect(chatAi(anthropic, "openai/gpt-5.4-mini")?.model).toBe(CHAT_DEFAULT_ANTHROPIC); // no OpenAI key any more |
| 39 | expect(chatAi(anthropic, "anthropic/made-up")?.model).toBe(CHAT_DEFAULT_ANTHROPIC); |
| 40 | expect(chatAi(anthropic, "anthropic/claude-opus-5-5")?.keys).toEqual(anthropic.keys); // same keys, only the model changes |
| 41 | }); |
| 42 | |
| 43 | it("reads a saved file strictly", () => { |
| 44 | expect(parseSavedChoice('{"model":"anthropic/claude-opus-5-5"}')).toBe("anthropic/claude-opus-5-5"); |
| 45 | for (const bad of ["", "nope", '{"model":3}', '{"model":"evil/x"}', '{"model":"anthropic/a b"}', '{"model":"anthropic/"}']) expect(parseSavedChoice(bad)).toBeNull(); |
| 46 | }); |
| 47 | }); |