Files
magpie/internal/gui/public_url_test.go
T
yetone 909d767472 gui: with MAGPIE_PUBLIC_URL set, Gateway's Connect starts at it until the user picks an address, and magpie web shows it as the gateway's address (#1551, everpcpc)
everpcpc runs magpie web in Docker behind a reverse proxy with
MAGPIE_PUBLIC_URL=https://magpie.example.com. The page is opened from
other machines, yet Connect's Base URL, the copied shell/curl/Python/Node
snippets and the gateway's address on the Gateway page started at
http://127.0.0.1:3425. v0.1.1174 kept a picked address; the public one
still had to be picked once on every browser.

- /api/providers' gateway carries publicURL: MAGPIE_PUBLIC_URL when it is
  among the addresses Connect offers (shared, or MAGPIE_ADDR on the
  network). With no address kept in magpie.connectAddress, Connect starts
  there, so the snippets and the gateway key picked for it follow. An
  address the user picked, this computer's included, still wins.
- The option is labelled "Public address" instead of "Local network".
- /api/settings carries publicURL under the same condition, and Settings >
  About lists it beside the local Gateway URL, which stays the address
  agents here are pointed at.
- In magpie web, a page served to other machines, the Gateway page's
  address and Settings' web-search MCP server show the public URL; in the
  app they stay local. Agents' own config wiring is unchanged (#373).
- New strings in zh, zh-TW, ja, de; docs/reference.md says where it shows.

Run: public-url-default.test.cjs (new) on chromium and webkit, en, zh,
zh-TW, ja, de, at 1100 and 420 wide, 26 pass; on the old app.js 12 of 13
chromium cases fail. Making the public URL override a kept pick turns it
red too. TestPublicURLInConsole and the new TestPublicURLOnlyWhereOffered
pass. Sibling GUI tests (connect-address-kept, the gateway-key-* and
gateway-connect/caller-keys tests, api-key-theme, public-url, lan-docker,
phone-web, search-mcp, settings-sections/groups, gui-ja/de/zh-tw): 192
pass; phone-web failed once under the parallel run and passes alone on
both engines. gofmt, GOOS=linux and GOOS=windows go vet -tags nogui
./internal/gui, go test -tags nogui ./... under the queue lock: its one red
is TestAccountDiscoveryOffReadsNoSignIn (internal/provider, from 8c5b9f928),
which fails on main without this change too (1 in ~20 runs alone).

Found on the way, not changed here: in magpie web at phone width, an
open Connect menu closes at once (a scroll of #view-gateway right after
it opens), with or without MAGPIE_PUBLIC_URL.
2026-10-11 11:04:21 +08:00

145 lines
4.8 KiB
Go

package gui
import (
"encoding/json"
"os"
"path/filepath"
"testing"
"time"
"github.com/yetone/magpie/internal/fx"
"github.com/yetone/magpie/internal/gateway"
"github.com/yetone/magpie/internal/settings"
"github.com/yetone/magpie/internal/testenv"
)
// Settings shows the local address agents use; the Gateway page advertises
// the public URL for its copy buttons and snippets.
func TestPublicURLInConsole(t *testing.T) {
h := t.TempDir()
testenv.SetHome(t, h)
t.Setenv("XDG_CONFIG_HOME", filepath.Join(h, ".config"))
t.Setenv("XDG_CACHE_HOME", filepath.Join(h, ".cache"))
t.Setenv("XDG_DATA_HOME", filepath.Join(h, ".local", "share"))
t.Setenv("MAGPIE_ADDR", "0.0.0.0:3425")
t.Setenv("MAGPIE_PUBLIC_URL", "https://magpie.example.com/magpie///")
// Seed the rate the Settings page reads, so it needs no network.
cache := fx.CachePath()
if err := os.MkdirAll(filepath.Dir(cache), 0o755); err != nil {
t.Fatal(err)
}
b, err := json.Marshal(fx.Rate{CNYPerUSD: 7.25, At: time.Now()})
if err != nil {
t.Fatal(err)
}
if err := os.WriteFile(cache, b, 0o644); err != nil {
t.Fatal(err)
}
fx.Reset()
t.Cleanup(fx.Reset)
if err := settings.Save(settings.Settings{LAN: true, LANKey: "sk-magpie-test"}); err != nil {
t.Fatal(err)
}
want := "https://magpie.example.com/magpie"
if got := settingsState().Gateway; got != gateway.URL() {
t.Errorf("Settings: %q, want %q", got, gateway.URL())
}
g := providersState().Gateway
if g.URL != gateway.URL() {
t.Errorf("Gateway address: %q, want the local %q", g.URL, gateway.URL())
}
if len(g.LANURLs) != 1 || g.LANURLs[0] != want {
t.Errorf("LAN addresses: %v, want [%s]", g.LANURLs, want)
}
if !g.OnNetwork {
t.Error("MAGPIE_ADDR 0.0.0.0 not said to be on the network")
}
// #1551: Connect starts at the public URL, and Settings shows it
if g.PublicURL != want {
t.Errorf("Gateway page's public URL: %q, want %q", g.PublicURL, want)
}
if got := settingsState().PublicURL; got != want {
t.Errorf("Settings' public URL: %q, want %q", got, want)
}
b, err = json.Marshal(g)
if err != nil {
t.Fatal(err)
}
var state map[string]any
if err := json.Unmarshal(b, &state); err != nil {
t.Fatal(err)
}
if state["lan"] != true {
t.Fatalf("LAN state missing from gateway page: %s", b)
}
}
// A gateway MAGPIE_ADDR puts on the network (the Docker image) asks other
// machines for a gateway key whether or not it is shared, so the Gateway
// page shows its keys and offers no key-less "this computer" then.
func TestGatewayOnNetworkInConsole(t *testing.T) {
h := t.TempDir()
testenv.SetHome(t, h)
t.Setenv("XDG_CONFIG_HOME", filepath.Join(h, ".config"))
t.Setenv("XDG_CACHE_HOME", filepath.Join(h, ".cache"))
t.Setenv("XDG_DATA_HOME", filepath.Join(h, ".local", "share"))
t.Setenv("MAGPIE_ADDR", "0.0.0.0:3425")
t.Setenv("MAGPIE_PUBLIC_URL", "")
if err := settings.Save(settings.Settings{LAN: false}); err != nil {
t.Fatal(err)
}
if g := providersState().Gateway; !g.OnNetwork || !g.LAN {
t.Errorf("gateway on the network, not shared, shown without its keys: %+v", g)
}
if err := settings.Save(settings.Settings{LAN: true}); err != nil {
t.Fatal(err)
}
if g := providersState().Gateway; !g.OnNetwork || !g.LAN {
t.Errorf("gateway on the network, shared: %+v", g)
}
t.Setenv("MAGPIE_ADDR", "127.0.0.1:3425")
if err := settings.Save(settings.Settings{LAN: false}); err != nil {
t.Fatal(err)
}
if g := providersState().Gateway; g.OnNetwork || g.LAN {
t.Errorf("gateway on loopback, not shared: %+v", g)
}
}
// #1551: MAGPIE_PUBLIC_URL is offered where other machines are let in,
// and nowhere else: on loopback, not shared, nothing reaches the gateway
// through it without a key it can't take.
func TestPublicURLOnlyWhereOffered(t *testing.T) {
h := t.TempDir()
testenv.SetHome(t, h)
t.Setenv("XDG_CONFIG_HOME", filepath.Join(h, ".config"))
t.Setenv("XDG_CACHE_HOME", filepath.Join(h, ".cache"))
t.Setenv("XDG_DATA_HOME", filepath.Join(h, ".local", "share"))
t.Setenv("MAGPIE_PUBLIC_URL", "https://magpie.example.com")
t.Setenv("MAGPIE_ADDR", "127.0.0.1:3425")
if err := settings.Save(settings.Settings{LAN: false}); err != nil {
t.Fatal(err)
}
if g := providersState().Gateway; g.PublicURL != "" {
t.Errorf("loopback, not shared: Gateway page offers %q", g.PublicURL)
}
if u := settingsState().PublicURL; u != "" {
t.Errorf("loopback, not shared: Settings shows %q", u)
}
if err := settings.Save(settings.Settings{LAN: true}); err != nil {
t.Fatal(err)
}
if g := providersState().Gateway; g.PublicURL != "https://magpie.example.com" {
t.Errorf("shared: Gateway page's public URL %q", g.PublicURL)
}
t.Setenv("MAGPIE_PUBLIC_URL", "")
if g := providersState().Gateway; g.PublicURL != "" {
t.Errorf("no MAGPIE_PUBLIC_URL: Gateway page's public URL %q", g.PublicURL)
}
if u := settingsState().PublicURL; u != "" {
t.Errorf("no MAGPIE_PUBLIC_URL: Settings shows %q", u)
}
}