"""Tests for the bundled tiktoken dependency.

tiktoken is bundled in earningscall-development so downstream projects (CCA)
can do OpenAI-style token counting for model cost estimation. These tests:

  * guard the version floor -- tiktoken < 0.8.0 has no cp313/cp314 prebuilt
    wheel and forces a Rust source build that fails in the slim base images,
  * verify the encodings CCA actually uses tokenize correctly.
"""

from importlib.metadata import version

import pytest
from packaging.version import Version

import tiktoken

# CCA uses o200k_base (gpt-4o family) with a cl100k_base fallback.
ENCODINGS_USED_BY_CCA = ["o200k_base", "cl100k_base"]


def test_tiktoken_version_supports_prebuilt_wheels():
    """tiktoken must be >= 0.8.0 so it installs from a prebuilt wheel on
    Python 3.13+ (no Rust compiler required in the base images)."""
    installed = Version(version("tiktoken"))
    assert installed >= Version("0.8.0"), (
        f"tiktoken {installed} predates cp313 wheels and would force a Rust "
        "source build; bump the lower bound in pyproject.toml"
    )


@pytest.mark.parametrize("encoding_name", ENCODINGS_USED_BY_CCA)
def test_encoding_roundtrip(encoding_name):
    enc = tiktoken.get_encoding(encoding_name)
    text = "Hello, earnings call! Revenue grew 12% year over year."
    tokens = enc.encode(text)
    assert len(tokens) > 0
    assert enc.decode(tokens) == text


def test_encoding_for_gpt4o_resolves_to_o200k_base():
    """CCA's cost estimator maps gpt-4o -> o200k_base; confirm tiktoken agrees."""
    enc = tiktoken.encoding_for_model("gpt-4o")
    assert enc.name == "o200k_base"
    assert len(enc.encode("net income was $4.2 billion")) > 0
