//! Gated integration test: the GER40 15m session-breakout strategy — the
//! Phase-1 `aura-std` node vocabulary wired VERBATIM from
//! `aura-engine/tests/ger40_breakout.rs` — driven over REAL GER40 M1 bars from
//! the local data-server archive (September 2024). Mirrors `real_bars.rs`:
//! skips with a note where the local Pepperstone archive is absent, so
//! `cargo test --workspace` stays green anywhere; exercises the real ingestion
//! path where the files exist.
//!
//! The breakout FlatGraph is built once in the shared module (`shared/
//! breakout_real.rs`) and reused by both this test and the runnable example, so
//! the 11-node wiring is never duplicated.

use std::sync::Arc;

use aura_core::Timestamp;
use aura_ingest::{default_data_server, open_ohlc, DataServer, DEFAULT_DATA_PATH};

#[path = "../examples/shared/breakout_real.rs"]
mod breakout_real;
use breakout_real::*;

// September 2024 (inclusive), the same window the example demonstrates.
const YEAR: i32 = 2024;
const MONTH: u32 = 9;

/// Build the real breakout harness, run it over the epoch-ns [`Timestamp`]
/// window `[from, to]`, and return the folded `RunReport` plus the drained
/// per-bar `(held, equity)` series. A fresh build per call keeps the two
/// determinism runs disjoint (C1).
fn run_real(
    server: &Arc<DataServer>,
    from: Timestamp,
    to: Timestamp,
) -> (aura_engine::RunReport, Vec<(f64, f64)>) {
    let sources =
        open_ohlc(server, SYMBOL, from, to).expect("GER40 has data in the Sept-2024 window");
    let (mut h, taps) = build_harness();
    h.run(sources);

    // Drain the trace once; fold the report from the same trace via the shared
    // helper so the mpsc channels are consumed exactly once and the report shape
    // matches the example's byte-for-byte.
    let trace = drain_trace(&taps);
    let report = report_from_trace(&trace, from, to);
    let series: Vec<(f64, f64)> = trace.iter().map(|b| (b.held, b.equity)).collect();
    (report, series)
}

/// Property: the real-bar GER40 breakout harness runs end-to-end over a real
/// data-server month and is **deterministic** (C1) — two disjoint runs over the
/// byte-identical archive window produce bit-identical reports AND bit-identical
/// recorded series. Also pins two observable invariants of the composed
/// vocabulary on real data: the metrics are finite, and the Latch output
/// (`held`) is *exactly* 0.0 or 1.0 every bar (a binary exposure register, never
/// a fractional or NaN value). This protects the whole node composition —
/// Resample / Delay / Gt / Session / EqConst / And / Latch / SimBroker — against
/// any regression that would only surface on real (irregular, gap-laden) M1
/// data, which the synthetic single-session capstone cannot reach.
#[test]
fn ger40_breakout_real_bars_run_is_deterministic() {
    let server = default_data_server();
    if !server.has_symbol(SYMBOL) {
        eprintln!("skip: no local data at {DEFAULT_DATA_PATH} (symbol {SYMBOL} absent)");
        return; // hermetic elsewhere; exercises the real path where files exist
    }

    let (from, to) = utc_month_window(YEAR, MONTH);

    let (r1, s1) = run_real(&server, from, to);
    assert!(!s1.is_empty(), "window resolved to zero bars");

    // Metrics are finite — a backtest actually ran over real bars.
    assert!(r1.metrics.total_pips.is_finite(), "total_pips finite");
    assert!(r1.metrics.max_drawdown.is_finite(), "max_drawdown finite");

    // Latch output is a binary exposure register: every recorded `held` is
    // exactly 0.0 or 1.0 — never fractional, never NaN.
    for (held, _) in &s1 {
        assert!(
            *held == 0.0 || *held == 1.0,
            "held must be exactly 0.0 or 1.0 (Latch register), got {held}"
        );
    }

    // C1 determinism: a fresh disjoint run over the identical window is
    // bit-identical in both the folded report and the raw recorded series.
    let (r2, s2) = run_real(&server, from, to);
    assert_eq!(r1.to_json(), r2.to_json(), "report bit-identical across runs (C1)");
    assert_eq!(s1, s2, "recorded (held, equity) series bit-identical across runs (C1)");
}
