Files
magpie/internal/provider/codex_store_unix_test.go
T
yetone 167df70550 provider tests: TestSecurityKeychainAnswers's fake security is a program macOS has let run
The full suite at a61a6f954 (+ #2440's helper fix, which doesn't touch
internal/provider) went red in TestSecurityKeychainAnswers after 10.03s:
"exit 44: the keychain didn't give it to magpie (refused, or no answer)".
10s is keychainWait. The test wrote its fake `security` with os.WriteFile,
and on macOS a newly written program's first exec waits for the system's
check, several seconds while a go test ./... starts its binaries, which
ate the whole wait. It now writes it through testenv.Program, as the
other timed fakes do, from the pool of files macOS has let run.

Run:
- go test ./internal/provider -run '^TestSecurityKeychainAnswers$'
  -count=5: ok; go test -timeout 45m ./internal/provider
  ./internal/claudebridge: ok.
- gofmt -l clean; go vet -tags nogui darwin, linux, windows on
  internal/provider.
- Not reproduced on purpose: the red needs the system's first-run check
  to be slow, which only the full run's load gave.
- Full suite: go test -timeout 45m ./... at 1a16fc0b5 + these two commits, EXIT 1: 65 packages ok; internal/gui red only in TestSetAnswersWhileDevinAndProvidersHang (set slow/slow-1 took 12.4s, three suites running at once; untouched by this change), output added to #1375.
2026-10-16 03:27:22 +08:00

58 lines
1.9 KiB
Go

//go:build !windows
package provider
import (
"encoding/hex"
"errors"
"io/fs"
"os"
"path/filepath"
"strings"
"testing"
"github.com/yetone/magpie/internal/testenv"
)
// securityKeychain reads `security` as macOS answers: exit 44 is no item,
// a password with a newline comes back as hex, and an error says neither
// the arguments nor the output, which can hold the sign-in. The program is
// a fake on PATH: no test runs the real one.
func TestSecurityKeychainAnswers(t *testing.T) {
bin := t.TempDir()
hexed := hex.EncodeToString([]byte("{\"tokens\":{\"access_token\":\"x\"}}\n"))
script := `#!/bin/sh
case "$1" in
find-generic-password)
case "$5" in
"cli|none") exit 44 ;;
"cli|hex") echo ` + hexed + `; exit 0 ;;
esac
echo "denied SECRET-OUT" >&2; exit 51 ;;
add-generic-password) echo "$@" >&2; exit 1 ;;
delete-generic-password) exit 44 ;;
esac
exit 2
`
// a program macOS has let run already: a new one's first exec waits for
// its check, past the keychain's wait under a full run's load
testenv.Program(t, filepath.Join(bin, "security"), script)
t.Setenv("PATH", bin+string(os.PathListSeparator)+os.Getenv("PATH"))
k := securityKeychain{}
if _, err := k.Find(codexKeychainService, "cli|none"); !errors.Is(err, fs.ErrNotExist) {
t.Fatalf("exit 44: %v, want no item", err)
}
if b, err := k.Find(codexKeychainService, "cli|hex"); err != nil || !strings.HasPrefix(string(b), `{"tokens"`) {
t.Fatalf("hex: %q %v", b, err)
}
if _, err := k.Find(codexKeychainService, "cli|refused"); err == nil || errors.Is(err, fs.ErrNotExist) || strings.Contains(err.Error(), "SECRET") {
t.Fatalf("refused: %v, want an error that isn't no item and says nothing it was given", err)
}
if err := k.Put(codexKeychainService, "cli|x", []byte(`{"SECRET-IN":1}`)); err == nil || strings.Contains(err.Error(), "SECRET") {
t.Fatalf("put: %v", err)
}
if err := k.Remove(codexKeychainService, "cli|x"); err != nil {
t.Fatalf("removing no item: %v", err)
}
}