|
| 1 | +"""Tests for YAML type coercion detection in frontmatter. |
| 2 | +
|
| 3 | +yaml.safe_load silently converts bare values: |
| 4 | + name: true → bool |
| 5 | + name: 123 → int |
| 6 | + name: 1.5 → float |
| 7 | + name: null → None |
| 8 | +
|
| 9 | +These produce confusing downstream errors. The type check rules catch |
| 10 | +them early with a clear fix (quote the value). |
| 11 | +""" |
| 12 | + |
| 13 | +import pytest |
| 14 | + |
| 15 | +from skillcheck.parser import parse |
| 16 | +from skillcheck.result import Severity |
| 17 | +from skillcheck.rules.frontmatter import check_description_type, check_name_type |
| 18 | + |
| 19 | + |
| 20 | +# --------------------------------------------------------------------------- |
| 21 | +# frontmatter.name.type |
| 22 | +# --------------------------------------------------------------------------- |
| 23 | + |
| 24 | + |
| 25 | +def test_name_boolean_detected(tmp_path): |
| 26 | + """name: true → parsed as bool, should fire type error.""" |
| 27 | + f = tmp_path / "SKILL.md" |
| 28 | + f.write_text("---\nname: true\ndescription: Boolean name.\n---\nBody.\n") |
| 29 | + skill = parse(f) |
| 30 | + diagnostics = check_name_type(skill) |
| 31 | + assert len(diagnostics) == 1 |
| 32 | + assert diagnostics[0].rule == "frontmatter.name.type" |
| 33 | + assert diagnostics[0].severity == Severity.ERROR |
| 34 | + assert "bool" in diagnostics[0].message |
| 35 | + assert 'name: "True"' in diagnostics[0].message |
| 36 | + |
| 37 | + |
| 38 | +def test_name_integer_detected(tmp_path): |
| 39 | + """name: 123 → parsed as int, should fire type error.""" |
| 40 | + f = tmp_path / "SKILL.md" |
| 41 | + f.write_text("---\nname: 123\ndescription: Numeric name.\n---\nBody.\n") |
| 42 | + skill = parse(f) |
| 43 | + diagnostics = check_name_type(skill) |
| 44 | + assert len(diagnostics) == 1 |
| 45 | + assert "int" in diagnostics[0].message |
| 46 | + |
| 47 | + |
| 48 | +def test_name_float_detected(tmp_path): |
| 49 | + """name: 1.5 → parsed as float, should fire type error.""" |
| 50 | + f = tmp_path / "SKILL.md" |
| 51 | + f.write_text("---\nname: 1.5\ndescription: Float name.\n---\nBody.\n") |
| 52 | + skill = parse(f) |
| 53 | + diagnostics = check_name_type(skill) |
| 54 | + assert len(diagnostics) == 1 |
| 55 | + assert "float" in diagnostics[0].message |
| 56 | + |
| 57 | + |
| 58 | +def test_name_string_passes(tmp_path): |
| 59 | + """name: my-skill → string, no type error.""" |
| 60 | + f = tmp_path / "SKILL.md" |
| 61 | + f.write_text("---\nname: my-skill\ndescription: Valid.\n---\nBody.\n") |
| 62 | + skill = parse(f) |
| 63 | + assert check_name_type(skill) == [] |
| 64 | + |
| 65 | + |
| 66 | +def test_name_none_skipped(tmp_path): |
| 67 | + """name absent → handled by check_name_required, not type check.""" |
| 68 | + f = tmp_path / "SKILL.md" |
| 69 | + f.write_text("---\ndescription: No name.\n---\nBody.\n") |
| 70 | + skill = parse(f) |
| 71 | + assert check_name_type(skill) == [] |
| 72 | + |
| 73 | + |
| 74 | +def test_name_quoted_true_passes(tmp_path): |
| 75 | + """name: "true" → stays as string, no type error.""" |
| 76 | + f = tmp_path / "SKILL.md" |
| 77 | + f.write_text('---\nname: "true"\ndescription: Quoted boolean.\n---\nBody.\n') |
| 78 | + skill = parse(f) |
| 79 | + assert check_name_type(skill) == [] |
| 80 | + |
| 81 | + |
| 82 | +# --------------------------------------------------------------------------- |
| 83 | +# frontmatter.description.type |
| 84 | +# --------------------------------------------------------------------------- |
| 85 | + |
| 86 | + |
| 87 | +def test_description_boolean_detected(tmp_path): |
| 88 | + """description: true → parsed as bool, should fire type error.""" |
| 89 | + f = tmp_path / "SKILL.md" |
| 90 | + f.write_text("---\nname: my-skill\ndescription: true\n---\nBody.\n") |
| 91 | + skill = parse(f) |
| 92 | + diagnostics = check_description_type(skill) |
| 93 | + assert len(diagnostics) == 1 |
| 94 | + assert diagnostics[0].rule == "frontmatter.description.type" |
| 95 | + assert diagnostics[0].severity == Severity.ERROR |
| 96 | + assert "bool" in diagnostics[0].message |
| 97 | + |
| 98 | + |
| 99 | +def test_description_integer_detected(tmp_path): |
| 100 | + """description: 42 → parsed as int, should fire type error.""" |
| 101 | + f = tmp_path / "SKILL.md" |
| 102 | + f.write_text("---\nname: my-skill\ndescription: 42\n---\nBody.\n") |
| 103 | + skill = parse(f) |
| 104 | + diagnostics = check_description_type(skill) |
| 105 | + assert len(diagnostics) == 1 |
| 106 | + assert "int" in diagnostics[0].message |
| 107 | + |
| 108 | + |
| 109 | +def test_description_string_passes(tmp_path): |
| 110 | + """description: Some text → string, no type error.""" |
| 111 | + f = tmp_path / "SKILL.md" |
| 112 | + f.write_text("---\nname: my-skill\ndescription: Validates things.\n---\nBody.\n") |
| 113 | + skill = parse(f) |
| 114 | + assert check_description_type(skill) == [] |
| 115 | + |
| 116 | + |
| 117 | +def test_description_none_skipped(tmp_path): |
| 118 | + """description absent → handled by check_description_required.""" |
| 119 | + f = tmp_path / "SKILL.md" |
| 120 | + f.write_text("---\nname: my-skill\n---\nBody.\n") |
| 121 | + skill = parse(f) |
| 122 | + assert check_description_type(skill) == [] |
0 commit comments