"""Tests for constants module."""

import pytest
from build_ai_context.constants import (
    generate_timestamp,
    is_timestamp,
    extract_timestamp_from_dir_name,
)


class TestTimestampFunctions:
    """Tests for timestamp helper functions."""

    def test_generate_timestamp_format(self):
        ts = generate_timestamp()
        # Should be YYYYMMDDTHHMMSSZ
        assert len(ts) == 16
        assert ts[8] == "T"
        assert ts[-1] == "Z"
        # All digits except T and Z
        assert ts[:8].isdigit()
        assert ts[9:15].isdigit()

    def test_is_timestamp_valid(self):
        assert is_timestamp("20260329T031811Z") == True
        assert is_timestamp("20260329T031811") == False
        assert is_timestamp("20260329T031811Z extra") == False
        assert is_timestamp("") == False
        assert is_timestamp("12345678T123456Z") == True

    def test_extract_timestamp_from_dir_name_with_timestamp(self):
        # Directory name generated by tool
        ts = extract_timestamp_from_dir_name("exported_sources_project_20260329T031811Z")
        assert ts == "20260329T031811Z"

    def test_extract_timestamp_from_dir_name_without_timestamp(self):
        # Custom directory name without timestamp
        ts = extract_timestamp_from_dir_name("my_output")
        # Should generate a fresh timestamp (format correct)
        assert is_timestamp(ts)

    def test_extract_timestamp_from_dir_name_multiple_underscores(self):
        ts = extract_timestamp_from_dir_name("exported_sources_project_20260329T031811Z")
        assert ts == "20260329T031811Z"
        ts2 = extract_timestamp_from_dir_name("exported_sources_nowinandroid_20260329T031811Z")
        assert ts2 == "20260329T031811Z"

    def test_extract_timestamp_from_dir_name_empty(self):
        ts = extract_timestamp_from_dir_name("")
        assert is_timestamp(ts)

    def test_extract_timestamp_from_dir_name_no_underscore(self):
        ts = extract_timestamp_from_dir_name("output")
        assert is_timestamp(ts)


if __name__ == "__main__":
    pytest.main([__file__])
