"""Fixture-driven tests for IO rules."""

import pytest

from fixtures import workflows_dir
from valn8n_lib.rules import get_rules
from valn8n_lib.validator import validate_workflow

FIXTURE_DIR = workflows_dir() / "test_io"


def _discover_cases() -> list[str]:
    """Discover test cases from .lst companion files."""
    return sorted(p.stem for p in FIXTURE_DIR.glob("*.lst"))


@pytest.mark.parametrize("case", _discover_cases())
def test_io_rule(case: str) -> None:
    lst_path = FIXTURE_DIR / f"{case}.lst"
    json_path = FIXTURE_DIR / f"{case}.json"
    assert json_path.exists(), f"Missing fixture: {json_path}"

    expected = sorted(line.strip() for line in lst_path.read_text().splitlines() if line.strip())

    diagnostics = validate_workflow(json_path, rules=get_rules())
    actual = sorted(d.code.code for d in diagnostics)

    assert actual == expected, f"{case}: expected {expected}, got {actual}"
