//! End-to-end coverage for the two new RTL standard cells (#281): `Select` (the
//! Bool -> F64 conditional bridge) and `Sign` (the promoted `sign0()` semantics).
//!
//! The in-module tests in `aura-std/src/select.rs` and `aura-std/src/sign.rs`
//! drive each node's `eval` directly — they never go through a `PrimitiveBuilder`
//! schema, a `GraphBuilder`-resolved wiring, or a compiled+bootstrapped
//! `FlatGraph`. That is the real seam a data-authored op-script or a Rust
//! `sma_signal`-style builder exercises (C24), and it is exactly what this file
//! proves: both cells are reachable from the public `aura_engine`/`aura_std`
//! construction surface, wire cleanly by declared port name/kind, and produce
//! the right values once actually driven cycle-by-cycle through a real
//! `Harness::run`.

use std::sync::mpsc;

use aura_core::{Firing, Scalar, ScalarKind, Timestamp};
use aura_engine::{GraphBuilder, VecSource};
use aura_std::{Abs, Gt, Recorder, Select, Sign};

/// A deterministic 5-tick F64 stream, no timestamps/randomness beyond a fixed
/// literal sequence.
fn ticks(values: &[f64]) -> Vec<(Timestamp, Scalar)> {
    values.iter().enumerate().map(|(i, &v)| (Timestamp(i as i64 + 1), Scalar::f64(v))).collect()
}

/// Property: **`Select` is the real Bool -> F64 bridge, through the compiled
/// engine, not merely by direct `eval`.** One `price` source feeds both `Abs`
/// (computing `|price|`) and `Gt(|price|, price)` (true exactly when
/// `price < 0`); `Select(cond, then=price, otherwise=|price|)` must forward the
/// RAW (possibly negative) `price` on the one row where `price < 0` and the
/// (always non-negative) `|price|` on every other row. Since `|price| == price`
/// for every non-negative row, the only way to distinguish "the mux correctly
/// read `cond` and picked `then`" from "the wiring is inverted / stuck on one
/// branch" is the single negative row: a stuck-on-`otherwise` bug would emit
/// `3.0` there instead of `-3.0`. This drives the REAL `PrimitiveBuilder`
/// schemas (`Gt`'s declared `bool` output, `Select`'s declared `bool`/`f64`/`f64`
/// inputs) through `GraphBuilder::build` -> `compile_with_params` ->
/// `Harness::run`, the seam an op-script or a `sma_signal`-style Rust builder
/// actually uses — the in-module `select.rs` unit tests never leave direct
/// `eval` calls.
#[test]
fn select_bridges_a_real_gt_bool_output_into_f64_end_to_end() {
    let (tx, rx) = mpsc::channel();
    let mut g = GraphBuilder::new("root");
    let price = g.source_role("price", ScalarKind::F64);
    let abs = g.add(Abs::builder());
    let gt = g.add(Gt::builder());
    let select = g.add(Select::builder());
    let rec = g.add(Recorder::builder(vec![ScalarKind::F64], Firing::Any, tx));

    g.feed(price, [abs.input("value"), gt.input("b"), select.input("then")]);
    g.connect(abs.output("value"), gt.input("a"));
    g.connect(abs.output("value"), select.input("otherwise"));
    g.connect(gt.output("value"), select.input("cond"));
    g.connect(select.output("value"), rec.input("col[0]"));

    let mut h = g.build().expect("resolves by name").bootstrap_with_params(vec![]).expect("bootstraps");
    h.run(vec![Box::new(VecSource::new(ticks(&[-3.0, 0.0, 2.0, -0.0, 5.0])))]);
    let trace: Vec<f64> = rx.try_iter().map(|(_, row)| row[0].as_f64()).collect();

    assert_eq!(
        trace,
        vec![-3.0, 0.0, 2.0, 0.0, 5.0],
        "row 0 (price<0) must forward the raw negative `then`; every other row forwards `|price|`"
    );
}

/// Property: **`Sign` correctly registers its `PrimitiveBuilder` schema
/// (F64 in, F64 out) and dispatches through a compiled+bootstrapped
/// `FlatGraph`, mapping the three sign regions across real engine cycles** —
/// not merely via a direct `eval` call (the in-module `sign.rs` unit tests'
/// seam). A schema/wiring regression (wrong port kind, wrong output arity)
/// would fail at `GraphBuilder::build` or `compile_with_params`, before a
/// single cycle runs; a dispatch regression would show up in the recorded
/// values below.
#[test]
fn sign_dispatches_through_a_compiled_graph_across_all_three_regions() {
    let (tx, rx) = mpsc::channel();
    let mut g = GraphBuilder::new("root");
    let spread = g.source_role("spread", ScalarKind::F64);
    let sign = g.add(Sign::builder());
    let rec = g.add(Recorder::builder(vec![ScalarKind::F64], Firing::Any, tx));

    g.feed(spread, [sign.input("value")]);
    g.connect(sign.output("value"), rec.input("col[0]"));

    let mut h = g.build().expect("resolves by name").bootstrap_with_params(vec![]).expect("bootstraps");
    h.run(vec![Box::new(VecSource::new(ticks(&[-3.0, 0.0, 2.5, -0.0, 4.0])))]);
    let trace: Vec<f64> = rx.try_iter().map(|(_, row)| row[0].as_f64()).collect();

    assert_eq!(trace, vec![-1.0, 0.0, 1.0, 0.0, 1.0], "all three sign regions, dispatched through the real engine");
}
