"""Regression tests for services/attendance.py's own deterministic
explicit-date regex family (_ATTENDANCE_EXPLICIT_NUMERIC_DATE_RE /
_resolve_explicit_date_symbolic) — a near-duplicate of
date_query_resolver.py's _NUMERIC_DATE_PATTERN, reused directly by
regularisation_node.py's _build_detected_dates_block.

Confirmed live/reported: a stray space landing inside the year component
("21 - 08 - 20 26") made the old \\d{2,4} year group stop at "20" and read
it as a 2-digit year, silently resolving to 2020 instead of 2026. Fixed
by widening the year group to also accept a 4-digit year split by
whitespace, then collapsing that whitespace before checking digit count.
"""
from datetime import date

import services.attendance as attendance


def _resolve_numeric(query: str, today: date = date(2026, 9, 10)):
    m = attendance._ATTENDANCE_EXPLICIT_NUMERIC_DATE_RE.search(query)
    assert m, f"pattern did not match {query!r}"
    return attendance._resolve_explicit_date_symbolic(
        int(m.group(1)), int(m.group(2)), m.group(3), today,
    )


class TestNumericDateYearSplitByWhitespace:

    def test_space_split_year_after_dashes(self):
        result = _resolve_numeric("21 - 08 - 20 26")
        assert result["explicit_date"] == "2026-08-21"

    def test_space_split_year_tight_dashes(self):
        result = _resolve_numeric("21-08-20 26")
        assert result["explicit_date"] == "2026-08-21"

    def test_ordinary_contiguous_year_is_unaffected(self):
        assert _resolve_numeric("21-08-2026")["explicit_date"] == "2026-08-21"
        assert _resolve_numeric("21 - 08 - 2026")["explicit_date"] == "2026-08-21"

    def test_genuine_two_digit_year_is_unaffected(self):
        assert _resolve_numeric("21-08-26")["explicit_date"] == "2026-08-21"

    def test_no_year_still_defaults_to_current_year(self):
        assert _resolve_numeric("21-08")["explicit_date"] == "2026-08-21"
