"""Tests for pooled winsorization across multiple experiment groups."""

import numpy as np
import pytest

from ab_stats import winsorize_experiment_groups


def test_pool_percentiles_differ_from_two_group_subset():
    """Full A+B+C pool max / percentiles must not equal A+B only."""
    a = np.array([1.0, 2.0, 3.0])
    b = np.array([4.0, 5.0])
    c = np.array([100.0])  # pulls pool max up
    full = winsorize_experiment_groups({"A": a, "B": b, "C": c}, upper_tail=None)
    partial = winsorize_experiment_groups({"A": a, "B": b}, upper_tail=None)
    assert full.p100 > partial.p100
    assert full.pool_n == 6
    assert partial.pool_n == 5


def test_upper_tail_none_preserves_values():
    groups = {"A": [1.0, 2.0, np.nan], "B": [3.0, 4.0]}
    r = winsorize_experiment_groups(groups, upper_tail=None)
    np.testing.assert_array_equal(r.winsorized["A"], np.array([1.0, 2.0]))
    np.testing.assert_array_equal(r.winsorized["B"], np.array([3.0, 4.0]))


def test_empty_group_raises():
    with pytest.raises(ValueError, match="no valid"):
        winsorize_experiment_groups({"A": [np.nan, np.nan]}, upper_tail=None)


def test_empty_mapping_raises():
    with pytest.raises(ValueError, match="non-empty"):
        winsorize_experiment_groups({}, upper_tail=None)


def test_invalid_upper_tail_raises():
    with pytest.raises(ValueError, match="upper_tail"):
        winsorize_experiment_groups({"A": [1.0], "B": [2.0, 3.0]}, upper_tail=0.1)
