Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

5.7 KB
Raw Blame
"""Unit tests for cost estimator — verifies pricing for all current models."""

from __future__ import annotations

import pytest

from repowise.cli.cost_estimator import PageTypePlan, estimate_cost, lookup_cost
from repowise.cli.cost_estimator.heuristics import heuristic_tokens

# ---------------------------------------------------------------------------
# Per-model pricing (input_rate, output_rate) per 1K tokens
# ---------------------------------------------------------------------------


@pytest.mark.parametrize(
    "model,expected_input,expected_output",
    [
        # OpenAI GPT-5.4 family
        ("gpt-5.4-nano", 0.0002, 0.00125),
        ("gpt-5.4-mini", 0.00075, 0.0045),
        ("gpt-5.4", 0.0025, 0.015),
        # The default. An unpriced model is estimated as free (lookup_cost
        # returns None), so this row is the guard.
        ("gpt-5.6-luna", 0.0002, 0.0012),
        ("gpt-6-luna", 0.0001, 0.0005),
        ("gpt-6-sol", 0.002, 0.010),
        # Gemini
        ("gemini-3.1-flash-lite-preview", 0.00025, 0.0015),
        ("gemini-3-flash-preview", 0.0005, 0.003),
        ("gemini-3.1-pro-preview", 0.002, 0.012),
        # Anthropic Claude
        ("claude-opus-4-6", 0.005, 0.025),
        ("claude-sonnet-4-6", 0.003, 0.015),
        ("claude-haiku-4-5", 0.001, 0.005),
        ("claude-haiku-5-5", 0.0001, 0.0005),
        ("claude-sonnet-5-5", 0.002, 0.01),
        ("claude-opus-5-5", 0.004, 0.02),
        # Free/local models
        ("mock", 0.0, 0.0),
        ("llama3", 0.0, 0.0),
        ("codex_cli/default", 0.0, 0.0),
        ("codex_cli/gpt-5.5", 0.0, 0.0),
    ],
)
def test_lookup_cost(model, expected_input, expected_output):
    inp, out = lookup_cost(model)
    assert inp == pytest.approx(expected_input, rel=1e-6)
    assert out == pytest.approx(expected_output, rel=1e-6)


# ---------------------------------------------------------------------------
# estimate_cost integration
# ---------------------------------------------------------------------------


def test_estimate_cost_gpt54_nano():
    plans = [PageTypePlan("repo_overview", 1, 6)]
    est = estimate_cost(plans, "openai", "gpt-5.4-nano")
    inp, out = heuristic_tokens("repo_overview")
    expected = (inp / 1000) * 0.0002 + (out / 1000) * 0.00125
    assert est.estimated_cost_usd == pytest.approx(expected, rel=1e-6)
    assert est.model_name == "gpt-5.4-nano"


def test_estimate_cost_claude_opus():
    plans = [PageTypePlan("repo_overview", 1, 6)]
    est = estimate_cost(plans, "anthropic", "claude-opus-4-6")
    inp, out = heuristic_tokens("repo_overview")
    expected = (inp / 1000) * 0.005 + (out / 1000) * 0.025
    assert est.estimated_cost_usd == pytest.approx(expected, rel=1e-6)


def test_estimate_cost_gemini_lite():
    # A model-written type: file_page renders from structure and is free.
    plans = [PageTypePlan("module_page", 10, 4)]
    est = estimate_cost(plans, "gemini", "gemini-3.1-flash-lite-preview")
    inp, out = heuristic_tokens("module_page")
    expected = (inp * 10 / 1000) * 0.00025 + (out * 10 / 1000) * 0.0015
    assert est.estimated_cost_usd == pytest.approx(expected, rel=1e-6)


def test_structural_file_pages_are_free():
    inp, _out = heuristic_tokens("file_page")
    assert inp > 0  # a heuristic exists, but the estimator must not apply it
    est = estimate_cost(
        [PageTypePlan("file_page", 1000, 2)], "gemini", "gemini-3.1-flash-lite-preview"
    )
    assert est.total_pages == 1000
    assert est.estimated_cost_usd == 0.0


def test_estimate_cost_zero_for_mock():
    plans = [PageTypePlan("module_page", 5, 4)]
    est = estimate_cost(plans, "mock", "mock")
    assert est.estimated_cost_usd == 0.0


def test_embedding_is_part_of_the_estimate():
    """Every page is embedded, structural ones included, so all are priced."""
    from repowise.core.cost_estimator.pricing import lookup_embedding_cost
    from repowise.core.persistence.vector_store._base import EMBED_TEXT_MAX_CHARS

    plans = [PageTypePlan("file_page", 1000, 2)]
    est = estimate_cost(plans, "gemini", "gemini-3.1-flash-lite-preview", embedder="openai")
    tokens = 1000 * (EMBED_TEXT_MAX_CHARS // 4)
    assert est.estimated_embedding_tokens == tokens
    assert est.embedding_cost_usd == pytest.approx(tokens / 1000 * 0.00002)
    assert est.estimated_cost_usd == pytest.approx(est.embedding_cost_usd)
    assert est.cost_range.high > est.embedding_cost_usd

    assert lookup_embedding_cost("gemini") == 0.00015
    assert lookup_embedding_cost("openai", "text-embedding-3-large") == 0.00013
    assert lookup_embedding_cost("ollama", "anything") == 0.0
    assert lookup_embedding_cost("mock") == 0.0


def test_local_and_unpriced_embedding_adds_nothing():
    plans = [PageTypePlan("file_page", 10, 2)]
    for embedder in ("ollama", "mock", None):
        est = estimate_cost(plans, "mock", "mock", embedder=embedder)
        assert est.embedding_cost_usd == 0.0
        assert est.estimated_cost_usd == 0.0


def test_the_embedding_note_names_the_share():
    from repowise.cli.cost_gate import embedding_note

    plans = [PageTypePlan("file_page", 1000, 2)]
    note = embedding_note(estimate_cost(plans, "mock", "mock", embedder="gemini"))
    assert note == "Includes about $0.60 USD to embed 1,000 pages with gemini."
    few = [PageTypePlan("file_page", 3, 2)]
    tiny = embedding_note(estimate_cost(few, "mock", "mock", embedder="openai"))
    assert tiny == "Includes under $0.01 USD to embed 3 pages with openai."
    free = embedding_note(estimate_cost(plans, "mock", "mock", embedder="ollama"))
    assert free == "Embedding 1,000 pages with ollama adds no cost."
    assert embedding_note(estimate_cost(plans, "mock", "mock", embedder="mock")) is None
    assert embedding_note(estimate_cost(plans, "mock", "mock")) is None