Review of Leon Zhao's Lody import (the two commits before this):
- Detection: a bare ~/.lody or ~/.lody-oss another tool made counted as
Lody. Now the home folder counts only with an entry Lody makes there
(workspace-catalog.json, loro-repo, agent-binaries, code-collab-v2, all
in the owner's real ~/.lody). A Lody.app counts only with an ai.lody.
bundle id (the real /Applications/Lody.app is ai.lody.desktop, and
registers the lody scheme) or a plist that isn't XML. Electron's data
folder under os.UserConfigDir (Application Support / %APPDATA% /
~/.config, "Lody", "Lody OSS", "Lody Nightly") still counts as before.
A never-opened install on Windows or Linux has no folder and isn't
shown; said in agent-wiring.md.
- Security: TestLodyLinkCarriesNoSecret turns LAN sharing on and checks
the link holds magpie-lody and never the LAN key, every endpoint is
loopback http, and MAGPIE_ADDR pinned beyond loopback gives no link at
all (0.0.0.0, 127.0.0.1 and [::1] still give one).
- `magpie lody add` with no link (a LAN-pinned gateway) said it opened
Lody and opened ""; it now says why and opens nothing.
- Added *bool: every consumer re-read. The CLI and TUI use agent.Agent's
func (nil-safe), the GUI reads a.added == null, and Cindy's false is now
sent as "added":false. TestLodyImportHandoff also checks Cindy's row
says false and Lody's leaves added out; the handoff test's ~/.lody
holds a catalog file now and checks it is left byte for byte.
- The hint "Opens {name} to add magpie as a provider — confirm it there"
was already translated in zh, zh-TW, ja and de (the import button's
tooltip). import-row now runs in all five languages, asserts Lody's
exact translated line and that no added/not-added wording shows, and
checks both import rows fit a 420px window with no sideways scroll
(waiting for the header's segments to settle after the resize, which
overflow for ~300ms in WebKit en on main too).
Run (on origin/main 4a6bf80f4 + Leon's two; after rebasing onto
dcf292cc8, a routing-only CSS change, import-row, agent-layout,
group-editor-align and i18n-keys were run again in both engines, 47/47,
with the Lody/Cindy/Import Go tests in agent and gui, ok):
- go test -tags nogui -count=1 -timeout 45m ./... under a sandbox HOME
(scratchpad dir, XDG_* there too, Go caches pinned): 64 packages ok,
exit 0 (agent 607s, gateway 674s, gui 130s, provider 219s).
- gofmt -l .: clean. go vet: darwin ./..., GOOS=linux -tags nogui ./...,
GOOS=windows ./...: clean.
- GUI, playwright 1.62.0, Chromium and WebKit: import-row 20/20 (5
languages x added/not x 2 engines, 960/700/420px); i18n-keys, gui-de,
gui-ja, gui-zh-tw, agent-layout, agent-connect, plugin-agent,
agent-cli-missing, agent-connected-unfolded, agent-bulk-visibility,
agents-hidden-last: 116/116.
Broken by hand, each went red, then restored:
- a bare ~/.lody counting again: TestLodyDetectionAndRegistration;
- any Lody.app counting: TestLodyBundle (com.example.lody, ai.lodyx.);
- the loopback switch removed: TestLodyLinkCarriesNoSecret (LAN-pinned
addresses got a link);
- the key taken from agentKeyAt at a LAN address: the LAN key showed in
the link, red;
- Cindy's false left out of state(): TestLodyImportHandoff;
- app.js's unknown-status branch taken out: import-row red in every
language; the de translation removed: import-row de red.
Not tried: Lody opening the link for real (Leon ran Lody's parser), and
Windows or Linux Lody installs.
241 lines
7.6 KiB
Go
241 lines
7.6 KiB
Go
package agent
|
|
|
|
import (
|
|
"encoding/base64"
|
|
"encoding/json"
|
|
"net"
|
|
"net/url"
|
|
"os"
|
|
"path/filepath"
|
|
"reflect"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/access"
|
|
"github.com/yetone/magpie/internal/gateway"
|
|
)
|
|
|
|
func TestLodyLink(t *testing.T) {
|
|
for _, base := range []string{"http://127.0.0.1:3425", "http://localhost:4567", "http://[::1]:4567"} {
|
|
t.Run(base, func(t *testing.T) {
|
|
u, err := url.Parse(LodyLink(base))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if u.Scheme != "lody" || u.Host != "provider" || u.Path != "/import" || u.Query().Get("v") != "1" || len(u.Query()) != 2 {
|
|
t.Fatalf("invalid import URL: %s", u)
|
|
}
|
|
data, err := base64.RawURLEncoding.DecodeString(u.Query().Get("data"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var got map[string]any
|
|
if err := json.Unmarshal(data, &got); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
want := map[string]any{
|
|
"kind": "custom", "id": "magpie", "name": "Magpie",
|
|
"auth": map[string]any{"method": "apiKey", "apiKey": "magpie-lody"},
|
|
"endpoints": []any{
|
|
map[string]any{"protocol": "anthropic-messages", "baseUrl": base, "targets": []any{"claude-code"}, "modelsUrl": base + "/v1/models"},
|
|
map[string]any{"protocol": "openai-responses", "baseUrl": base + "/v1", "targets": []any{"codex"}, "modelsUrl": base + "/v1/models"},
|
|
map[string]any{"protocol": "openai-chat", "baseUrl": base + "/v1", "targets": []any{"pi", "dsh", "kimi-code", "grok", "bub"}, "modelsUrl": base + "/v1/models"},
|
|
},
|
|
}
|
|
if !reflect.DeepEqual(got, want) {
|
|
t.Fatalf("wrong Lody envelope: %s", data)
|
|
}
|
|
if LodyLink(base+"/") != LodyLink(base) {
|
|
t.Fatal("trailing slash changed the endpoint")
|
|
}
|
|
})
|
|
}
|
|
for raw, canonical := range map[string]string{"http://localhost:80": "http://localhost", "http://127.0.0.1:03425": "http://127.0.0.1:3425"} {
|
|
if LodyLink(raw) != LodyLink(canonical) {
|
|
t.Errorf("%s must use canonical origin %s", raw, canonical)
|
|
}
|
|
}
|
|
for _, base := range []string{"", "https://127.0.0.1:3425", "http://192.168.1.2:3425", "http://example.com", "http://127.0.0.1.evil.test", "http://user:secret@127.0.0.1", "http://127.0.0.1/v1", "http://localhost?token=secret", "http://localhost#fragment", "http://localhost?", "http://localhost:0", "http://localhost:65536", "http://localhost:", "http://::1"} {
|
|
if got := LodyLink(base); got != "" {
|
|
t.Errorf("unsupported gateway %q generated %s", base, got)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestLodyDetectionAndRegistration(t *testing.T) {
|
|
home := t.TempDir()
|
|
t.Setenv("HOME", home)
|
|
t.Setenv("USERPROFILE", home)
|
|
t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, "config"))
|
|
t.Setenv("APPDATA", filepath.Join(home, "config"))
|
|
a := lody(home)
|
|
if a.Detected() {
|
|
t.Fatal("detected Lody in an empty home")
|
|
}
|
|
for _, name := range []string{".lody", ".lody-oss"} {
|
|
dir := filepath.Join(home, name)
|
|
// a bare folder of that name another tool made isn't Lody
|
|
if err := os.MkdirAll(filepath.Join(dir, "bin"), 0700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if a.Detected() {
|
|
t.Fatalf("a %s without Lody's own entries counted as Lody", name)
|
|
}
|
|
for _, m := range lodyMarks {
|
|
if err := os.WriteFile(filepath.Join(dir, m), nil, 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !a.Detected() {
|
|
t.Fatalf("missed %s with %s", name, m)
|
|
}
|
|
if err := os.Remove(filepath.Join(dir, m)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if err := os.RemoveAll(dir); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
cfg, err := os.UserConfigDir()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, name := range lodyNames {
|
|
dir := filepath.Join(cfg, name)
|
|
if err := os.MkdirAll(dir, 0700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !a.Detected() {
|
|
t.Fatalf("missed Lody's data folder %s", dir)
|
|
}
|
|
if err := os.Remove(dir); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if a.Detected() {
|
|
t.Fatal("still detected after its folders went")
|
|
}
|
|
if a.Added != nil || len(a.Fields) != 0 || a.Sync != nil || a.Unwire != nil {
|
|
t.Fatal("import adapter must not infer status or edit Lody settings")
|
|
}
|
|
for _, a := range All() {
|
|
if a.ID == "lody" {
|
|
if a.Import == nil || a.Name != "Lody" || a.Icon != "lody" {
|
|
t.Fatalf("bad registered adapter: %+v", a)
|
|
}
|
|
return
|
|
}
|
|
}
|
|
t.Fatal("Lody missing from Agents")
|
|
}
|
|
|
|
// A Lody.app is Lody's only with its bundle id (ai.lody.desktop on the
|
|
// real app) or a plist that says nothing; another vendor's Lody.app isn't.
|
|
func TestLodyBundle(t *testing.T) {
|
|
plist := func(id string) string {
|
|
return `<?xml version="1.0" encoding="UTF-8"?><plist version="1.0"><dict><key>CFBundleIdentifier</key><string>` + id + `</string></dict></plist>`
|
|
}
|
|
for _, c := range []struct {
|
|
name, plist string
|
|
want bool
|
|
}{
|
|
{"Lody", plist("ai.lody.desktop"), true},
|
|
{"Lody Nightly", plist("ai.lody.desktop.nightly"), true},
|
|
{"Lody", "bplist00", true},
|
|
{"Lody", plist("com.example.lody"), false},
|
|
{"Lody", plist("ai.lodyx.desktop"), false},
|
|
{"Lodyssey", plist("ai.lody.desktop"), false},
|
|
} {
|
|
dir := t.TempDir()
|
|
contents := filepath.Join(dir, c.name+".app", "Contents")
|
|
if err := os.MkdirAll(contents, 0700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(contents, "Info.plist"), []byte(c.plist), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := lodyBundle([]string{dir}); got != c.want {
|
|
t.Errorf("%s.app with %q: %v, want %v", c.name, c.plist, got, c.want)
|
|
}
|
|
}
|
|
if lodyBundle([]string{t.TempDir()}) {
|
|
t.Error("no bundle counted as Lody")
|
|
}
|
|
}
|
|
|
|
// Lody's link carries only the public magpie-lody token and a loopback
|
|
// gateway, with LAN sharing on too; with MAGPIE_ADDR pinned beyond
|
|
// loopback, where that token is turned away, there is no link at all
|
|
// rather than one carrying the LAN key.
|
|
func TestLodyLinkCarriesNoSecret(t *testing.T) {
|
|
home := t.TempDir()
|
|
t.Setenv("HOME", home)
|
|
t.Setenv("USERPROFILE", home)
|
|
t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config"))
|
|
t.Setenv("MAGPIE_ADDR", "")
|
|
if err := access.ConfigureLAN(true, false); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { access.ConfigureLAN(false, false) })
|
|
lan := access.LANSecret()
|
|
if !strings.HasPrefix(lan, access.Prefix) {
|
|
t.Fatalf("LAN key %q", lan)
|
|
}
|
|
link := LodyLink(gateway.URL())
|
|
if link == "" {
|
|
t.Fatalf("no link for the loopback gateway %s", gateway.URL())
|
|
}
|
|
u, err := url.Parse(link)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
data, err := base64.RawURLEncoding.DecodeString(u.Query().Get("data"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if strings.Contains(link, lan) || strings.Contains(string(data), lan) {
|
|
t.Fatalf("the LAN key went into Lody's link: %s", data)
|
|
}
|
|
var got struct {
|
|
Auth struct {
|
|
APIKey string `json:"apiKey"`
|
|
} `json:"auth"`
|
|
Endpoints []struct {
|
|
BaseURL string `json:"baseUrl"`
|
|
ModelsURL string `json:"modelsUrl"`
|
|
} `json:"endpoints"`
|
|
}
|
|
if err := json.Unmarshal(data, &got); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got.Auth.APIKey != gateway.TokenFor("lody") || got.Auth.APIKey != "magpie-lody" {
|
|
t.Fatalf("key %q, want magpie-lody", got.Auth.APIKey)
|
|
}
|
|
if len(got.Endpoints) == 0 {
|
|
t.Fatal("no endpoints")
|
|
}
|
|
for _, e := range got.Endpoints {
|
|
for _, s := range []string{e.BaseURL, e.ModelsURL} {
|
|
p, err := url.Parse(s)
|
|
if err != nil || p.Scheme != "http" || !loopbackHost(p.Hostname()) {
|
|
t.Fatalf("endpoint %q is not loopback", s)
|
|
}
|
|
}
|
|
}
|
|
for addr, want := range map[string]bool{"172.23.80.1:3425": false, "192.168.1.2:3425": false, "0.0.0.0:3425": true, "127.0.0.1:4567": true, "[::1]:4567": true} {
|
|
t.Setenv("MAGPIE_ADDR", addr)
|
|
gw := gateway.URL()
|
|
if got := LodyLink(gw) != ""; got != want {
|
|
t.Errorf("MAGPIE_ADDR %s (%s): link %v, want %v", addr, gw, got, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func loopbackHost(h string) bool {
|
|
if h == "localhost" {
|
|
return true
|
|
}
|
|
ip := net.ParseIP(h)
|
|
return ip != nil && ip.IsLoopback()
|
|
}
|