Files
magpie/internal/provider/azure_test.go
T
yetone c35471cf5b provider: Azure OpenAI, asked at the user's own resource on its v1 API with the key in api-key, for Codex, Claude Code and chat clients alike, and brought in from other apps instead of left out — Azure OpenAI had no preset, and importing an Azure entry from Alma was skipped with "magpie doesn't support Azure OpenAI yet". A new Azure OpenAI preset (vendor) has no URL of its own: its editor asks for the resource's endpoint in a field above the key, focused first, with https://<resource>.openai.azure.com as the placeholder and a hint on where the portal shows it and that the model ids are the deployments' names (en and zh); adding without one says "Your resource's endpoint is needed" and sends nothing, and Save refuses one without an endpoint too. Whatever is pasted — the bare endpoint, …/openai, …/openai/v1/, a classic deployment URL with its api-version, a *.cognitiveservices.azure.com or *.services.ai.azure.com host, or only the resource's name — is saved as https://<host>/openai/v1 for both chat completions and Responses. magpie uses the v1 GA API rather than the classic /openai/deployments/<name>/…?api-version= paths: the paths match OpenAI's, the body's model is the deployment's name, and no api-version is needed (the reasoning is in internal/provider/azure.go). The key goes in api-key, never as a Bearer, which Azure takes as an Entra ID token; nothing sends Authorization or x-api-key there. Deployments named after GPT/o/codex models go to Responses first, as OpenAI's do, and others to chat completions. Chat bodies ask max_completion_tokens instead of max_tokens and drop thinking/enable_thinking, both in passthrough and in Claude Code's translated requests, and the Test probe asks max_completion_tokens as well. The model list is the resource's deployments (/openai/deployments?api-version=2022-12-01), falling back to /openai/v1/models, with the key in api-key; when neither answers, the error says to type the deployment names in as model ids. Before a list, no catalog models are offered, since a model the resource hasn't deployed would be turned away. An Alma azure entry, and any imported entry at a resource's host, becomes the preset at its v1 base; an Alma azure entry with no endpoint is skipped as "it has no Azure OpenAI endpoint", and one at a non-Azure host (an API Management gateway) keeps its URL and is still signed with api-key. Adds the lobe-icons Azure icon. Tested in Go against fake resources (httptest): TestAzureRoutes checks that Codex /v1/responses, a chat client's /v1/chat/completions and Claude Code's /v1/messages on both a GPT-named and another deployment reach /openai/v1/responses or /openai/v1/chat/completions with api-key set, no Authorization or x-api-key, the deployment name as the model, max_completion_tokens and no thinking; TestAzureBase covers each pasted form and the hosts that aren't resources; TestAzurePresetSaved covers the endpoint required, both URLs on the v1 base, api-key-only headers, Native, the probe's URL and body, and nothing available before a list; TestAzureModels covers deployments first, the v1 fallback and a refused key's message; TestImportAzure covers Alma rows with an endpoint, with a bare resource name and with none, plus a classic URL from another app. azure-endpoint.test.cjs, in Chromium and WebKit, en and zh, checks the field, placeholder, hint and focus, the warning without an endpoint with nothing sent and the page not scrolled, the endpoint sent as chat and responses with the key, and that OpenAI's editor has no such field. add-sheet and old-webkit pass. go build -tags nogui (and GOOS=linux, GOOS=windows), go vet and go test -tags nogui ./... pass (TestClaudeAccountNamedAsItsLogin failed under load and passes alone). Not tried against a real Azure resource: whether a key still lists deployments with api-version 2022-12-01, whether Azure's Responses API accepts every field Codex sends, Claude models on Azure AI Foundry's Anthropic endpoint (not wired), Entra ID tokens, and API Management custom domains beyond keeping their URL. Not tried in the real Wails window.
2026-09-30 02:17:15 +08:00

203 lines
8.1 KiB
Go

