"""Tests for generic YAML linter."""

from yaml_doctor.analyzers.yaml_lint import lint_yaml


class TestYAMLLinter:
    def test_clean(self):
        content = "---\nname: test\nvalue: 42\n"
        findings = lint_yaml(content)
        assert len(findings) == 0

    def test_yml001_tabs(self):
        content = "name: test\n\tvalue: 42\n"
        findings = lint_yaml(content)
        assert any(f.rule.rule_id == "YML001" for f in findings)

    def test_yml002_trailing_whitespace(self):
        content = "---\nname: test   \nvalue: 42\n"
        findings = lint_yaml(content)
        assert any(f.rule.rule_id == "YML002" for f in findings)

    def test_yml003_long_line(self):
        content = "---\nname: " + "x" * 250 + "\n"
        findings = lint_yaml(content)
        assert any(f.rule.rule_id == "YML003" for f in findings)

    def test_yml004_no_doc_marker(self):
        content = "name: test\nvalue: 42\n"
        findings = lint_yaml(content)
        assert any(f.rule.rule_id == "YML004" for f in findings)

    def test_yml005_hardcoded_password(self):
        content = "---\npassword: mysecret123\n"
        findings = lint_yaml(content)
        assert any(f.rule.rule_id == "YML005" for f in findings)

    def test_yml005_api_key(self):
        content = "---\napi_key: AKIAIOSFODNN7EXAMPLE\n"
        findings = lint_yaml(content)
        assert any(f.rule.rule_id == "YML005" for f in findings)

    def test_yml005_no_false_positive_on_ref(self):
        content = "---\npassword:\n  valueFrom:\n    secretKeyRef:\n      name: db-secret\n"
        findings = lint_yaml(content)
        # Should not flag if value is not a simple credential
        yml005 = [f for f in findings if f.rule.rule_id == "YML005"]
        # The key 'password' alone without a direct simple value shouldn't trigger
        assert len(yml005) == 0

    def test_yml002_max_five(self):
        """Trailing whitespace limited to 5 findings + 1 summary."""
        lines = ["---"] + [f"line{i}: value   " for i in range(20)]
        content = "\n".join(lines) + "\n"
        findings = lint_yaml(content)
        trailing = [f for f in findings if f.rule.rule_id == "YML002"]
        assert len(trailing) == 6  # 5 individual + 1 summary

    def test_empty_content(self):
        findings = lint_yaml("")
        assert isinstance(findings, list)

    def test_yml006_duplicate_keys(self):
        content = "---\nname: test\nname: test2\n"
        findings = lint_yaml(content)
        assert any(f.rule.rule_id == "YML006" for f in findings)
