test: make the sync refresh-margin test actually test the margin - #9
Conversation
test_token_manager_refresh_margin never called get_token(), so is_valid was False merely because no token had ever been acquired -- the tok is None branch. The refresh margin was never exercised. It passed with expires_in=3600, far outside the 60s margin, having made 0 calls to the token endpoint, and would have passed with the refresh-ahead logic deleted entirely. auth.py reported 95% line coverage throughout, because coverage counts executed lines rather than meaningful assertions. Now mirrors the async twin fixed in #7: two mocked responses, two get_token() calls, asserting the second returns the fresh token and the route was hit twice. This drives the real path -- get_token does not consult the is_valid property, it re-checks is_expired inline, twice (outer check, then again under the lock). Verified sensitive: with refresh_margin=0 the second get_token returns the cached STALE token and the new assertions fail. The old assertion passed either way. Both managers' refresh-ahead behaviour is now covered symmetrically. Tests only; no source changes. 303 tests, ruff/format/mypy clean. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (2)
📝 WalkthroughWalkthroughAuthentication tests now validate the token request payload and exercise refresh-margin token acquisition across successive responses, including token validity transitions and HTTP call counts. ChangesAuthentication test strengthening
Estimated code review effort: 2 (Simple) | ~10 minutes Possibly related PRs
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@AGENTS.md`:
- Line 131: Update the AGENTS.md lesson about the refresh-margin tests to remove
the incorrect claim that they are verified with refresh_margin=0. Describe that
the sync test uses the default 60-second margin and accurately summarize the
mutation check that confirms the refresh-ahead behavior.
In `@tests/unit/test_auth.py`:
- Around line 121-136: Update the test around mgr.get_token() and the
respx_mock.post route to inspect the captured outbound token requests, asserting
the expected request headers, form payload, and any applicable query parameters
for both refresh calls. Keep the existing stale-to-fresh response and validity
assertions unchanged.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro Plus
Run ID: fcc2cdc0-ae94-44ab-bc93-04e2bad84b17
📒 Files selected for processing (2)
AGENTS.mdtests/unit/test_auth.py
1. AGENTS.md overstated its own verification. It said both refresh-margin twins "are verified against refresh_margin=0", which reads as though a test in the suite uses that value. None does -- the committed tests use the default 60s margin, and refresh_margin=0 was a throwaway scratch mutation run that was deleted. Reworded to say what the tests actually do and to flag the mutation check as a manual step to repeat if the refresh-ahead logic changes. Worth fixing carefully: this file spends several sections warning against overstated verification claims, so it should not contain one. 2. The reviewer asked for outbound-request assertions on the refresh-margin test. The underlying gap is real -- the sync suite never asserted the grant payload, a hole noted when the async twin was fixed in #7 -- but the margin test is the wrong home for it. That test was just rewritten to do exactly one thing: prove a token expiring inside the margin is re-acquired. Loading it with request-shape assertions would blur that. Added instead to test_token_manager_acquires_token, mirroring test_async_auth.py::test_async_token_manager_acquires_token exactly, so both suites now pin the grant form in the same place. Verified it catches what it exists for: injecting the typo "client_credentials" -> "client_credential" into _build_form fails this test and ONLY this test, confirming nothing else covered the payload. Tests and docs only; no source changes. 303 tests, ruff/format/mypy clean. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Tests only. No source changes. Addresses the outside-diff review comment left on #8, which was deliberately deferred to keep that PR single-concern.
The test was vacuous
test_token_manager_refresh_marginnever calledget_token():So
is_validwas False purely via thetok is Nonebranch. The refresh margin was never involved. Proven twice:expires_in=3600— far outside the 60s margin — making 0 calls to the token endpoint.Worth noting:
auth.pyreported 95% line coverage throughout. Coverage counts executed lines, not meaningful assertions, so the healthiest-looking metric was hiding the untested path.The fix
Mirrors the async twin fixed in #7 — two mocked responses, two
get_token()calls, asserting the second returns the fresh token androute.call_count == 2.This drives the real path rather than the flag:
get_tokendoes not consult theis_validproperty, it re-checksis_expiredinline, twice (outer check, then again under the lock).Verified it can fail
With
refresh_margin=0the secondget_token()returns the cachedSTALEtoken and the new assertions fail:The old assertion passed either way. Both managers' refresh-ahead behaviour is now covered symmetrically.
Testing
303 tests.
ruff check,ruff format --check,mypy src/myinvoisall clean.🤖 Generated with Claude Code
Summary by CodeRabbit