Files
magpie/internal/provider/claude_keychain_write_fail_test.go
T
ggbdpq cd0875f5a0 provider: a Claude sign-in whose keychain write fails leaves logins.json alone (#4563)
addLogin wrote logins.json before the credential was stored, and a
keychain write that failed (the login keychain out of reach over ssh)
left the account kept all the same: importing the same file again
reported it as already there. The credential is stored first now; the
login is written only once it is in.

Reviewed and applied from #4563 (ggbdpq, 7b42e98a1, SHA-256 matches).
The test runs in signIn's sandbox HOME with a fake `security` first on
PATH that exits 51. proc.Command looks PATH up at call time, so the real
Keychain is never reached. Switching to a saved login still writes its
On marks before the credential: #4592.

Broken by hand: signin.go back at main with the test kept, and
TestClaudeLoginKeychainWriteFailureKeepsLoginsFile fails.

Run (on mote, through mote-run):
- Full `go test -tags nogui -count=1 -timeout 45m ./...` with this stack
  on c30d3b7b6: 82 packages ok. internal/gui was red only on
  TestSettingsSaveKeepsWhatItDoesNotSend (#4514, SearchAll from
  b09349d17) and TestColdTurnsTaken (#4510). Both are filed, and neither
  touches this stack.
- Rebased onto 29d0ab39f, which changed internal/gateway since then:
  gofmt -l, go vet (darwin, GOOS=linux -tags nogui, GOOS=windows) and
  go test -count=1 ./internal/gateway ./internal/usage ./internal/provider
  ./internal/sessions all ok at the pushed head. go build ./... ok for
  darwin, linux (nogui) and windows.
- go test -race of the gateway tests this stack adds or touches, on
  the stack at fb29cd30f: ok.
2026-10-31 23:35:27 +08:00

54 lines
1.6 KiB
Go

package provider
// A Claude sign-in whose Keychain write fails (mote over ssh: the login
// keychain out of reach, #4563) leaves logins.json alone: the account
// isn't kept when its credential couldn't be stored, or importing the
// same file again would report it as already there.
import (
"bytes"
"os"
"path/filepath"
"runtime"
"testing"
"time"
"github.com/yetone/magpie/internal/testenv"
)
func TestClaudeLoginKeychainWriteFailureKeepsLoginsFile(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("a shell script stands in for the keychain")
}
signIn(t) // a temp home: no Claude sign-in in it, no saved logins
bin := t.TempDir()
// a refused keychain: no item there (51), and no write kept
testenv.Program(t, filepath.Join(bin, "security"), "#!/bin/sh\nexit 51\n")
t.Setenv("PATH", bin+string(os.PathListSeparator)+os.Getenv("PATH"))
oldKeychain := claudeKeychain
claudeKeychain = true
defer func() { claudeKeychain = oldKeychain }()
l, err := claudeLogin(claudeCredentials{OAuth: claudeAuth{
AccessToken: "a", RefreshToken: "r",
ExpiresAt: time.Now().Add(time.Hour).UnixMilli(), SubscriptionType: "max",
}}, map[string]any{"emailAddress": "ssh@example.com"})
if err != nil {
t.Fatal(err)
}
before, err := os.ReadFile(loginsPath())
if err != nil && !os.IsNotExist(err) {
t.Fatal(err)
}
if _, err := addLogin(l); err == nil {
t.Fatal("a sign-in whose keychain write failed was kept")
}
after, err := os.ReadFile(loginsPath())
if err != nil && !os.IsNotExist(err) {
t.Fatal(err)
}
if !bytes.Equal(before, after) {
t.Fatalf("logins.json changed on a failed keychain write: %s", after)
}
}