"""Tests for CostOracle — cost and savings calculations."""

from llmhosts.metrics.cost_oracle import CostOracle


class TestCostOracle:
    def test_known_model_cloud_cost(self):
        oracle = CostOracle()
        report = oracle.calculate("gpt-4o", input_tokens=1000, output_tokens=500)
        # 1000 * 2.50/1M + 500 * 10.00/1M = 0.0025 + 0.005 = 0.0075
        assert abs(report.cloud_cost_usd - 0.0075) < 0.0001

    def test_local_cost_is_near_zero(self):
        oracle = CostOracle()
        report = oracle.calculate("llama3.2:3b", input_tokens=1000, output_tokens=500)
        assert report.local_cost_usd < 0.001  # electricity only

    def test_savings_positive(self):
        oracle = CostOracle()
        report = oracle.calculate("gpt-4o", input_tokens=1000, output_tokens=500)
        assert report.savings_usd > 0
        assert report.savings_pct > 0

    def test_cumulative_savings(self):
        oracle = CostOracle()
        oracle.calculate("gpt-4o", 1000, 500)
        oracle.calculate("gpt-4o", 1000, 500)
        assert oracle.cumulative_savings() > 0
        summary = oracle.session_summary()
        assert summary["total_calls"] == 2

    def test_unknown_model_fallback(self):
        oracle = CostOracle()
        # Should not raise; falls back to gpt-4o-mini
        report = oracle.calculate("unknown-model-xyz", 1000, 500)
        assert report.cloud_cost_usd > 0

    def test_savings_report_to_dict(self):
        oracle = CostOracle()
        report = oracle.calculate("gpt-4o", 1000, 500)
        d = report.to_dict()
        assert "savings_usd" in d
        assert "savings_pct" in d
        assert "timestamp" in d

    def test_empty_session_summary(self):
        oracle = CostOracle()
        summary = oracle.session_summary()
        assert summary["total_calls"] == 0
        assert summary["total_savings_usd"] == 0.0

    def test_70b_model_size_watt_detection(self):
        oracle = CostOracle()
        report = oracle.calculate("llama3.3:70b", 1000, 500)
        # 70b has higher electricity cost than default
        oracle2 = CostOracle()
        report2 = oracle2.calculate("llama3.2:3b", 1000, 500)
        assert report.local_cost_usd > report2.local_cost_usd
