"""
test_memory_policy.py - Basic tests for CyberBrain26 memory policy
"""

import sys
from pathlib import Path

PROJECT_ROOT = Path(__file__).resolve().parent.parent
sys.path.insert(0, str(PROJECT_ROOT))

def test_test_records_excluded():
    """Test records with kind='test' should not appear in active context"""
    print("\n=== Test: test records excluded ===")
    test_record = {'kind': 'test', 'status': 'active'}
    active_kinds = {'rule', 'preference', 'project_decision', 'fact'}
    if test_record['kind'] in active_kinds:
        print("FAIL: test kind incorrectly included")
        return False
    else:
        print("PASS: test kind correctly excluded")
        return True

def test_archived_records_excluded():
    """Archived records should not appear in active context"""
    print("\n=== Test: archived records excluded ===")
    archived_record = {'kind': 'fact', 'status': 'archived'}
    if archived_record['status'] == 'archived':
        print("PASS: archived status correctly excluded")
        return True
    else:
        print("FAIL: archived status check missing")
        return False

def test_supersedes_override():
    """New decision with supersedes should override older one"""
    print("\n=== Test: supersedes override ===")
    old_decision = {'id': 'dec-001', 'supersedes': None}
    new_decision = {'id': 'dec-002', 'supersedes': 'dec-001'}
    decisions = [old_decision, new_decision]
    superseded_ids = {d['supersedes'] for d in decisions if d['supersedes']}
    active_decisions = [d for d in decisions if d['id'] not in superseded_ids]
    if len(active_decisions) == 1 and active_decisions[0]['id'] == 'dec-002':
        print("PASS: supersedes logic works correctly")
        return True
    else:
        print(f"FAIL: expected 1 active decision, got {len(active_decisions)}")
        return False

def test_conversation_kind():
    """Conversation kind should stay in DB but not in active prompt"""
    print("\n=== Test: conversation kind ===")
    conversation_record = {'kind': 'conversation', 'status': 'active'}
    active_kinds = {'rule', 'preference', 'project_decision', 'fact'}
    in_active_context = conversation_record['kind'] in active_kinds
    if not in_active_context:
        print("PASS: conversation correctly excluded from active context")
        return True
    else:
        print("FAIL: conversation handling incorrect")
        return False

def run_all_tests():
    print("=" * 60)
    print("CyberBrain26 Memory Policy Tests")
    print(f"Project root: {PROJECT_ROOT}")
    print("=" * 60)
    
    tests = [
        test_test_records_excluded,
        test_archived_records_excluded,
        test_supersedes_override,
        test_conversation_kind,
    ]
    
    results = [test() for test in tests]
    
    print("\n" + "=" * 60)
    print(f"Results: {sum(results)}/{len(results)} passed")
    print("=" * 60)
    return all(results)

if __name__ == '__main__':
    success = run_all_tests()
    sys.exit(0 if success else 1)
