Skip to content

fix(test): mock keychain lookup in anthropic oauth setup tests - #35093

Closed
zons-zhaozhy wants to merge 1 commit into
NousResearch:mainfrom
zons-zhaozhy:fix/anthropic-keychain-mock-test
Closed

zons-zhaozhy wants to merge 1 commit into
NousResearch:mainfrom
zons-zhaozhy:fix/anthropic-keychain-mock-test

Conversation

@zons-zhaozhy

Copy link
Copy Markdown

Problem

3 tests in tests/agent/test_anthropic_adapter.py fail on macOS:

FAILED TestRunOauthSetupToken::test_returns_token_from_credential_files
FAILED TestRunOauthSetupToken::test_returns_token_from_env_var
FAILED TestRunOauthSetupToken::test_returns_none_when_no_creds_found

All fail with:

TypeError: the JSON object must be str, bytes or bytearray, not MagicMock

Root Cause

_read_claude_code_credentials_from_keychain() calls subprocess.run internally. The tests mock subprocess.run at the outer level via patch("subprocess.run"), but the MagicMock return value leaks into json.loads() inside the keychain helper.

This only manifests on macOS where keychain lookups are attempted. The upstream CI runs on Linux and skips the keychain path entirely, so it never hits this.

Fix

monkeypatch.setattr the keychain helper to return None in the three affected tests, preventing the MagicMock from reaching json.loads().

Testing

Before: 3 FAILED, 150 passed
After:  153 passed

The upstream tests for _run_oauth_setup_token mock subprocess.run at the
outer level, but _read_claude_code_credentials_from_keychain() calls
subprocess.run internally. The MagicMock return value from the outer patch
leaks through to json.loads() inside the keychain helper, causing:

    TypeError: the JSON object must be str, bytes or bytearray, not MagicMock

This only manifests on macOS where keychain lookups are attempted.
Linux CI (the upstream default) skips the keychain path entirely.

Fix: monkeypatch _read_claude_code_credentials_from_keychain to return
None in the three affected tests, preventing the MagicMock from reaching
json.loads().

Before: 3 FAILED, 150 passed
After:  153 passed
@alt-glitch alt-glitch added type/test Test coverage or test infrastructure comp/agent Core agent runtime: loop, agent_init, prompt builder, context-compression, responses endpoint provider/anthropic Anthropic native Messages API P3 Low — cosmetic, nice to have labels May 30, 2026
@teknium1

Copy link
Copy Markdown
Collaborator

Thanks for isolating this macOS-specific test path. The premise remains valid on current main: run_oauth_setup_token() calls read_claude_code_credentials() after the test's mocked subprocess.run (agent/anthropic_adapter.py:1350-1355), and the Darwin keychain helper passes that mocked stdout to json.loads() while catching only JSONDecodeError (agent/anthropic_adapter.py:884-912). The three affected tests still install that global mock without keychain isolation (tests/agent/test_anthropic_adapter.py:684,702,715).

Automated hermes-sweeper review.

@teknium1 teknium1 added the sweeper:blast-contained Sweeper blast radius: contained — one narrow path / opt-in / few users label Jul 13, 2026
@teknium1

Copy link
Copy Markdown
Collaborator

Thanks @zons-zhaozhy — closing as resolved on main: the suite-wide autouse Keychain guard from PR #74517 (via #35464) supersedes the per-test mock, covering every test module. You were the earliest on this class (May 30) — credited. Verified post-merge.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/agent Core agent runtime: loop, agent_init, prompt builder, context-compression, responses endpoint P3 Low — cosmetic, nice to have provider/anthropic Anthropic native Messages API sweeper:blast-contained Sweeper blast radius: contained — one narrow path / opt-in / few users type/test Test coverage or test infrastructure

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants