test: replace false-green checks with behavioral coverage (#6282)

* test: replace false-green checks with behavioral coverage

* test: complete behavior-focused audit coverage
This commit is contained in:
chengyongru authored and GitHub committed 2026-10-18 03:16:25 +08:00
1 parent c7ab7aa6da
commit 74942bd190
91 files changed
+1891 -1391

No files matched your search

@@ -12,11 +12,13 @@ from nanobot.providers import github_copilot_provider as gc
@pytest.mark.asyncio
async def test_concurrent_token_refresh_fetches_once(monkeypatch):
"""Two concurrent _get_copilot_access_token calls should trigger only one
HTTP fetch when the token is expired, not two."""
"""A competing caller waits for the in-flight exchange and reuses its token."""
monkeypatch.setattr(gc, "_load_github_token", lambda: SimpleNamespace(access="github-token"))
fetch_count = 0
entered = asyncio.Event()
release = asyncio.Event()
competing_started = asyncio.Event()
class FakeResponse:
def raise_for_status(self):
@@ -38,82 +40,37 @@ async def test_concurrent_token_refresh_fetches_once(monkeypatch):
async def get(self, url, *, headers):
nonlocal fetch_count
fetch_count += 1
# Simulate network latency so both coroutines overlap before the
# lock serializes them.
await asyncio.sleep(0.05)
entered.set()
await release.wait()
return FakeResponse()
monkeypatch.setattr(gc.httpx, "AsyncClient", FakeAsyncClient)
provider = gc.GitHubCopilotProvider()
# Force token expiry.
provider._copilot_access_token = None
provider._copilot_expires_at = 0.0
token_a, token_b = await asyncio.gather(
provider._get_copilot_access_token(),
provider._get_copilot_access_token(),
)
async def competing_call():
# No suspension between this signal and entering the token path.
competing_started.set()
return await provider._get_copilot_access_token()
assert token_a == "copilot-token"
assert token_b == "copilot-token"
assert fetch_count == 1, (
f"Expected exactly 1 token fetch under concurrency, got {fetch_count}"
)
@pytest.mark.asyncio
async def test_second_call_returns_cached_token_while_first_in_flight(monkeypatch):
"""If task A is mid-fetch inside the lock, task B should wait, then find
the cached token and skip the HTTP call entirely."""
monkeypatch.setattr(gc, "_load_github_token", lambda: SimpleNamespace(access="github-token"))
fetch_count = 0
class FakeResponse:
def raise_for_status(self):
pass
def json(self):
return {"token": "cached-token", "refresh_in": 1500}
class FakeAsyncClient:
def __init__(self, *args, **kwargs):
pass
async def __aenter__(self):
return self
async def __aexit__(self, *args):
return False
async def get(self, url, *, headers):
nonlocal fetch_count
fetch_count += 1
await asyncio.sleep(0.1)
return FakeResponse()
monkeypatch.setattr(gc.httpx, "AsyncClient", FakeAsyncClient)
provider = gc.GitHubCopilotProvider()
provider._copilot_access_token = None
provider._copilot_expires_at = 0.0
# Start task A, let it acquire the lock and begin the HTTP fetch.
task_a = asyncio.create_task(provider._get_copilot_access_token())
await asyncio.sleep(0.02) # task A is now inside the lock, mid-fetch
tasks = [task_a]
try:
await asyncio.wait_for(entered.wait(), timeout=5)
task_b = asyncio.create_task(competing_call())
tasks.append(task_b)
await asyncio.wait_for(competing_started.wait(), timeout=5)
assert not task_a.done()
assert not task_b.done()
assert fetch_count == 1
# Task B starts while A is still in flight.
token_b = await provider._get_copilot_access_token()
token_a = await task_a
assert token_a == "cached-token"
assert token_b == "cached-token"
assert fetch_count == 1
@pytest.mark.asyncio
async def test_copilot_token_lock_exists():
"""Provider should have an asyncio.Lock for token refresh."""
provider = gc.GitHubCopilotProvider()
assert isinstance(provider._copilot_token_lock, asyncio.Lock)
release.set()
tokens = await asyncio.wait_for(asyncio.gather(*tasks), timeout=5)
assert tokens == ["copilot-token", "copilot-token"]
assert fetch_count == 1
finally:
release.set()
for task in tasks:
if not task.done():
task.cancel()
await asyncio.gather(*tasks, return_exceptions=True)
await provider.aclose()