"""Chunker: Ausschlussliste, Symbol-Schnitt, Chunk-Größen."""
from pathlib import Path

import chunker

MINI = Path(__file__).parent / "fixtures" / "mini_repo"


def test_mini_repo_dateien():
    rel = [str(p.relative_to(MINI)) for p in chunker.dateien(MINI)]
    assert rel == ["anzeige.py", "rechner.py"]


def test_ausschluss(tmp_path):
    (tmp_path / "ok.py").write_text("X = 1\n")
    (tmp_path / ".env").write_text("GEHEIM=1\n")
    (tmp_path / ".env.local").write_text("GEHEIM=2\n")
    (tmp_path / "node_modules").mkdir()
    (tmp_path / "node_modules" / "paket.py").write_text("Y = 1\n")
    (tmp_path / ".planer").mkdir()
    (tmp_path / ".planer" / "alt.py").write_text("Z = 1\n")
    rel = [str(p.relative_to(tmp_path)) for p in chunker.dateien(tmp_path)]
    assert rel == ["ok.py"]


def test_schnitt_mini():
    text = (MINI / "rechner.py").read_text()
    chunks = chunker.schneide("rechner.py", text)
    assert len(chunks) == 1  # klein → ein Chunk
    assert chunks[0]["symbole"] == ["addiere", "skaliere", "Speicher"]
    assert chunks[0]["text"] == text.rstrip("\n")  # Kopf (Docstring, BASIS) im ersten Chunk


def test_schnitt_grosse_datei():
    """Symbole à 60 Zeilen → Chunks bleiben um CHUNK_ZEILEN, Schnitt nie mitten im Symbol."""
    bloecke = [f"def f{i}():\n" + "".join(f"    x{j} = {j}\n" for j in range(60)) for i in range(20)]
    text = "\n".join(bloecke)
    chunks = chunker.schneide("gross.py", text)
    assert len(chunks) > 1
    assert all(c["groesse"] <= chunker.CHUNK_ZEILEN + 61 for c in chunks)
    alle = [s for c in chunks for s in c["symbole"]]
    assert alle == [f"f{i}" for i in range(20)]  # jedes Symbol genau einmal, in Reihenfolge
    for c in chunks:
        assert c["text"].splitlines()[0].startswith(("def ", "\n", '"'))  # Schnitt an Blockgrenze


def test_creator_smoke():
    """Read-only gegen das echte Creator-Backend: board_inventory.py (2344 Z.) wird zerlegt."""
    wurzel = Path("/home/arbeit/projects/creator")
    chunks = [c for c in chunker.chunks_fuer_repo(wurzel, {"backend/board_inventory.py"})]
    assert len(chunks) >= 6
    assert all(c["groesse"] <= 700 for c in chunks)  # kein Riesen-Chunk


def test_top_level_symbole():
    text = (MINI / "rechner.py").read_text()
    assert chunker.top_level_symbole(text) == {"BASIS", "addiere", "skaliere", "Speicher"}
