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

import pytest

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

FIXTURE_DIR = workflows_dir() / "test_fil"


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_fil_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}"


class TestND0101Suggestions:
    """Test that ND0101 includes valid operations in message."""

    def test_message_contains_valid_operations(self) -> None:
        json_path = FIXTURE_DIR / "invalid_op_other.json"
        diagnostics = validate_workflow(json_path, rules=get_rules(select={"ND0101"}))
        assert diagnostics, "Expected at least one ND0101 diagnostic"
        msg = diagnostics[0].message
        assert "valid values:" in msg

    def test_suggestions_none_hides_operations(self) -> None:
        json_path = FIXTURE_DIR / "invalid_op_other.json"
        diagnostics = validate_workflow(
            json_path,
            rules=get_rules(select={"ND0101"}),
            suggestions=SuggestionLevel.NONE,
        )
        assert diagnostics
        msg = diagnostics[0].message
        assert "valid values:" not in msg
