Download tests/test_artifacts.py from polyHedra/PolyHedra: direct link, hf CLI and curl.
- Browser
- Download file 6.44 kB
-
https://huggingface.co/spaces/polyHedra/PolyHedra/resolve/main/tests/test_artifacts.py
- Command line
-
hf download hf://spaces/polyHedra/PolyHedra/tests/test_artifacts.py
-
curl -L -o test_artifacts.py https://huggingface.co/spaces/polyHedra/PolyHedra/resolve/main/tests/test_artifacts.py
6.44 kB
| """Common quant contracts reject ambiguous identities before artifact access.""" | |
| import json | |
| import pytest | |
| from pydantic import TypeAdapter, ValidationError | |
| from polyhedra.core.artifacts import ( | |
| AgentArtifact, AgentInput, ArtifactRef, DataIssue, Direction, EvidenceRef, | |
| JsonArtifactRef, RenderingRef, TradeDirection, TypedModel, | |
| ) | |
| def ref(**updates): | |
| data = dict( | |
| format="JSON", schema_version="8.0", artifact_id="current-data", | |
| session_id="session1", run_id="run1", producer="DataFetcher", | |
| path="quant/runs/run1/raw/SPY.json", | |
| ) | |
| data.update(updates) | |
| return JsonArtifactRef.model_validate(data) | |
| class EmptyContext(TypedModel): | |
| pass | |
| class Dependencies(TypedModel): | |
| market_data: JsonArtifactRef | |
| class Payload(TypedModel): | |
| interpretation: str | |
| def input_data(**updates): | |
| data = dict( | |
| run_id="run1", session_id="session1", run_mode="BASELINE", | |
| as_of="2026-09-09T12:00:00Z", ticker_scope=["SPY"], | |
| dependencies={"market_data": ref().model_dump(mode="json")}, optional_context={}, | |
| ) | |
| data.update(updates) | |
| return data | |
| def artifact_data(**updates): | |
| data = dict( | |
| schema_version="8.0", artifact_id="trend", run_id="run1", session_id="session1", | |
| producer="TrendAnalyst", ticker_scope=["SPY"], created_at="2026-09-09T12:01:00Z", | |
| status="COMPLETE", input_refs=[ref().model_dump(mode="json")], baseline_refs=[], | |
| data_gaps=[], payload={"interpretation": "Stable prices"}, | |
| ) | |
| data.update(updates) | |
| return data | |
| def test_typed_envelope_json_round_trip(): | |
| inputs = AgentInput[Dependencies, EmptyContext].model_validate_json(json.dumps(input_data()), strict=True) | |
| assert inputs.dependencies.market_data.producer == "DataFetcher" | |
| artifact = AgentArtifact[Payload].model_validate_json(json.dumps(artifact_data()), strict=True) | |
| assert AgentArtifact[Payload].model_validate_json(artifact.model_dump_json(), strict=True) == artifact | |
| def test_bad_json_reference(changes): | |
| with pytest.raises(ValidationError): | |
| ref(**changes) | |
| def test_rendering_has_format_and_source_instead_of_schema_version(): | |
| rendering = RenderingRef( | |
| format="MARKDOWN", artifact_id="summary", session_id="session1", run_id="run1", | |
| producer="DataFetcher", path="quant/runs/run1/raw/SPY_summary.md", source=ref(), | |
| ) | |
| adapter = TypeAdapter(ArtifactRef) | |
| assert adapter.validate_json(rendering.model_dump_json(), strict=True) == rendering | |
| with pytest.raises(ValidationError): | |
| RenderingRef.model_validate({**rendering.model_dump(), "schema_version": "8.0"}) | |
| for changes in ( | |
| {"session_id": "other"}, {"artifact_id": "current-data"}, | |
| {"path": "quant/runs/run1/raw/QQQ_summary.md"}, {"format": "PNG"}, | |
| ): | |
| with pytest.raises(ValidationError): | |
| RenderingRef.model_validate({**rendering.model_dump(), **changes}) | |
| def test_evidence_requires_exact_locator(): | |
| assert EvidenceRef(artifact=ref(), field_path="/ohlcv/0/close").field_path | |
| assert EvidenceRef(artifact=ref(), record_id="2026-09-08").record_id | |
| with pytest.raises(ValidationError): | |
| EvidenceRef(artifact=ref()) | |
| with pytest.raises(ValidationError): | |
| EvidenceRef(artifact=ref(), field_path="/invalid~escape") | |
| def test_bad_input_envelope(changes): | |
| with pytest.raises(ValidationError): | |
| AgentInput[Dependencies, EmptyContext].model_validate(input_data(**changes)) | |
| def test_delta_reference_preserves_prior_run_and_session(): | |
| prior = ref(run_id="prior", path="quant/runs/prior/raw/SPY.json", artifact_id="prior-data") | |
| model = AgentInput[Dependencies, EmptyContext] | |
| assert model.model_validate(input_data(run_mode="DELTA", baseline_refs=[prior])).baseline_refs == [prior] | |
| for references in ([ref()], [prior, prior], [prior.model_copy(update={"session_id": "other"})]): | |
| with pytest.raises(ValidationError): | |
| model.model_validate(input_data(run_mode="DELTA", baseline_refs=references)) | |
| def test_bad_output_envelope(changes): | |
| with pytest.raises(ValidationError): | |
| AgentArtifact[Payload].model_validate(artifact_data(**changes)) | |
| def test_render_reference_must_point_to_own_artifact(): | |
| source = ref(producer="TrendAnalyst", artifact_id="trend", path="quant/runs/run1/trend_analysis.json") | |
| rendering = RenderingRef( | |
| format="MARKDOWN", artifact_id="trend-render", session_id="session1", run_id="run1", | |
| producer="TrendAnalyst", path="quant/runs/run1/trend_analysis.md", source=source, | |
| ) | |
| model = AgentArtifact[Payload] | |
| assert model.model_validate(artifact_data(render_ref=rendering)).render_ref == rendering | |
| with pytest.raises(ValidationError): | |
| model.model_validate(artifact_data(artifact_id="different", render_ref=rendering)) | |
| def test_trade_direction_is_not_market_direction(): | |
| with pytest.raises(ValidationError): | |
| TypeAdapter(TradeDirection).validate_python("BULLISH") | |
| with pytest.raises(ValidationError): | |
| TypeAdapter(Direction).validate_python("LONG") | |
| def test_runtime_aggregate_has_explicit_owner(): | |
| assert ref(producer="Runtime", path="quant/runs/run1/asset_analysis.json").producer == "Runtime" | |
| with pytest.raises(ValidationError): | |
| ref(producer="Runtime", path="quant/runs/run1/trend_analysis.json") | |