"""Tests for node-worker deadline handling."""
import sys
import os
import time

sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "services", "node-worker"))

from models import JobRequest


def test_remaining_ms_with_future_deadline():
    job = JobRequest(deadline_ts=int(time.time() * 1000) + 10000)
    rem = job.remaining_ms()
    assert 9000 <= rem <= 10500


def test_remaining_ms_with_past_deadline():
    job = JobRequest(deadline_ts=int(time.time() * 1000) - 5000)
    assert job.remaining_ms() == 0


def test_remaining_ms_no_deadline():
    job = JobRequest(deadline_ts=0)
    assert job.remaining_ms() == 30_000


def test_effective_idem_key_uses_idempotency_key():
    job = JobRequest(job_id="j1", idempotency_key="custom-key")
    assert job.effective_idem_key() == "custom-key"


def test_effective_idem_key_falls_back_to_job_id():
    job = JobRequest(job_id="j2", idempotency_key="")
    assert job.effective_idem_key() == "j2"
