"""read_cost_from_record_safe reads both cost_usd (canonical) and cost (legacy)."""
from __future__ import annotations

import logging

from recoil.pipeline.core.cost import read_cost_from_record_safe


class TestRecordSafeReadsLegacyKey:
    def test_reads_cost_usd(self):
        assert read_cost_from_record_safe({"cost_usd": 0.42}) == 0.42

    def test_reads_legacy_cost(self):
        assert read_cost_from_record_safe({"cost": 0.42}) == 0.42

    def test_missing_returns_zero_with_fallback(self, caplog):
        with caplog.at_level(logging.WARNING):
            v = read_cost_from_record_safe({"foo": 1})
        assert v == 0.0
        assert any("cost_unknown_telemetry_zero" in r.getMessage() for r in caplog.records)

    def test_non_numeric_returns_zero(self, caplog):
        with caplog.at_level(logging.WARNING):
            v = read_cost_from_record_safe({"cost": "free"})
        assert v == 0.0
