"""Tests for Rust CH writer feature flag routing (Phase 37, INTEG-03, Issue #318).

The feature flag OPENDEVIATIONBAR_USE_RUST_CH_WRITER controls which write path
the sidecar uses:
  - "off"    : Python-only CH writes (current behavior, default)
  - "shadow" : Dual-write with shadow comparison
  - "live"   : Rust-only CH writes (Python skips _write_batch_with_retry)
"""
from __future__ import annotations


def test_should_python_write_off():
    """off mode: Python should write."""
    from opendeviationbar.sidecar import _should_python_write

    assert _should_python_write("off") is True


def test_should_python_write_shadow():
    """shadow mode: Python should still write (dual-write)."""
    from opendeviationbar.sidecar import _should_python_write

    assert _should_python_write("shadow") is True


def test_should_python_write_live():
    """live mode: Python should NOT write (Rust handles it)."""
    from opendeviationbar.sidecar import _should_python_write

    assert _should_python_write("live") is False


def test_should_shadow_compare_shadow():
    """shadow mode: comparison should run."""
    from opendeviationbar.sidecar import _should_shadow_compare

    assert _should_shadow_compare("shadow") is True


def test_should_shadow_compare_off():
    """off mode: no comparison."""
    from opendeviationbar.sidecar import _should_shadow_compare

    assert _should_shadow_compare("off") is False


def test_should_shadow_compare_live():
    """live mode: no comparison."""
    from opendeviationbar.sidecar import _should_shadow_compare

    assert _should_shadow_compare("live") is False


def test_invalid_mode_defaults_to_off(monkeypatch):
    """Invalid mode string should default to 'off' with a warning."""

    from opendeviationbar.sidecar import _validate_rust_ch_writer_mode

    result = _validate_rust_ch_writer_mode("bogus")
    assert result == "off"


def test_valid_modes_pass_through():
    """Valid modes pass through unchanged."""
    from opendeviationbar.sidecar import _validate_rust_ch_writer_mode

    assert _validate_rust_ch_writer_mode("off") == "off"
    assert _validate_rust_ch_writer_mode("shadow") == "shadow"
    assert _validate_rust_ch_writer_mode("live") == "live"


def test_mode_case_insensitive():
    """Mode should be case-insensitive."""
    from opendeviationbar.sidecar import _validate_rust_ch_writer_mode

    assert _validate_rust_ch_writer_mode("OFF") == "off"
    assert _validate_rust_ch_writer_mode("Shadow") == "shadow"
    assert _validate_rust_ch_writer_mode("LIVE") == "live"
