|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import copy |
| 4 | +import importlib.util |
| 5 | +import json |
| 6 | +import sys |
| 7 | +import unittest |
| 8 | +from pathlib import Path |
| 9 | + |
| 10 | + |
| 11 | +ROOT = Path(__file__).resolve().parents[1] |
| 12 | +SCRIPTS = ROOT / "scripts" |
| 13 | + |
| 14 | + |
| 15 | +def _load_module(name: str): |
| 16 | + spec = importlib.util.spec_from_file_location(name, SCRIPTS / f"{name}.py") |
| 17 | + module = importlib.util.module_from_spec(spec) |
| 18 | + assert spec.loader is not None |
| 19 | + sys.modules[spec.name] = module |
| 20 | + spec.loader.exec_module(module) |
| 21 | + return module |
| 22 | + |
| 23 | + |
| 24 | +composer = _load_module("long_horizon_risk_composer") |
| 25 | +ingress = _load_module("long_horizon_risk_observation_ingress") |
| 26 | + |
| 27 | + |
| 28 | +class LongHorizonRiskObservationIngressTest(unittest.TestCase): |
| 29 | + @staticmethod |
| 30 | + def _returns(gain_bps: int, drawdown_bps: int) -> list[int]: |
| 31 | + return [gain_bps] * 240 + [-drawdown_bps] * 12 |
| 32 | + |
| 33 | + def _observation(self) -> dict[str, object]: |
| 34 | + paths = [ |
| 35 | + { |
| 36 | + "scenario_id": f"soxl_soxx_{kind.lower()}_{index}", |
| 37 | + "scenario_kind": kind, |
| 38 | + "session_count": 253, |
| 39 | + "strategy_returns_bps": self._returns(16 - index, 124 + index), |
| 40 | + "benchmark_returns_bps": self._returns(10 - index, 100 + index), |
| 41 | + } |
| 42 | + for index, kind in enumerate(("WALK_FORWARD", "BOOTSTRAP", "STRESS"), start=1) |
| 43 | + ] |
| 44 | + result: dict[str, object] = { |
| 45 | + "schema": "qsl.long_horizon_risk_observation.v1", |
| 46 | + "candidate": { |
| 47 | + "candidate_id": "soxl_soxx_longterm_compounding", |
| 48 | + "candidate_kind": "individual", |
| 49 | + "strategy_repository": "QuantStrategyLab/UsEquityStrategies", |
| 50 | + "strategy_revision": "a" * 40, |
| 51 | + }, |
| 52 | + "source_evidence": { |
| 53 | + "p1_input_digest": "1" * 64, |
| 54 | + "p2_config_digest": "2" * 64, |
| 55 | + "p3_evidence_sha256": "3" * 64, |
| 56 | + "plugin_bundle_sha256": "4" * 64, |
| 57 | + }, |
| 58 | + "benchmark": { |
| 59 | + "benchmark_id": "soxx", |
| 60 | + "benchmark_kind": "unlevered_reference", |
| 61 | + "sessions_per_year": 252, |
| 62 | + }, |
| 63 | + "scenario_paths": paths, |
| 64 | + "observation_sha256": "", |
| 65 | + } |
| 66 | + result["observation_sha256"] = composer.calculate_risk_observation_sha256(result) |
| 67 | + return result |
| 68 | + |
| 69 | + def test_reads_only_the_exact_candidate_and_p3_object_then_returns_a_redacted_recommendation(self): |
| 70 | + observation = self._observation() |
| 71 | + raw = json.dumps(observation, sort_keys=True, separators=(",", ":"), ensure_ascii=True).encode("utf-8") |
| 72 | + calls: list[str] = [] |
| 73 | + |
| 74 | + result = ingress.compose_from_private_long_horizon_risk_observation( |
| 75 | + candidate_id="soxl_soxx_longterm_compounding", |
| 76 | + p3_evidence_sha256="3" * 64, |
| 77 | + risk_preference="BALANCED_COMPOUNDING", |
| 78 | + read_exact=lambda object_name: calls.append(object_name) or raw, |
| 79 | + ) |
| 80 | + |
| 81 | + self.assertEqual( |
| 82 | + calls, |
| 83 | + [ |
| 84 | + "long-horizon-risk-observations/v1/soxl_soxx_longterm_compounding/" |
| 85 | + + ("3" * 64) |
| 86 | + + ".json" |
| 87 | + ], |
| 88 | + ) |
| 89 | + self.assertEqual(result["status"], "ADVISORY_RECOMMENDATION_READY") |
| 90 | + serialized = json.dumps(result, sort_keys=True).lower() |
| 91 | + self.assertNotIn("strategy_returns", serialized) |
| 92 | + self.assertNotIn("benchmark_returns", serialized) |
| 93 | + self.assertNotIn("broker", serialized) |
| 94 | + self.assertNotIn("account", serialized) |
| 95 | + |
| 96 | + def test_reader_failure_tampering_and_identity_mismatch_fail_closed_without_fallback(self): |
| 97 | + observation = self._observation() |
| 98 | + raw = json.dumps(observation).encode("utf-8") |
| 99 | + with self.assertRaisesRegex(ingress.LongHorizonRiskObservationIngressError, "unavailable"): |
| 100 | + ingress.load_private_long_horizon_risk_observation( |
| 101 | + candidate_id="soxl_soxx_longterm_compounding", |
| 102 | + p3_evidence_sha256="3" * 64, |
| 103 | + read_exact=lambda _object_name: (_ for _ in ()).throw(RuntimeError("storage hostname")), |
| 104 | + ) |
| 105 | + |
| 106 | + tampered = copy.deepcopy(observation) |
| 107 | + tampered["scenario_paths"][0]["strategy_returns_bps"][0] = 99 |
| 108 | + with self.assertRaisesRegex(ingress.LongHorizonRiskObservationIngressError, "unavailable"): |
| 109 | + ingress.load_private_long_horizon_risk_observation( |
| 110 | + candidate_id="soxl_soxx_longterm_compounding", |
| 111 | + p3_evidence_sha256="3" * 64, |
| 112 | + read_exact=lambda _object_name: json.dumps(tampered).encode("utf-8"), |
| 113 | + ) |
| 114 | + |
| 115 | + with self.assertRaisesRegex(ingress.LongHorizonRiskObservationIngressError, "unavailable"): |
| 116 | + ingress.load_private_long_horizon_risk_observation( |
| 117 | + candidate_id="soxl_soxx_longterm_compounding", |
| 118 | + p3_evidence_sha256="4" * 64, |
| 119 | + read_exact=lambda _object_name: raw, |
| 120 | + ) |
| 121 | + |
| 122 | + def test_invalid_identities_and_oversized_input_never_reach_the_reader(self): |
| 123 | + calls: list[str] = [] |
| 124 | + with self.assertRaisesRegex(ingress.LongHorizonRiskObservationIngressError, "unavailable"): |
| 125 | + ingress.load_private_long_horizon_risk_observation( |
| 126 | + candidate_id="../latest", |
| 127 | + p3_evidence_sha256="3" * 64, |
| 128 | + read_exact=lambda object_name: calls.append(object_name) or b"{}", |
| 129 | + ) |
| 130 | + self.assertEqual(calls, []) |
| 131 | + |
| 132 | + with self.assertRaisesRegex(ingress.LongHorizonRiskObservationIngressError, "unavailable"): |
| 133 | + ingress.load_private_long_horizon_risk_observation( |
| 134 | + candidate_id="soxl_soxx_longterm_compounding", |
| 135 | + p3_evidence_sha256="3" * 64, |
| 136 | + read_exact=lambda _object_name: b"x" * (ingress.MAX_PRIVATE_OBSERVATION_BYTES + 1), |
| 137 | + ) |
| 138 | + |
| 139 | + |
| 140 | +if __name__ == "__main__": |
| 141 | + unittest.main() |
0 commit comments