Files
magpie/internal/provider/keychain_denied_test.go
T
ggbdpq d830d460f0 provider: a refused keychain read is not asked again for ten minutes (#2408)
What changed: copilotCLISecret backs a denied keychain read off (10
minutes, "don't ask again at once"), but the two other readers of the
same family took every failure as an absent item and asked again after
their ordinary 10s TTL. On macOS a denied item prompts again on every
read, so a user who answers Deny once was asked again every 10 seconds
while Cursor's status or Claude's credentials were polled, and every 10
minutes only for Copilot (#2408's report is the Copilot face of this).

Now cursorKeychainToken and readClaudeKeychainItem (through
claudeCredential's cache) treat any `security` failure but 44 — the item
not being there, which asks nobody — as a read that may have shown the
user a dialog, and don't repeat it inside keychainDeniedTTL (10 minutes,
copilot's own pause). A sign-in or out in magpie reads afresh
(forgetCursorStatus, forgetClaudeCredential clear the pause), and a
fresh read in CursorToken is unchanged.

Run:
- gofmt -l internal/provider: clean.
- go test -tags nogui -p 2 -count=1 ./internal/provider: ok (72s).
- TestDeniedCursorKeychainReadIsNotAskedAgain and
  TestDeniedClaudeKeychainReadIsNotAskedAgain fail on the code before
  this (a refused read asked again 11s later).

Not tried: a real macOS keychain dialog answered no (a fake `security`
stands in for it); Copilot's own reader is unchanged.
- go vet -tags nogui ./... for darwin, linux and windows ok; go build
  for the three ok (reviewer, at the batch head).
Full suite: go test -tags nogui -count=1 -timeout 45m ./... ok, every
  package (reviewer, at the batch head before #2424 was added).

Generated-by: GLM-5.3-Flash (ZCode)
2026-10-16 00:16:43 +08:00

88 lines
2.6 KiB
Go

package provider
// A keychain read that can have shown the user a dialog and was refused is
// not asked again every poll: copilotCLISecret backs one off, and so do
// the Cursor and Claude Code readers (#2408). A fake `security` answers
// the way a refused read does (51, not 44, the item not being there), and
// logs each ask; the reader may reach it once, and then not again inside
// the denial's pause.
import (
"bytes"
"os"
"path/filepath"
"runtime"
"testing"
"time"
"github.com/yetone/magpie/internal/testenv"
)
func fakeSecurityDenied(t *testing.T) func() int {
t.Helper()
dir := t.TempDir()
log := filepath.Join(dir, "asked.log")
testenv.Program(t, filepath.Join(dir, "security"), "#!/bin/sh\necho \"$*\" >> "+log+"\nexit 51\n")
t.Setenv("PATH", dir+string(os.PathListSeparator)+os.Getenv("PATH"))
return func() int {
b, _ := os.ReadFile(log)
return bytes.Count(b, []byte("\n"))
}
}
func TestDeniedCursorKeychainReadIsNotAskedAgain(t *testing.T) {
if runtime.GOOS != "darwin" {
t.Skip("the Cursor keychain read is a macOS one")
}
signIn(t)
asks := fakeSecurityDenied(t)
cursorTok.Lock()
cursorTok.tok, cursorTok.at = "", time.Time{}
cursorTok.Unlock()
if tok := cursorKeychainToken(false); tok != "" {
t.Fatalf("a refused read is no token: %q", tok)
}
if n := asks(); n != 1 {
t.Fatalf("the first read asks the keychain once, asked %d times", n)
}
// past the 10s token TTL, still inside the denial's pause: not asked again
cursorTok.Lock()
cursorTok.at = time.Now().Add(-11 * time.Second)
cursorTok.Unlock()
if tok := cursorKeychainToken(false); tok != "" {
t.Fatalf("a refused read is no token: %q", tok)
}
if n := asks(); n != 1 {
t.Fatalf("a refused read was asked again 11s later: %d asks", n)
}
}
func TestDeniedClaudeKeychainReadIsNotAskedAgain(t *testing.T) {
if runtime.GOOS != "darwin" {
t.Skip("the Claude keychain read is a macOS one")
}
signIn(t)
asks := fakeSecurityDenied(t)
oldKeychain := claudeKeychain
claudeKeychain = true
defer func() { claudeKeychain = oldKeychain }()
forgetClaudeCredential()
if _, _, ok := claudeCredential(); ok {
t.Fatal("a refused read is no sign-in")
}
n := asks()
if n == 0 {
t.Fatal("the read never reached the keychain")
}
// past the 10s cache, still inside the denial's pause: not asked again
claudeCacheMu.Lock()
claudeCacheAt = time.Now().Add(-11 * time.Second)
claudeCacheMu.Unlock()
if _, _, ok := claudeCredential(); ok {
t.Fatal("a refused read is no sign-in")
}
if got := asks(); got != n {
t.Fatalf("a refused read was asked again 11s later: %d asks, then %d", n, got)
}
}