|
| 1 | +"""Validation tests for se-kernel artifacts.""" |
| 2 | + |
| 3 | +from typing import Any, cast |
| 4 | + |
| 5 | +from se_kernel.types import ConstantsData, IdentifiersData |
| 6 | +from se_kernel.validate import validate_constants, validate_identifiers |
| 7 | + |
| 8 | + |
| 9 | +def make_valid_identifiers() -> IdentifiersData: |
| 10 | + """Return valid identifiers data.""" |
| 11 | + return { |
| 12 | + "meta": {"version": "0.1.0", "status": "draft"}, |
| 13 | + "global": { |
| 14 | + "character_set": "ascii-lower-hyphen", |
| 15 | + "case": "lower", |
| 16 | + "separator": "-", |
| 17 | + "allow_unicode": False, |
| 18 | + }, |
| 19 | + "kind": { |
| 20 | + "repo": { |
| 21 | + "summary": "Canonical repository identifier.", |
| 22 | + "pattern": "se-{name}", |
| 23 | + "examples": ["se-kernel"], |
| 24 | + } |
| 25 | + }, |
| 26 | + "rules": { |
| 27 | + "must_be_stable": True, |
| 28 | + "must_be_ascii": True, |
| 29 | + "must_be_lowercase": True, |
| 30 | + "must_use_hyphen_separator": True, |
| 31 | + "must_not_embed_semantics": True, |
| 32 | + }, |
| 33 | + } |
| 34 | + |
| 35 | + |
| 36 | +def make_valid_constants() -> ConstantsData: |
| 37 | + """Return valid constants data.""" |
| 38 | + return { |
| 39 | + "meta": {"version": "0.1.0", "status": "draft"}, |
| 40 | + "reserved": { |
| 41 | + "repo_classes": {"values": ["constitution", "kernel"]}, |
| 42 | + }, |
| 43 | + "base_types": { |
| 44 | + "identifier": "string", |
| 45 | + "string_list": "list[string]", |
| 46 | + }, |
| 47 | + } |
| 48 | + |
| 49 | + |
| 50 | +def test_validate_identifiers_accepts_valid_data() -> None: |
| 51 | + """Valid identifiers data should produce no errors.""" |
| 52 | + errors = validate_identifiers(make_valid_identifiers()) |
| 53 | + assert errors == [] |
| 54 | + |
| 55 | + |
| 56 | +def test_validate_identifiers_requires_kind_entries() -> None: |
| 57 | + """Identifier kinds must not be empty.""" |
| 58 | + data = make_valid_identifiers() |
| 59 | + data["kind"] = {} |
| 60 | + |
| 61 | + errors = validate_identifiers(data) |
| 62 | + |
| 63 | + assert "identifiers.toml: [kind] must not be empty." in errors |
| 64 | + |
| 65 | + |
| 66 | +def test_validate_identifiers_requires_summary_pattern_and_examples() -> None: |
| 67 | + """Identifier kinds must define summary, pattern, and examples.""" |
| 68 | + data = cast(dict[str, Any], make_valid_identifiers()) |
| 69 | + data["kind"]["repo"] = {"summary": "", "pattern": "", "examples": []} |
| 70 | + |
| 71 | + errors = validate_identifiers(cast(IdentifiersData, data)) |
| 72 | + |
| 73 | + assert "identifiers.toml: [kind.repo] must define summary." in errors |
| 74 | + assert "identifiers.toml: [kind.repo] must define pattern." in errors |
| 75 | + assert "identifiers.toml: [kind.repo] must define examples." in errors |
| 76 | + |
| 77 | + |
| 78 | +def test_validate_constants_accepts_valid_data() -> None: |
| 79 | + """Valid constants data should produce no errors.""" |
| 80 | + errors = validate_constants(make_valid_constants()) |
| 81 | + assert errors == [] |
| 82 | + |
| 83 | + |
| 84 | +def test_validate_constants_requires_reserved_values() -> None: |
| 85 | + """Reserved groups must define values.""" |
| 86 | + data = make_valid_constants() |
| 87 | + data["reserved"]["repo_classes"] = {"values": []} |
| 88 | + |
| 89 | + errors = validate_constants(data) |
| 90 | + |
| 91 | + assert "constants.toml: [reserved.repo_classes] must define values." in errors |
| 92 | + |
| 93 | + |
| 94 | +def test_validate_constants_requires_base_types() -> None: |
| 95 | + """Base types must not be empty.""" |
| 96 | + data = make_valid_constants() |
| 97 | + data["base_types"] = {} |
| 98 | + |
| 99 | + errors = validate_constants(data) |
| 100 | + |
| 101 | + assert "constants.toml: [base_types] must not be empty." in errors |
0 commit comments