"""Tests for hash-chained audit log."""

from orxaq.kernel.audit import AuditEntry, AuditLog


class TestAuditLog:
    def test_append_and_verify(self):
        log = AuditLog()
        log.append("create_entity", "conductor", "Created entity GDP")
        log.append("add_edge", "conductor", "Added GDP→Unemployment")
        assert len(log) == 2
        assert log.verify() is True

    def test_genesis_block(self):
        log = AuditLog()
        entry = log.append("init", "system")
        assert entry.prev_hash == "genesis"
        assert entry.sequence == 0

    def test_chain_linkage(self):
        log = AuditLog()
        e1 = log.append("action_1", "actor_1")
        e2 = log.append("action_2", "actor_2")
        assert e2.prev_hash == e1.entry_hash

    def test_tamper_detection(self):
        log = AuditLog()
        log.append("create", "conductor")
        log.append("modify", "conductor")

        # Tamper with the first entry
        original = log._entries[0]
        tampered = AuditEntry(
            sequence=original.sequence,
            timestamp=original.timestamp,
            action="TAMPERED",
            actor=original.actor,
            details=original.details,
            prev_hash=original.prev_hash,
            entry_hash=original.entry_hash,  # Hash no longer matches content
        )
        log._entries[0] = tampered
        assert log.verify() is False

    def test_search_by_action(self):
        log = AuditLog()
        log.append("create", "alice")
        log.append("delete", "bob")
        log.append("create", "charlie")
        results = log.search(action="create")
        assert len(results) == 2

    def test_search_by_actor(self):
        log = AuditLog()
        log.append("create", "alice")
        log.append("delete", "bob")
        log.append("update", "alice")
        results = log.search(actor="alice")
        assert len(results) == 2

    def test_entries_returns_copy(self):
        log = AuditLog()
        log.append("test", "system")
        entries = log.entries()
        entries.clear()
        assert len(log) == 1


# ── Targeted coverage (line 107: invalid genesis prev_hash) ──────


class TestGenesisHashTamper:
    """Cover line 107: first entry's prev_hash != 'genesis' → verify fails."""

    def test_tampered_genesis_fails_verify(self):
        log = AuditLog()
        log.append("create", "alice", "first action")
        log.append("update", "bob", "second action")
        entry = log._entries[0]
        # Tamper prev_hash away from "genesis"
        object.__setattr__(entry, "prev_hash", "not_genesis")
        # Recompute entry_hash so the hash check passes (line 101-102)
        new = AuditEntry(
            sequence=entry.sequence,
            timestamp=entry.timestamp,
            action=entry.action,
            actor=entry.actor,
            details=entry.details,
            prev_hash="not_genesis",
        )
        object.__setattr__(entry, "entry_hash", new.compute_hash())
        assert log.verify() is False
