from grantflow.core.job_runner import RedisJobRunner
from grantflow.tests.job_runner_helpers import FakeRedisClient, redis_client_factory


def test_redis_job_runner_worker_heartbeat_status_for_dispatcher_mode():
    fake_client = FakeRedisClient()
    runner = RedisJobRunner(
        worker_count=1,
        queue_maxsize=8,
        redis_url="redis://local-test/0",
        queue_name="grantflow:test:jobs:hb-status",
        pop_timeout_seconds=0.1,
        redis_client_factory=redis_client_factory(fake_client),
        consumer_enabled=False,
    )
    try:
        before = runner.worker_heartbeat_status()
        assert before["present"] is False
        assert before["healthy"] is False

        assert runner.touch_worker_heartbeat(source="test-worker") is True
        after = runner.worker_heartbeat_status()
        assert after["present"] is True
        assert after["healthy"] is True
        assert after["source"] == "test-worker"
        diag = runner.diagnostics()
        assert diag["worker_heartbeat"]["healthy"] is True
    finally:
        runner.stop()
