macOS checks a newly written executable before its first exec: 0.27s alone, 2-5s while other new programs start (a go test ./... starting its test binaries). The same file again takes 0.009s, and the pass is kept across a rename and an in-place rewrite.108852b4worked round this for one test with /bin/sh -c; the class was every test that writes a `#!/bin/sh` fake and times its answer. testenv.Program(t, path, script) replaces os.WriteFile(path, script, 0o755) for fakes. On macOS it renames a file that has run once from a pool to path and writes the script into it in place; at the test's cleanup the file goes back to the pool (only a path the test created; a stand-in it wrote over stays). Fakes still end with their tests: nothing about how they are started or waited on changed. Run (and gateway's TestMain) remove the pool at the end. TestMain's PATH stand-ins are run once in the background (testenv.StandIns). Other systems write the file as before. Converted every `#!/bin/sh` fake written by tests under internal/ (agent, gateway, gui, library, plugin, proc, provider, update). Windows, binary and node/bun fakes are unchanged. Under the same load (3 loops each starting 8 freshly written scripts at a time), 4 rounds each, before (108852b4/ origin/main) vs after: - provider TestSavedButSignedOutClaudeServed: 4/4 fail (8.01s) -> 4/4 pass - provider TestClaudeSignInOverStaleCredentials: 4/4 fail (5.00s) -> 4/4 pass - provider TestDevinSignedInOnlyInMagpie: 3/4 fail ("CLI's own []") -> 4/4 pass - gateway TestClaudeRunEndsWhenTheClientLeaves: 4/4 fail -> 4/4 pass (and 3/3 fail -> 3/3 pass at a third of that load) - gateway TestClientGoneRecordedAsCanceled: 4/4 fail -> 4/4 pass - gui TestProvidersAnswerWhileListsComeIn ("cursor found false" at 3.03s): 4/4 fail -> 4/4 pass. The same class, not bfe8bcaf's test order. - proc TestUserPath (login shell's PATH never came, 7.01s): 4/4 fail -> 4/4 pass Unloaded, every touched package passes; no pool directory or process is left after a run.
165 lines
5.6 KiB
Go
165 lines
5.6 KiB
Go
package agent
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/edit"
|
|
"github.com/yetone/magpie/internal/testenv"
|
|
)
|
|
|
|
// #304: PI_CODING_AGENT_DIR moves Pi's agent folder (config.js,
|
|
// getAgentDir, "~" expanded), and a model set on Pi's row goes to
|
|
// settings.json and models.json there; ~/.pi is left alone. A relative
|
|
// one, or this machine's variable for a WSL distro's Pi, is not taken.
|
|
func TestPiAgentDir(t *testing.T) {
|
|
home := syncHome(t)
|
|
custom := filepath.Join(home, "dotfiles", "pi")
|
|
for _, c := range []struct{ env, want string }{
|
|
{"", filepath.Join(home, ".pi", "agent")},
|
|
{custom, custom},
|
|
{"~/dotfiles/pi", custom},
|
|
{"rel/pi", filepath.Join(home, ".pi", "agent")},
|
|
} {
|
|
t.Setenv("PI_CODING_AGENT_DIR", c.env)
|
|
if a := pi(home); a.Dir != c.want || a.Path != filepath.Join(c.want, "settings.json") {
|
|
t.Errorf("%q: %s", c.env, a.Dir)
|
|
}
|
|
}
|
|
|
|
t.Setenv("PI_CODING_AGENT_DIR", custom)
|
|
var a *Agent
|
|
for _, x := range All() {
|
|
if x.ID == "pi" {
|
|
a = x
|
|
}
|
|
}
|
|
if a == nil {
|
|
t.Fatal("no pi in All()")
|
|
}
|
|
os.MkdirAll(custom, 0o755)
|
|
os.WriteFile(filepath.Join(custom, "settings.json"), []byte(`{"theme": "dark", "defaultProvider": "xai", "defaultModel": "grok-4.6"}`), 0o644)
|
|
if f := a.Field("model"); f.Get() != "xai/grok-4.6" {
|
|
t.Fatalf("model %q", f.Get())
|
|
}
|
|
if err := a.Field("model").Set("magpie/relay/glm-4.6"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
settings := filepath.Join(custom, "settings.json")
|
|
for k, want := range map[string]string{"defaultProvider": "magpie", "defaultModel": "relay/glm-4.6", "theme": "dark"} {
|
|
if v, _ := edit.GetJSON(settings, k); v != want {
|
|
t.Fatalf("%s = %q:\n%s", k, v, readFile(settings))
|
|
}
|
|
}
|
|
if v, _ := edit.GetJSON(filepath.Join(custom, "models.json"), "providers.magpie.baseUrl"); v == "" {
|
|
t.Fatalf("models.json:\n%s", readFile(filepath.Join(custom, "models.json")))
|
|
}
|
|
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)
|
|
}
|
|
|
|
// a distro's Pi is at its home
|
|
root := t.TempDir()
|
|
d := distro{Name: "Ubuntu", Root: root, Home: "/home/me", Has: map[string]bool{"dir:.pi": true}, Mirrored: true, Running: true}
|
|
if w := wslAgent(wslKindOf("pi"), d); w.Dir != filepath.Join(root, "home", "me", ".pi", "agent") {
|
|
t.Fatalf("wsl: %s", w.Dir)
|
|
}
|
|
}
|
|
|
|
// omp reads PI_CODING_AGENT_DIR too, but only with no profile, and as
|
|
// given; PI_CONFIG_DIR renames ~/.omp and OMP_PROFILE (else PI_PROFILE)
|
|
// picks profiles/<name>/agent in it (pi-utils dirs.ts).
|
|
func TestOmpAgentDir(t *testing.T) {
|
|
home := syncHome(t)
|
|
custom := filepath.Join(home, "omp-here")
|
|
for _, c := range []struct{ agentDir, configDir, ompProfile, piProfile, want string }{
|
|
{"", "", "", "", filepath.Join(home, ".omp", "agent")},
|
|
{custom, "", "", "", custom},
|
|
{"~/omp-here", "", "", "", filepath.Join(home, ".omp", "agent")},
|
|
{"", ".omp-work", "", "", filepath.Join(home, ".omp-work", "agent")},
|
|
{custom, "", "work", "", filepath.Join(home, ".omp", "profiles", "work", "agent")},
|
|
{"", "", "", "home", filepath.Join(home, ".omp", "profiles", "home", "agent")},
|
|
{"", "", "default", "", filepath.Join(home, ".omp", "agent")},
|
|
{"", "", "Bad Name", "", filepath.Join(home, ".omp", "agent")},
|
|
} {
|
|
t.Setenv("PI_CODING_AGENT_DIR", c.agentDir)
|
|
t.Setenv("PI_CONFIG_DIR", c.configDir)
|
|
t.Setenv("OMP_PROFILE", c.ompProfile)
|
|
if c.ompProfile == "" {
|
|
os.Unsetenv("OMP_PROFILE")
|
|
}
|
|
t.Setenv("PI_PROFILE", c.piProfile)
|
|
if a := omp(home); a.Dir != c.want || a.Path != filepath.Join(c.want, "config.yml") {
|
|
t.Errorf("%+v: %s", c, a.Dir)
|
|
}
|
|
}
|
|
// OMP_PROFILE set but empty is the default profile, PI_PROFILE aside
|
|
t.Setenv("PI_CODING_AGENT_DIR", "")
|
|
t.Setenv("PI_CONFIG_DIR", "")
|
|
t.Setenv("OMP_PROFILE", "")
|
|
t.Setenv("PI_PROFILE", "home")
|
|
if a := omp(home); a.Dir != filepath.Join(home, ".omp", "agent") {
|
|
t.Errorf("empty OMP_PROFILE: %s", a.Dir)
|
|
}
|
|
|
|
t.Setenv("PI_CODING_AGENT_DIR", custom)
|
|
os.Unsetenv("OMP_PROFILE")
|
|
t.Setenv("PI_PROFILE", "")
|
|
if err := omp(home).Field("model").Set("magpie/relay/glm-4.6"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if v, _ := edit.GetYAML(filepath.Join(custom, "config.yml"), "modelRoles.default"); v != "magpie/relay/glm-4.6" {
|
|
t.Fatalf("config.yml:\n%s", readFile(filepath.Join(custom, "config.yml")))
|
|
}
|
|
if _, err := os.Stat(filepath.Join(home, ".omp")); !os.IsNotExist(err) {
|
|
t.Fatalf("~/.omp was touched: %v", err)
|
|
}
|
|
}
|
|
|
|
// PI_CODING_AGENT_DIR may name Pi's folder, which is no sign of omp: its
|
|
// config.yml, its own folder or its command is.
|
|
func TestOmpDetectedWithPiDir(t *testing.T) {
|
|
home := syncHome(t)
|
|
pi := filepath.Join(home, ".pi", "agent")
|
|
if err := os.MkdirAll(pi, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
bin := t.TempDir()
|
|
t.Setenv("PATH", bin)
|
|
t.Setenv("PI_CODING_AGENT_DIR", pi)
|
|
t.Setenv("PI_CONFIG_DIR", "")
|
|
os.Unsetenv("OMP_PROFILE")
|
|
t.Setenv("PI_PROFILE", "")
|
|
if omp(home).Detected() {
|
|
t.Error("Pi's folder is taken for omp's")
|
|
}
|
|
testenv.Program(t, filepath.Join(bin, "omp"), "#!/bin/sh\n")
|
|
if !omp(home).Detected() {
|
|
t.Error("omp's command is not")
|
|
}
|
|
os.Remove(filepath.Join(bin, "omp"))
|
|
if err := os.WriteFile(filepath.Join(pi, "config.yml"), []byte("modelRoles: {}\n"), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !omp(home).Detected() {
|
|
t.Error("omp's config.yml is not")
|
|
}
|
|
|
|
// with no variable, ~/.omp/agent is omp's own
|
|
os.Remove(filepath.Join(pi, "config.yml"))
|
|
t.Setenv("PI_CODING_AGENT_DIR", "")
|
|
if omp(home).Detected() {
|
|
t.Fatal("omp is here with nothing of it")
|
|
}
|
|
if err := os.MkdirAll(filepath.Join(home, ".omp", "agent"), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !omp(home).Detected() {
|
|
t.Error("~/.omp/agent is not taken for omp's")
|
|
}
|
|
}
|