"""Test de integración: persistent memory + retrieve con shcortex-lib (PyO3)."""
import shcortex_lib
import time
import os

DB = os.path.expanduser("~/.shcortex/memory.db")

def test_persistent_flow():
    print("=== Testing SHCortex Persistent Memory ===")

    sc = shcortex_lib.SHCortex()

    # 1. Compress
    text = "Alice is a data scientist developing SHCortex in Rust. Bob researches quantum physics in London."
    print(f"\n--- Compress ---")
    start = time.perf_counter()
    sc.compress(text, 0.7, 5)
    lat1 = time.perf_counter() - start
    print(f"Compress latency: {lat1*1000:.2f} ms")

    # 2. Build prompt (now with context retrieval)
    print(f"\n--- Prompt ---")
    prompt = sc.build_prompt("What does Alice do?")
    print(f"Prompt: {prompt[:150]}...")

    # 3. Retrieve from persistent memory
    print(f"\n--- Retrieve ---")
    start = time.perf_counter()
    results = sc.retrieve("Alice data scientist")
    lat2 = time.perf_counter() - start
    print(f"Retrieve latency: {lat2*1000:.2f} ms")
    print(f"Results ({len(results)}):")
    for r in results:
        print(f"  • {r[:100]}")

    # 4. Mem stats
    print(f"\n--- Memory Stats ---")
    stats = sc.mem_stats()
    print(stats)

    # Verify
    assert len(results) > 0, "Should find at least one result"
    assert os.path.exists(DB), "DB should exist"
    print(f"\n✅ All tests passed")

if __name__ == "__main__":
    test_persistent_flow()
