import importlib.util from pathlib import Path import sys import types import unittest from unittest.mock import patch MODULE_PATH = Path(__file__).resolve().parents[1] / "core" / "device_heartbeat.py" SPEC = importlib.util.spec_from_file_location("device_heartbeat_under_test", MODULE_PATH) HEARTBEAT = importlib.util.module_from_spec(SPEC) SPEC.loader.exec_module(HEARTBEAT) class DeviceHeartbeatTests(unittest.TestCase): def test_sends_current_device_id_to_server(self): calls = [] requests_module = types.ModuleType("requests") class Response: def raise_for_status(self): return None def json(self): return {"success": True, "lastSeenAt": "2026-07-28T00:00:00.000Z"} def post(url, json, timeout): calls.append((url, json, timeout)) return Response() requests_module.post = post api_module = types.ModuleType("api") api_module.data_record_url = "https://server.example/api" api_module.the_folder = "company/line" with patch.dict(sys.modules, {"requests": requests_module, "api": api_module}): timestamp = HEARTBEAT.heartbeat_device(timeout=7) self.assertEqual(timestamp, "2026-07-28T00:00:00.000Z") self.assertEqual(calls, [("https://server.example/api", { "type": "deviceHeartbeat", "deviceId": "company/line" }, 7)])