TestDshHomePickSurvivesSyncAndEffort failed under full-suite load and
passed alone. The cause is cliIdentity: a look starts an ask of the CLI
(a failing testenv stand-in) and, once the answer is a minute old, a
refresh in a goroutine. keep() writes cli-identity.json under the
XDG_CONFIG_HOME current when it finishes, so an ask slow under load,
still going when its test ended, wrote into the config folder of the
test running then while t.TempDir's cleanup was removing it:
"TempDir RemoveAll cleanup: ... directory not empty".
4ee3a76b fixed this for internal/gateway alone (KeepCLIIdentities=false
in its TestMain). internal/agent, and every other package whose tests
read the accounts (., access, backup, davsync, gui, profile, sessions,
stats, tui, usage), still could. keep() now skips in any binary testenv
isolated (MAGPIE_TEST_SANDBOX, as internal/proc's sandboxVar reads it),
and the provider tests of the file itself turn keeping back on with
keepIdentities(t). The gateway's own switch and its test are gone;
TestCLIIdentityNotKeptInATestBinary covers every package.
Ran:
- Reproduced by injection (get refreshes on every look, 15ms sleep after
c.ask()), agent Dsh tests -count=3: old code red 2 of 6 runs with the
exact TempDir cleanup error; new code 0 of 6.
- Broke by hand (keepsIdentities() returns true):
TestCLIIdentityNotKeptInATestBinary red, "written in a test binary".
- go test -count=1 ./internal/provider ./internal/agent ./internal/gateway: ok
- gofmt -l clean; go vet provider, agent, gateway on darwin,
GOOS=linux -tags nogui, GOOS=windows: clean.
110 lines
3.7 KiB
Go
110 lines
3.7 KiB
Go
package provider
|
|
|
|
import (
|
|
"errors"
|
|
"path/filepath"
|
|
"runtime"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/testenv"
|
|
)
|
|
|
|
// an ask that couldn't tell leaves the account served, on disk too, where
|
|
// it had dropped Cursor from the Providers page and routing (#154); a CLI
|
|
// sure nobody is signed in still signs it out
|
|
func TestCLIIdentityUnsureKeepsAnswer(t *testing.T) {
|
|
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
|
|
keepIdentities(t)
|
|
c := &cliIdentity{name: "x", exe: func() string { return "/bin/sh" },
|
|
ask: func() (string, string, bool, error) { return "me@example.com", "Pro", true, nil }}
|
|
if _, _, ok := c.get(); !ok {
|
|
t.Fatal("never signed in")
|
|
}
|
|
ask := func(u string, ok bool, err error) {
|
|
t.Helper()
|
|
c.Lock()
|
|
c.ask = func() (string, string, bool, error) { return u, "", ok, err }
|
|
c.at = time.Now().Add(-2 * time.Minute) // due to be asked again
|
|
done := c.refresh()
|
|
c.Unlock()
|
|
<-done
|
|
}
|
|
ask("", false, errors.New("signal: killed"))
|
|
if u, p, ok := c.get(); !ok || u != "me@example.com" || p != "Pro" {
|
|
t.Fatalf("a failed ask dropped the account: %q %q %v", u, p, ok)
|
|
}
|
|
if k := readIdentities()["x"]; !k.OK || k.User != "me@example.com" {
|
|
t.Fatalf("a failed ask was kept: %+v", k)
|
|
}
|
|
c.Lock()
|
|
again := time.Since(c.at) < time.Minute
|
|
c.Unlock()
|
|
if !again {
|
|
t.Fatal("a failed ask is asked again at once, every look")
|
|
}
|
|
ask("", false, nil)
|
|
if _, _, ok := c.get(); ok {
|
|
t.Fatal("signed out, still served")
|
|
}
|
|
if k := readIdentities()["x"]; k.OK {
|
|
t.Fatalf("signed out, still kept: %+v", k)
|
|
}
|
|
}
|
|
|
|
func TestParseCursorAbout(t *testing.T) {
|
|
for _, c := range []struct {
|
|
out, user, plan string
|
|
said bool
|
|
}{
|
|
{`{"userEmail":"me@example.com","subscriptionTier":"Pro"}`, "me@example.com", "Pro", true},
|
|
{"\x1b[33mA new version is available\x1b[0m\n{\"userEmail\":\" me@example.com \",\"subscriptionTier\":\"Ultra\"}\n", "me@example.com", "Ultra", true},
|
|
{"{\n \"userEmail\": \"me@example.com\"\n}", "me@example.com", "", true},
|
|
{`{"userEmail":null,"subscriptionTier":"Free"}`, "", "Free", true},
|
|
{"", "", "", false},
|
|
{"Error: network request failed", "", "", false},
|
|
{`{"userEmail":"me@exa`, "", "", false},
|
|
} {
|
|
u, p, said := parseCursorAbout([]byte(c.out))
|
|
if u != c.user || p != c.plan || said != c.said {
|
|
t.Errorf("%q: %q %q %v, want %q %q %v", c.out, u, p, said, c.user, c.plan, c.said)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestAskCursorStatus(t *testing.T) {
|
|
if runtime.GOOS == "windows" {
|
|
t.Skip("fake CLI is a shell script")
|
|
}
|
|
home := t.TempDir()
|
|
t.Setenv("HOME", home)
|
|
t.Setenv("USERPROFILE", home)
|
|
exe := filepath.Join(home, "cursor-agent")
|
|
wasExe, wasOut := CursorExecutable, cursorSignedOut
|
|
t.Cleanup(func() { CursorExecutable, cursorSignedOut = wasExe, wasOut })
|
|
CursorExecutable = func() string { return exe }
|
|
signedOut := false
|
|
cursorSignedOut = func() bool { return signedOut }
|
|
|
|
for _, c := range []struct {
|
|
name, script string
|
|
out bool // the token is gone
|
|
user string
|
|
sure bool
|
|
}{
|
|
{"signed in", `echo '{"userEmail":"me@example.com","subscriptionTier":"Pro"}'`, false, "me@example.com", true},
|
|
{"signed in after an update notice", `echo 'Updating…'; echo '{"userEmail":"me@example.com"}'`, false, "me@example.com", true},
|
|
{"says nobody", `echo '{"userEmail":""}'`, false, "", true},
|
|
{"fails", `echo 'fetch failed' >&2; exit 1`, false, "", false},
|
|
{"prints no JSON", `echo 'Something went wrong'`, false, "", false},
|
|
{"fails, token gone", `exit 1`, true, "", true},
|
|
} {
|
|
testenv.Program(t, exe, "#!/bin/sh\n"+c.script+"\n")
|
|
signedOut = c.out
|
|
u, _, ok, err := askCursorStatus()
|
|
if u != c.user || ok != (c.user != "") || (err == nil) != c.sure {
|
|
t.Errorf("%s: %q %v %v", c.name, u, ok, err)
|
|
}
|
|
}
|
|
}
|