Skip to content

Add tests for async cost observation and PR frequency throttling#109

Merged
BillJr99 merged 2 commits into
mainfrom
claude/free-model-cost-detection-3qgu0i
Jun 19, 2026
Merged

Add tests for async cost observation and PR frequency throttling#109
BillJr99 merged 2 commits into
mainfrom
claude/free-model-cost-detection-3qgu0i

Conversation

@BillJr99

Copy link
Copy Markdown
Owner

Summary

This PR adds comprehensive test coverage for the background cost observation reaction mechanism and PR creation frequency throttling in the usage server.

Key Changes

  • New test imports: Added datetime and threading modules to support async testing patterns
  • Cost observation tests: Added 4 tests covering the _react_to_cost_observed_async() method:
    • test_react_to_cost_observed_skips_when_flags_off: Verifies no-op behavior when feature flags are disabled
    • test_react_to_cost_observed_calls_updater_when_update_on_startup: Confirms background thread spawning when free_tier.update_on_startup is enabled
    • test_react_to_cost_observed_inflight_guard_deduplicates: Validates that concurrent observations are deduplicated via an inflight guard
    • test_react_to_cost_observed_calls_updater_when_pr_enabled: Confirms background thread spawning when providers_pr.enabled is enabled
  • PR frequency throttling test: Added test_maybe_open_providers_pr_respects_frequency_days to verify that PR creation is skipped when the configured frequency window has not elapsed, while still allowing the updater to run

Notable Implementation Details

  • Tests use threading.Event() for synchronization to reliably wait for background thread completion
  • Configuration patching via monkeypatch allows testing different feature flag combinations
  • The inflight guard test validates deduplication by calling _react_to_cost_observed_async() twice and confirming the updater runs exactly once
  • PR frequency test mocks both config_mod.load_pr_state and github_pr_mod.create_or_update_pr to simulate recent PR state and verify the throttle prevents API calls

https://claude.ai/code/session_01SgKggwKhD56z3tY1QyNhi5

claude added 2 commits June 19, 2026 14:25
Five new tests in test_usage_endpoint.py verify the async reaction that
fires after a believed_free model reports a cost at runtime:

- skips entirely when neither update_on_startup nor providers_pr.enabled is set
- calls _run_free_models_update when free_tier.update_on_startup is True
- calls _run_free_models_update when providers_pr.enabled is True
- inflight guard ensures concurrent observations only trigger one scrape run
- _maybe_open_providers_pr skips PR creation when frequency_days has not
  elapsed since the last PR (throttle respected even on reactive path)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SgKggwKhD56z3tY1QyNhi5
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SgKggwKhD56z3tY1QyNhi5
@BillJr99 BillJr99 merged commit 9f3272a into main Jun 19, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants