Files
magpie/internal/provider/install_test.go
T
yetone 06420a7121 tests: a fake program is a file macOS has already let run (testenv.Program), so a fake's first start no longer eats a test's wait under load
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. 108852b4 worked 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.
2026-10-07 03:00:47 +08:00

163 lines
4.9 KiB
Go

package provider
import (
"context"
"errors"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/yetone/magpie/internal/testenv"
)
// a sign-in that needs a CLI this machine lacks installs it, then goes on
func TestSignInInstallsMissingCLI(t *testing.T) {
if os.PathSeparator != '/' {
t.Skip("the fake devin is a shell script")
}
home := claudeHome(t)
t.Setenv("XDG_DATA_HOME", filepath.Join(home, "data"))
exe := filepath.Join(home, "bin", "devin")
oldExe := DevinExecutable
DevinExecutable = func() string {
if isFile(exe) {
return exe
}
return ""
}
t.Cleanup(func() { DevinExecutable = oldExe })
release := make(chan struct{})
var ran string
oldRun := runInstaller
runInstaller = func(ctx context.Context, c agentCLI) ([]byte, error) {
ran = c.sh
<-release
os.MkdirAll(filepath.Dir(exe), 0o755)
testenv.Program(t, exe, "#!/bin/sh\nprintf 'Logged in (via Devin).\\n Email: dev@example.com\\n Tier: Devin Pro\\n'\n")
// as Devin's does: `devin setup` gives up on its sign-in without a terminal
return []byte("Installed devin\nError: Login canceled\n"), errors.New("exit status 1")
}
t.Cleanup(func() { runInstaller = oldRun })
fake := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`{"sessionToken":"devin-session-token$sk-test"}`))
}))
defer fake.Close()
oldTok := devinExchangeURL
devinExchangeURL = fake.URL
t.Cleanup(func() { devinExchangeURL = oldTok })
st, err := StartSignIn("devin")
if err != nil {
t.Fatal(err)
}
if st.State != "installing" || st.Installing != "Devin CLI" || st.URL != "" {
t.Fatalf("started %+v", st)
}
close(release)
st = waitPast(t, st.ID, "installing")
if st.State != "waiting" || !strings.HasPrefix(st.URL, devinAuthorizeURL) || st.Installing != "" {
t.Fatalf("after install %+v", st)
}
if !strings.Contains(ran, "cli.devin.ai/install.sh") {
t.Fatalf("installer %q", ran)
}
finishInBrowser(t, st, "dv-code")
if st = waitDone(t, st.ID); st.State != "done" || st.User != "dev@example.com" {
t.Fatalf("state %+v", st)
}
}
// an installer that fails says so, and how to install it by hand
func TestSignInInstallFails(t *testing.T) {
claudeHome(t)
oldExe := DevinExecutable
DevinExecutable = func() string { return "" }
t.Cleanup(func() { DevinExecutable = oldExe })
oldRun := runInstaller
runInstaller = func(ctx context.Context, c agentCLI) ([]byte, error) {
return []byte("downloading…\n\x1b[0;31mError: Failed to download\x1b[0m\n"), errors.New("exit status 1")
}
t.Cleanup(func() { runInstaller = oldRun })
st, err := StartSignIn("devin")
if err != nil {
t.Fatal(err)
}
st = waitPast(t, st.ID, "installing")
if st.State != "failed" || !strings.Contains(st.Error, "Error: Failed to download") || !strings.Contains(st.Error, "install it yourself") {
t.Fatalf("state %+v", st)
}
}
// canceled while installing: the sign-in never starts
func TestSignInCanceledWhileInstalling(t *testing.T) {
claudeHome(t)
oldExe := DevinExecutable
DevinExecutable = func() string { return "" }
t.Cleanup(func() { DevinExecutable = oldExe })
stopped := make(chan struct{})
oldRun := runInstaller
runInstaller = func(ctx context.Context, c agentCLI) ([]byte, error) {
<-ctx.Done()
close(stopped)
return nil, ctx.Err()
}
t.Cleanup(func() { runInstaller = oldRun })
st, _ := StartSignIn("devin")
CancelSignIn(st.ID)
select {
case <-stopped:
case <-time.After(5 * time.Second):
t.Fatal("the installer went on")
}
if st, _ = SignInStatus(st.ID); st.State != "canceled" {
t.Fatalf("state %+v", st)
}
}
// signed in, but the CLI can't see it: a failure, not "done" with no one
func TestDevinSignInUnseen(t *testing.T) {
if os.PathSeparator != '/' {
t.Skip("the fake devin is a shell script")
}
home := claudeHome(t)
t.Setenv("XDG_DATA_HOME", filepath.Join(home, "data"))
exe := filepath.Join(home, "devin")
testenv.Program(t, exe, "#!/bin/sh\necho 'Not logged in.'\n")
oldExe := DevinExecutable
DevinExecutable = func() string { return exe }
t.Cleanup(func() { DevinExecutable = oldExe })
fake := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`{"sessionToken":"devin-session-token$sk-test"}`))
}))
defer fake.Close()
oldTok := devinExchangeURL
devinExchangeURL = fake.URL
t.Cleanup(func() { devinExchangeURL = oldTok })
st, err := StartSignIn("devin")
if err != nil {
t.Fatal(err)
}
finishInBrowser(t, st, "dv-code")
if st = waitDone(t, st.ID); st.State != "failed" || !strings.Contains(st.Error, "devin auth status") {
t.Fatalf("state %+v", st)
}
}
func waitPast(t *testing.T, id, state string) SignInState {
t.Helper()
for end := time.Now().Add(5 * time.Second); time.Now().Before(end); time.Sleep(20 * time.Millisecond) {
if st, _ := SignInStatus(id); st.State != state {
return st
}
}
t.Fatalf("still %s", state)
return SignInState{}
}