# tests/test_helpers.py
"""Tests for helper functions."""
import tempfile
import os

from app.main_helpers import (
    is_safe_filename,
    chunk_text,
    load_registry,
    save_registry,
    mask_redis_url,
    get_all_courses,
)


def test_is_safe_filename() -> None:
    """Test filename validation."""
    assert is_safe_filename("document.txt")
    assert is_safe_filename("my-file (v2).txt")
    assert not is_safe_filename("../malicious.txt")
    assert not is_safe_filename("bad/filename.txt")
    assert not is_safe_filename("bad\\filename.txt")


def test_chunk_text() -> None:
    """Test text chunking."""
    text = "word " * 300  # 1500 words
    chunks = chunk_text(text, chunk_size=100, chunk_overlap=20)
    assert len(chunks) > 1
    assert all(isinstance(c, str) for c in chunks)


def test_registry_save_load() -> None:
    """Test registry persistence."""
    with tempfile.TemporaryDirectory() as tmpdir:
        registry_path = os.path.join(tmpdir, "registry.json")
        test_data = {"file1": {"id": "file1", "chunks": 5}}

        save_registry(test_data, registry_path)
        loaded = load_registry(registry_path)

        assert loaded == test_data


def test_mask_redis_url() -> None:
    """Test Redis URL masking."""
    url = "redis://:mypassword@localhost:6379"
    masked = mask_redis_url(url)
    assert "mypassword" not in masked
    assert "***" in masked
    assert "localhost" in masked


def test_get_all_courses() -> None:
    """Test retrieving all courses from registry files."""
    with tempfile.TemporaryDirectory() as tmpdir:
        base_path = os.path.join(tmpdir, "assets_index.json")
        
        # Initially no courses
        courses = get_all_courses(base_path)
        assert courses == []
        
        # Create registry files for two courses
        registry1 = os.path.join(tmpdir, "assets_index_course1.json")
        registry2 = os.path.join(tmpdir, "assets_index_course2.json")
        save_registry({"file1": {"id": "file1"}}, registry1)
        save_registry({"file2": {"id": "file2"}}, registry2)
        
        # Now should find courses
        courses = get_all_courses(base_path)
        assert len(courses) == 2
        assert "course1" in courses
        assert "course2" in courses
        assert courses == sorted(courses)  # Should be sorted
