Files
magpie/internal/gui/resolve_test.go
T
yetone 4b93d391ca gui: the provider editor's connect addresses are saved and shown (quantum_lsfc on X)
/api/providers carries resolve and takesResolve; Save, Test, Fetch
models and peekcert take resolve (left out keeps what was saved, {}
clears it); provider.Save refuses a rule CheckResolve refuses or one on a
provider that takes none (TestResolveSaved, TestProviderSaveResolve).

Run on mote at the head rebased on origin/main b942a872b: go test
-count=1 -timeout 45m ./internal/netproxy ./internal/provider
./internal/gateway ./internal/gui, all ok; gofmt -l clean; go vet ./...
for darwin, linux (-tags nogui) and windows ok; provider-resolve.test.cjs
(chromium and webkit; en zh zh-TW ja de; 1100 and 420) 60 of 60 ok.
Rebased again on origin/main dea131174 and run on mote:
netproxy, provider and gui (TestCoreScriptBudget) ok, gofmt and the
three vets ok, provider-resolve 60 of 60 and i18n-keys ok; internal/gateway was
not run again at that head (no new main commit touches the offline
check or the dial).
The 94 GUI files the editor's Save, Test and certificate rows reach,
run at origin/main 436d507f0: 88 reds in 15 files, the same tests red
on that main run alone (filed: #6424, and #6010 #6156 #5838 #4621
#4391 #5147 #6030 #6209 #4397 #4673 #6099).
Not tried: a real managed network with an L4 SNI gateway (the
reporter's), and the rule's dial on Windows or Linux beyond go vet.
2026-11-13 14:30:19 +08:00

71 lines
2.5 KiB
Go

package gui
import (
"net/http"
"net/http/httptest"
"os"
"reflect"
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// quantum_lsfc on X: the editor's connect addresses are saved on a
// provider of the user's, normalised as netproxy.CheckResolve keeps them,
// open as saved, are kept by a Save that leaves them out, cleared by an
// empty one, and refused when they can't be used.
func TestProviderSaveResolve(t *testing.T) {
t.Setenv("HOME", t.TempDir())
t.Setenv("USERPROFILE", os.Getenv("HOME"))
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
t.Setenv("XDG_CACHE_HOME", t.TempDir())
mux := http.NewServeMux()
providerRoutes(mux, nil)
post := func(body string) *httptest.ResponseRecorder {
t.Helper()
w := httptest.NewRecorder()
mux.ServeHTTP(w, httptest.NewRequest("POST", "/api/provider/save", strings.NewReader(body)))
return w
}
saved := func() map[string][]string {
t.Helper()
p, err := provider.Find("gw")
if err != nil {
t.Fatal(err)
}
info := providerInfo(*p, nil)
if !info.TakesResolve || !reflect.DeepEqual(info.Resolve, p.Resolve) {
t.Fatalf("the editor is shown %v (takes %v), saved %v", info.Resolve, info.TakesResolve, p.Resolve)
}
return p.Resolve
}
base := `"id":"gw","name":"Gateway","key":"sk-g","anthropic":"https://llm.corp.example/v1","icon":"generic"`
if w := post(`{` + base + `,"new":true,"resolve":{" LLM.corp.example ":["dns:gw.managed.test","10.0.0.1"]}}`); w.Code != 200 {
t.Fatalf("%d %s", w.Code, w.Body.String())
}
want := map[string][]string{"llm.corp.example": {"dns:gw.managed.test", "10.0.0.1"}}
if got := saved(); !reflect.DeepEqual(got, want) {
t.Fatalf("saved %v, want %v", got, want)
}
keep := `{"id":"gw","from":"gw","name":"Gateway","anthropic":"https://llm.corp.example/v1","icon":"generic","models":[],"headers":{}`
if w := post(keep + `}`); w.Code != 200 {
t.Fatalf("%d %s", w.Code, w.Body.String())
}
if got := saved(); !reflect.DeepEqual(got, want) {
t.Fatalf("a Save that left them out changed them: %v", got)
}
if w := post(keep + `,"resolve":{"llm.corp.example":["gateway.corp"]}}`); w.Code == 200 || !strings.Contains(w.Body.String(), "resolve: ") {
t.Fatalf("a place that is a bare name was kept: %d %s", w.Code, w.Body.String())
}
if got := saved(); !reflect.DeepEqual(got, want) {
t.Fatalf("a refused Save changed them: %v", got)
}
if w := post(keep + `,"resolve":{}}`); w.Code != 200 {
t.Fatalf("%d %s", w.Code, w.Body.String())
}
if got := saved(); len(got) != 0 {
t.Fatalf("emptied, still %v", got)
}
}