test: tighten phase 1 coverage contracts

This commit is contained in:
huanghuoguoguo
2026-05-16 10:30:17 +08:00
parent 3ba727f0e4
commit bb55cd7ba9
44 changed files with 708 additions and 1164 deletions

View File

@@ -8,6 +8,7 @@ from __future__ import annotations
import pytest
import aiohttp
from aiohttp import web
from langbot.pkg.utils import httpclient
@@ -108,19 +109,30 @@ class TestSessionPoolIntegration:
"""Integration tests for session pool behavior."""
async def test_session_can_make_request(self):
"""Session can be used for actual HTTP requests."""
"""Session can be used for HTTP requests without relying on external network."""
app = web.Application()
async def handle_get(request):
return web.json_response({'ok': True})
app.router.add_get('/get', handle_get)
runner = web.AppRunner(app)
await runner.setup()
site = web.TCPSite(runner, '127.0.0.1', 0)
await site.start()
port = site._server.sockets[0].getsockname()[1]
session = httpclient.get_session()
# Make a simple request (using httpbin or similar)
# This is a basic smoke test
try:
async with session.get('https://httpbin.org/get', timeout=aiohttp.ClientTimeout(total=5)) as resp:
async with session.get(
f'http://127.0.0.1:{port}/get',
timeout=aiohttp.ClientTimeout(total=5),
) as resp:
assert resp.status == 200
except Exception:
# Network may be unavailable in CI, just verify session is usable
pass
await httpclient.close_all()
assert await resp.json() == {'ok': True}
finally:
await httpclient.close_all()
await runner.cleanup()
async def test_multiple_requests_same_session(self):
"""Multiple requests can use the same session."""

View File

@@ -137,11 +137,9 @@ class TestReadResourceFile:
"""Should read content from a resource file."""
from langbot.pkg.utils import importutil
try:
content = importutil.read_resource_file("templates/config.yaml")
assert isinstance(content, str)
except FileNotFoundError:
pass
content = importutil.read_resource_file("templates/config.yaml")
assert "admins:" in content
assert "edition: community" in content
def test_raises_for_nonexistent_file(self):
"""Should raise exception for non-existent resource file."""
@@ -158,11 +156,9 @@ class TestReadResourceFileBytes:
"""Should read content as bytes from a resource file."""
from langbot.pkg.utils import importutil
try:
content = importutil.read_resource_file_bytes("templates/config.yaml")
assert isinstance(content, bytes)
except FileNotFoundError:
pass
content = importutil.read_resource_file_bytes("templates/config.yaml")
assert b"admins:" in content
assert b"edition: community" in content
def test_raises_for_nonexistent_file_bytes(self):
"""Should raise exception for non-existent resource file."""
@@ -179,13 +175,10 @@ class TestListResourceFiles:
"""Should list files in a resource directory."""
from langbot.pkg.utils import importutil
try:
files = importutil.list_resource_files("templates")
assert isinstance(files, list)
for f in files:
assert isinstance(f, str)
except (FileNotFoundError, Exception):
pass
files = importutil.list_resource_files("templates")
assert "config.yaml" in files
assert "default-pipeline-config.json" in files
assert all(isinstance(file, str) for file in files)
def test_raises_for_nonexistent_directory(self):
"""Should raise exception for non-existent directory."""
@@ -196,4 +189,4 @@ class TestListResourceFiles:
if __name__ == "__main__":
pytest.main([__file__, "-v"])
pytest.main([__file__, "-v"])

View File

@@ -6,7 +6,6 @@ Tests log page management and pointer-based retrieval.
from __future__ import annotations
import pytest
from langbot.pkg.utils.logcache import LogPage, LogCache, LOG_PAGE_SIZE, MAX_CACHED_PAGES

View File

@@ -6,8 +6,7 @@ Tests pip command generation without actual installation.
from __future__ import annotations
import pytest
from unittest.mock import patch, Mock
from unittest.mock import patch
from langbot.pkg.utils import pkgmgr

View File

@@ -87,15 +87,6 @@ class TestGetRunnerCategory:
assert get_runner_category("test", "https://example.com") == RunnerCategory.CLOUD
assert get_runner_category("test", "https://myserver.example.org") == RunnerCategory.CLOUD
def test_invalid_url_returns_unknown(self):
"""Invalid URL that causes parsing error should return UNKNOWN."""
# URLs that cause exceptions during parsing return UNKNOWN
# Note: "not a valid url" is actually parseable by urlparse, it just has no scheme
# Use a URL that genuinely causes an exception
result = get_runner_category("test", "://invalid")
# urlparse may handle this differently, but exceptions return UNKNOWN
assert result in (RunnerCategory.UNKNOWN, RunnerCategory.CLOUD)
def test_urlparse_exception_returns_unknown(self):
"""Exception during URL parsing should return UNKNOWN."""
# Test by mocking urlparse to raise an exception
@@ -108,14 +99,6 @@ class TestGetRunnerCategory:
result = runner.get_runner_category("test", "http://example.com")
assert result == RunnerCategory.UNKNOWN
def test_url_without_scheme(self):
"""URL without scheme should still be parseable."""
# urlparse can parse this, hostname might be None
result = get_runner_category("test", "example.com")
# Without scheme, urlparse treats it as path, so hostname is None
# This should return UNKNOWN or CLOUD depending on implementation
assert result in (RunnerCategory.UNKNOWN, RunnerCategory.CLOUD)
class TestIsCloudRunner:
"""Test is_cloud_runner helper function."""
@@ -295,4 +278,4 @@ class TestConstants:
if __name__ == "__main__":
pytest.main([__file__, "-v"])
pytest.main([__file__, "-v"])

View File

@@ -6,7 +6,6 @@ Tests version comparison logic without network calls.
from __future__ import annotations
import pytest
from unittest.mock import Mock
from langbot.pkg.utils.version import VersionManager