Download tests/test_certificate.py from fahadhafeezofficial/clsa-code: direct link, hf CLI and curl.
- Browser
- Download file 3.13 kB
-
https://huggingface.co/datasets/fahadhafeezofficial/clsa-code/resolve/main/tests/test_certificate.py
- Command line
-
hf download hf://datasets/fahadhafeezofficial/clsa-code/tests/test_certificate.py
-
curl -L -o test_certificate.py https://huggingface.co/datasets/fahadhafeezofficial/clsa-code/resolve/main/tests/test_certificate.py
3.13 kB
| # Copyright (c) 2026 CLSA contributors | |
| # SPDX-License-Identifier: MIT | |
| """Certificates: schema conformance, hash chain, tamper detection.""" | |
| from __future__ import annotations | |
| import json | |
| import sys | |
| from pathlib import Path | |
| sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src")) | |
| import jsonschema # noqa: E402 | |
| import pytest # noqa: E402 | |
| from clsa.controller.certificate import ( # noqa: E402 | |
| SCHEMA_PATH, | |
| CertificateLedger, | |
| audit_ledger, | |
| make_certificate, | |
| ) | |
| BOUND = { | |
| "alpha": 0.1, | |
| "coverage": 0.9, | |
| "selective_risk_upper": 0.12, | |
| "n": 300, | |
| "method": "clopper_pearson_selective", | |
| "feasible": True, | |
| "budget_spent": 700.0, | |
| "budget_limit": 1200.0, | |
| "components": {"monitor": 100, "simulation": 400, "verification": 150, "allocator": 50}, | |
| } | |
| def _cert(item_id="item-1", decision="abstain"): | |
| return make_certificate( | |
| item_id=item_id, | |
| episode_id="ep-1", | |
| step=3, | |
| decision=decision, | |
| action={"name": "send_email", "args": {"to": "x@example.org"}}, | |
| mode="verify/acquire", | |
| graph={"method": "feature_ablation_attribution", "nodes": [], "edges": []}, | |
| alternative={"name": "read_email", "args": {}}, | |
| predicted={"success": True, "harm": 0.2, "irreversible_harm": False}, | |
| replayed={"success": True, "harm": 0.25, "irreversible_harm": False}, | |
| consistency=(True, 0.05), | |
| evidence={"claims_used": [], "stale": False, "conflict": False, "acquired": []}, # noqa: E501 | |
| bound=dict(BOUND), | |
| cost={"tokens": 700, "latency_ms": 420, "dollars": 0.0, "rollouts": 6, "verifications": 1}, | |
| ) | |
| def test_certificate_validates(tmp_path): | |
| # The hash is filled in by the ledger on write, so validate the written record. | |
| written = CertificateLedger(tmp_path / "l.jsonl").write(_cert("item-1")) | |
| jsonschema.validate(written, json.loads(SCHEMA_PATH.read_text())) | |
| assert written["cert_id"] and written["cert_hash"] != "pending" | |
| assert written["fork"]["consistent"] is True | |
| def test_missing_bound_method_is_rejected(): | |
| c = _cert("item-1") | |
| del c["bound"]["method"] | |
| c["cert_hash"] = "0" * 16 | |
| with pytest.raises(jsonschema.ValidationError): | |
| jsonschema.validate(c, json.loads(SCHEMA_PATH.read_text())) | |
| def test_chain_verifies_and_detects_tampering(tmp_path): | |
| path = tmp_path / "ledger.jsonl" | |
| ledger = CertificateLedger(path) | |
| ledger.write(_cert("item-a")) | |
| ledger.write(_cert("item-b")) | |
| ok, msg = ledger.verify_chain() | |
| assert ok, msg | |
| lines = path.read_text().splitlines() | |
| rec = json.loads(lines[1]) | |
| rec["decision"] = "execute" | |
| lines[1] = json.dumps(rec) | |
| path.write_text("\n".join(lines) + "\n") | |
| ok2, _ = ledger.verify_chain() | |
| assert not ok2, "ledger tampering was not detected" | |
| def test_audit_counts_and_schema(tmp_path): | |
| path = tmp_path / "ledger.jsonl" | |
| ledger = CertificateLedger(path) | |
| for i in range(3): | |
| ledger.write(_cert(f"item-{i}")) | |
| audit = audit_ledger(path) | |
| assert audit["n_certificates"] == 3 | |
| assert audit["chain_ok"] and audit["schema_valid"] | |