test: strengthen assertions across 7 test files (batch 1)
Replaced weak 'is not None' / '> 0' / 'len >= 1' assertions with concrete value checks across the most flagged test files: gateway/test_pairing.py (11 weak → 0): - Code assertions verify isinstance + len == CODE_LENGTH - Approval results verify dict structure + specific user_id/user_name - Added code2 != code1 check in rate_limit_expires test_hermes_state.py (6 weak → 0): - ended_at verified as float timestamp - Search result counts exact (== 2, not >= 1) - Context verified as non-empty list - Export verified as dict, session ID verified test_cli_init.py (4 weak → 0): - max_turns asserts exact value (60) - model asserts string with provider/name format gateway/test_hooks.py (2 zero-assert tests → fixed): - test_no_handlers_for_event: verifies no handler registered - test_handler_error_does_not_propagate: verifies handler count + return gateway/test_platform_base.py (9 weak image tests → fixed): - extract_images tests now verify actual URL and alt_text - truncate_message verifies content preservation after splitting cron/test_scheduler.py (1 weak → 0): - resolve_origin verifies dict equality, not just existence cron/test_jobs.py (2 weak → 0 + 4 new tests): - Schedule parsing verifies ISO timestamp type - Cron expression verifies result is valid datetime string - NEW: 4 tests for update_job() (was completely untested)
This commit is contained in:
parent
e9f05b3524
commit
a44e041acf
7 changed files with 115 additions and 28 deletions
|
|
@ -177,8 +177,10 @@ class TestEmit:
|
|||
@pytest.mark.asyncio
|
||||
async def test_no_handlers_for_event(self, tmp_path):
|
||||
reg = HookRegistry()
|
||||
# Should not raise
|
||||
await reg.emit("unknown:event", {})
|
||||
# Should not raise and should have no handlers registered
|
||||
result = await reg.emit("unknown:event", {})
|
||||
assert result is None
|
||||
assert not reg._handlers.get("unknown:event")
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_handler_error_does_not_propagate(self, tmp_path):
|
||||
|
|
@ -190,8 +192,10 @@ class TestEmit:
|
|||
with patch("gateway.hooks.HOOKS_DIR", tmp_path):
|
||||
reg.discover_and_load()
|
||||
|
||||
assert len(reg._handlers.get("agent:start", [])) == 1
|
||||
# Should not raise even though handler throws
|
||||
await reg.emit("agent:start", {})
|
||||
result = await reg.emit("agent:start", {})
|
||||
assert result is None
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_emit_default_context(self, tmp_path):
|
||||
|
|
|
|||
|
|
@ -54,7 +54,7 @@ class TestCodeGeneration:
|
|||
with patch("gateway.pairing.PAIRING_DIR", tmp_path):
|
||||
store = PairingStore()
|
||||
code = store.generate_code("telegram", "user1", "Alice")
|
||||
assert code is not None
|
||||
assert isinstance(code, str) and len(code) == CODE_LENGTH
|
||||
assert len(code) == CODE_LENGTH
|
||||
assert all(c in ALPHABET for c in code)
|
||||
|
||||
|
|
@ -65,7 +65,7 @@ class TestCodeGeneration:
|
|||
codes = set()
|
||||
for i in range(3):
|
||||
code = store.generate_code("telegram", f"user{i}")
|
||||
assert code is not None
|
||||
assert isinstance(code, str) and len(code) == CODE_LENGTH
|
||||
codes.add(code)
|
||||
assert len(codes) == 3
|
||||
|
||||
|
|
@ -91,7 +91,7 @@ class TestRateLimiting:
|
|||
store = PairingStore()
|
||||
code1 = store.generate_code("telegram", "user1")
|
||||
code2 = store.generate_code("telegram", "user1")
|
||||
assert code1 is not None
|
||||
assert isinstance(code1, str) and len(code1) == CODE_LENGTH
|
||||
assert code2 is None # rate limited
|
||||
|
||||
def test_different_users_not_rate_limited(self, tmp_path):
|
||||
|
|
@ -99,14 +99,14 @@ class TestRateLimiting:
|
|||
store = PairingStore()
|
||||
code1 = store.generate_code("telegram", "user1")
|
||||
code2 = store.generate_code("telegram", "user2")
|
||||
assert code1 is not None
|
||||
assert code2 is not None
|
||||
assert isinstance(code1, str) and len(code1) == CODE_LENGTH
|
||||
assert isinstance(code2, str) and len(code2) == CODE_LENGTH
|
||||
|
||||
def test_rate_limit_expires(self, tmp_path):
|
||||
with patch("gateway.pairing.PAIRING_DIR", tmp_path):
|
||||
store = PairingStore()
|
||||
code1 = store.generate_code("telegram", "user1")
|
||||
assert code1 is not None
|
||||
assert isinstance(code1, str) and len(code1) == CODE_LENGTH
|
||||
|
||||
# Simulate rate limit expiry
|
||||
limits = store._load_json(store._rate_limit_path())
|
||||
|
|
@ -114,7 +114,8 @@ class TestRateLimiting:
|
|||
store._save_json(store._rate_limit_path(), limits)
|
||||
|
||||
code2 = store.generate_code("telegram", "user1")
|
||||
assert code2 is not None
|
||||
assert isinstance(code2, str) and len(code2) == CODE_LENGTH
|
||||
assert code2 != code1
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
|
|
@ -132,7 +133,7 @@ class TestMaxPending:
|
|||
codes.append(code)
|
||||
|
||||
# First MAX_PENDING_PER_PLATFORM should succeed
|
||||
assert all(c is not None for c in codes[:MAX_PENDING_PER_PLATFORM])
|
||||
assert all(isinstance(c, str) and len(c) == CODE_LENGTH for c in codes[:MAX_PENDING_PER_PLATFORM])
|
||||
# Next one should be blocked
|
||||
assert codes[MAX_PENDING_PER_PLATFORM] is None
|
||||
|
||||
|
|
@ -143,7 +144,7 @@ class TestMaxPending:
|
|||
store.generate_code("telegram", f"user{i}")
|
||||
# Different platform should still work
|
||||
code = store.generate_code("discord", "user0")
|
||||
assert code is not None
|
||||
assert isinstance(code, str) and len(code) == CODE_LENGTH
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
|
|
@ -158,7 +159,9 @@ class TestApprovalFlow:
|
|||
code = store.generate_code("telegram", "user1", "Alice")
|
||||
result = store.approve_code("telegram", code)
|
||||
|
||||
assert result is not None
|
||||
assert isinstance(result, dict)
|
||||
assert "user_id" in result
|
||||
assert "user_name" in result
|
||||
assert result["user_id"] == "user1"
|
||||
assert result["user_name"] == "Alice"
|
||||
|
||||
|
|
@ -187,14 +190,18 @@ class TestApprovalFlow:
|
|||
store = PairingStore()
|
||||
code = store.generate_code("telegram", "user1", "Alice")
|
||||
result = store.approve_code("telegram", code.lower())
|
||||
assert result is not None
|
||||
assert isinstance(result, dict)
|
||||
assert result["user_id"] == "user1"
|
||||
assert result["user_name"] == "Alice"
|
||||
|
||||
def test_approve_strips_whitespace(self, tmp_path):
|
||||
with patch("gateway.pairing.PAIRING_DIR", tmp_path):
|
||||
store = PairingStore()
|
||||
code = store.generate_code("telegram", "user1", "Alice")
|
||||
result = store.approve_code("telegram", f" {code} ")
|
||||
assert result is not None
|
||||
assert isinstance(result, dict)
|
||||
assert result["user_id"] == "user1"
|
||||
assert result["user_name"] == "Alice"
|
||||
|
||||
def test_invalid_code_returns_none(self, tmp_path):
|
||||
with patch("gateway.pairing.PAIRING_DIR", tmp_path):
|
||||
|
|
|
|||
|
|
@ -92,36 +92,50 @@ class TestExtractImages:
|
|||
content = ""
|
||||
images, _ = BasePlatformAdapter.extract_images(content)
|
||||
assert len(images) == 1
|
||||
assert images[0][0] == "https://example.com/photo.jpg"
|
||||
assert images[0][1] == "photo"
|
||||
|
||||
def test_markdown_image_jpeg(self):
|
||||
content = ""
|
||||
images, _ = BasePlatformAdapter.extract_images(content)
|
||||
assert len(images) == 1
|
||||
assert images[0][0] == "https://example.com/photo.jpeg"
|
||||
assert images[0][1] == ""
|
||||
|
||||
def test_markdown_image_gif(self):
|
||||
content = ""
|
||||
images, _ = BasePlatformAdapter.extract_images(content)
|
||||
assert len(images) == 1
|
||||
assert images[0][0] == "https://example.com/anim.gif"
|
||||
assert images[0][1] == "anim"
|
||||
|
||||
def test_markdown_image_webp(self):
|
||||
content = ""
|
||||
images, _ = BasePlatformAdapter.extract_images(content)
|
||||
assert len(images) == 1
|
||||
assert images[0][0] == "https://example.com/img.webp"
|
||||
assert images[0][1] == ""
|
||||
|
||||
def test_fal_media_cdn(self):
|
||||
content = ""
|
||||
images, _ = BasePlatformAdapter.extract_images(content)
|
||||
assert len(images) == 1
|
||||
assert images[0][0] == "https://fal.media/files/abc123/output.png"
|
||||
assert images[0][1] == "gen"
|
||||
|
||||
def test_fal_cdn_url(self):
|
||||
content = ""
|
||||
images, _ = BasePlatformAdapter.extract_images(content)
|
||||
assert len(images) == 1
|
||||
assert images[0][0] == "https://fal-cdn.example.com/result"
|
||||
assert images[0][1] == ""
|
||||
|
||||
def test_replicate_delivery(self):
|
||||
content = ""
|
||||
images, _ = BasePlatformAdapter.extract_images(content)
|
||||
assert len(images) == 1
|
||||
assert images[0][0] == "https://replicate.delivery/pbxt/abc/output"
|
||||
assert images[0][1] == ""
|
||||
|
||||
def test_non_image_ext_not_extracted(self):
|
||||
"""Markdown image with non-image extension should not be extracted."""
|
||||
|
|
@ -142,11 +156,15 @@ class TestExtractImages:
|
|||
content = '<img src="https://example.com/photo.png"/>'
|
||||
images, _ = BasePlatformAdapter.extract_images(content)
|
||||
assert len(images) == 1
|
||||
assert images[0][0] == "https://example.com/photo.png"
|
||||
assert images[0][1] == ""
|
||||
|
||||
def test_html_img_with_closing_tag(self):
|
||||
content = '<img src="https://example.com/photo.png"></img>'
|
||||
images, _ = BasePlatformAdapter.extract_images(content)
|
||||
assert len(images) == 1
|
||||
assert images[0][0] == "https://example.com/photo.png"
|
||||
assert images[0][1] == ""
|
||||
|
||||
def test_multiple_images(self):
|
||||
content = "\n"
|
||||
|
|
@ -267,6 +285,11 @@ class TestTruncateMessage:
|
|||
msg = "word " * 200 # ~1000 chars
|
||||
chunks = adapter.truncate_message(msg, max_length=200)
|
||||
assert len(chunks) > 1
|
||||
# Verify all original content is preserved across chunks
|
||||
reassembled = "".join(chunks)
|
||||
# Strip chunk indicators like (1/N) to get raw content
|
||||
for word in msg.strip().split():
|
||||
assert word in reassembled, f"Word '{word}' lost during truncation"
|
||||
|
||||
def test_chunks_have_indicators(self):
|
||||
adapter = self._adapter()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue