import pytest
from atomic_agents.core import AuditLedger


class TestAuditLedger:
    def test_record_entry(self):
        ledger = AuditLedger()
        entry = ledger.record({"event": "test_event", "data": "test"})
        
        assert entry["data"]["event"] == "test_event"
        assert "hash" in entry
        assert "timestamp" in entry
        assert len(entry["hash"]) == 64  # SHA256 hex length

    def test_chain_integrity(self):
        ledger = AuditLedger()
        entry1 = ledger.record({"event": "event1"})
        entry2 = ledger.record({"event": "event2"})
        
        assert entry2["previous_hash"] == entry1["hash"]
        assert len(ledger.entries) == 2

    def test_immutability(self):
        ledger = AuditLedger()
        entry1 = ledger.record({"event": "event1"})
        hash1 = entry1["hash"]
        
        # Record another entry
        ledger.record({"event": "event2"})
        
        # First entry should remain unchanged
        assert ledger.entries[0]["hash"] == hash1

    def test_multiple_entries(self):
        ledger = AuditLedger()
        for i in range(5):
            ledger.record({"event": f"event_{i}"})
        
        assert len(ledger.entries) == 5
        # Check chain integrity
        for i in range(1, 5):
            assert ledger.entries[i]["previous_hash"] == ledger.entries[i-1]["hash"]

    def test_hash_uniqueness(self):
        ledger = AuditLedger()
        entry1 = ledger.record({"event": "event1", "data": "a"})
        entry2 = ledger.record({"event": "event1", "data": "b"})
        
        assert entry1["hash"] != entry2["hash"]


