"""Tests for snapshot loader/index robustness paths."""

from __future__ import annotations

from pathlib import Path

import llmdebug._snapshot_loader as snapshot_loader


def test_find_all_snapshots_skips_invalid_index_mtime(
    tmp_path: Path,
    monkeypatch,
) -> None:
    good_path = tmp_path / "good.json"
    good_path.write_text("{}", encoding="utf-8")
    bad_path = tmp_path / "bad.json"
    bad_path.write_text("{}", encoding="utf-8")

    monkeypatch.setattr(
        snapshot_loader,
        "list_snapshot_records",
        lambda _out_dir, limit=None: [
            {
                "path": str(good_path),
                "mtime": "1700000000.0",
                "name": "good",
                "exc_type": "ValueError",
                "exc_message": "ok",
                "file": "app.py",
                "line": "12",
            },
            {
                "path": str(bad_path),
                "mtime": "nan",
                "name": "bad",
                "exc_type": "TypeError",
                "exc_message": "bad",
                "file": "bad.py",
                "line": "5",
            },
        ],
    )

    removed: list[str] = []
    monkeypatch.setattr(
        snapshot_loader,
        "remove_snapshot_records",
        lambda _out_dir, paths: removed.extend(paths),
    )

    entries = snapshot_loader.find_all_snapshots(str(tmp_path))
    assert len(entries) == 1
    assert entries[0]["path"] == good_path.resolve()
    assert str(bad_path) in removed
