zot/packages/provider/openai_url_test.go
Pietro Di Bello 9bb884ebbc
fix(provider): keep /v4 base from getting a spurious /v1 chat path
The OpenAI-compatible client only treated a base URL ending in "/v1" as
already-versioned; any other base got "/v1/chat/completions" appended.

Z.AI's coding-plan base ends in "/paas/v4", so requests were sent to
".../paas/v4/v1/chat/completions" — a path that does not exist — and
every GLM model returned 404.

Match any trailing "/vN" version segment instead. This is behaviour-
identical for all existing providers (their versioned bases all end in
"/v1") and only changes Z.AI, which now hits ".../paas/v4/chat/completions".
2026-06-14 21:45:53 +02:00

40 lines
1.2 KiB
Go

package provider
import "testing"
// TestChatCompletionsURL pins the endpoint built for OpenAI-compatible
// providers. The regression that motivated it: Z.AI's coding-plan base
// carries a "/v4" version segment, so blindly appending
// "/v1/chat/completions" produced ".../paas/v4/v1/chat/completions"
// and a 404. Any base that already ends in a version segment must get
// "/chat/completions" appended directly.
func TestChatCompletionsURL(t *testing.T) {
cases := []struct {
name string
baseURL string
want string
}{
{
name: "bare host gets conventional /v1 path",
baseURL: "https://api.openai.com",
want: "https://api.openai.com/v1/chat/completions",
},
{
name: "v1 base is not doubled",
baseURL: "https://api.moonshot.ai/v1",
want: "https://api.moonshot.ai/v1/chat/completions",
},
{
name: "zai coding plan v4 base is not given a spurious /v1",
baseURL: "https://api.z.ai/api/coding/paas/v4",
want: "https://api.z.ai/api/coding/paas/v4/chat/completions",
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
if got := chatCompletionsURL(tc.baseURL); got != tc.want {
t.Errorf("chatCompletionsURL(%q) = %q, want %q", tc.baseURL, got, tc.want)
}
})
}
}