Files
marqdo-cli/tests/consumers/view_consumer.py
T
cflmy 6d4209bf32 tests: verify the closed Analysis boundary with independent consumers
Add black-box tests for exact source/AST projection, complete heading scopes,
explicit reference states, output safety (input/symlink/hardlink/directory and
owner-only bytes), Observation digest binding, multi-entry snapshot context and
the protocol mutation gate. Upgrade the Python consumer into a full independent
semantic validator, add view/lint/knowledge consumer fixtures driven by the
real CLI, and run schema plus semantic mutation checks from validate_protocols.
2026-11-25 11:01:24 +08:00

58 lines
2.1 KiB
Python

#!/usr/bin/env python3
"""View-like independent consumer of the public Analysis artifact.
Uses only the JSON document: section ranges, source text and function headings.
Never imports a Marqdo/Rust crate and never executes the document.
usage: view_consumer.py ANALYSIS.json
"""
import json
import sys
def utf8_slice(source, span):
data = source.encode("utf-8")
return data[span["start"] : span["end"]].decode("utf-8")
def main(argv):
if len(argv) != 2:
raise SystemExit("usage: view_consumer.py ANALYSIS.json")
with open(argv[1], encoding="utf-8") as handle:
analysis = json.load(handle)
if analysis.get("schema") != "marqdo.analysis":
raise SystemExit("view: unexpected analysis schema")
rendered = 0
for module in analysis["modules"]:
source = module["source"]
units = module["units"]
if not units:
raise SystemExit("view: module has no units")
by_id = {unit["id"]: unit for unit in units}
for unit in units:
parent = unit.get("parent_id")
if parent is not None and parent not in by_id:
raise SystemExit("view: dangling unit parent")
if unit["span"]["start"] > unit["span"]["end"]:
raise SystemExit("view: reversed section span")
for unit in units:
if unit["kind"] != "function":
continue
section = utf8_slice(source, unit["span"])
heading = utf8_slice(source, unit["heading_span"])
if not heading.strip():
raise SystemExit("view: empty function heading")
if not section.startswith(heading):
raise SystemExit("view: function section does not start at its heading")
label = "/".join(unit["path"])
print(f"view: {module['id']}#{label} [{len(section)} bytes]")
rendered += 1
if not rendered:
raise SystemExit("view: no callable section to render")
print(f"view: rendered={rendered}")
return 0
if __name__ == "__main__":
sys.exit(main(sys.argv))