reuse-surface/tests/test_index_parity.py

92 lines
3.5 KiB
Python
Raw Normal View History

"""Exercise the same strict native validator used by the existing Forgejo job."""
from __future__ import annotations
import copy
import subprocess
import sys
from pathlib import Path
import pytest
import yaml
ROOT = Path(__file__).resolve().parent.parent
def fixture_registry(tmp_path):
index = yaml.safe_load((ROOT / "registry/indexes/capabilities.yaml").read_text())
row = copy.deepcopy(index["capabilities"][0])
source = ROOT / row["path"]
target = tmp_path / row["path"]
target.parent.mkdir(parents=True)
target.write_bytes(source.read_bytes())
index["capabilities"] = [row]
index_path = tmp_path / "registry/indexes/capabilities.yaml"
index_path.parent.mkdir(parents=True)
return index_path, index
def validate(root):
return subprocess.run(
[sys.executable, "-m", "reuse_surface.cli", "validate", "--root", str(root),
"--fail-on-warnings"], cwd=ROOT, capture_output=True, text=True,
)
@pytest.mark.parametrize("field,value", [
("id", "capability.synthetic.stale"), ("name", "stale name"),
("summary", "stale summary"), ("domain", "stale-domain"),
("status", "retired"), ("owner", "wrong-owner"),
("tags", ["stale"]), ("consumption_modes", ["stale"]),
("vector", "D0 / A0 / C0 / R0"),
])
def test_existing_strict_validator_refuses_stale_projection(tmp_path, field, value):
path, index = fixture_registry(tmp_path)
path.write_text(yaml.safe_dump(index))
assert validate(tmp_path).returncode == 0
original_entry = (tmp_path / index["capabilities"][0]["path"]).read_bytes()
index["capabilities"][0][field] = value
path.write_text(yaml.safe_dump(index))
result = validate(tmp_path)
assert result.returncode == 1, result.stdout + result.stderr
assert f"{field} differs from entry" in result.stderr
assert (tmp_path / index["capabilities"][0]["path"]).read_bytes() == original_entry
def test_duplicate_id_and_path_cannot_satisfy_exactly_once(tmp_path):
path, index = fixture_registry(tmp_path)
index["capabilities"].append(copy.deepcopy(index["capabilities"][0]))
path.write_text(yaml.safe_dump(index))
result = validate(tmp_path)
assert result.returncode == 1
assert "duplicate id:" in result.stderr
assert "duplicate path:" in result.stderr
def test_equivalent_vector_whitespace_is_allowed(tmp_path):
path, index = fixture_registry(tmp_path)
index["capabilities"][0]["vector"] = index["capabilities"][0]["vector"].replace(" ", "")
path.write_text(yaml.safe_dump(index))
assert validate(tmp_path).returncode == 0
@pytest.mark.parametrize("source", ["null", "[]", "availability: null", "availability: []", "availability: ["])
def test_malformed_frontmatter_returns_failure_without_traceback(tmp_path, source):
path, index = fixture_registry(tmp_path)
path.write_text(yaml.safe_dump(index))
(tmp_path / index["capabilities"][0]["path"]).write_text("---\n" + source + "\n---\n")
result = validate(tmp_path)
assert result.returncode == 1
assert "Traceback" not in result.stderr
assert "error:" in result.stderr
@pytest.mark.parametrize("rows", [None, {}, [None], [[]], [{}], [{"id": [], "path": "x"}], [{"id": "x", "path": None}]])
def test_unvalidated_index_shape_returns_failure_without_traceback(tmp_path, rows):
path, index = fixture_registry(tmp_path)
index["capabilities"] = rows
path.write_text(yaml.safe_dump(index))
result = validate(tmp_path)
assert result.returncode == 1
assert "Traceback" not in result.stderr
assert "index drift:" in result.stderr