fix(heartbeat): make start idempotent and check exact OK token

This commit is contained in:
yzchen
2026-02-23 13:56:37 +08:00
parent bc32e85c25
commit bfdae1b177
2 changed files with 54 additions and 1 deletions

View File

@@ -0,0 +1,40 @@
import asyncio
import pytest
from nanobot.heartbeat.service import (
HeartbeatService,
_is_heartbeat_ok_response,
)
def test_heartbeat_ok_response_requires_exact_token() -> None:
assert _is_heartbeat_ok_response("HEARTBEAT_OK")
assert _is_heartbeat_ok_response("`HEARTBEAT_OK`")
assert _is_heartbeat_ok_response("**HEARTBEAT_OK**")
assert not _is_heartbeat_ok_response("HEARTBEAT_OK, done")
assert not _is_heartbeat_ok_response("done HEARTBEAT_OK")
assert not _is_heartbeat_ok_response("HEARTBEAT_NOT_OK")
@pytest.mark.asyncio
async def test_start_is_idempotent(tmp_path) -> None:
async def _on_heartbeat(_: str) -> str:
return "HEARTBEAT_OK"
service = HeartbeatService(
workspace=tmp_path,
on_heartbeat=_on_heartbeat,
interval_s=9999,
enabled=True,
)
await service.start()
first_task = service._task
await service.start()
assert service._task is first_task
service.stop()
await asyncio.sleep(0)