Files
next-ai-draw-io/tests/unit/base-url.test.ts
T
Dayuan Jiang 8639779a23 fix(ollama): send chat requests to the /api path (#954)
The Ollama SDK appends /chat to the base URL, and Ollama serves chat at
/api/chat. The model list already added the missing /api, so with
"http://localhost:11434" (the address our docs showed) models were listed
but every chat request went to /chat and got a 404. An OpenAI-style
".../v1" address went to /v1/chat.

ollamaApiUrl() turns the server address, ".../v1" and ".../api" into
".../api". Chat, the model list and the settings dialog's request URL
hint all use it. The docs now show http://localhost:11434/api, which also
works on released versions.
2026-10-06 09:37:05 +09:00

68 lines
2.4 KiB
TypeScript

import { describe, expect, it } from "vitest"
import {
chatRequestUrl,
normalizeBaseUrl,
ollamaApiUrl,
} from "@/lib/types/model-config"
describe("normalizeBaseUrl", () => {
it("drops spaces, trailing slashes and a pasted endpoint path", () => {
expect(normalizeBaseUrl(" https://api.x.com/v1/ ")).toBe(
"https://api.x.com/v1",
)
expect(normalizeBaseUrl("https://api.x.com/v1/chat/completions")).toBe(
"https://api.x.com/v1",
)
expect(
normalizeBaseUrl("https://api.x.com/anthropic/v1/messages/"),
).toBe("https://api.x.com/anthropic/v1")
})
it("keeps provider paths such as /api/paas/v4 and /api/v3", () => {
expect(normalizeBaseUrl("https://open.bigmodel.cn/api/paas/v4")).toBe(
"https://open.bigmodel.cn/api/paas/v4",
)
expect(
normalizeBaseUrl("https://ark.cn-beijing.volces.com/api/v3"),
).toBe("https://ark.cn-beijing.volces.com/api/v3")
})
})
describe("chatRequestUrl", () => {
it("shows the endpoint the SDK will call", () => {
expect(chatRequestUrl("glm", "https://api.x.com/v1/")).toBe(
"https://api.x.com/v1/chat/completions",
)
expect(
chatRequestUrl("anthropic", "https://proxy.example.com/v1"),
).toBe("https://proxy.example.com/v1/messages")
expect(chatRequestUrl("ollama", "http://localhost:11434")).toBe(
"http://localhost:11434/api/chat",
)
})
it("stays out of the way for SDKs that build their own paths", () => {
expect(chatRequestUrl("google", "https://x.example.com")).toBeNull()
expect(chatRequestUrl("minimax", "https://x.example.com")).toBeNull()
expect(chatRequestUrl("glm", " ")).toBeNull()
})
})
describe("ollamaApiUrl", () => {
it("points at Ollama's /api whatever form the address takes", () => {
for (const url of [
"http://localhost:11434",
"http://localhost:11434/",
"http://localhost:11434/api",
"http://localhost:11434/api/",
"http://localhost:11434/v1",
"http://localhost:11434/v1/chat/completions",
]) {
expect(ollamaApiUrl(url)).toBe("http://localhost:11434/api")
}
expect(ollamaApiUrl("https://ollama.com/api")).toBe(
"https://ollama.com/api",
)
})
})