63 lines
2.9 KiB
Python
63 lines
2.9 KiB
Python
"""Behavioral tests for the Metadata nil sentinel.
|
|
|
|
Child 1 of metadata_ssdl_defusing_20260624. Asserts:
|
|
- NIL_METADATA constant is defined in src/aggregate.py (the Metadata parent module).
|
|
- NIL_METADATA is a valid Metadata (dict[str, Any]).
|
|
- Sentinel pattern is usable: `entry or NIL_METADATA` returns a safe Metadata.
|
|
- detect_nil_check_pattern returns False for at least one migrated function in
|
|
src/aggregate.py + src/ai_client.py (the files named in the parent campaign spec).
|
|
"""
|
|
from __future__ import annotations
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
|
|
|
|
from src.code_path_audit_ssdl import detect_nil_check_pattern
|
|
|
|
|
|
def test_nil_metadata_is_defined() -> None:
|
|
from src.aggregate import NIL_METADATA
|
|
assert NIL_METADATA is not None, "NIL_METADATA must be a valid Metadata sentinel"
|
|
assert isinstance(NIL_METADATA, dict), f"NIL_METADATA must be a dict (Metadata TypeAlias); got {type(NIL_METADATA)}"
|
|
|
|
|
|
def test_nil_metadata_safe_defaults() -> None:
|
|
from src.aggregate import NIL_METADATA
|
|
assert NIL_METADATA.get("any_missing_key") is None, "NIL_METADATA must return None for missing keys"
|
|
assert NIL_METADATA.get("any_missing_key", "default") == "default", "NIL_METADATA must honor .get(default)"
|
|
assert len(NIL_METADATA) >= 0, "NIL_METADATA must be a valid dict with len() support"
|
|
|
|
|
|
def test_sentinel_pattern_works() -> None:
|
|
from src.aggregate import NIL_METADATA
|
|
entry: dict = {}
|
|
result = entry or NIL_METADATA
|
|
assert result is NIL_METADATA, "empty dict should fall through to NIL_METADATA"
|
|
entry_filled: dict = {"key": "value"}
|
|
result2 = entry_filled or NIL_METADATA
|
|
assert result2 is entry_filled, "filled dict should NOT be replaced by NIL_METADATA"
|
|
|
|
|
|
def test_migration_reduces_nil_check_count() -> None:
|
|
from src.code_path_audit import build_pcg
|
|
from src.code_path_audit_ssdl import detect_nil_check_pattern
|
|
pcg = build_pcg("src").data
|
|
metadata_consumers = pcg.consumers.get("Metadata", [])
|
|
target_files = {"aggregate.py", "ai_client.py"}
|
|
remaining = [f for f in metadata_consumers if f.file in target_files and detect_nil_check_pattern(f, "src")]
|
|
migrated_funcs = ["_build_files_section_from_items"]
|
|
migrated = [f for f in metadata_consumers if f.file in target_files and f.fqname.rsplit(".", 1)[-1] in migrated_funcs]
|
|
for m in migrated:
|
|
assert not detect_nil_check_pattern(m, "src"), f"{m.fqname} should no longer have nil-check pattern"
|
|
|
|
|
|
def test_detect_nil_check_pattern_works_for_migrated_function() -> None:
|
|
from src.code_path_audit import FunctionRef
|
|
from src.aggregate import _build_files_section_from_items
|
|
fref = FunctionRef(fqname="src.aggregate._build_files_section_from_items", file="aggregate.py", line=300, role="consumer")
|
|
has_nil = detect_nil_check_pattern(fref, "src")
|
|
assert has_nil is False, (
|
|
"_build_files_section_from_items should no longer have a nil-check pattern after sentinel migration"
|
|
)
|