File size: 3,820 Bytes
ebcde1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
from __future__ import annotations

import json
from pathlib import Path
from typing import Any

from streamlit.testing.v1 import AppTest

from microstructure.reporting import write_checksum_manifest

PROJECT_ROOT = Path(__file__).parents[1]
APP_PATH = PROJECT_ROOT / "dashboard" / "app.py"


def _write_json(path: Path, payload: Any) -> None:
    path.parent.mkdir(parents=True, exist_ok=True)
    path.write_text(json.dumps(payload, sort_keys=True) + "\n", encoding="utf-8")


def _dashboard_bundle(root: Path) -> Path:
    _write_json(
        root / "run_manifest.json",
        {
            "artifacts": {
                "execution_metrics": "metrics/execution_metrics.json",
                "execution_sensitivity": "metrics/execution_sensitivity.json",
                "market_state": "dashboard/market_state.json",
                "predictive_metrics": "metrics/predictive_metrics.json",
                "quality_summary": "quality/summary.json",
            },
            "data": {
                "mode": "synthetic",
                "observed_end_utc": "2024-01-02T00:10:00Z",
                "observed_start_utc": "2024-01-02T00:00:00Z",
                "source": "synthetic_fixture_v1",
                "symbols": ["BTCUSDT", "ETHUSDT"],
            },
            "evidence_tier": "SYNTHETIC_SMOKE",
            "execution_assumptions": {"taker_fee_bps": 4.0},
            "run_id": "dashboard-smoke",
            "status": "complete",
        },
    )
    _write_json(
        root / "provenance.json",
        {
            "config_sha256": "c" * 64,
            "evidence_tier": "SYNTHETIC_SMOKE",
            "generated_at_utc": "2026-08-07T12:00:00Z",
            "git": {"commit": "UNBORN", "dirty": True},
            "input_manifest_sha256": ["d" * 64],
        },
    )
    _write_json(root / "metrics" / "predictive_metrics.json", [{"model": "baseline"}])
    _write_json(root / "metrics" / "execution_metrics.json", [{"net_bps": -1.0}])
    _write_json(
        root / "metrics" / "execution_sensitivity.json",
        [{"order_type": "market", "size_multiplier": 1.0, "net_pnl": -1.0}],
    )
    _write_json(root / "dashboard" / "market_state.json", [{"spread_bps": 2.0}])
    _write_json(root / "quality" / "summary.json", {"error_count": 0})
    write_checksum_manifest(root)
    (root / "_SUCCESS").write_text("", encoding="utf-8")
    return root


def test_dashboard_rejects_an_incomplete_run(tmp_path: Path, monkeypatch: Any) -> None:
    incomplete = tmp_path / "incomplete"
    incomplete.mkdir()
    monkeypatch.setenv("MICROSTRUCTURE_RUN_DIR", str(incomplete))

    app = AppTest.from_file(str(APP_PATH)).run(timeout=10)

    assert not app.exception
    assert app.error
    assert "incomplete or invalid" in app.error[0].value
    assert "_SUCCESS" in app.caption[0].value


def test_dashboard_is_read_only_and_displays_all_evidence_tabs(
    tmp_path: Path, monkeypatch: Any
) -> None:
    run_dir = _dashboard_bundle(tmp_path / "complete")
    before = {
        path.relative_to(run_dir): path.read_bytes()
        for path in run_dir.rglob("*")
        if path.is_file()
    }
    monkeypatch.setenv("MICROSTRUCTURE_RUN_DIR", str(run_dir))

    app = AppTest.from_file(str(APP_PATH)).run(timeout=10)

    assert not app.exception
    assert app.warning
    assert "SYNTHETIC SMOKE" in app.warning[0].value
    assert [tab.label for tab in app.tabs] == [
        "Overview",
        "Data Quality",
        "Market State",
        "Predictions",
        "Simulated Performance",
        "Reproducibility & Limitations",
    ]
    assert any("Execution sensitivity grid" in item.value for item in app.markdown)
    after = {
        path.relative_to(run_dir): path.read_bytes()
        for path in run_dir.rglob("*")
        if path.is_file()
    }
    assert after == before