Files
magpie/internal/agent/omo_test.go
T
yetone 27956002c3 agent: OmO's update runs the npm of the node it was installed with, not a system one first on magpie's PATH (#2021, ggbdpq)
OmO updates itself: magpie runs `omo update`, and omo-ai 5.1.32's
updateTarget (bin/lib/package-paths.js) then runs a bare
`npm i -g omo-ai@<v>` (or `bun add -g` for a bun install). The
updater for a self-updating CLI had no path, so that npm was whichever
came first on magpie's PATH. With OmO installed under an fnm/nvm node
and a system npm in /usr/local/bin, the update went to the system
prefix (or failed on its permissions), and the omo the agent runs
stayed old.

howInstalled's self branch now gives the updater the bin dir of the
package manager that holds the CLI (selfPath): for an npm install, the
prefix's bin that fromNodeModules already finds for npm-installed CLIs;
for bun or pnpm, the dir of that manager's binary. That dir goes first
on the update's PATH, as it does for npm updates.

Run: TestOmoUpdateTakesItsOwnNpm (new: a fake system npm that fails and
an fnm prefix's npm that records; on the old code the update ran the
system npm and failed with exit status 1), TestOmo*, TestHowInstalled*,
go test ./internal/agent (ok in 1783s at load average ~100;
a 15m timeout ran out under that load, on no failure),
gofmt -l, GOOS=linux and GOOS=windows go vet -tags nogui ./internal/agent.
2026-10-14 05:22:47 +08:00

179 lines
6.7 KiB
Go

package agent
import (
"encoding/json"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
"github.com/yetone/magpie/internal/edit"
"github.com/yetone/magpie/internal/gateway"
"github.com/yetone/magpie/internal/usage"
)
// OmO (#264) is an agent of its own, wired as Pi is but in ~/.omo/agent:
// a model through magpie sets settings.json's defaultProvider/defaultModel
// and magpie's provider in models.json there, the same block Pi gets, and
// Pi's ~/.pi is left alone; its own model takes magpie out again.
func TestOmo(t *testing.T) {
home := syncHome(t)
t.Setenv("PATH", t.TempDir()) // no omo on PATH: only its folder tells (#1646)
var a *Agent
for _, x := range All() {
if x.ID == "omo" {
a = x
}
}
if a == nil {
t.Fatal("no omo in All()")
}
dir := filepath.Join(home, ".omo", "agent")
settings, models := filepath.Join(dir, "settings.json"), filepath.Join(dir, "models.json")
if a.Name != "OmO" || a.Icon != "omo" || a.Bin != "omo" || a.Dir != dir || a.Path != settings || a.Detected() {
t.Fatalf("%+v", a)
}
os.MkdirAll(dir, 0o755)
os.WriteFile(settings, []byte(`{"theme": "dark", "defaultProvider": "xai", "defaultModel": "grok-4.6"}`), 0o644)
if !a.Detected() || a.Field("model").Get() != "xai/grok-4.6" {
t.Fatalf("detected %v, model %q", a.Detected(), a.Field("model").Get())
}
if err := a.Field("model").Set("magpie/relay/glm-4.6"); err != nil {
t.Fatal(err)
}
if err := a.Field("effort").Set("high"); err != nil {
t.Fatal(err)
}
for k, want := range map[string]string{"defaultProvider": "magpie", "defaultModel": "relay/glm-4.6", "defaultThinkingLevel": "high", "theme": "dark"} {
if v, _ := edit.GetJSON(settings, k); v != want {
t.Fatalf("%s = %q:\n%s", k, v, readFile(settings))
}
}
var file struct {
Providers map[string]map[string]any `json:"providers"`
}
if err := json.Unmarshal([]byte(readFile(models)), &file); err != nil {
t.Fatal(err)
}
got := file.Providers["magpie"]
if got["baseUrl"] != gateway.URL()+"/v1" || got["apiKey"] != gateway.Token {
t.Fatalf("models:\n%s", readFile(models))
}
want, _ := json.Marshal(magpieProviderJSON("pi"))
if b, _ := json.Marshal(got); string(b) != string(want) {
t.Fatalf("not Pi's block:\n got %s\nwant %s", b, want)
}
if c := a.Check(); c != "" {
t.Fatal(c)
}
if _, err := os.Stat(filepath.Join(home, ".pi")); !os.IsNotExist(err) {
t.Fatalf("~/.pi was touched: %v", err)
}
if err := a.Field("model").Set("xai/grok-4.6"); err != nil {
t.Fatal(err)
}
if p, _ := edit.GetJSON(settings, "defaultProvider"); p != "xai" {
t.Fatalf("own:\n%s", readFile(settings))
}
// its requests say omo/<version>
if got := usage.AgentOf("omo/5.1.4 (darwin; bun/1.3.0; arm64)"); got != "omo" {
t.Fatalf("user agent: %q", got)
}
}
// OMO_CODING_AGENT_DIR, else SENPI_CODING_AGENT_DIR, moves OmO's folder,
// as OmO's own agent-dir.js has it; a relative one is not taken.
func TestOmoDir(t *testing.T) {
home := syncHome(t)
own, senpi := filepath.Join(home, "own"), filepath.Join(home, "senpi")
for _, c := range []struct{ omo, senpi, want string }{
{"", "", filepath.Join(home, ".omo", "agent")},
{own, senpi, own},
{"", senpi, senpi},
{"rel/dir", "", filepath.Join(home, ".omo", "agent")},
} {
t.Setenv("OMO_CODING_AGENT_DIR", c.omo)
t.Setenv("SENPI_CODING_AGENT_DIR", c.senpi)
if a := omo(home); a.Dir != c.want || a.Path != filepath.Join(c.want, "settings.json") {
t.Errorf("%+v: %s", c, a.Dir)
}
}
// a distro's OmO is at its home, whatever this machine's says
t.Setenv("OMO_CODING_AGENT_DIR", own)
root := t.TempDir()
d := distro{Name: "Ubuntu", Root: root, Home: "/home/me", Has: map[string]bool{"dir:.omo": true}, Mirrored: true, Running: true}
a := wslAgent(wslKindOf("omo"), d)
if a.ID != "omo@wsl:Ubuntu" || a.Name != "OmO · WSL Ubuntu" || a.Dir != filepath.Join(root, "home", "me", ".omo", "agent") {
t.Fatalf("%+v", a)
}
}
// The WSL probe asks after OmO too.
func TestWSLProbeFindsOmo(t *testing.T) {
for _, want := range []string{`[ -d "$HOME/.omo" ] && echo dir:.omo`, `p=$(command -v omo 2>/dev/null || wslbin omo) && echo "bin:omo $p"`} {
if !strings.Contains(wslProbeScript, want) {
t.Errorf("probe lacks %q", want)
}
}
if d := parseProbe("U", "home:/home/me\nbin:omo\n"); d == nil || !wslFound(*d) {
t.Fatalf("%+v", d)
}
}
// omo --version names the engine after OmO's own version; omo update is
// its updater, however it was installed.
func TestOmoCLI(t *testing.T) {
if v := parseVersion("omo 5.1.4 (engine: senpi 2026.9.29-5)\n"); v != "5.1.4" {
t.Fatalf("version %q", v)
}
u := howInstalled(cliSpecs["omo"], "/home/me/.bun/bin/omo")
if u == nil || u.via != "self" || u.pkg != "omo-ai" || strings.Join(u.cmd, " ") != "/home/me/.bun/bin/omo update" {
t.Fatalf("%+v", u)
}
}
// omo update runs npm by its bare name, so it is given the npm (and node)
// of the prefix OmO was installed in first on PATH: a system node's npm in
// /usr/local/bin, which magpie's PATH holds before a version manager's
// (fnm), would install into a folder only root may change (#2021, ggbdpq).
func TestOmoUpdateTakesItsOwnNpm(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("symlinks and sh scripts")
}
tmp, _ := filepath.EvalSymlinks(t.TempDir())
ran := filepath.Join(tmp, "ran")
// the system's npm, found first on magpie's PATH
sys := filepath.Join(tmp, "usr/local/bin")
file(t, filepath.Join(sys, "npm"), "#!/bin/sh\necho system >> "+ran+"\nexit 1\n")
// fnm's node, where OmO is
prefix := filepath.Join(tmp, "fnm/node-versions/v24.21.0/installation")
file(t, filepath.Join(prefix, "bin/npm"), "#!/bin/sh\necho fnm >> "+ran+"\n")
file(t, filepath.Join(prefix, "bin/node"), "#!/bin/sh\n")
file(t, filepath.Join(prefix, "lib/node_modules/omo-ai/bin/omo.js"), "#!/bin/sh\n[ \"$1\" = update ] && npm i -g omo-ai@5.1.32\n")
omoBin := link(t, "../lib/node_modules/omo-ai/bin/omo.js", filepath.Join(prefix, "bin/omo"))
t.Setenv("PATH", sys+string(os.PathListSeparator)+"/usr/bin:/bin")
u := howInstalled(cliSpecs["omo"], omoBin)
if u == nil || u.via != "self" || u.path != filepath.Join(prefix, "bin") {
t.Fatalf("%+v", u)
}
if out, err := runUpdate(t.Context(), u); err != nil {
t.Fatalf("%v: %s", err, out)
}
if b, _ := os.ReadFile(ran); string(b) != "fnm\n" {
t.Fatalf("omo update ran npm of %q, want fnm's", b)
}
// bun's OmO (bun add -g) keeps bun's folder first the same way
bunHome := filepath.Join(tmp, ".bun")
file(t, filepath.Join(bunHome, "bin/bun"), "#!/bin/sh\n")
file(t, filepath.Join(bunHome, "install/global/node_modules/omo-ai/bin/omo.js"), "")
bunOmo := link(t, "../install/global/node_modules/omo-ai/bin/omo.js", filepath.Join(bunHome, "bin/omo"))
if u := howInstalled(cliSpecs["omo"], bunOmo); u == nil || u.via != "self" || u.path != filepath.Join(bunHome, "bin") {
t.Fatalf("bun: %+v", u)
}
}