package provider
import (
"context"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"sync"
"testing"
)
func azureHome(t *testing.T) {
t.Helper()
isolate(t)
h := t.TempDir()
t.Setenv("HOME", h)
t.Setenv("USERPROFILE", h)
t.Setenv("APPDATA", filepath.Join(h, "AppData", "Roaming"))
t.Setenv("XDG_CONFIG_HOME", filepath.Join(h, ".config"))
t.Setenv("XDG_CACHE_HOME", filepath.Join(h, ".cache"))
t.Setenv("PATH", h)
for _, v := range []string{"CLAUDE_CONFIG_DIR", "CODEX_HOME"} {
t.Setenv(v, "")
}
}
// Whatever the portal or another app gave — the resource's endpoint, its
// /openai, the v1 base, a classic deployment's URL, a newer resource's
// host, or only its name — magpie asks the resource's v1 API.
func TestAzureBase(t *testing.T) {
for in, want := range map[string]string{
"https://contoso.openai.azure.com": "https://contoso.openai.azure.com/openai/v1",
"https://contoso.openai.azure.com/": "https://contoso.openai.azure.com/openai/v1",
"https://contoso.openai.azure.com/openai": "https://contoso.openai.azure.com/openai/v1",
" https://Contoso.openai.azure.com/openai/v1/ ": "https://contoso.openai.azure.com/openai/v1",
"https://contoso.openai.azure.com/openai/deployments/gpt-4o/chat/completions?api-version=2024-10-21": "https://contoso.openai.azure.com/openai/v1",
"contoso.openai.azure.com": "https://contoso.openai.azure.com/openai/v1",
"https://eastus2-x.cognitiveservices.azure.com/": "https://eastus2-x.cognitiveservices.azure.com/openai/v1",
"https://proj.services.ai.azure.com/openai/v1": "https://proj.services.ai.azure.com/openai/v1",
"contoso": "https://contoso.openai.azure.com/openai/v1",
} {
if got, ok := AzureBase(in); !ok || got != want {
t.Errorf("%q: %q %v, want %q", in, got, ok, want)
}
}
for _, in := range []string{"", "https://api.openai.com/v1", "https://openai.azure.com", "https://apim.example.com/openai"} {
if got, ok := AzureBase(in); ok {
t.Errorf("%q taken for a resource: %q", in, got)
}
}
if !(Provider{Chat: "https://x.openai.azure.com/openai/v1"}).IsAzure() || (Provider{Chat: "https://api.openai.com/v1"}).IsAzure() {
t.Error("IsAzure by host")
}
}
// The preset is filled in with the resource's endpoint alone: saved, both
// chat completions and Responses are on its v1 API. Without one it isn't
// saved. The key goes in api-key alone, never as a Bearer.
func TestAzurePresetSaved(t *testing.T) {
azureHome(t)
p, err := FromPreset(AzurePreset)
if err != nil {
t.Fatal(err)
}
pr := Preset(AzurePreset)
if pr.Endpoint == "" || pr.EndpointHint == "" || p.Chat != "" || p.Responses != "" {
t.Fatalf("preset: %+v %+v", pr, p)
}
p.Key = "k"
if err := Save(p); err == nil || !strings.Contains(err.Error(), "endpoint") {
t.Fatalf("saved without an endpoint: %v", err)
}
p.Chat, p.Responses = "https://contoso.openai.azure.com/", "https://contoso.openai.azure.com/"
if err := Save(p); err != nil {
t.Fatal(err)
}
q, err := Find(AzurePreset)
if err != nil {
t.Fatal(err)
}
if q.Chat != "https://contoso.openai.azure.com/openai/v1" || q.Responses != q.Chat || q.Anthropic != "" || !q.IsAzure() {
t.Fatalf("saved: %+v", q)
}
for _, proto := range []Protocol{Chat, Responses} {
if h := AuthHeaders(*q, proto); h["api-key"] != "k" || len(h) != 1 {
t.Errorf("%s: %v", proto, h)
}
}
// a deployment named for a GPT model on Responses, like OpenAI's own;
// its test asks max_completion_tokens
if q.Native("gpt-5-codex") != Responses || q.Native("my-deploy") != Chat {
t.Errorf("native: %s %s", q.Native("gpt-5-codex"), q.Native("my-deploy"))
}
url, body := tinyBody(*q, Chat, "my-deploy")
if url != "https://contoso.openai.azure.com/openai/v1/chat/completions" || !strings.Contains(body, `"max_completion_tokens":16`) {
t.Errorf("test: %s %s", url, body)
}
if url, _ := tinyBody(*q, Responses, "gpt-5-codex"); url != "https://contoso.openai.azure.com/openai/v1/responses" {
t.Errorf("test: %s", url)
}
// nothing offered from the catalog before the resource's list: a
// model not deployed there would be turned away
if ms := q.Available(); len(ms) != 0 {
t.Errorf("available before a list: %d", len(ms))
}
}
// The resource's deployments are its model ids: asked on the data plane
// with the key in api-key, no Authorization, and the v1 API's model list
// where that is refused.
func TestAzureModels(t *testing.T) {
azureHome(t)
var mu sync.Mutex
var asked []string
deployments := true
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
mu.Lock()
asked = append(asked, r.URL.RequestURI())
mu.Unlock()
if r.Header.Get("api-key") != "k" || r.Header.Get("Authorization") != "" || r.Header.Get("x-api-key") != "" {
w.WriteHeader(http.StatusUnauthorized)
return
}
switch {
case r.URL.Path == "/openai/deployments" && r.URL.Query().Get("api-version") == "2022-12-01" && deployments:
w.Write([]byte(`{"data":[{"id":"team-gpt","model":"gpt-5","object":"deployment","status":"succeeded"},{"id":"gpt-5-codex","model":"gpt-5-codex","object":"deployment","status":"succeeded"}],"object":"list"}`))
case r.URL.Path == "/openai/v1/models":
w.Write([]byte(`{"object":"list","data":[{"id":"gpt-4.1","object":"model"}]}`))
default:
w.WriteHeader(http.StatusNotFound)
w.Write([]byte(`{"error":{"code":"404","message":"Resource not found"}}`))
}
}))
defer srv.Close()
p, _ := FromPreset(AzurePreset)
p.Key = "k"
p.Chat, p.Responses = srv.URL+"/openai/v1", srv.URL+"/openai/v1"
if err := Save(p); err != nil {
t.Fatal(err)
}
q, _ := Find(AzurePreset)
ms, err := q.Fetch(context.Background())
if err != nil || len(ms) != 2 || ms[0].ID != "team-gpt" || ms[1].ID != "gpt-5-codex" {
t.Fatalf("deployments: %v %+v", err, ms)
}
if asked[0] != "/openai/deployments?api-version=2022-12-01" {
t.Errorf("asked %v", asked)
}
if got := q.Available(); len(got) != 2 {
t.Errorf("available %+v", got)
}
deployments = false
asked = nil
ms, err = q.Fetch(context.Background())
if err != nil || len(ms) != 1 || ms[0].ID != "gpt-4.1" {
t.Fatalf("v1 models: %v %+v", err, ms)
}
if len(asked) != 2 || asked[1] != "/openai/v1/models" {
t.Errorf("asked %v", asked)
}
q.Key = "wrong"
if _, err := q.Fetch(context.Background()); err == nil || !strings.Contains(err.Error(), "deployments' names") {
t.Errorf("refused: %v", err)
}
}
// Another app's Azure OpenAI entry is brought in as the preset, at its
// resource's v1 API, where magpie used to leave it out.
func TestImportAzure(t *testing.T) {
azureHome(t)
cfg, _ := os.UserConfigDir()
sqliteFixture(t, filepath.Join(cfg, "alma", "chat_threads.db"),
`CREATE TABLE providers (id TEXT PRIMARY KEY, name TEXT, type TEXT, api_key TEXT, models TEXT, base_url TEXT, enabled INTEGER, created_at TEXT, api_format TEXT, is_response_api INTEGER, custom_headers TEXT)`,
`INSERT INTO providers VALUES ('z1','Azure','azure','az-key','["team-gpt"]','https://contoso.openai.azure.com/openai',1,'1',NULL,0,NULL)`,
`INSERT INTO providers VALUES ('z2','Work Azure','azure','az-2','[]','contoso2',1,'2',NULL,0,NULL)`,
`INSERT INTO providers VALUES ('z3','Azure','azure','az-3','[]',NULL,1,'3',NULL,0,NULL)`,
)
items := itemsOf(t, "alma")
it := items["z1"]
if it.Skip != "" || it.Provider.Preset != AzurePreset || it.Provider.Name != "Azure OpenAI" || it.Provider.Key != "az-key" ||
it.Provider.Chat != "https://contoso.openai.azure.com/openai/v1" || it.Provider.Responses != it.Provider.Chat ||
len(it.Provider.Models) != 1 || it.Provider.Icon != "azure-color" {
t.Fatalf("azure: %+v", it)
}
if it := items["z2"]; it.Skip != "" || it.Provider.Name != "Work Azure" || it.Provider.Chat != "https://contoso2.openai.azure.com/openai/v1" {
t.Fatalf("by name: %+v", it)
}
if it := items["z3"]; it.Skip == "" {
t.Fatalf("no endpoint offered: %+v", it)
}
// an entry at a resource's host from an app that doesn't say Azure
im, skip := imported("My Azure", "k", endpoints{chat: "https://contoso.openai.azure.com/openai/deployments/gpt-4o?api-version=2024-10-21"}, nil)
if skip != "" || im.Preset != AzurePreset || im.Chat != "https://contoso.openai.azure.com/openai/v1" || im.Responses != im.Chat {
t.Fatalf("imported: %q %+v", skip, im)
}
}