"""Independent byte/Git identity, raw position/named copy and graph checks.""" import datetime import hashlib import json from build_definition import OUT, REF, REPO, build, canonical from inventory import inventory def main(): block = build() rows = json.loads((REF / "sources.json").read_bytes())["sources"] for e in rows: raw = (REF / e["local_file"]).read_bytes() assert hashlib.sha256(raw).hexdigest() == e["sha256"] assert ( hashlib.sha1(b"blob " + str(len(raw)).encode() + b"\0" + raw).hexdigest() == e["git_blob_sha1"] ) t = json.loads((REF / (e["id"] + "-tree.json")).read_bytes()) assert t["sha"] == e["tree"] and not t["truncated"] assert next(x["sha"] for x in t["tree"] if x["path"] == e["file"]) == e["git_blob_sha1"] c = (REF / (e["id"] + "-commit.txt")).read_bytes() assert ( hashlib.sha1(b"commit " + str(len(c)).encode() + b"\0" + c).hexdigest() == e["commit"] ) assert c.decode().splitlines()[0] == "tree " + e["tree"] tag = json.loads((REF / (e["id"] + "-tag.json")).read_bytes()) assert tag["commit"] == e["commit"] tr = (REF / (e["id"] + "-tag.txt")).read_bytes() if tag["object"] != tag["commit"]: assert ( hashlib.sha1(b"tag " + str(len(tr)).encode() + b"\0" + tr).hexdigest() == tag["object"] ) assert tr.decode().startswith("object " + e["commit"] + "\n") else: assert tr == c inv = inventory() assert len(inv["fields"]) == 24 and inv["nodes"] == 17 and inv["edges"] == 20 assert inv["notes"] == inv["named_nodes"] == 0 assert inv["empty"] == [ dict(node=n, pointer=f"/nodes/{i}", state=state) for n, i, state in [ (83, 3, "empty_list"), (82, 5, "empty_list"), (94, 6, "missing"), (95, 7, "missing"), ] ] for a, f in zip(inv["fields"], block["fields"], strict=True): assert (a["pointer"], a["json_type"], canonical(a["value"])) == ( f["pointer"], f["json_type"], canonical(f["value"]), ) assert "named_value" not in f g = json.loads((REPO / block["source_path"]).read_bytes()) ns = {n["id"]: n for n in g["nodes"]} for edge, src, slot, dst, port, kind in g["links"]: assert edge in ns[src]["outputs"][slot]["links"] assert ns[dst]["inputs"][port]["link"] == edge assert ns[src]["outputs"][slot]["type"] == kind assert all(n["mode"] == 0 for n in g["nodes"]) and not g.get("definitions") assert ns[82]["outputs"][1]["links"] == [] assert g["extra"]["frontendVersion"] == "1.36.14" for e in block["sources"]: data = (OUT / e["local_file"]).read_bytes() assert hashlib.sha256(data).hexdigest() == e["sha256"] assert 1 <= e["lines"][0] <= e["lines"][1] <= len(data.decode().splitlines()) (OUT / "source-check.json").write_text( json.dumps( dict( all_pass=True, reverified_at=datetime.datetime.now(datetime.timezone.utc).isoformat(), source_files=len(rows), citations=len(block["sources"]), counts=block["counts"], named_nodes=0, inventory=inv, upstream_executed=False, ), ensure_ascii=False, indent=2, ) + "\n" ) print( ( "PASS 24 positions, zero named va" "lues/Notes, 20 physical edges, e" "xact Git/tag/tree/blob identitie" "s" ) ) if __name__ == "__main__": main()