import numpy as np

from wormhole_proof.core.wormhole_smooth import SmoothConfig, run_smooth_experiment


def make_config_for_test() -> SmoothConfig:
    return SmoothConfig(
        dim=32,
        steps=240,
        warmup_steps=120,
        horizon_steps=24,
        soft_rank=6,
        max_packets=48,
        traversable_window=3,
        hp_message_bits=3,
        hp_decode_packets=4,
    )


def test_smooth_wormhole_produces_packets_and_hp_signal():
    cfg = make_config_for_test()
    result = run_smooth_experiment(cfg, seed=11)
    metrics = result.metrics

    assert metrics.packets > 0
    assert metrics.throughput > 0.0
    assert np.isfinite(metrics.hp_match)
    assert metrics.cfl_margin_min > 0.0
    assert metrics.relaxation_steps >= 0.0


def test_smooth_margin_stays_positive_and_hp_match_tracks_bits():
    cfg = make_config_for_test()
    cfg = SmoothConfig(**{**cfg.__dict__, "hp_message_bits": 2, "hp_decode_packets": 2})

    result = run_smooth_experiment(cfg, seed=5)
    metrics = result.metrics

    assert metrics.cfl_margin_min > 0.75
    if np.isfinite(metrics.hp_match):
        assert 0.0 <= metrics.hp_match <= 1.0
