-
Notifications
You must be signed in to change notification settings - Fork 0
Integrate api-simulator for Quota Guard End-to-End HTTP Testing #613
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Trecek
merged 8 commits into
integration
from
integrate-api-simulator-for-quota-guard-end-to-end-http-test/607
Apr 5, 2026
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
68a1498
feat: add base_url parameter to quota functions and api-simulator dev…
Trecek 6fecc98
test: add 7 end-to-end HTTP tests for quota guard via api-simulator
Trecek c4aaee3
fix: use branch ref for api-simulator source, auto-format, update loc…
Trecek 28d714e
fix: update mock_fetch signatures to accept base_url keyword arg
Trecek 9842461
fix(review): extract _DEFAULT_BASE_URL constant and add _httpx_timeou…
Trecek 8cc898e
fix(review): refactor tests to public API, fix timeout test, remove A…
Trecek 1341361
fix: update api-simulator lockfile to current main HEAD
Trecek a6c7683
ci: configure git auth for private api-simulator dependency
Trecek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| """End-to-end HTTP tests for quota guard using api-simulator mock_http_server. | ||
|
|
||
| These tests exercise the real httpx client path — no monkeypatching of _fetch_quota. | ||
| They complement the unit tests in test_quota.py which mock at the function level. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import json | ||
| import time | ||
| from datetime import UTC, datetime, timedelta | ||
| from types import SimpleNamespace | ||
|
|
||
| import pytest | ||
|
|
||
| from autoskillit.execution.quota import check_and_sleep_if_needed | ||
|
|
||
| pytestmark = pytest.mark.anyio | ||
|
|
||
| QUOTA_ENDPOINT = "/api/oauth/usage" | ||
|
|
||
|
|
||
| @pytest.fixture() | ||
| def credentials(tmp_path): | ||
| """Write a valid .credentials.json and return its path as a string.""" | ||
| creds_file = tmp_path / ".credentials.json" | ||
| creds_file.write_text( | ||
| json.dumps( | ||
| { | ||
| "claudeAiOauth": { | ||
| "accessToken": "test-token-abc123", | ||
| "expiresAt": (time.time() + 3600) * 1000, | ||
| } | ||
| } | ||
| ) | ||
| ) | ||
| return str(creds_file) | ||
|
|
||
|
|
||
| @pytest.fixture() | ||
| def quota_config(credentials, tmp_path): | ||
| """Minimal config namespace for check_and_sleep_if_needed.""" | ||
| return SimpleNamespace( | ||
| enabled=True, | ||
| credentials_path=credentials, | ||
| cache_path=str(tmp_path / "quota_cache.json"), | ||
| cache_max_age=120, | ||
| threshold=80, | ||
| buffer_seconds=60, | ||
| ) | ||
|
|
||
|
|
||
| @pytest.fixture(autouse=True) | ||
| def _reset_mock(mock_http_server): | ||
| """Reset mock_http_server before each test to clear routes and recordings.""" | ||
| mock_http_server.reset() | ||
|
|
||
|
|
||
| async def test_normal_utilization_returns_status_and_sends_correct_headers( | ||
| mock_http_server, quota_config | ||
| ): | ||
| mock_http_server.register( | ||
| "GET", | ||
| QUOTA_ENDPOINT, | ||
| json={ | ||
| "five_hour": { | ||
| "utilization": 50.0, | ||
| "resets_at": "2026-04-05T00:00:00+00:00", | ||
| } | ||
| }, | ||
| ) | ||
|
|
||
| result = await check_and_sleep_if_needed(quota_config, base_url=mock_http_server.url) | ||
|
|
||
| assert result["should_sleep"] is False | ||
| assert result["utilization"] == 50.0 | ||
| assert result["resets_at"] == "2026-04-05T00:00:00+00:00" | ||
|
|
||
| requests = mock_http_server.get_requests("GET", QUOTA_ENDPOINT) | ||
| assert len(requests) == 1 | ||
| assert requests[0].headers["authorization"] == "Bearer test-token-abc123" | ||
| assert requests[0].headers["anthropic-beta"] == "oauth-2025-04-20" | ||
|
|
||
|
|
||
| async def test_above_threshold_triggers_double_fetch(mock_http_server, quota_config): | ||
| resets_at = (datetime.now(UTC) + timedelta(hours=2)).isoformat() | ||
| mock_http_server.register_sequence( | ||
| "GET", | ||
| QUOTA_ENDPOINT, | ||
| responses=[ | ||
| {"json": {"five_hour": {"utilization": 95.0, "resets_at": resets_at}}}, | ||
| {"json": {"five_hour": {"utilization": 95.0, "resets_at": resets_at}}}, | ||
| ], | ||
| ) | ||
|
|
||
| result = await check_and_sleep_if_needed(quota_config, base_url=mock_http_server.url) | ||
|
|
||
| assert result["should_sleep"] is True | ||
| assert result["sleep_seconds"] > 0 | ||
| assert mock_http_server.request_count("GET", QUOTA_ENDPOINT) == 2 | ||
|
|
||
|
|
||
| async def test_resets_at_null_blocks_with_fallback(mock_http_server, quota_config): | ||
| mock_http_server.register( | ||
| "GET", | ||
| QUOTA_ENDPOINT, | ||
| json={"five_hour": {"utilization": 95.0, "resets_at": None}}, | ||
| ) | ||
|
|
||
| result = await check_and_sleep_if_needed(quota_config, base_url=mock_http_server.url) | ||
|
|
||
| assert result["should_sleep"] is True | ||
| assert result["sleep_seconds"] >= 60 | ||
| assert result["reason"] == "unknown_reset" | ||
| assert mock_http_server.request_count("GET", QUOTA_ENDPOINT) == 1 | ||
|
|
||
|
|
||
| async def test_http_429_fails_open(mock_http_server, quota_config): | ||
| mock_http_server.register("GET", QUOTA_ENDPOINT, status=429) | ||
|
|
||
| result = await check_and_sleep_if_needed(quota_config, base_url=mock_http_server.url) | ||
|
|
||
| assert result["should_sleep"] is False | ||
| assert "error" in result | ||
|
|
||
|
|
||
| async def test_http_503_fails_open(mock_http_server, quota_config): | ||
| mock_http_server.register("GET", QUOTA_ENDPOINT, status=503) | ||
|
|
||
| result = await check_and_sleep_if_needed(quota_config, base_url=mock_http_server.url) | ||
|
|
||
| assert result["should_sleep"] is False | ||
| assert "error" in result | ||
|
|
||
|
|
||
| async def test_network_timeout_fails_open(mock_http_server, quota_config): | ||
| mock_http_server.register("GET", QUOTA_ENDPOINT, json={}, delay_seconds=0.5) | ||
|
|
||
| result = await check_and_sleep_if_needed( | ||
| quota_config, base_url=mock_http_server.url, _httpx_timeout=0.1 | ||
| ) | ||
|
|
||
| assert result["should_sleep"] is False | ||
| assert "error" in result | ||
|
|
||
|
|
||
| async def test_z_suffix_resets_at_parsed_correctly(mock_http_server, quota_config): | ||
| mock_http_server.register( | ||
| "GET", | ||
| QUOTA_ENDPOINT, | ||
| json={ | ||
| "five_hour": { | ||
| "utilization": 50.0, | ||
| "resets_at": "2026-04-05T00:00:00Z", | ||
| } | ||
| }, | ||
| ) | ||
|
|
||
| result = await check_and_sleep_if_needed(quota_config, base_url=mock_http_server.url) | ||
|
|
||
| assert result["resets_at"] == "2026-04-05T00:00:00+00:00" | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[warning] tests: _reset_mock is autouse=True and calls mock_http_server.reset() per-test. If mock_http_server is session-scoped (common for server fixtures), route registrations from parallel xdist workers sharing the same server can cross-contaminate. Verify that mock_http_server is function-scoped before relying on this reset pattern.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Investigated — this is intentional. pytest-xdist with
-n 4creates a separate OS process per worker; session-scoped fixtures are initialized once per worker process, not globally. Each worker gets its ownmock_http_serverinstance on a different port (confirmed via api_simulator/plugin.py). Tests within a worker run sequentially, so the autouse_reset_mockcleanly isolates state. No cross-worker contamination is possible in this architecture.