Add tests for async cost observation and PR frequency throttling#109
Merged
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
datetimeandthreadingmodules to support async testing patterns_react_to_cost_observed_async()method:test_react_to_cost_observed_skips_when_flags_off: Verifies no-op behavior when feature flags are disabledtest_react_to_cost_observed_calls_updater_when_update_on_startup: Confirms background thread spawning whenfree_tier.update_on_startupis enabledtest_react_to_cost_observed_inflight_guard_deduplicates: Validates that concurrent observations are deduplicated via an inflight guardtest_react_to_cost_observed_calls_updater_when_pr_enabled: Confirms background thread spawning whenproviders_pr.enabledis enabledtest_maybe_open_providers_pr_respects_frequency_daysto verify that PR creation is skipped when the configured frequency window has not elapsed, while still allowing the updater to runNotable Implementation Details
threading.Event()for synchronization to reliably wait for background thread completionmonkeypatchallows testing different feature flag combinations_react_to_cost_observed_async()twice and confirming the updater runs exactly onceconfig_mod.load_pr_stateandgithub_pr_mod.create_or_update_prto simulate recent PR state and verify the throttle prevents API callshttps://claude.ai/code/session_01SgKggwKhD56z3tY1QyNhi5