from pathlib import Path

import pytest

from absolutelyright import load_sessions, session_files
from absolutelyright._records import default_claude_dir


def test_only_top_level_transcripts_are_discovered(claude_dir: Path) -> None:
    files = session_files(claude_dir)
    assert [f.name for f in files] == ["s1.jsonl"]


def test_typed_prompts_survive_and_noise_is_dropped(claude_dir: Path) -> None:
    (session,) = load_sessions(claude_dir)
    assert [p.text for p in session.prompts] == [
        "fix the bug in auth",
        "fix the bug in auth",
        "no, wait — look at the tests first",
        "please dude look at this [Image #1]",
    ]


def test_interrupts_are_counted_not_treated_as_prompts(claude_dir: Path) -> None:
    (session,) = load_sessions(claude_dir)
    assert session.interrupts == 1


def test_tool_calls_and_tokens_come_from_assistant_records(claude_dir: Path) -> None:
    (session,) = load_sessions(claude_dir)
    assert session.tool_calls == {"Bash": 1, "Edit": 1}
    assert session.output_tokens == 200


def test_project_derived_from_cwd_with_home_stripped(claude_dir: Path) -> None:
    (session,) = load_sessions(claude_dir)
    assert session.project == "github.com/me/proj"


def test_days_filter_excludes_old_records(claude_dir: Path) -> None:
    assert load_sessions(claude_dir, days=10_000)
    assert not load_sessions(claude_dir, days=0)


def test_claude_config_dir_env_is_honored(monkeypatch: pytest.MonkeyPatch) -> None:
    monkeypatch.setenv("CLAUDE_CONFIG_DIR", "/tmp/elsewhere")
    assert default_claude_dir() == Path("/tmp/elsewhere")


def test_new_richness_is_mined(claude_dir: Path) -> None:
    (session,) = load_sessions(claude_dir)
    assert session.title == "fixing auth"
    assert session.pr_urls == {"https://github.com/me/proj/pull/1"}
    assert session.models == {"claude-fable-5": 2}
    assert session.cache_read_tokens == 10000
    assert session.claude_phrases[3]["you're absolutely right"] == 1
    assert session.your_words["please"] == 1
    assert session.your_words["dude"] == 1
    assert session.images == 1
    assert session.slash_commands == {"/digest": 1}
    assert session.active_seconds == 60.0
