Files
magpie/internal/gui/icon_from_test.go
T
yetone 1e6a27d4f8 gui: an icon's image URL typed and saved is fetched, kept and shown again (hoobnn)
hoobnn on Discord #general: a custom provider's icon given by an image
URL was neither fetched nor applied, and on editing it again the URL
was gone.

Cause: the Image URL… box (1dc22a37c) sent its address with Enter only.
An address typed and then saved, or left by clicking elsewhere, was
never fetched, so Save sent the old icon ("generic"). And nothing kept
the address: the box went away after a fetch that worked, and the
provider had no field for it, so a reopened editor showed none.

Now:
- The box has a Use button (an existing string, in every language), and
  leaving it fetches too. Save fetches an address typed and not fetched
  yet, then saves with the picture; one that can't be used keeps the
  editor open with the note why, and saves nothing.
- Provider.IconFrom (iconFrom) records the address the uploaded picture
  came from. /api/providers gives it back, the editor opens with it in
  its box, a save that doesn't name the icon (CLI, TUI) keeps both, and
  provider.Save drops it when Icon isn't an uploaded picture (a built-in,
  Default, the letters, a chosen file or the site's icon clear it in the
  editor too). It is a record only, never fetched again.
- The fetch itself is unchanged: POST /api/icons/url through
  guardClient (public addresses only), https only, MaxIcon 1 MB, type
  sniffed. Remote images are never loaded by the page itself.
Siblings: iconPicker is the only editor with an icon address; the
import link's iconUrl is fetched on confirm (fetchImportIcon) and stays
import-only.

Core script budget: this adds ~1KB to app.js; it fits the unchanged
1,533,105 (now 1,532,840) by cutting four long comments in app.js
(text-cut tooltips, rowOwnRect, modalHold, modalDepth) to two lines
each. No raise.

Run at this tree (based on origin/main 0516c18ca), on mote:
- GUI: icon-url-saved.test.cjs (new) and icon-url.test.cjs, Chromium and
  WebKit, 420 and 1100 wide, en/zh/zh-TW/ja/de: 40 of 40 ok (the
  existing test now expects the box to stay with the working address).
- Broken by hand: Save's flush removed -> icon-url-saved red in
  Chromium ("Save didn't send the fetched picture", actual 'generic',
  hoobnn's case); draftOf without iconFrom -> red in WebKit (the box
  with the address never shows on reopening).
- go test ./internal/gui ./internal/provider: provider ok; gui ok but
  for three reds that fail the same on a fresh origin/main 586b59fe7
  copy, each already filed: TestSettingsSaveKeepsWhatItDoesNotSend
  (BusyLoad, #4739), TestMainWindowCorners/macOS_26_SDK (#3949) and
  TestColdTurnsTaken in the full package run (#4510; it passes alone
  on main). TestCoreScriptBudget ok.
  TestProviderSaveKeepsIconFrom (new) red with the save merge's IconFrom
  keep removed ("a save without the icon lost it"), green restored.
- gofmt -l clean; go vet ./... (darwin), GOOS=linux go vet -tags nogui
  ./..., GOOS=windows go vet ./...; go build, linux nogui and windows
  builds: ok.
Not yet at push (the owner's ship-at-once rule): the whole GUI suite
(mote was busy with another session's full GUI run) and the full Go
suite (running on the Linux box); both run after the push, and any red
they show is fixed or filed in its own commit.
Not tried: a real image URL against the real backend in the app; the
fetch path is unchanged from 1dc22a37c, which was.
2026-11-01 14:33:24 +08:00

53 lines
2.2 KiB
Go

package gui
import (
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// hoobnn on Discord: a custom provider's icon fetched from an image URL
// had its address gone when the editor was opened again. The address is
// saved with the picture, given back by /api/providers, kept by a save
// that doesn't name the icon, and dropped with a picture that came from
// elsewhere.
func TestProviderSaveKeepsIconFrom(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)
do := func(method, path, body string) string {
t.Helper()
w := httptest.NewRecorder()
mux.ServeHTTP(w, httptest.NewRequest(method, path, strings.NewReader(body)))
if w.Code != 200 {
t.Fatalf("%s: %d %s", path, w.Code, w.Body)
}
return w.Body.String()
}
const from = "https://cdn.example.com/logo.png"
do("POST", "/api/provider/save", `{"id":"box","name":"Box","chat":"http://127.0.0.1:9/v1","key":"k","icon":"file:00000000000000bb.png","iconFrom":"`+from+`","new":true}`)
if p, err := provider.Find("box"); err != nil || p.Icon != "file:00000000000000bb.png" || p.IconFrom != from {
t.Fatalf("the icon's address wasn't kept: %+v %v", p, err)
}
if !strings.Contains(do("GET", "/api/providers", ""), `"iconFrom":"`+from+`"`) {
t.Fatal("/api/providers doesn't give the address back")
}
// a save that doesn't name the icon (the CLI's, the TUI's) keeps both
do("POST", "/api/provider/save", `{"id":"box","from":"box","name":"Box","chat":"http://127.0.0.1:9/v1","key":""}`)
if p, err := provider.Find("box"); err != nil || p.Icon != "file:00000000000000bb.png" || p.IconFrom != from {
t.Fatalf("a save without the icon lost it: %+v %v", p, err)
}
// a built-in icon picked after: the address isn't that picture's
do("POST", "/api/provider/save", `{"id":"box","from":"box","name":"Box","chat":"http://127.0.0.1:9/v1","key":"","icon":"openai","iconFrom":"`+from+`"}`)
if p, err := provider.Find("box"); err != nil || p.Icon != "openai" || p.IconFrom != "" {
t.Fatalf("the address outlived its picture: %+v %v", p, err)
}
}