2026-02-26 13:55:21 +01:00
|
|
|
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"
|
2026-01-13 22:14:45 +09:00
|
|
|
import {
|
2026-02-26 13:55:21 +01:00
|
|
|
getAIModel,
|
2026-06-15 12:54:18 +08:00
|
|
|
isAihubmixStandardBaseURL,
|
2026-01-13 22:14:45 +09:00
|
|
|
resolveBaseURL,
|
|
|
|
|
supportsPromptCaching,
|
|
|
|
|
} from "@/lib/ai-providers"
|
2026-06-15 12:54:18 +08:00
|
|
|
import { extractAihubmixModelIds } from "@/lib/aihubmix-models"
|
|
|
|
|
|
|
|
|
|
describe("extractAihubmixModelIds", () => {
|
|
|
|
|
it("extracts unique chat model IDs from the AIHubMix model list payload", () => {
|
|
|
|
|
const models = extractAihubmixModelIds({
|
|
|
|
|
data: [
|
|
|
|
|
{ model_id: "claude-sonnet-4-5-20250929", types: "llm" },
|
|
|
|
|
{ model_id: "gpt-5.1", types: "llm" },
|
|
|
|
|
{ model_id: "gpt-5.1", types: "llm" },
|
|
|
|
|
{ model_id: "gpt-image-2", types: "image_generation,llm" },
|
|
|
|
|
{ model_id: "cohere-rerank-v4.0", types: "rerank" },
|
|
|
|
|
{ model_id: "", types: "llm" },
|
|
|
|
|
{ types: "llm" },
|
|
|
|
|
],
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
expect(models).toEqual(["claude-sonnet-4-5-20250929", "gpt-5.1"])
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it("returns an empty list for malformed payloads", () => {
|
|
|
|
|
expect(extractAihubmixModelIds({ data: null })).toEqual([])
|
|
|
|
|
expect(extractAihubmixModelIds({})).toEqual([])
|
|
|
|
|
expect(extractAihubmixModelIds(null)).toEqual([])
|
|
|
|
|
})
|
|
|
|
|
})
|
2026-01-13 22:14:45 +09:00
|
|
|
|
|
|
|
|
describe("resolveBaseURL", () => {
|
|
|
|
|
const SERVER_BASE_URL = "https://server-proxy.example.com"
|
|
|
|
|
const USER_BASE_URL = "https://user-proxy.example.com"
|
|
|
|
|
const DEFAULT_BASE_URL = "https://api.provider.com/v1"
|
|
|
|
|
const USER_API_KEY = "user-api-key-123"
|
|
|
|
|
|
|
|
|
|
describe("when user provides their own API key", () => {
|
|
|
|
|
it("uses user's baseUrl when provided", () => {
|
|
|
|
|
const result = resolveBaseURL(
|
|
|
|
|
USER_API_KEY,
|
|
|
|
|
USER_BASE_URL,
|
|
|
|
|
SERVER_BASE_URL,
|
|
|
|
|
DEFAULT_BASE_URL,
|
|
|
|
|
)
|
|
|
|
|
expect(result).toBe(USER_BASE_URL)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it("uses default baseUrl when user provides no baseUrl", () => {
|
|
|
|
|
const result = resolveBaseURL(
|
|
|
|
|
USER_API_KEY,
|
|
|
|
|
null,
|
|
|
|
|
SERVER_BASE_URL,
|
|
|
|
|
DEFAULT_BASE_URL,
|
|
|
|
|
)
|
|
|
|
|
expect(result).toBe(DEFAULT_BASE_URL)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it("returns undefined when user provides no baseUrl and no default exists", () => {
|
|
|
|
|
const result = resolveBaseURL(
|
|
|
|
|
USER_API_KEY,
|
|
|
|
|
null,
|
|
|
|
|
SERVER_BASE_URL,
|
|
|
|
|
undefined,
|
|
|
|
|
)
|
|
|
|
|
expect(result).toBeUndefined()
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it("does NOT use server's baseUrl even when available", () => {
|
|
|
|
|
const result = resolveBaseURL(
|
|
|
|
|
USER_API_KEY,
|
|
|
|
|
undefined,
|
|
|
|
|
SERVER_BASE_URL,
|
|
|
|
|
undefined,
|
|
|
|
|
)
|
|
|
|
|
// Should NOT return SERVER_BASE_URL
|
|
|
|
|
expect(result).not.toBe(SERVER_BASE_URL)
|
|
|
|
|
expect(result).toBeUndefined()
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it("prefers user's baseUrl over default", () => {
|
|
|
|
|
const result = resolveBaseURL(
|
|
|
|
|
USER_API_KEY,
|
|
|
|
|
USER_BASE_URL,
|
|
|
|
|
SERVER_BASE_URL,
|
|
|
|
|
DEFAULT_BASE_URL,
|
|
|
|
|
)
|
|
|
|
|
expect(result).toBe(USER_BASE_URL)
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
describe("when using server credentials (no user API key)", () => {
|
|
|
|
|
it("uses user's baseUrl when provided (overrides server)", () => {
|
|
|
|
|
const result = resolveBaseURL(
|
|
|
|
|
null,
|
|
|
|
|
USER_BASE_URL,
|
|
|
|
|
SERVER_BASE_URL,
|
|
|
|
|
DEFAULT_BASE_URL,
|
|
|
|
|
)
|
|
|
|
|
expect(result).toBe(USER_BASE_URL)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it("falls back to server's baseUrl when no user baseUrl", () => {
|
|
|
|
|
const result = resolveBaseURL(
|
|
|
|
|
null,
|
|
|
|
|
null,
|
|
|
|
|
SERVER_BASE_URL,
|
|
|
|
|
DEFAULT_BASE_URL,
|
|
|
|
|
)
|
|
|
|
|
expect(result).toBe(SERVER_BASE_URL)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it("falls back to default when no user or server baseUrl", () => {
|
|
|
|
|
const result = resolveBaseURL(
|
|
|
|
|
null,
|
|
|
|
|
null,
|
|
|
|
|
undefined,
|
|
|
|
|
DEFAULT_BASE_URL,
|
|
|
|
|
)
|
|
|
|
|
expect(result).toBe(DEFAULT_BASE_URL)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it("returns undefined when no baseUrl available anywhere", () => {
|
|
|
|
|
const result = resolveBaseURL(null, null, undefined, undefined)
|
|
|
|
|
expect(result).toBeUndefined()
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it("handles undefined apiKey same as null", () => {
|
|
|
|
|
const result = resolveBaseURL(
|
|
|
|
|
undefined,
|
|
|
|
|
null,
|
|
|
|
|
SERVER_BASE_URL,
|
|
|
|
|
DEFAULT_BASE_URL,
|
|
|
|
|
)
|
|
|
|
|
expect(result).toBe(SERVER_BASE_URL)
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
describe("edge cases", () => {
|
|
|
|
|
it("handles empty string apiKey as falsy (uses server config)", () => {
|
|
|
|
|
const result = resolveBaseURL(
|
|
|
|
|
"",
|
|
|
|
|
null,
|
|
|
|
|
SERVER_BASE_URL,
|
|
|
|
|
DEFAULT_BASE_URL,
|
|
|
|
|
)
|
|
|
|
|
// Empty string is falsy, so should use server config
|
|
|
|
|
expect(result).toBe(SERVER_BASE_URL)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it("handles empty string baseUrl as falsy", () => {
|
|
|
|
|
const result = resolveBaseURL(
|
|
|
|
|
USER_API_KEY,
|
|
|
|
|
"",
|
|
|
|
|
SERVER_BASE_URL,
|
|
|
|
|
DEFAULT_BASE_URL,
|
|
|
|
|
)
|
|
|
|
|
// Empty string baseUrl is falsy, should fall back to default
|
|
|
|
|
expect(result).toBe(DEFAULT_BASE_URL)
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
})
|
test: add Vitest and Playwright testing infrastructure (#512)
* test: add Vitest and Playwright testing infrastructure
- Add Vitest for unit tests (39 tests)
- cached-responses.test.ts
- ai-providers.test.ts
- chat-helpers.test.ts
- utils.test.ts
- Add Playwright for E2E tests (3 smoke tests)
- Homepage load
- Japanese locale
- Settings dialog
- Add CI workflow (.github/workflows/test.yml)
- Add vitest.config.mts and playwright.config.ts
- Update .gitignore for test artifacts
* test: add more E2E tests for UI components
- Chat panel tests (interactive elements, iframe)
- Settings tests (dark mode, language, draw.io theme)
- Save dialog tests (buttons exist)
- History dialog tests
- Model config tests
- Keyboard interaction tests
- Upload area tests
Total: 15 E2E tests, all passing
* test: fix E2E test issues from review
Fixes based on Gemini and Codex review:
- Remove brittle nth(1) selector in keyboard tests
- Remove waitForTimeout(500) race condition
- Remove if(isVisible) silent skip patterns
- Add proper assertions instead of no-op checks
- Remove expect(count >= 0) that always passes
- Remove unused hasProviderUI variable
All 14 E2E tests and 39 unit tests pass.
* style: auto-format with Biome
* fix: resolve lint errors for CI
* test(e2e): add diagram generation tests with mocked AI responses
- Add tests for generate, edit, and append diagram operations
- Use SSE mocked responses matching AI SDK UI message stream format
- Generate mxCell XML directly in tests for deterministic assertions
- Tests verify tool card rendering and 'Complete' badge state
* test: add comprehensive E2E tests for all major features
- Error handling tests (API errors, rate limits, network timeout, truncated XML)
- Multi-turn conversation tests (sequential requests, history preservation)
- File upload tests (upload button, file preview, sending with message)
- Theme switching tests (dark mode toggle, persistence, system preference)
- Language switching tests (EN/JA/ZH, persistence, locale URLs)
- Iframe interaction tests (draw.io loading, toolbar, diagram rendering)
- Copy/paste tests (chat input, XML input, special characters)
- History restore tests (new chat, persistence, browser navigation)
* refactor: extract shared test helpers and improve error assertions
- Create tests/e2e/lib/helpers.ts with shared SSE mock functions
- Add proper error UI assertions to error-handling.spec.ts
- Remove waitForTimeout calls in favor of real assertions
- Update 6 test files to use shared helpers
* docs: add testing section to CONTRIBUTING.md
* fix: improve test infrastructure based on PR review
- Fix double build in CI: remove redundant build from playwright webServer
- Export chat helpers from shared module for proper unit testing
- Replace waitForTimeout with explicit waits in E2E tests
- Add data-testid attributes to settings and new chat buttons
- Add list reporter for CI to show failures in logs
- Add Playwright browser caching to speed up CI
- Add vitest coverage configuration
- Fix conditional test assertions to use test.skip() instead of silent pass
- Remove unused variables flagged by linter
* fix: improve E2E test assertions and remove silent skips
- Replace silent test.skip() with explicit conditional skips
- Add actual persistence assertion after page reload
- Use data-testid selector for new chat button test
* refactor: add shared fixtures and test.step() patterns
- Add tests/e2e/lib/fixtures.ts with shared test helpers
- Add tests/e2e/fixtures/diagrams.ts with XML test data
- Add expectBeforeAndAfterReload() helper for persistence tests
- Add test.step() for better test reporting in complex tests
- Consolidate mock helpers into fixtures module
- Reduce code duplication across 17 test files
* fix: make persistence tests more reliable
- Remove expectBeforeAndAfterReload from mocked API tests
- Add explicit test.step() for before/after reload checks
- Add retry config for flaky clipboard tests
- Add sleep after reload for language persistence test
* test: remove flaky XML paste test
* docs: run both unit and e2e tests before PR
* chore: add type check and unit test git hooks
---------
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2026-01-05 01:37:32 +09:00
|
|
|
|
|
|
|
|
describe("supportsPromptCaching", () => {
|
|
|
|
|
it("returns true for Claude models", () => {
|
|
|
|
|
expect(supportsPromptCaching("claude-sonnet-4-5")).toBe(true)
|
|
|
|
|
expect(supportsPromptCaching("anthropic.claude-3-5-sonnet")).toBe(true)
|
|
|
|
|
expect(supportsPromptCaching("us.anthropic.claude-3-5-sonnet")).toBe(
|
|
|
|
|
true,
|
|
|
|
|
)
|
|
|
|
|
expect(supportsPromptCaching("eu.anthropic.claude-3-5-sonnet")).toBe(
|
|
|
|
|
true,
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it("returns false for non-Claude models", () => {
|
|
|
|
|
expect(supportsPromptCaching("gpt-4o")).toBe(false)
|
|
|
|
|
expect(supportsPromptCaching("gemini-pro")).toBe(false)
|
|
|
|
|
expect(supportsPromptCaching("deepseek-chat")).toBe(false)
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
2026-02-26 13:55:21 +01:00
|
|
|
vi.mock("ollama-ai-provider-v2", () => {
|
|
|
|
|
const mockModel = { modelId: "test-model" }
|
|
|
|
|
const mockProviderFn = vi.fn(() => mockModel)
|
|
|
|
|
const mockCreateOllama = vi.fn(() => mockProviderFn)
|
|
|
|
|
const mockOllama = vi.fn(() => mockModel)
|
|
|
|
|
return { createOllama: mockCreateOllama, ollama: mockOllama }
|
|
|
|
|
})
|
|
|
|
|
|
2026-05-15 13:02:26 +08:00
|
|
|
vi.mock("@ai-sdk/deepseek", () => {
|
|
|
|
|
const mockModel = { modelId: "test-model" }
|
|
|
|
|
const mockProviderFn = vi.fn(() => mockModel)
|
|
|
|
|
const mockCreateDeepSeek = vi.fn(() => mockProviderFn)
|
|
|
|
|
const mockDeepseek = vi.fn(() => mockModel)
|
|
|
|
|
return { createDeepSeek: mockCreateDeepSeek, deepseek: mockDeepseek }
|
|
|
|
|
})
|
|
|
|
|
|
2026-06-15 12:54:18 +08:00
|
|
|
vi.mock("@aihubmix/ai-sdk-provider", () => {
|
|
|
|
|
const mockModel = { modelId: "test-model" }
|
|
|
|
|
const mockProviderFn = vi.fn(() => mockModel)
|
|
|
|
|
const mockCreateAihubmix = vi.fn(() => mockProviderFn)
|
|
|
|
|
const mockAihubmix = vi.fn(() => mockModel)
|
|
|
|
|
return { aihubmix: mockAihubmix, createAihubmix: mockCreateAihubmix }
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
describe("AIHubMix provider", () => {
|
|
|
|
|
let createAihubmixMock: ReturnType<typeof vi.fn>
|
|
|
|
|
const savedEnv: Record<string, string | undefined> = {}
|
|
|
|
|
|
|
|
|
|
beforeEach(async () => {
|
|
|
|
|
savedEnv.AIHUBMIX_API_KEY = process.env.AIHUBMIX_API_KEY
|
|
|
|
|
savedEnv.AIHUBMIX_BASE_URL = process.env.AIHUBMIX_BASE_URL
|
|
|
|
|
delete process.env.AIHUBMIX_BASE_URL
|
|
|
|
|
|
|
|
|
|
const mod = await import("@aihubmix/ai-sdk-provider")
|
|
|
|
|
createAihubmixMock = mod.createAihubmix as ReturnType<typeof vi.fn>
|
|
|
|
|
createAihubmixMock.mockClear()
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
|
process.env.AIHUBMIX_API_KEY = savedEnv.AIHUBMIX_API_KEY
|
|
|
|
|
process.env.AIHUBMIX_BASE_URL = savedEnv.AIHUBMIX_BASE_URL
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it("uses AIHUBMIX_API_KEY for server configured AIHubMix", () => {
|
|
|
|
|
process.env.AIHUBMIX_API_KEY = "server-aihubmix-key"
|
|
|
|
|
|
|
|
|
|
getAIModel({
|
|
|
|
|
provider: "aihubmix",
|
|
|
|
|
modelId: "claude-sonnet-4-5-20250929",
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
expect(createAihubmixMock).toHaveBeenCalledWith({
|
|
|
|
|
apiKey: "server-aihubmix-key",
|
|
|
|
|
appCode: "MSBS9675",
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it("uses client BYOK API key for AIHubMix", () => {
|
|
|
|
|
getAIModel({
|
|
|
|
|
provider: "aihubmix",
|
|
|
|
|
apiKey: "client-aihubmix-key",
|
|
|
|
|
modelId: "gpt-5.1",
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
expect(createAihubmixMock).toHaveBeenCalledWith({
|
|
|
|
|
apiKey: "client-aihubmix-key",
|
|
|
|
|
appCode: "MSBS9675",
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it("recognizes AIHubMix standard endpoints", () => {
|
|
|
|
|
expect(isAihubmixStandardBaseURL(undefined)).toBe(true)
|
|
|
|
|
expect(isAihubmixStandardBaseURL("https://aihubmix.com")).toBe(true)
|
|
|
|
|
expect(isAihubmixStandardBaseURL("https://aihubmix.com/v1/")).toBe(true)
|
|
|
|
|
expect(isAihubmixStandardBaseURL("https://proxy.example.com/v1")).toBe(
|
|
|
|
|
false,
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
2026-05-15 13:02:26 +08:00
|
|
|
describe("Kimi provider uses createDeepSeek for reasoning_content support", () => {
|
|
|
|
|
let createDeepSeekMock: ReturnType<typeof vi.fn>
|
|
|
|
|
const savedEnv: Record<string, string | undefined> = {}
|
|
|
|
|
|
|
|
|
|
beforeEach(async () => {
|
|
|
|
|
savedEnv.KIMI_API_KEY = process.env.KIMI_API_KEY
|
|
|
|
|
savedEnv.KIMI_BASE_URL = process.env.KIMI_BASE_URL
|
|
|
|
|
delete process.env.KIMI_BASE_URL
|
|
|
|
|
|
|
|
|
|
const mod = await import("@ai-sdk/deepseek")
|
|
|
|
|
createDeepSeekMock = mod.createDeepSeek as ReturnType<typeof vi.fn>
|
|
|
|
|
createDeepSeekMock.mockClear()
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
|
process.env.KIMI_API_KEY = savedEnv.KIMI_API_KEY
|
|
|
|
|
process.env.KIMI_BASE_URL = savedEnv.KIMI_BASE_URL
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it("uses createDeepSeek with Kimi default base URL for reasoning_content support", () => {
|
|
|
|
|
process.env.KIMI_API_KEY = "test-kimi-key"
|
|
|
|
|
|
|
|
|
|
getAIModel({
|
|
|
|
|
provider: "kimi",
|
|
|
|
|
apiKey: "test-kimi-key",
|
|
|
|
|
modelId: "moonshot-v1-8k",
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
expect(createDeepSeekMock).toHaveBeenCalledWith(
|
|
|
|
|
expect.objectContaining({
|
|
|
|
|
baseURL: "https://api.moonshot.cn/v1",
|
|
|
|
|
}),
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it("uses custom base URL when provided for kimi provider", () => {
|
|
|
|
|
process.env.KIMI_API_KEY = "test-kimi-key"
|
|
|
|
|
|
|
|
|
|
getAIModel({
|
|
|
|
|
provider: "kimi",
|
|
|
|
|
apiKey: "test-kimi-key",
|
|
|
|
|
baseUrl: "https://custom-kimi-endpoint.com/v1",
|
|
|
|
|
modelId: "kimi-k2.6",
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
expect(createDeepSeekMock).toHaveBeenCalledWith(
|
|
|
|
|
expect.objectContaining({
|
|
|
|
|
baseURL: "https://custom-kimi-endpoint.com/v1",
|
|
|
|
|
}),
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
2026-02-26 13:55:21 +01:00
|
|
|
describe("Ollama API key security", () => {
|
|
|
|
|
let createOllamaMock: ReturnType<typeof vi.fn>
|
|
|
|
|
const savedEnv: Record<string, string | undefined> = {}
|
|
|
|
|
|
|
|
|
|
beforeEach(async () => {
|
|
|
|
|
savedEnv.OLLAMA_API_KEY = process.env.OLLAMA_API_KEY
|
|
|
|
|
savedEnv.OLLAMA_BASE_URL = process.env.OLLAMA_BASE_URL
|
|
|
|
|
delete process.env.OLLAMA_BASE_URL
|
|
|
|
|
|
|
|
|
|
const mod = await import("ollama-ai-provider-v2")
|
|
|
|
|
createOllamaMock = mod.createOllama as ReturnType<typeof vi.fn>
|
|
|
|
|
createOllamaMock.mockClear()
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
|
process.env.OLLAMA_API_KEY = savedEnv.OLLAMA_API_KEY
|
|
|
|
|
process.env.OLLAMA_BASE_URL = savedEnv.OLLAMA_BASE_URL
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it("applies server OLLAMA_API_KEY when no client baseUrl is provided", () => {
|
|
|
|
|
process.env.OLLAMA_API_KEY = "server-secret-key"
|
|
|
|
|
|
|
|
|
|
getAIModel({ provider: "ollama", modelId: "llama2" })
|
|
|
|
|
|
|
|
|
|
expect(createOllamaMock).toHaveBeenCalledWith(
|
|
|
|
|
expect.objectContaining({
|
|
|
|
|
headers: { Authorization: "Bearer server-secret-key" },
|
|
|
|
|
}),
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it("does NOT leak server OLLAMA_API_KEY when client provides a custom baseUrl", () => {
|
|
|
|
|
process.env.OLLAMA_API_KEY = "server-secret-key"
|
|
|
|
|
|
|
|
|
|
// When server has OLLAMA_API_KEY, the SSRF guard rejects
|
|
|
|
|
// client-provided baseUrl without an apiKey outright
|
|
|
|
|
expect(() =>
|
|
|
|
|
getAIModel({
|
|
|
|
|
provider: "ollama",
|
|
|
|
|
baseUrl: "https://evil-server.com",
|
|
|
|
|
modelId: "llama2",
|
|
|
|
|
}),
|
|
|
|
|
).toThrow("API key is required")
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it("uses client API key when client provides both baseUrl and apiKey", () => {
|
|
|
|
|
process.env.OLLAMA_API_KEY = "server-secret-key"
|
|
|
|
|
|
|
|
|
|
getAIModel({
|
|
|
|
|
provider: "ollama",
|
|
|
|
|
baseUrl: "https://my-ollama.com",
|
|
|
|
|
apiKey: "client-key",
|
|
|
|
|
modelId: "llama2",
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
expect(createOllamaMock).toHaveBeenCalledWith(
|
|
|
|
|
expect.objectContaining({
|
|
|
|
|
baseURL: "https://my-ollama.com",
|
|
|
|
|
headers: { Authorization: "Bearer client-key" },
|
|
|
|
|
}),
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it("applies both server OLLAMA_BASE_URL and OLLAMA_API_KEY when no client overrides", () => {
|
|
|
|
|
process.env.OLLAMA_BASE_URL = "https://cloud.ollama.com"
|
|
|
|
|
process.env.OLLAMA_API_KEY = "server-key"
|
|
|
|
|
|
|
|
|
|
getAIModel({ provider: "ollama", modelId: "llama2" })
|
|
|
|
|
|
|
|
|
|
expect(createOllamaMock).toHaveBeenCalledWith(
|
|
|
|
|
expect.objectContaining({
|
|
|
|
|
baseURL: "https://cloud.ollama.com",
|
|
|
|
|
headers: { Authorization: "Bearer server-key" },
|
|
|
|
|
}),
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it("works when OLLAMA_API_KEY is set but OLLAMA_BASE_URL is not", () => {
|
|
|
|
|
process.env.OLLAMA_API_KEY = "server-key"
|
|
|
|
|
delete process.env.OLLAMA_BASE_URL
|
|
|
|
|
|
|
|
|
|
getAIModel({ provider: "ollama", modelId: "llama2" })
|
|
|
|
|
|
|
|
|
|
expect(createOllamaMock).toHaveBeenCalledTimes(1)
|
|
|
|
|
const callArgs = createOllamaMock.mock.calls[0][0]
|
|
|
|
|
expect(callArgs).not.toHaveProperty("baseURL")
|
|
|
|
|
expect(callArgs).toEqual(
|
|
|
|
|
expect.objectContaining({
|
|
|
|
|
headers: { Authorization: "Bearer server-key" },
|
|
|
|
|
}),
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it("allows client custom baseUrl without apiKey when no server OLLAMA_API_KEY", () => {
|
|
|
|
|
delete process.env.OLLAMA_API_KEY
|
|
|
|
|
|
|
|
|
|
getAIModel({
|
|
|
|
|
provider: "ollama",
|
|
|
|
|
baseUrl: "https://my-ollama.com",
|
|
|
|
|
modelId: "llama2",
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
expect(createOllamaMock).toHaveBeenCalledTimes(1)
|
|
|
|
|
const callArgs = createOllamaMock.mock.calls[0][0]
|
|
|
|
|
expect(callArgs.baseURL).toBe("https://my-ollama.com")
|
|
|
|
|
expect(callArgs).not.toHaveProperty("headers")
|
|
|
|
|
})
|
|
|
|
|
})
|