Added integration tests for uningest_message.
CI / Formatting (push) Successful in 6s
CI / Linting (push) Successful in 4s
CI / Tests (push) Successful in 24s
CI / Type Checking (push) Successful in 11s
CI / Spelling (push) Successful in 5s

This commit is contained in:
2026-03-23 15:45:31 -04:00
parent bc9f7fa539
commit cf46b71883
+118
View File
@@ -21,6 +21,7 @@ import pytest
from crabstero.cache import CachedMessage, IngestCache
from crabstero.database import ChannelImage
from crabstero.markov import ingest, uningest
from crabstero.messages import uningest_message
if TYPE_CHECKING:
import aiosqlite
@@ -148,3 +149,120 @@ class TestUningestRestoresState:
await uningest(db, 1, 100, text)
after = await _snapshot(db)
assert after == before
class TestUningestMessage:
"""Orchestrated uningest via cache lookup and database reversal."""
async def test_content_only(self, db: Database) -> None:
"""Uningest reverses a content-only message via the cache."""
await ingest(db, 1, 100, "Hello beautiful world.")
cache = IngestCache()
cache.put(
555,
CachedMessage(
channel_id=1,
user_id=100,
content="Hello beautiful world.",
embed_texts=[],
image_urls=[],
),
)
await uningest_message(db, cache, 555)
assert await db.get_random_start_word(1) is None
assert await db.get_random_next_word(1, "Hello") is None
async def test_embeds_only(self, db: Database) -> None:
"""Uningest reverses embed text ingestion."""
await ingest(db, 1, 100, "Embed title here.")
await ingest(db, 1, 100, "Embed description here.")
cache = IngestCache()
cache.put(
556,
CachedMessage(
channel_id=1,
user_id=100,
content=None,
embed_texts=["Embed title here.", "Embed description here."],
image_urls=[],
),
)
await uningest_message(db, cache, 556)
assert await db.get_random_start_word(1) is None
async def test_content_with_embeds_and_images(self, db: Database) -> None:
"""Uningest reverses content, embed text, and image data together."""
await ingest(db, 1, 100, "Body text here.")
await ingest(db, 1, 100, "Embed title.")
await db.add_images([ChannelImage(1, 100, "https://example.com/img.png")])
cache = IngestCache()
cache.put(
557,
CachedMessage(
channel_id=1,
user_id=100,
content="Body text here.",
embed_texts=["Embed title."],
image_urls=["https://example.com/img.png"],
),
)
await uningest_message(db, cache, 557)
assert await db.get_random_start_word(1) is None
assert await db.get_random_image(1) is None
async def test_cache_miss_is_noop(self, db: Database) -> None:
"""A message not in the cache leaves the database unchanged."""
await ingest(db, 1, 100, "Keep this data.")
cache = IngestCache()
before = await _snapshot(db)
await uningest_message(db, cache, 999)
after = await _snapshot(db)
assert after == before
async def test_preserves_other_messages(self, db: Database) -> None:
"""Uningesting one message leaves another message's data intact."""
await ingest(db, 1, 100, "First message.")
await ingest(db, 1, 100, "Second message.")
cache = IngestCache()
cache.put(
601,
CachedMessage(
channel_id=1,
user_id=100,
content="First message.",
embed_texts=[],
image_urls=[],
),
)
await uningest_message(db, cache, 601)
assert await db.get_random_start_word(1) == "Second"
assert await db.get_random_next_word(1, "Second") == "message."
async def test_pops_entry_from_cache(self, db: Database) -> None:
"""The cache entry is consumed after uningest."""
await ingest(db, 1, 100, "Hello world.")
cache = IngestCache()
cache.put(
602,
CachedMessage(
channel_id=1,
user_id=100,
content="Hello world.",
embed_texts=[],
image_urls=[],
),
)
await uningest_message(db, cache, 602)
assert cache.pop(602) is None