Spaces:
Running
Running
Download tests/test_hashing.py from warenterprise/trace2mcp: direct link, hf CLI and curl.
- Browser
- Download file 1.76 kB
-
https://huggingface.co/spaces/warenterprise/trace2mcp/resolve/main/tests/test_hashing.py
- Command line
-
hf download hf://spaces/warenterprise/trace2mcp/tests/test_hashing.py
-
curl -L -o test_hashing.py https://huggingface.co/spaces/warenterprise/trace2mcp/resolve/main/tests/test_hashing.py
1.76 kB
| from __future__ import annotations | |
| import pytest | |
| from trace2mcp.hashing import ( | |
| CanonicalizationError, | |
| canonical_json, | |
| stable_hash, | |
| trace_hash, | |
| workflow_hash, | |
| ) | |
| from trace2mcp.models import Workflow, WorkflowNode | |
| def test_canonical_json_and_hash_ignore_mapping_insertion_order() -> None: | |
| left = {"nested": {"b": 2, "a": 1}, "items": [True, None, "á"]} | |
| right = {"items": [True, None, "á"], "nested": {"a": 1, "b": 2}} | |
| assert canonical_json(left) == canonical_json(right) | |
| assert stable_hash(left) == stable_hash(right) | |
| assert len(stable_hash(left)) == 64 | |
| def test_workflow_hash_excludes_only_self_referential_workflow_hash() -> None: | |
| workflow = Workflow("demo", {}, [WorkflowNode("a", "read", {}, observed_result=1)]) | |
| payload = workflow.to_dict() | |
| payload["provenance"] = { | |
| "workflow_hash": "old-value", | |
| "tool_contract_hashes": {"read": "contract-hash"}, | |
| } | |
| first = workflow_hash(payload) | |
| payload["provenance"]["workflow_hash"] = "new-value" | |
| assert workflow_hash(payload) == first | |
| payload["provenance"]["tool_contract_hashes"]["read"] = "changed" | |
| assert workflow_hash(payload) != first | |
| def test_trace_hash_excludes_embedded_trace_hash() -> None: | |
| trace = {"version": "1", "events": [], "metadata": {"trace_hash": "one"}} | |
| digest = trace_hash(trace) | |
| trace["metadata"]["trace_hash"] = "two" | |
| assert trace_hash(trace) == digest | |
| def test_canonicalization_rejects_non_json_data_and_non_finite_numbers() -> None: | |
| with pytest.raises(CanonicalizationError, match="not canonical JSON"): | |
| canonical_json({"bad": object()}) | |
| with pytest.raises(CanonicalizationError, match="Cannot canonicalize"): | |
| canonical_json({"bad": float("nan")}) | |