| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- import importlib.util
- import sys
- from pathlib import Path
- import pandas as pd
- import pytest
- def load_module():
- path = Path(__file__).resolve().parents[1] / "scripts" / "build_eth_nextgen_micro_signal_intent.py"
- spec = importlib.util.spec_from_file_location("build_eth_nextgen_micro_signal_intent", path)
- assert spec is not None
- module = importlib.util.module_from_spec(spec)
- assert spec.loader is not None
- sys.modules[spec.name] = module
- spec.loader.exec_module(module)
- return module
- def test_latest_active_engine_uses_shifted_prior_day_regime(monkeypatch):
- module = load_module()
- index = pd.date_range("2026-01-01", periods=32, freq="D", tz="UTC")
- existing = pd.DataFrame({"cost_model": ["maker_taker"], "name": ["equal-2-c0003"], "date": [index[0].strftime("%Y-%m-%d")]})
- nextgen = pd.Series([100.0] * 31 + [80.0], index=index)
- micro = pd.Series([100.0] * 31 + [120.0], index=index)
- monkeypatch.setattr(module.pd, "read_csv", lambda _: existing)
- monkeypatch.setattr(module.portfolio, "load_nextgen", lambda _, __: (nextgen, []))
- monkeypatch.setattr(module.portfolio, "load_micro_candidates", lambda _, __: {module.MICRO_NAME: (micro, [])})
- state = module.latest_active_engine()
- assert state["active_engine"] == "nextgen"
- def test_latest_active_engine_switches_on_prior_completed_day(monkeypatch):
- module = load_module()
- index = pd.date_range("2026-01-01", periods=33, freq="D", tz="UTC")
- existing = pd.DataFrame({"cost_model": ["maker_taker"], "name": ["equal-2-c0003"], "date": [index[0].strftime("%Y-%m-%d")]})
- nextgen = pd.Series([100.0] * 31 + [80.0, 80.0], index=index)
- micro = pd.Series([100.0] * 31 + [120.0, 120.0], index=index)
- monkeypatch.setattr(module.pd, "read_csv", lambda _: existing)
- monkeypatch.setattr(module.portfolio, "load_nextgen", lambda _, __: (nextgen, []))
- monkeypatch.setattr(module.portfolio, "load_micro_candidates", lambda _, __: {module.MICRO_NAME: (micro, [])})
- state = module.latest_active_engine()
- assert state["active_engine"] == "micro"
- def test_payload_is_readonly(monkeypatch):
- module = load_module()
- monkeypatch.setattr(module, "latest_active_engine", lambda: {"active_engine": "nextgen", "decision_date": "2026-01-01"})
- monkeypatch.setattr(
- module.nextgen_intent,
- "build_payload",
- lambda: {"decision": {"signal": "no_signal"}, "legs": []},
- )
- monkeypatch.setattr(module, "micro_signal", lambda: {"signal": "short"})
- payload = module.build_payload()
- assert payload["submitted_orders"] == 0
- assert payload["private_key_required"] is False
- assert payload["risk_limits"]["no_order_submission"] is True
- assert payload["risk_limits"]["blocked_for_live_trading"] is True
- assert payload["decision"]["selected_signal"] == "no_signal"
|