The model picker of a gateway key gets a search box (name or id) and takes "re:<regexp>" typed then Enter; the row menu gets Model names (aliases-key). Docs: gateway-routing.md "A key's model patterns and names". Run (mote): go vet darwin, windows, linux nogui on access/gateway/cli/gui; go test internal/access, gateway (937s), cli, gui all ok at the pre-rebase base; after rebasing on origin/main: go build ./..., the new Go tests, TestCoreScriptBudget, and GUI gateway-key-search-names + gateway-key-models + i18n-keys (28 ok, chromium and webkit, en/zh/zh-TW/ja/de, 1100 and 440). Broken by hand: pattern match off (gateway + access tests red), aliasRequest off (alias test red), search argument removed (all 20 chromium GUI red). Not tried: Windows/Linux GUI run, a real client against a real key; the full suite runs after the tag.
49 lines
2.1 KiB
Go
49 lines
2.1 KiB
Go
package gui
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"slices"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/access"
|
|
)
|
|
|
|
// A key's model may be a pattern ("re:<regular expression>"), refused when
|
|
// it isn't one rather than kept to allow every model, and a key keeps the
|
|
// names it gives models (tsingchao on X).
|
|
func TestCallerKeyPatternsAndNames(t *testing.T) {
|
|
sandboxHome(t)
|
|
mux := http.NewServeMux()
|
|
callerKeyRoutes(mux)
|
|
request := func(path, body string) *httptest.ResponseRecorder {
|
|
w := httptest.NewRecorder()
|
|
mux.ServeHTTP(w, httptest.NewRequest("POST", path, strings.NewReader(body)))
|
|
return w
|
|
}
|
|
var s struct{ Keys []access.Key }
|
|
if w := request("/api/caller-keys/add-key", `{"name":"Phone"}`); w.Code != 200 || json.Unmarshal(w.Body.Bytes(), &s) != nil {
|
|
t.Fatal(w.Code, w.Body)
|
|
}
|
|
id := s.Keys[0].ID
|
|
if w := request("/api/caller-keys/models-key", `{"key":"`+id+`","models":["re:^gpt-","openai/o3"]}`); w.Code != 200 || json.Unmarshal(w.Body.Bytes(), &s) != nil || !slices.Equal(s.Keys[0].Models, []string{"re:^gpt-", "openai/o3"}) {
|
|
t.Fatal("pattern", w.Code, w.Body)
|
|
}
|
|
if w := request("/api/caller-keys/models-key", `{"key":"`+id+`","models":["re:("]}`); w.Code != 400 || !strings.Contains(w.Body.String(), "regular expression") {
|
|
t.Fatal("kept a bad pattern", w.Code, w.Body)
|
|
}
|
|
if w := request("/api/caller-keys/aliases-key", `{"key":"`+id+`","aliases":{"fast":"openai/gpt-5"}}`); w.Code != 200 || json.Unmarshal(w.Body.Bytes(), &s) != nil || s.Keys[0].Aliases["fast"] != "openai/gpt-5" {
|
|
t.Fatal("alias", w.Code, w.Body)
|
|
}
|
|
// a model the key may not use can't be named
|
|
if w := request("/api/caller-keys/aliases-key", `{"key":"`+id+`","aliases":{"x":"anthropic/claude"}}`); w.Code != 400 {
|
|
t.Fatal("named a model the key may not use", w.Code, w.Body)
|
|
}
|
|
var none struct{ Keys []access.Key }
|
|
if w := request("/api/caller-keys/aliases-key", `{"key":"`+id+`","aliases":{}}`); w.Code != 200 || json.Unmarshal(w.Body.Bytes(), &none) != nil || len(none.Keys[0].Aliases) != 0 {
|
|
t.Fatal("names not taken off", w.Code, w.Body)
|
|
}
|
|
}
|