"""Tests for tiered MemoryManager."""
from __future__ import annotations


class TestMemoryType:
    def test_enum_values(self) -> None:
        from claim_validator.chains.memory.manager import MemoryType
        assert MemoryType.WITHIN_CLAIM == "within_claim"
        assert MemoryType.CROSS_CLAIM == "cross_claim"
        assert MemoryType.MULTI_TURN == "multi_turn"


class TestMemoryManagerWithinClaim:
    def test_get_history_creates_new(self) -> None:
        from claim_validator.chains.memory.manager import MemoryManager, MemoryType
        mgr = MemoryManager()
        h = mgr.get_history("claim-1", MemoryType.WITHIN_CLAIM)
        assert h is not None

    def test_get_history_returns_same_instance(self) -> None:
        from claim_validator.chains.memory.manager import MemoryManager, MemoryType
        mgr = MemoryManager()
        h1 = mgr.get_history("claim-1", MemoryType.WITHIN_CLAIM)
        h2 = mgr.get_history("claim-1", MemoryType.WITHIN_CLAIM)
        assert h1 is h2

    def test_different_sessions_different_history(self) -> None:
        from claim_validator.chains.memory.manager import MemoryManager, MemoryType
        mgr = MemoryManager()
        h1 = mgr.get_history("claim-1", MemoryType.WITHIN_CLAIM)
        h2 = mgr.get_history("claim-2", MemoryType.WITHIN_CLAIM)
        assert h1 is not h2

    def test_clear_claim_memory(self) -> None:
        from claim_validator.chains.memory.manager import MemoryManager, MemoryType
        mgr = MemoryManager()
        mgr.get_history("claim-1", MemoryType.WITHIN_CLAIM)
        mgr.clear_claim_memory("claim-1")
        h = mgr.get_history("claim-1", MemoryType.WITHIN_CLAIM)
        assert h.messages == []

    def test_clear_nonexistent_is_noop(self) -> None:
        from claim_validator.chains.memory.manager import MemoryManager
        mgr = MemoryManager()
        mgr.clear_claim_memory("nonexistent")  # Should not raise


class TestMemoryManagerThreadSafety:
    def test_concurrent_access(self) -> None:
        import threading
        from claim_validator.chains.memory.manager import MemoryManager, MemoryType
        mgr = MemoryManager()
        results: list[object] = []
        def get_hist(session_id: str) -> None:
            h = mgr.get_history(session_id, MemoryType.WITHIN_CLAIM)
            results.append(h)
        threads = [threading.Thread(target=get_hist, args=(f"s-{i}",)) for i in range(10)]
        for t in threads:
            t.start()
        for t in threads:
            t.join()
        assert len(results) == 10
