Files
magpie/internal/gui/web_applink_host_test.go
T
yetone 11a7e50bd4 gui: Add magpie for Cindy and the other apps opens their link on the host (Jealousy_Du on X)
In a browser tab (magpie web, or the app served to a browser) api("open")
always ran window.open in the viewer's browser, so a cindy:// import link
opened the viewer's own Cindy while the "added" status is read from the
host's Cindy database. App import links now go to /api/open and webHost
opens them with the host's system opener (openOnHost); http(s) links stay
the page's. Fixes Cindy, Lody, AionUi and Cherry Studio rows alike.

Run (mote, at this head): go test -tags nogui ./internal/gui ok; go vet nogui,
windows, darwin ok; gofmt clean; GUI import-web-host, import-row, click-scroll,
i18n-keys: 27 pass / 0 fail in chromium and in webkit.
Broken by hand: old app.js, new import-web-host test in chromium: red (the
page tried to launch cindy:// itself, no /api/open request). The Go test does
not compile on the old web.go (no openOnHost).
Not tried: a real remote host with a real Cindy; the old-code run in webkit.
2026-11-12 20:09:27 +08:00

41 lines
1.2 KiB
Go

package gui
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/yetone/magpie/internal/agent"
)
// An app's import link from a page in a browser is opened on the computer
// magpie runs on, where the app's database "added" reads is, not left to the
// viewing browser, which may be another machine's (Jealousy_Du on X).
func TestWebOpensAppLinkOnTheHost(t *testing.T) {
sandboxHome(t)
oldLink, oldOpen := appLink, openOnHost
t.Cleanup(func() { appLink, openOnHost = oldLink, oldOpen })
appLink = func(link string, open func(string) error) error { return open(link) }
var got []string
openOnHost = func(u string) error {
if strings.HasPrefix(u, "http") {
return nil
}
got = append(got, u)
return nil
}
link := agent.CindyScheme + "v=1&data=x"
rec := httptest.NewRecorder()
webPageHandler(t, webHost{}, nil).ServeHTTP(rec, httptest.NewRequest("POST", "/api/open", strings.NewReader(`{"url":"`+link+`"}`)))
if rec.Code != http.StatusNoContent || len(got) != 1 || got[0] != link {
t.Fatalf("open: %d %s, opened on the host: %v", rec.Code, rec.Body, got)
}
}
func TestOpenOnHostLeavesWebLinksToThePage(t *testing.T) {
if err := webHost.OpenURL(webHost{}, "https://example.invalid/x"); err != nil {
t.Fatal(err)
}
}