Files
magpie/internal/gui/gemini_cleared_save_test.go
T
yetone 253aee08b4 provider: an AI Studio key's Gemini URL is filled in once, and stays empty when the user clears it
3e61d814e gave a provider at AI Studio's OpenAI-compatible URL AI
Studio's Gemini API base in normalize, which runs on every load and
save, so a user who cleared that URL got it back each time: magpie's
default overrode the user's value (code standards).

Now it is filled in once and recorded (GeminiFilled, geminiFilled in
providers.json). A URL cleared after that stays empty, from the CLI
(`magpie provider set <id> gemini=`), by hand in providers.json, or in
the editor: the form doesn't send geminiFilled, so its save carries it
over from the saved provider, as it does the other fields the form
doesn't show. Providers that already got the URL from 3e61d814e have
no record yet; their first load records it, the URL unchanged, so a
clear after that holds.

Tests: TestAIStudioGeminiClearedStays (provider: clear, then two loads
and saves), TestSaveKeepsGeminiCleared (gui: /api/provider/save with
the URL cleared). Broken by hand: the old fill (no GeminiFilled check)
turns both red, the URL back each time; the editor's save without the
carry-over turns TestSaveKeepsGeminiCleared red.

Run on mote at this change on 2ab923571: gofmt -l clean; GOOS=linux go
vet -tags nogui ./internal/... ok; GOOS=windows go vet ./internal/... ok;
go test ./internal/provider ok; go test ./internal/gui ok except
TestMainWindowCorners/macOS_26_SDK, #3949 (macOS 27 AppKit, untouched
here); go test ./internal/gateway -run Gemini/AIStudio ok.

Not tried: the editor itself in a browser (no JS change; the field and
its save were already there). A user who cleared the URL in the hours
between 3e61d814e and this commit got it back then; clearing it again
now holds.
2026-11-03 02:37:56 +08:00

43 lines
1.3 KiB
Go

package gui
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// The editor doesn't send that magpie filled an AI Studio key's Gemini URL
// in (GeminiFilled), so its save keeps it: the URL cleared in the form
// stays cleared, and isn't filled in again.
func TestSaveKeepsGeminiCleared(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
t.Setenv("XDG_CACHE_HOME", t.TempDir())
if err := provider.Save(provider.Provider{ID: "google", Name: "Google Gemini", Preset: "google", Chat: provider.AIStudioChat, Key: "AIza-k"}); err != nil {
t.Fatal(err)
}
if p, _ := provider.Find("google"); p == nil || p.Gemini != provider.AIStudioGemini {
t.Fatalf("not filled in: %+v", p)
}
mux := http.NewServeMux()
providerRoutes(mux, nil)
w := httptest.NewRecorder()
mux.ServeHTTP(w, httptest.NewRequest("POST", "/api/provider/save", strings.NewReader(
`{"id":"google","name":"Google Gemini","preset":"google","chat":"`+provider.AIStudioChat+`","gemini":""}`)))
if w.Code != 200 {
t.Fatalf("%d %s", w.Code, w.Body)
}
got, err := provider.Find("google")
if err != nil {
t.Fatal(err)
}
if got.Gemini != "" || got.Key != "AIza-k" {
t.Fatalf("after clearing it in the editor: gemini %q key %q", got.Gemini, got.Key)
}
}