Files
magpie/internal/provider/scheme_test.go
T
yetone 6055f626e8 address: a scheme already typed is kept once, in any letter case (eachg0 on X)
Remote magpie and System One addresses got "http://" put in front by
several normalisers that each tested for "://" their own way, and one
saved "http://http://host" stayed that way until the address was deleted
and added again.

provider.WithScheme (new, internal/provider/scheme.go) is the one rule:
trim, add the default scheme when none is there, lowercase http/https,
and collapse a repeated http/https prefix to the innermost one. Path and
trailing slash are left as written; "host:3425/x?u=http://y" has no
scheme. It is used by provider.normalize (every provider URL, on read),
remoteMagpieEndpoints, DetectDecide, parseAddress and gui remoteAddr.
app.js has withScheme as its mirror, used by hostOf, localAddress,
vertexRoot and respellURL (whose gemini/openai regexes now match any
letter case).

Old data: an address saved with a doubled scheme is repaired when it is
read (provider.normalize for providers and System One addresses,
loadRemote for remote-magpie.json). The file is written out correctly on
the next save; nobody has to delete and re-add. Reading alone does not
rewrite the file.

app.js: the core script budget is met without a raise;
withScheme was cut down to fit (final package run after the rebase on fd9033cbf: gui and provider ok).

Run:
- go test ./internal/provider ./internal/gui ./internal/settings
  ./internal/cli on mote: ok, except TestCoreScriptBudget (284 over),
  fixed by shortening the JS and re-run alone: ok.
- gofmt -l, go vet darwin, GOOS=windows, GOOS=linux -tags nogui: clean.
- GUI on mica, chromium and webkit: scheme-once, local-address,
  custom-systemone-url, decide-endpoint-label, azure-endpoint, remote-*,
  i18n-keys, connect-address-kept: 158 of 164 pass; the 6 reds are
  "Remote magpie asks for the other magpie's address", red the same at
  origin/main 5e933e153 (#6099).
- add-sheet-custom, provider-fetch-again, provider-gemini: 14 of 54 red
  on mica, the same 14 at origin/main ab98228cc (#5232, #6209).
Red on the old code: scheme_test.go, remote_test.go (Go) and
scheme-once.test.cjs (withScheme undefined) were run against an
origin/main copy and failed there.

Not tried: the place that produced "http://http://" was not found by
reading (every guard used Contains("://")), so this hardens every
normaliser instead of one site; no real remote magpie was used; the full
GUI suite was not run.
2026-11-12 19:48:02 +08:00

75 lines
2.6 KiB
Go

package provider
import "testing"
func TestWithScheme(t *testing.T) {
for in, want := range map[string]string{
"192.168.1.20:3425": "http://192.168.1.20:3425",
"http://192.168.1.20:3425": "http://192.168.1.20:3425",
"HTTP://192.168.1.20:3425/": "http://192.168.1.20:3425/",
"Http://h:1/v1/": "http://h:1/v1/",
"https://h": "https://h",
"HTTPS://h/": "https://h/",
"http://http://192.168.1.20:3425": "http://192.168.1.20:3425",
"http://http://h:1/": "http://h:1/",
"https://https://h": "https://h",
"HTTP://HTTP://h": "http://h",
" http://h:1 ": "http://h:1",
"localhost:3425/x?u=http://y": "http://localhost:3425/x?u=http://y",
"": "",
} {
if got := WithScheme(in, "http"); got != want {
t.Errorf("WithScheme(%q) = %q, want %q", in, got, want)
}
}
}
// A Remote magpie's address typed with its scheme, in any case, with or
// without the slash, is one scheme in every URL; one saved twice by an older
// editor reads as one (eachg0 on X).
func TestRemoteMagpieSchemeOnce(t *testing.T) {
for _, in := range []string{
"http://192.168.1.20:3425", "HTTP://192.168.1.20:3425/", "Http://192.168.1.20:3425/v1",
"http://http://192.168.1.20:3425", "http://http://192.168.1.20:3425/v1/", "192.168.1.20:3425",
} {
for _, field := range []string{"chat", "decide", "anthropic"} {
p := Provider{ID: "r", Preset: RemoteMagpiePreset}
switch field {
case "chat":
p.Chat, p.Responses = in, in
case "decide":
p.Decide = in
case "anthropic":
p.Anthropic = in
}
p = normalize(p)
for name, got := range map[string]string{"chat": p.Chat, "responses": p.Responses, "decide": p.Decide, "anthropic": p.Anthropic} {
want := "http://192.168.1.20:3425"
if name != "anthropic" {
want += "/v1"
}
if got != want {
t.Errorf("%s=%q: %s = %q, want %q", field, in, name, got, want)
}
}
}
}
}
// A custom provider's System One address with its scheme already there.
func TestDecideSchemeOnce(t *testing.T) {
p := normalize(Provider{ID: "c", Chat: "https://api.example.com/v1", Decide: "HTTPS://https://api.example.com/v1/"})
if p.Decide != "https://api.example.com/v1" {
t.Errorf("Decide = %q", p.Decide)
}
}
func TestAddressSchemeOnce(t *testing.T) {
for _, in := range []string{"HTTP://h:1/", "http://http://h:1", "http://h:1"} {
u, err := parseAddress(in)
if err != nil || u.String() != "http://h:1" {
t.Errorf("parseAddress(%q) = %v, %v", in, u, err)
}
}
}