test: add tests for discover() functionality

8 tests covering:
- Basic discovery returns entries
- Count limits
- Topic filtering (case-insensitive)
- Empty archive handling
- Vitality boost via touch
This commit is contained in:
2026-04-12 10:28:39 +00:00
parent af297927a2
commit 7b3f8f09df

View File

@@ -0,0 +1,85 @@
"""Tests for Mnemosyne discover functionality."""
import tempfile
from pathlib import Path
from nexus.mnemosyne.archive import MnemosyneArchive
from nexus.mnemosyne.ingest import ingest_event
def _make_archive_with_entries():
"""Helper: create an archive with test entries."""
path = Path(tempfile.mkdtemp()) / "test_archive.json"
archive = MnemosyneArchive(archive_path=path, auto_embed=False)
ingest_event(archive, title="Python automation", content="Building tools in Python", topics=["python", "automation"])
ingest_event(archive, title="Cooking pasta", content="How to make carbonara", topics=["cooking"])
ingest_event(archive, title="Bitcoin basics", content="Understanding Bitcoin and blockchain", topics=["bitcoin", "crypto"])
ingest_event(archive, title="AI agents", content="Building autonomous AI agents", topics=["ai", "agents"])
ingest_event(archive, title="Meditation guide", content="Mindfulness and meditation techniques", topics=["wellness"])
return archive
def test_discover_returns_entries():
archive = _make_archive_with_entries()
results = archive.discover(count=3)
assert len(results) == 3
for r in results:
assert "entry_id" in r
assert "title" in r
assert "content_preview" in r
assert "topics" in r
assert "vitality" in r
assert "age_days" in r
def test_discover_respects_count():
archive = _make_archive_with_entries()
results = archive.discover(count=2)
assert len(results) == 2
def test_discover_count_exceeds_entries():
archive = _make_archive_with_entries()
results = archive.discover(count=100)
assert len(results) == archive.count
def test_discover_topic_filter():
archive = _make_archive_with_entries()
results = archive.discover(count=10, topic="python")
assert len(results) == 1
assert results[0]["title"] == "Python automation"
def test_discover_topic_case_insensitive():
archive = _make_archive_with_entries()
results = archive.discover(count=10, topic="Python")
assert len(results) == 1
def test_discover_empty_topic_returns_nothing():
archive = _make_archive_with_entries()
results = archive.discover(count=10, topic="nonexistent")
assert len(results) == 0
def test_discover_boosts_vitality():
archive = _make_archive_with_entries()
# Get initial vitality
before = archive.fading(limit=5)
# Discover (which touches entries)
archive.discover(count=3)
# The touched entries should have higher vitality now
after = archive.fading(limit=5)
# At least some entries should have changed vitality
before_vitals = {e["entry_id"]: e["vitality"] for e in before}
after_vitals = {e["entry_id"]: e["vitality"] for e in after}
changed = sum(1 for eid in before_vitals if eid in after_vitals and abs(before_vitals[eid] - after_vitals[eid]) > 0.001)
assert changed >= 1, "Discover should touch and boost vitality of selected entries"
def test_discover_empty_archive():
path = Path(tempfile.mkdtemp()) / "empty.json"
archive = MnemosyneArchive(archive_path=path, auto_embed=False)
results = archive.discover(count=5)
assert len(results) == 0