"""Tests for axm-audit Auditor class."""


class TestAuditor:
    """Tests for Auditor class."""

    def test_auditor_on_validate_start(self) -> None:
        """Auditor tracks validation start."""
        from axm_audit.auditor import Auditor

        auditor = Auditor()
        auditor.on_validate_start("test_entry")
        # Should not raise

    def test_auditor_on_validate_end_tracks_metrics(self) -> None:
        """Auditor accumulates metrics on validate end."""
        from axm_audit.auditor import Auditor

        auditor = Auditor()
        auditor.on_validate_end("test_entry", is_valid=True, duration_ns=1_000_000)

        assert auditor.metrics.total_validations == 1
        assert auditor.metrics.total_duration_ns == 1_000_000
        assert "test_entry" in auditor.metrics.entries

    def test_auditor_metrics_total_ms(self) -> None:
        """AuditMetrics.total_ms converts to milliseconds."""
        from axm_audit.auditor import AuditMetrics

        metrics = AuditMetrics(total_duration_ns=5_000_000)
        assert metrics.total_ms == 5.0

    def test_global_auditor_exists(self) -> None:
        """Global auditor instance is available."""
        from axm_audit import auditor

        assert auditor is not None


class TestPipelineMetrics:
    """Tests for pipeline timing metrics (Trust Tax)."""

    def test_auditor_on_pipeline_start(self) -> None:
        """Auditor tracks pipeline start."""
        from axm_audit.auditor import Auditor

        auditor = Auditor()
        auditor.on_pipeline_start("test_pipeline")
        # Should not raise

    def test_auditor_on_pipeline_end_tracks_metrics(self) -> None:
        """Auditor accumulates pipeline duration on end."""
        from axm_audit.auditor import Auditor

        auditor = Auditor()
        auditor.on_pipeline_start("test_pipeline")
        auditor.on_pipeline_end("test_pipeline", duration_ns=50_000_000)

        assert auditor.metrics.pipeline_duration_ns == 50_000_000

    def test_auditor_metrics_pipeline_ms(self) -> None:
        """AuditMetrics.pipeline_ms converts to milliseconds."""
        from axm_audit.auditor import AuditMetrics

        metrics = AuditMetrics(pipeline_duration_ns=45_000_000)
        assert metrics.pipeline_ms == 45.0

    def test_trust_tax_calculation(self) -> None:
        """Trust Tax = validation_time / pipeline_time x 100."""
        from axm_audit.auditor import AuditMetrics

        metrics = AuditMetrics(
            total_duration_ns=1_500_000,  # 1.5ms validation
            pipeline_duration_ns=50_000_000,  # 50ms total
        )
        assert metrics.trust_tax_percent == 3.0  # 1.5 / 50 * 100 = 3%


class TestAutoRegistration:
    """Tests for auto-registration on import."""

    def test_import_registers_hook(self) -> None:
        """Importing axm_audit registers the auditor as hook."""
        from axm.observability.hooks import get_audit_hook, set_audit_hook
        from axm_audit import auditor

        # Explicitly register (may have been reset by other tests)
        set_audit_hook(auditor)
        hook = get_audit_hook()
        assert hook is auditor
