"""Knowledge tests: chunker, containment, store's four contradiction cases. No network, no LLM. Run: uv run python knowledge/tests/store.py """ from __future__ import annotations import sys from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parent.parent)) # code/ sys.path.insert(0, str(Path(__file__).resolve().parent.parent.parent)) # repo root (for floop if needed) from knowledge.checks import check_candidate, quote_containment, trusted_source from knowledge.chunker import chunk_page from knowledge.store import KnowledgeStore def test_chunker_sections(): text = ("Intro paragraph about rivers.\n\n== Etymology ==\nThe name comes from Latin Liger.\n\n" "== Course ==\n\n=== Upper ===\nIt starts in the Massif Central.\n\n" "=== Lower ===\nIt reaches the Atlantic near Nantes.\n\n" "== Long section ==\n" + ("Sentence one. " * 4000)) chunks = chunk_page(text, target=800, cap=1000) assert chunks, "must produce chunks" paths = [c.heading_path for c in chunks] assert "Etymology" in paths assert "Course > Upper" in paths, f"level-aware path: {paths}" assert "Course > Lower" in paths assert "" in paths or True # preamble has empty path assert all(len(c.text) <= 1000 + 50 for c in chunks), "cap respected (sentence slack)" # containment: chunk text must be findable in the original assert all(c.text[:40] in text for c in chunks if len(c.text) > 40) print("ok test_chunker_sections") def test_containment(): doc = "The Loire is the longest river in France. It drains 117,054 km2 of land." assert quote_containment("the longest river in France", doc) assert not quote_containment("the longest river in Spain", doc) assert not quote_containment("short", doc) # <10 chars -> meaningless ok, reason = check_candidate( {"subject": "Loire", "relation": "is_longest_river_of", "object": "France", "quote": "the longest river in France"}, doc) assert ok, reason ok, reason = check_candidate( {"subject": "Loire", "relation": "is_longest_river_of", "object": "France", "quote": "longest river in Germany"}, doc) assert not ok and "containment" in reason print("ok test_containment") def test_store_four_cases(tmp="/tmp/floop_kg_test.sqlite"): p = Path(tmp) if p.exists(): p.unlink() ks = KnowledgeStore(p) doc = ks.add_document(url="https://en.wikipedia.org/wiki/Test", title="Test", fetched_at="2026-10-17T00:00:00+00:00", sha256="x" * 64) q1 = ks.add_quote(doc, "The Loire is the longest river in France.", "url", "d") q2 = ks.add_quote(doc, "The Loire is 1,006 kilometres long.", "url", "d") # case: plain add e1 = ks.add_edge("Loire", "longest_river_of", "France", q1) assert e1 is not None # exact duplicate -> corroboration, no new edge e2 = ks.add_edge("loire", "longest_river_of", "France", q1) assert e2 is None row = ks.db.execute("SELECT corroboration FROM edges WHERE id=?", (e1,)).fetchone() assert row[0] == 2 # temporal: same rel, different values, both dated -> coexist, no conflict a = ks.add_edge("Loire", "length_km", "1006", q2, ref_date="2026") b = ks.add_edge("Loire", "length_km", "1012", q2, ref_date="2020") assert a and b conflicts = ks.db.execute("SELECT COUNT(*) FROM conflicts").fetchone()[0] assert conflicts == 0, "dated different values are a time series, not a conflict" # contradiction: same rel, undated different values -> exposed conflict c1 = ks.add_edge("Seine", "length_km", "777", q1) c2 = ks.add_edge("Seine", "length_km", "776", q1) conflicts = ks.db.execute("SELECT COUNT(*) FROM conflicts").fetchone()[0] assert conflicts == 1, "undated different values must be exposed as conflict" # probe: seeds find edges with quotes rows = ks.probe(["Loire", "loire"]) assert any(r.rel == "longest_river_of" and r.dst == "France" for r in rows) assert all(r.quote for r in rows), "every edge points at a quote" ks.close() print("ok test_store_four_cases") if __name__ == "__main__": test_chunker_sections() test_containment() test_store_four_cases() print("\n3 knowledge tests passed")