"""Tests for memory guard per-segment estimation (Issue #53, Phase 4).

Validates:
- MEM-010 estimates memory for largest ouroboros segment, not full range
- Aion mode produces a single segment spanning the full range
"""

from __future__ import annotations

from datetime import date


class TestMEM010PerSegmentEstimation:
    """Test that MEM-010 estimates per-segment, not full range."""

    def test_iter_ouroboros_segments_aion_mode(self):
        """Aion mode should produce a single segment spanning the full range."""
        from opendeviationbar.ouroboros import iter_ouroboros_segments

        start = date(2024, 1, 1)
        end = date(2024, 4, 30)
        segments = list(iter_ouroboros_segments(start, end, "aion"))

        # Aion: single continuous segment
        assert len(segments) == 1

    def test_largest_segment_selection(self):
        """MEM-010 should find the largest segment by time span."""
        from opendeviationbar.ouroboros import iter_ouroboros_segments

        start = date(2024, 1, 15)
        end = date(2024, 3, 15)
        segments = list(iter_ouroboros_segments(start, end, "aion"))

        assert len(segments) == 1

        # Find largest by time span (same logic as open_deviation_bars.py)
        largest = max(segments, key=lambda s: (s[1] - s[0]).total_seconds())
        assert largest is not None

    def test_aion_mode_produces_single_segment(self):
        """Aion mode should produce 1 segment for any date range."""
        from opendeviationbar.ouroboros import iter_ouroboros_segments

        start = date(2024, 1, 1)
        end = date(2024, 12, 31)
        segments = list(iter_ouroboros_segments(start, end, "aion"))

        assert len(segments) == 1

    def test_open_deviation_bars_mem010_comment_present(self):
        """Verify MEM-010 comment is present in open_deviation_bars.py."""
        import inspect

        from opendeviationbar.orchestration.open_deviation_bars import (
            get_open_deviation_bars,
        )

        source = inspect.getsource(get_open_deviation_bars)
        assert "MEM-010" in source
        assert "LARGEST ouroboros segment" in source or "largest" in source.lower()
