Files
magpie/internal/gateway/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

135 lines
5.4 KiB
Go

package gateway
import (
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"strings"
"sync"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// azureUp plays an Azure OpenAI resource's v1 API: chat completions and
// Responses under /openai/v1, a key in api-key and nothing else — a
// Bearer is an Entra ID token there, and a key sent as one is refused.
type azureUp struct {
mu sync.Mutex
calls []bedrockCall
}
func (a *azureUp) ServeHTTP(w http.ResponseWriter, r *http.Request) {
body, _ := io.ReadAll(r.Body)
var m map[string]any
json.Unmarshal(body, &m)
model, _ := m["model"].(string)
a.mu.Lock()
a.calls = append(a.calls, bedrockCall{r.URL.RequestURI(), r.Header.Clone(), model, m})
a.mu.Unlock()
if r.Header.Get("api-key") != "azure-key" || r.Header.Get("Authorization") != "" {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusUnauthorized)
io.WriteString(w, `{"error":{"code":"401","message":"Access denied due to invalid subscription key or wrong API endpoint."}}`)
return
}
switch r.URL.Path {
case "/openai/v1/chat/completions":
w.Header().Set("Content-Type", "text/event-stream")
io.WriteString(w, sse(
`data: {"id":"c1","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"role":"assistant","content":"from azure chat"}}]}`,
`data: {"id":"c1","object":"chat.completion.chunk","choices":[{"index":0,"delta":{},"finish_reason":"stop"}],"usage":{"prompt_tokens":5,"completion_tokens":2}}`,
`data: [DONE]`))
case "/openai/v1/responses":
w.Header().Set("Content-Type", "text/event-stream")
io.WriteString(w, sse(
`data: {"type":"response.created","response":{"id":"r1","status":"in_progress"}}`,
`data: {"type":"response.output_item.added","output_index":0,"item":{"type":"message","id":"m1","role":"assistant","content":[]}}`,
`data: {"type":"response.output_text.delta","output_index":0,"content_index":0,"item_id":"m1","delta":"from azure responses"}`,
`data: {"type":"response.output_item.done","output_index":0,"item":{"type":"message","id":"m1","role":"assistant","content":[{"type":"output_text","text":"from azure responses"}]}}`,
`data: {"type":"response.completed","response":{"id":"r1","status":"completed","usage":{"input_tokens":5,"output_tokens":2}}}`))
default:
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusNotFound)
io.WriteString(w, `{"error":{"code":"404","message":"Resource not found"}}`)
}
}
func (a *azureUp) last() bedrockCall {
a.mu.Lock()
defer a.mu.Unlock()
if len(a.calls) == 0 {
return bedrockCall{}
}
return a.calls[len(a.calls)-1]
}
// The Azure OpenAI preset at a fake resource: Codex's Responses, a chat
// client's completions and Claude Code's messages all reach the resource's
// v1 API with the key in api-key and no Authorization, the model in the
// body the deployment's name as the agent picked it, and a reply length
// asked as max_completion_tokens, which its reasoning deployments want.
// A deployment named for a GPT model goes to Responses, one named any
// other way to chat completions.
func TestAzureRoutes(t *testing.T) {
fresh(t)
up := &azureUp{}
srv := httptest.NewServer(up)
t.Cleanup(srv.Close)
p, err := provider.FromPreset(provider.AzurePreset)
if err != nil {
t.Fatal(err)
}
p.Key = "azure-key"
// A local fake isn't a resource's host, so it is kept as given; the
// endpoint field sets both, as here.
p.Chat = srv.URL + "/openai/v1"
p.Responses = p.Chat
p.Models = []string{"gpt-5-codex", "team-chat"}
if err := provider.Save(p); err != nil {
t.Fatal(err)
}
saved, err := provider.Find("azure")
if err != nil || saved.Responses != saved.Chat || !saved.IsAzure() {
t.Fatalf("saved: %v %+v", err, saved)
}
for _, tc := range []struct{ name, path, body, reply, upstream, model string }{
{"codex", "/v1/responses",
`{"model":"azure/gpt-5-codex","stream":true,"input":[{"role":"user","content":[{"type":"input_text","text":"hi"}]}]}`,
"from azure responses", "/openai/v1/responses", "gpt-5-codex"},
{"claude code on a gpt deployment", "/v1/messages",
`{"model":"azure/gpt-5-codex","max_tokens":20,"stream":true,"messages":[{"role":"user","content":"hi"}]}`,
"from azure responses", "/openai/v1/responses", "gpt-5-codex"},
{"chat client", "/v1/chat/completions",
`{"model":"azure/team-chat","max_tokens":20,"stream":true,"thinking":{"type":"enabled"},"messages":[{"role":"user","content":"hi"}]}`,
"from azure chat", "/openai/v1/chat/completions", "team-chat"},
{"claude code on another deployment", "/v1/messages",
`{"model":"azure/team-chat","max_tokens":20,"stream":true,"messages":[{"role":"user","content":"hi"}]}`,
"from azure chat", "/openai/v1/chat/completions", "team-chat"},
} {
t.Run(tc.name, func(t *testing.T) {
code, body := post(t, tc.path, tc.body)
if code != 200 || !strings.Contains(body, tc.reply) {
t.Fatalf("status %d: %s", code, body)
}
c := up.last()
if c.path != tc.upstream || c.model != tc.model {
t.Fatalf("upstream: %s %q", c.path, c.model)
}
if c.head.Get("api-key") != "azure-key" || c.head.Get("Authorization") != "" || c.head.Get("x-api-key") != "" {
t.Fatalf("auth headers: %v", c.head)
}
if strings.HasSuffix(c.path, "/chat/completions") {
if _, ok := c.body["max_tokens"]; ok || c.body["max_completion_tokens"] != float64(20) {
t.Fatalf("length asked as: %v", c.body)
}
if _, ok := c.body["thinking"]; ok {
t.Fatalf("thinking sent to Azure: %v", c.body)
}
}
})
}
}