"""
Integration tests for ALPHANSO beam calculations.

These tests compare current calculation results against validated
ground truth to ensure no regressions have occurred.
"""

import pytest
from alphanso.transport import Transport
from tests.integration.configs import BEAM_CONFIGS
from tests.utils.comparison import compare_results


class TestBeamCalculations:
    """Integration tests for beam problem calculations."""

    @pytest.mark.integration
    @pytest.mark.parametrize("config", BEAM_CONFIGS, ids=lambda c: c['name'])
    def test_beam_calculation(self, config, beam_ground_truth):
        """
        Test that beam calculations match ground truth within tolerance.

        Compares:
        - an_yield: Neutron yield per incident alpha
        - an_spectrum: Normalized neutron energy spectrum
        """
        name = config['name']

        # Skip if no ground truth for this config
        if name not in beam_ground_truth:
            pytest.skip(f"No ground truth for {name}")

        expected = beam_ground_truth[name]

        # Check for error in ground truth
        if 'error' in expected:
            pytest.skip(f"Ground truth recorded error: {expected['error']}")

        # Run calculation
        actual = Transport.calculate(config)

        # Compare results
        passed, messages = compare_results(actual, expected, 'beam')

        # Build assertion message
        full_message = f"\n{name} comparison results:\n" + "\n".join(f"  {m}" for m in messages)

        assert passed, full_message
