from __future__ import annotations

from app.modules.rag.indexing.common.path_filter import (
    count_indexable_change_upserts,
    filter_changes_for_indexing,
    filter_snapshot_files,
    should_skip_indexing_path,
)


def test_should_skip_indexing_path_for_hidden_and_cache_paths() -> None:
    assert should_skip_indexing_path(".env") is True
    assert should_skip_indexing_path(".venv/lib/site-packages/a.py") is True
    assert should_skip_indexing_path("src/.secrets/config.py") is True
    assert should_skip_indexing_path("src/__pycache__/module.cpython-312.pyc") is True
    assert should_skip_indexing_path("src/main.py") is False


def test_filter_snapshot_files_excludes_hidden_and_cache_paths() -> None:
    files = [
        {"path": ".env", "content": "A"},
        {"path": "src/__pycache__/x.py", "content": "B"},
        {"path": "src/main.py", "content": "C"},
    ]

    filtered = filter_snapshot_files(files)

    assert [item["path"] for item in filtered] == ["src/main.py"]


def test_filter_changes_for_indexing_keeps_deletes_and_filters_upserts() -> None:
    changed_files = [
        {"op": "upsert", "path": ".env", "content": "A"},
        {"op": "upsert", "path": "src/main.py", "content": "B"},
        {"op": "delete", "path": ".cache/legacy.txt"},
    ]

    filtered = filter_changes_for_indexing(changed_files)

    assert filtered == [
        {"op": "upsert", "path": "src/main.py", "content": "B"},
        {"op": "delete", "path": ".cache/legacy.txt"},
    ]
    assert count_indexable_change_upserts(filtered) == 1
