
from genesis_memory_cortex import MemoryCortex, MemoryTier
import time

def test_synapse():
    print("🧠 Initializing Core for Synapse Test...")
    cortex = MemoryCortex()
    
    # Ensure some existing context
    print("\n📝 Step 1: Writing first memory...")
    cortex.remember(
        "The Genesis Heartbeat is the central cognitive loop that synchronizes all components including surprise detection and axiom generation.",
        source="synapse_test",
        domain="core_logic",
        force_tier=MemoryTier.SEMANTIC
    )
    
    print("\n⏳ Waiting for index sync...")
    time.sleep(2)
    
    print("\n📝 Step 2: Writing related memory (should trigger Synapse Linking)...")
    res = cortex.remember(
        "Axiom generation in Genesis is driven by high-surprise events captured during the heartbeat's collection phase.",
        source="synapse_test",
        domain="core_logic",
        force_tier=MemoryTier.SEMANTIC
    )
    
    print("\n📊 RESULTS:")
    print(f"  Memory stored in: {res['stored_in']}")
    
    # Check metadata for links
    id_list = res.get("stored_in", [])
    if "vector_qdrant" in id_list:
        print("  ✅ Successfully stored in Vector Qdrant")
        
    # Recall to verify risk level and relations
    print("\n🔍 Recalling to verify metadata associations...")
    recalled = cortex.recall("heartbeat axiom generation", tiers=[MemoryTier.SEMANTIC], use_vectors=True)
    if recalled:
        meta = recalled[0]['memory']['metadata']
        print(f"  Risk Level: {meta.get('risk_level', 'MISSING')}")
        print(f"  Relations: {meta.get('relations', 'MISSING')}")
        
        if meta.get('relations'):
            print("  ✅ SYNAPSE LINKING VERIFIED: Relations found!")
        else:
            print("  ❌ SYNAPSE LINKING FAILED: No relations found.")
    else:
        print("  ❌ Recall failed.")

if __name__ == "__main__":
    test_synapse()
