# ruff: noqa: INP001
"""Unit tests for provider usage parsing and normalization helpers."""

from __future__ import annotations

from app.services.provider_usage import (
    _extract_openai_usage,
    _normalize_base,
    _parse_openai_reset,
)


def test_normalize_base_strips_known_suffixes() -> None:
    assert (
        _normalize_base(
            "https://api.openai.com/v1/",
            "https://api.openai.com",
            strip_suffixes=("/v1",),
        )
        == "https://api.openai.com"
    )
    assert (
        _normalize_base(
            "https://ollama.com/api/",
            "http://localhost:11434",
            strip_suffixes=("/api",),
        )
        == "https://ollama.com"
    )


def test_extract_openai_usage_supports_responses_shape() -> None:
    payload = {"usage": {"input_tokens": 12, "output_tokens": 3}}
    assert _extract_openai_usage(payload) == (12, 3)


def test_extract_openai_usage_supports_chat_completions_shape() -> None:
    payload = {"usage": {"prompt_tokens": 9, "completion_tokens": 1}}
    assert _extract_openai_usage(payload) == (9, 1)


def test_parse_openai_reset_duration_format() -> None:
    reset_at = _parse_openai_reset("6m0s")
    assert reset_at is not None
