from datetime import UTC, datetime

import pytest

from worker_watcher.models import JobDescriptor, RunDescriptor, validate_job_key


def test_valid_job_key() -> None:
    assert validate_job_key("youtube.transcript.batch") == "youtube.transcript.batch"


@pytest.mark.parametrize("key", ["Youtube.Transcript.Batch", "a..b", "a.b", "a.b.c-1", "a.b.c_1"])
def test_invalid_job_key(key: str) -> None:
    with pytest.raises(ValueError):
        validate_job_key(key)


def test_naive_datetime_rejected() -> None:
    with pytest.raises(ValueError):
        RunDescriptor(started_at=datetime(2026, 1, 1))  # noqa: DTZ001 - intentionally invalid input


def test_run_time_order() -> None:
    with pytest.raises(ValueError):
        RunDescriptor(started_at=datetime(2026, 1, 2, tzinfo=UTC), finished_at=datetime(2026, 1, 1, tzinfo=UTC))


def test_instance_key_is_stable() -> None:
    job = JobDescriptor("backup.vps.full", "Backup", "test", "batch")
    from worker_watcher.models import InstanceDescriptor
    first = InstanceDescriptor(job, "hermes", "process", "/opt/backup", instance_name="primary")
    second = InstanceDescriptor(job, "hermes", "process", "/different", instance_name="primary")
    assert first.instance_key == second.instance_key
    assert first.instance_key == "backup.vps.full|hermes|process|primary"
