Files
magpie/internal/provider/devin_cli_signed_out_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

103 lines
3.7 KiB
Go

package provider
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"
"github.com/yetone/magpie/internal/testenv"
)
// TestDevinSignedInOnlyInMagpie is 蓝猫's Devin on Discord: the CLI kept a
// sign-in Devin no longer took, so magpie signed the account in in a home
// of its own. Every request failed until `devin auth login`: the model
// list, which turns a family (claude-sonnet-5-5) into the variant Devin's
// API takes, was asked of the CLI's own sign-in only. And once the CLI
// signed in to the same account, it was listed twice.
func TestDevinSignedInOnlyInMagpie(t *testing.T) {
home := claudeHome(t)
data := filepath.Join(home, "data")
t.Setenv("XDG_DATA_HOME", data)
os.MkdirAll(filepath.Join(data, "devin"), 0o700)
cliSignIn := func(key string) {
t.Helper()
if err := os.WriteFile(filepath.Join(data, "devin", "credentials.toml"), devinCredentials(key, "", "", ""), 0o600); err != nil {
t.Fatal(err)
}
forgetDevinStatus()
}
// a CLI that answers for the key in its data folder's credentials.toml:
// an old account's, one Devin refused, or the account signed in (by
// magpie, or by `devin auth login` after)
exe := filepath.Join(home, "devin")
testenv.Program(t, exe, `#!/bin/sh
f="$XDG_DATA_HOME/devin/credentials.toml"
[ -f "$f" ] || { echo "Not logged in"; exit 1; }
if grep -q refused "$f"; then
if [ "$1" = models ]; then echo "Authentication required: Invalid token" >&2; exit 1; fi
`+devinKeyRefused+`
exit 0
fi
if [ "$1" = models ]; then
echo '{"families":[{"family_uid":"claude-sonnet-5-5","family_label":"Claude Sonnet 5.5","variants":[{"model_uid":"claude-sonnet-5-5-medium"},{"model_uid":"claude-sonnet-5-5-high"}]}]}'
exit 0
fi
if grep -q old "$f"; then who=old@example.com; else who=dev@example.com; fi
printf 'Logged in (via Devin).\n\nUser:\n Email: %s\n\nAccount:\n Tier: Devin Pro\n' "$who"
`)
fakeDevin(t, exe)
devinFamiliesCached(nil)
t.Cleanup(func() { devinFamiliesCached(nil) })
// the CLI was signed in to another account once, remembered as its own
cliSignIn("devin-session-token$old")
if ls := devinLogins(); len(ls) != 1 || ls[0].User != "old@example.com" || ls[0].Home != "" {
t.Fatalf("CLI's own %+v", ls)
}
// then Devin refused its key: nobody is signed in there
cliSignIn("devin-session-token$refused")
if ls := devinLogins(); len(ls) != 0 {
t.Fatalf("refused %+v", ls)
}
fake := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(map[string]any{"sessionToken": "devin-session-token$magpie"})
}))
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 != "done" || st.User != "dev@example.com" || !st.Using {
t.Fatalf("state %+v", st)
}
ls := devinLogins()
if len(ls) != 1 || ls[0].User != "dev@example.com" || ls[0].Home == "" {
t.Fatalf("signed in in magpie %+v", ls)
}
// a request for the family goes to Devin as its variant at the effort,
// read from the account magpie signed in
if got := DevinVariant(context.Background(), "claude-sonnet-5-5", "high"); got != "claude-sonnet-5-5-high" {
t.Fatalf("variant %q, want claude-sonnet-5-5-high", got)
}
// `devin auth login` to the same account: still one account
cliSignIn("devin-session-token$cli")
if ls := Logins("devin"); len(ls) != 1 || ls[0].User != "dev@example.com" || !ls[0].Active {
t.Fatalf("after the CLI signed in %+v", ls)
}
if p, ok := devinAccount(); !ok || p.Account.User != "dev@example.com" || p.AlsoOn() != nil {
t.Fatalf("account %+v also %+v", p.Account, p.AlsoOn())
}
}