File
Blob: tests/worker/lib/ai/index.test.ts
| 1 | import { describe, expect, it } from "vitest"; |
| 2 | import { createAiClient, AiMisconfiguredError } from "@/worker/lib/ai"; |
| 3 | |
| 4 | function baseEnv(overrides?: Partial<Env>): Env { |
| 5 | return { |
| 6 | BLAND_AI_MODE: "", |
| 7 | BLAND_AI_OPENAI_ENDPOINT: "", |
| 8 | BLAND_AI_OPENAI_API_KEY: "", |
| 9 | BLAND_AI_OPENAI_CHAT_MODEL: "", |
| 10 | BLAND_AI_OPENAI_SUMMARIZE_MODEL: "", |
| 11 | AI: {} as Ai, |
| 12 | ...overrides, |
| 13 | } as Env; |
| 14 | } |
| 15 | |
| 16 | describe("createAiClient mode selection", () => { |
| 17 | it("returns mock client when mode is mock", () => { |
| 18 | const client = createAiClient(baseEnv({ BLAND_AI_MODE: "mock" })); |
| 19 | expect(typeof client.chat).toBe("function"); |
| 20 | expect(typeof client.summarize).toBe("function"); |
| 21 | }); |
| 22 | |
| 23 | it("defaults to workers-ai when mode is empty", () => { |
| 24 | const client = createAiClient(baseEnv()); |
| 25 | expect(typeof client.chat).toBe("function"); |
| 26 | }); |
| 27 | |
| 28 | it("returns openai-compat client when endpoint is configured", () => { |
| 29 | const client = createAiClient( |
| 30 | baseEnv({ |
| 31 | BLAND_AI_MODE: "openai-compat", |
| 32 | BLAND_AI_OPENAI_ENDPOINT: "http://127.0.0.1:1234/v1", |
| 33 | BLAND_AI_OPENAI_CHAT_MODEL: "llama3", |
| 34 | }), |
| 35 | ); |
| 36 | expect(typeof client.chat).toBe("function"); |
| 37 | }); |
| 38 | |
| 39 | it("throws a misconfiguration error when openai-compat is selected without an endpoint", () => { |
| 40 | expect(() => createAiClient(baseEnv({ BLAND_AI_MODE: "openai-compat" }))).toThrow(AiMisconfiguredError); |
| 41 | }); |
| 42 | |
| 43 | it("throws on unknown mode", () => { |
| 44 | expect(() => createAiClient(baseEnv({ BLAND_AI_MODE: "hypothetical" }))).toThrow(AiMisconfiguredError); |
| 45 | }); |
| 46 | }); |