104 lines
3.9 KiB
Python
104 lines
3.9 KiB
Python
#!/usr/bin/env python3
|
|
"""Smoke tests for scripts/wiki.py — retrieval and lint basics."""
|
|
|
|
import json
|
|
import os
|
|
import sys
|
|
import tempfile
|
|
from pathlib import Path
|
|
|
|
SCRIPT_DIR = Path(__file__).parent.absolute()
|
|
sys.path.insert(0, str(SCRIPT_DIR))
|
|
|
|
import wiki
|
|
|
|
def test_retrieve_facts():
|
|
"""Test fact retrieval ranking."""
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
kdir = Path(tmpdir) / "knowledge"
|
|
kdir.mkdir()
|
|
index = {
|
|
"version": 1,
|
|
"total_facts": 3,
|
|
"facts": [
|
|
{
|
|
"id": "test:fact:001",
|
|
"fact": "Gitea token is stored at ~/.config/gitea/token",
|
|
"category": "tool-quirk",
|
|
"domain": "global",
|
|
"confidence": 0.95,
|
|
"tags": ["token", "gitea", "auth"],
|
|
"last_confirmed": "2026-04-01"
|
|
},
|
|
{
|
|
"id": "test:fact:002",
|
|
"fact": "Use gitea-api-first-burn worker for large repos",
|
|
"category": "pattern",
|
|
"domain": "timmy-config",
|
|
"confidence": 0.9,
|
|
"tags": ["gitea", "burn", "api"],
|
|
},
|
|
{
|
|
"id": "test:fact:003",
|
|
"fact": "Hermes gateway restarts required after Telegram config changes",
|
|
"category": "pitfall",
|
|
"domain": "hermes-agent",
|
|
"confidence": 0.85,
|
|
"tags": ["telegram", "gateway"],
|
|
}
|
|
]
|
|
}
|
|
index_path = kdir / "index.json"
|
|
with open(index_path, 'w') as f:
|
|
json.dump(index, f)
|
|
|
|
original_index = wiki.INDEX_PATH
|
|
wiki.INDEX_PATH = index_path
|
|
|
|
try:
|
|
results = wiki.retrieve_facts("where is gitea token stored?", limit=5)
|
|
assert len(results) >= 1, f"Expected at least 1 result, got {len(results)}"
|
|
assert results[0]['id'] == 'test:fact:001', f"Expected fact 001 first, got {results[0]['id']}"
|
|
print(" [PASS] retrieve_facts ranks correctly")
|
|
|
|
results2 = wiki.retrieve_facts("gitea burn large repos", limit=5)
|
|
assert len(results2) >= 1
|
|
assert results2[0]['id'] == 'test:fact:002'
|
|
print(" [PASS] tag-based retrieval works")
|
|
finally:
|
|
wiki.INDEX_PATH = original_index
|
|
|
|
def test_format_context():
|
|
"""Test context formatting for LLM."""
|
|
facts = [
|
|
{"id": "a:1", "fact": "Test fact A", "category": "fact", "confidence": 0.9},
|
|
{"id": "b:2", "fact": "Test fact B", "category": "pitfall", "confidence": 0.8},
|
|
]
|
|
ctx = wiki.format_facts_as_context(facts)
|
|
assert "[1]" in ctx and "a:1" in ctx
|
|
assert "Test fact A" in ctx
|
|
assert "Test fact B" in ctx
|
|
print(" [PASS] format_facts_as_context includes IDs and facts")
|
|
|
|
def test_detect_contradictions():
|
|
"""Test contradiction detection."""
|
|
index = {
|
|
"facts": [
|
|
{"id": "x:1", "fact": "Deploy uses port 22 for SSH", "category": "fact", "domain": "deploy"},
|
|
{"id": "x:2", "fact": "Deploy uses SSH on port 22", "category": "fact", "domain": "deploy"},
|
|
{"id": "x:3", "fact": "Cron jobs require model field", "category": "pitfall", "domain": "hermes-agent"},
|
|
]
|
|
}
|
|
contradictions = wiki.detect_contradictions(index)
|
|
assert len(contradictions) >= 1, "Expected at least one potential contradiction"
|
|
found = any('x:1' in c.get('fact_a','') or 'x:1' in c.get('fact_b','') for c in contradictions)
|
|
assert found, "Should detect similarity between x:1 and x:2"
|
|
print(" [PASS] detect_contradictions flags similar facts")
|
|
|
|
if __name__ == "__main__":
|
|
print("Running wiki module smoke tests...")
|
|
test_retrieve_facts()
|
|
test_format_context()
|
|
test_detect_contradictions()
|
|
print("\nAll wiki tests passed.")
|