# ABOUTME: Tests that verify the example values in docs/summary_statistics_reference.md.
# ABOUTME: Uses the high-level msastats API with inline sequences.

"""
Tests for the documented example MSA in docs/summary_statistics_reference.md.

These tests ensure the documentation stays in sync with the implementation
by verifying all 27 metric values against the reference example.
"""

import pytest

import msastats

# The example MSA from docs/summary_statistics_reference.md
REFERENCE_MSA = [
    "-AAAAAAAAAAAAAAAAA---AAAAAAAAAAA-----",
    "A----AA-AA-AAAA--A---A---A-----AAAAAA",
    "A---AAA-AA-A--A--AAAAA---A-----A-----",
    "A--AAAAAAA-A--A--AAAAA---AAAAAAA-----",
]

# Expected values from docs/summary_statistics_reference.md
# Format: (stat_name, expected_value)
REFERENCE_VALUES = [
    ("AVG_GAP_SIZE", pytest.approx(2.71, abs=0.01)),
    ("MSA_LEN", 37),
    ("LONGEST_UNALIGNED_SEQ", 28),
    ("SHORTEST_UNALIGNED_SEQ", 15),
    ("TOT_NUM_GAPS", 24),
    ("NUM_GAPS_LEN_ONE", 6),
    ("NUM_GAPS_LEN_TWO", 6),
    ("NUM_GAPS_LEN_THREE", 6),
    ("NUM_GAPS_LEN_AT_LEAST_FOUR", 6),
    ("AVG_UNIQUE_GAP_SIZE", pytest.approx(2.67, abs=0.01)),
    ("TOT_NUM_UNIQUE_GAPS", 12),
    ("NUM_GAPS_LEN_ONE_IN_ONE_SEQ", 1),
    ("NUM_GAPS_LEN_ONE_IN_TWO_SEQS", 1),
    ("NUM_GAPS_LEN_ONE_IN_ALL_EXCEPT_ONE", 1),
    ("NUM_GAPS_LEN_TWO_IN_ONE_SEQ", 1),
    ("NUM_GAPS_LEN_TWO_IN_TWO_SEQS", 1),
    ("NUM_GAPS_LEN_TWO_IN_ALL_EXCEPT_ONE", 1),
    ("NUM_GAPS_LEN_THREE_IN_ONE_SEQ", 1),
    ("NUM_GAPS_LEN_THREE_IN_TWO_SEQS", 1),
    ("NUM_GAPS_LEN_THREE_IN_ALL_EXCEPT_ONE", 1),
    ("NUM_GAPS_LEN_AT_LEAST_FOUR_IN_ONE_SEQ", 1),
    ("NUM_GAPS_LEN_AT_LEAST_FOUR_IN_TWO_SEQS", 1),
    ("NUM_GAPS_LEN_AT_LEAST_FOUR_IN_ALL_EXCEPT_ONE", 1),
    ("MSA_POSITION_WITH_0_GAPS", 10),
    ("MSA_POSITION_WITH_1_GAPS", 2),
    ("MSA_POSITION_WITH_2_GAPS", 12),
    ("MSA_POSITION_WITH_N_MINUS_1_GAPS", 13),
]


@pytest.fixture(scope="module")
def reference_stats() -> dict[str, float]:
    """Compute stats for the reference MSA once, return as name->value dict."""
    stats = msastats.calculate_msa_stats(REFERENCE_MSA)
    names = msastats.stats_names()
    return dict(zip(names, stats))


@pytest.mark.parametrize("stat_name, expected_value", REFERENCE_VALUES)
def test_reference_example(reference_stats: dict[str, float], stat_name: str, expected_value):
    """Verify each metric matches the documented value in summary_statistics_reference.md."""
    assert reference_stats[stat_name] == expected_value
