Files
magpie/internal/gateway/omarchy_test.go
T
yetone 8f385e113b site: usemagpie.ai draws itself in Omarchy's look on Omarchy, in the theme Omarchy has on (owner: 整个官网和文档页面能否智能识别是不是 Omarchy 在访问,如果是 Omarchy 在访问,那么整个页面风格能否变成 Omarchy 风格?而且能够适配当前 Omarchy 就更牛逼了) — every page (home, /import, the docs in en/zh/ja) loads omarchy.js in a Linux desktop browser only; it tells Omarchy by its font (a page may load local("omarchy"), which Omarchy installs system-wide and nothing else has), remembers it, and from then on paints before the page shows: the theme's palette, the system monospace (Omarchy's font), square corners, no shadows or glass, a solid bar, buttons and what's chosen in the accent, images framed in it. Until magpie tells the theme it takes Omarchy's default (tokyo-night, or catppuccin-latte on a light desktop). magpie's gateway answers GET /v1/magpie/omarchy (name, mode, stamp and the CSS variables magpie's own windows use) to this machine only, readable only by usemagpie.ai (CORS + Private Network Access; MAGPIE_SITE_ORIGIN adds an origin to try the site locally), 404 off Omarchy. The page asks it by itself only once the browser allows it to reach this computer; a chip in the corner (Omarchy's logo and the theme) asks once on a click, lists the 22 built-in themes to pick one, and turns the look off; ?omarchy=1|0|<theme> sets it anywhere. Followed while the page shows: a theme set in Omarchy's menu comes through in about 3 s. Tried in the omarchy VM's Chromium 152 with this magpie (nogui serve): detection, the home page and zh docs in tokyo-night, the chip's menu, following the computer, and omarchy-theme-set catppuccin-latte repainting the open page light. TestOmarchyThemeForTheSite covers 404 off Omarchy, the theme and its CORS for usemagpie.ai only, LAN refused and the preflight. go vet, builds (darwin, GOOS=linux, GOOS=windows) and go test -tags nogui -count=1 ./... under a temp HOME pass. The permission prompt from the public site wasn't clicked through in a real browser.
2026-10-03 16:01:53 +08:00

77 lines
2.5 KiB
Go

package gateway
import (
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"
)
// usemagpie.ai on an Omarchy machine asks the magpie there for the theme it
// draws itself in: only from this machine, only readable by the site.
func TestOmarchyThemeForTheSite(t *testing.T) {
fresh(t)
t.Setenv("MAGPIE_ADDR", "")
dir := t.TempDir()
if err := os.WriteFile(filepath.Join(dir, "colors.toml"), []byte("mode = \"light\"\naccent = \"#1e66f5\"\nbackground = \"#eff1f5\"\nforeground = \"#4c4f69\"\n"), 0o644); err != nil {
t.Fatal(err)
}
t.Setenv("MAGPIE_OMARCHY_THEME", dir)
t.Setenv("MAGPIE_OMARCHY_FONT", "iA Writer Mono S")
t.Setenv("HYPRLAND_INSTANCE_SIGNATURE", "")
was := onOmarchy
t.Cleanup(func() { onOmarchy = was })
h := lanGuard(New().Handler())
call := func(method, from, origin string) *httptest.ResponseRecorder {
r := httptest.NewRequest(method, "/v1/magpie/omarchy", nil)
r.RemoteAddr = from
if origin != "" {
r.Header.Set("Origin", origin)
}
if method == "OPTIONS" {
r.Header.Set("Access-Control-Request-Method", "GET")
r.Header.Set("Access-Control-Request-Private-Network", "true")
}
w := httptest.NewRecorder()
h.ServeHTTP(w, r)
return w
}
onOmarchy = false
if w := call("GET", "127.0.0.1:5000", "https://usemagpie.ai"); w.Code != http.StatusNotFound {
t.Fatal("not on Omarchy:", w.Code, w.Body)
}
onOmarchy = true
w := call("GET", "127.0.0.1:5000", "https://usemagpie.ai")
if w.Code != 200 || w.Header().Get("Access-Control-Allow-Origin") != "https://usemagpie.ai" {
t.Fatal("the site, on this machine:", w.Code, w.Header(), w.Body)
}
var th struct {
Mode string
Vars map[string]string
}
if err := json.Unmarshal(w.Body.Bytes(), &th); err != nil {
t.Fatal(err)
}
if th.Mode != "light" || th.Vars["--bg"] != "#eff1f5" || th.Vars["--accent"] != "#1e66f5" || th.Vars["--om-radius"] != "0px" {
t.Fatal("theme:", th.Mode, th.Vars)
}
if f := th.Vars["--font"]; len(f) < 18 || f[:18] != `"iA Writer Mono S"` {
t.Fatal("font:", f)
}
if w := call("GET", "127.0.0.1:5000", "https://evil.example"); w.Header().Get("Access-Control-Allow-Origin") != "" {
t.Fatal("another site may read it:", w.Header())
}
if w := call("GET", "192.168.1.9:5000", ""); w.Code == 200 {
t.Fatal("another machine got it:", w.Code)
}
w = call("OPTIONS", "127.0.0.1:5000", "https://usemagpie.ai")
if w.Code != http.StatusNoContent || w.Header().Get("Access-Control-Allow-Private-Network") != "true" {
t.Fatal("preflight:", w.Code, w.Header())
}
}