"""Golden tests for WALK_FORWARD_BOOTSTRAP_V1 (governance/todo/v8_9_p3_adoption_plan.yaml P3-B). Maps to v8.9 proposal golden cases V89_014 (same_regime_sample_low) and V89_048 (solver_failure -- here, no historical_returns at all). """ from __future__ import annotations import importlib.util import random from pathlib import Path ROOT = Path(__file__).resolve().parents[3] MODULE_PATH = ROOT / "tools" / "build_walk_forward_bootstrap_v1.py" def _load_module(): spec = importlib.util.spec_from_file_location("build_walk_forward_bootstrap_v1", MODULE_PATH) module = importlib.util.module_from_spec(spec) assert spec.loader is not None spec.loader.exec_module(module) return module def _sample_returns(n=30): rng = random.Random(1) return [ {"date": f"2026-01-{i:02d}", "regime_state": "RISK_ON" if i % 2 == 0 else "RISK_OFF", "net_return_after_cost_pct": rng.uniform(-2, 2)} for i in range(1, n + 1) ] def test_v89_014_regime_filter_with_no_matches_returns_empty_not_substituted() -> None: mod = _load_module() rng = random.Random(1) distribution = mod.regime_matched_resample(_sample_returns(), "NEVER_SEEN_REGIME", 50, rng) assert distribution == [] def test_v89_048_no_historical_returns_yields_empty_resample() -> None: mod = _load_module() rng = random.Random(1) distribution = mod.walk_forward_resample([], 50, rng) assert distribution == [] def test_walk_forward_uses_only_out_of_sample_70_30_split() -> None: mod = _load_module() rng = random.Random(1) returns = _sample_returns(20) distribution = mod.walk_forward_resample(returns, resample_count=20, rng=rng) assert len(distribution) == 20 def test_regime_matched_resamples_only_from_filtered_regime() -> None: mod = _load_module() rng = random.Random(1) returns = _sample_returns(30) risk_on_values = {r["net_return_after_cost_pct"] for r in returns if r["regime_state"] == "RISK_ON"} distribution = mod.regime_matched_resample(returns, "RISK_ON", 50, rng) assert all(v in risk_on_values for v in distribution)