test_mock_client.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. from __future__ import annotations
  2. import asyncio
  3. from datetime import timedelta
  4. import pytest
  5. from hth_sdk.advanced import ActionBatch, MockClient
  6. from hth_sdk.exceptions import ActionRejected, DataGap, DeviceEpochChanged, LeaseError, LeaseExpired
  7. from hth_sdk.profiles import prod2u
  8. def test_mock_connect_without_daemon():
  9. async def _run():
  10. async with MockClient() as client:
  11. devices = await client.list_devices()
  12. assert devices[0].backend_kind == "mock"
  13. caps = await client.get_capabilities("hood.0")
  14. assert caps.api_version == "hth.api.v1"
  15. assert caps.ack_supported is False
  16. assert caps.rpm_feedback_supported is False
  17. assert caps.firmware_version_quality == "NOT_SUPPORTED"
  18. client.set_temperature("temperature.channel.26", None)
  19. snap = await client.get_snapshot("hood.0")
  20. assert snap.device_confirmation == "UNCONFIRMED"
  21. invalid = snap.measurement("temperature.channel.26")
  22. assert invalid is not None
  23. assert invalid.quality == "INVALID"
  24. assert invalid.value is None
  25. assert invalid.raw_code == 255
  26. fan = snap.target("fan.chamber.1")
  27. assert fan is not None
  28. assert fan.actual_quality == "NOT_SUPPORTED"
  29. asyncio.run(_run())
  30. def test_mock_validate_and_apply_unconfirmed():
  31. async def _run():
  32. async with MockClient() as client:
  33. snap = await client.get_snapshot("hood.0")
  34. batch = (
  35. prod2u.builder("hood.0", allow_unverified=True)
  36. .command_id("idem-1")
  37. .based_on(snap.snapshot_seq, snap.control_revision)
  38. .set_fan(1, 15)
  39. .valid_for(timedelta(seconds=15))
  40. .build()
  41. )
  42. validated = await client.validate(batch)
  43. assert validated.accepted
  44. assert validated.confirmation == "UNCONFIRMED"
  45. assert validated.dispatched is None
  46. async with await client.acquire_lease("hood.0", "test", timedelta(seconds=5), auto_renew=False) as lease:
  47. result = await lease.apply(batch)
  48. assert result.confirmation == "UNCONFIRMED"
  49. assert result.dispatched is not None
  50. again = await lease.apply(batch)
  51. assert again.command_id == "idem-1"
  52. asyncio.run(_run())
  53. def test_mock_rejects_unknown_and_range():
  54. async def _run():
  55. async with MockClient() as client:
  56. async with await client.acquire_lease("hood.0", "test", timedelta(seconds=5), auto_renew=False) as lease:
  57. bad = ActionBatch.builder("hood.0", allow_unverified=True).set("no.such", 1).valid_for(timedelta(seconds=15)).build()
  58. with pytest.raises(ActionRejected):
  59. await lease.apply(bad)
  60. asyncio.run(_run())
  61. def test_lease_required_for_apply():
  62. async def _run():
  63. async with MockClient() as client:
  64. batch = prod2u.builder("hood.0", allow_unverified=True).set_fan(2, 10).valid_for(timedelta(seconds=15)).build()
  65. with pytest.raises(LeaseExpired):
  66. await client.apply(batch)
  67. asyncio.run(_run())
  68. def test_epoch_and_gap_notifications():
  69. async def _run():
  70. epochs = []
  71. gaps = []
  72. client = MockClient(
  73. on_device_epoch_changed=lambda old, new, snap: epochs.append((old, new)),
  74. on_data_gap=lambda snap: gaps.append(snap.dropped_telemetry_count),
  75. )
  76. async with client:
  77. agen = client.subscribe_telemetry("hood.0")
  78. first = await agen.__anext__()
  79. assert first.epoch_changed is False
  80. client.bump_epoch()
  81. second = await agen.__anext__()
  82. assert second.epoch_changed is True
  83. assert epochs
  84. client.inject_data_gap(2)
  85. third = await agen.__anext__()
  86. assert third.data_gap is True
  87. assert gaps
  88. await agen.aclose()
  89. asyncio.run(_run())
  90. def test_raise_on_epoch_change():
  91. async def _run():
  92. async with MockClient(raise_on_epoch_change=True) as client:
  93. agen = client.subscribe_telemetry("hood.0")
  94. await agen.__anext__()
  95. client.bump_epoch()
  96. with pytest.raises(DeviceEpochChanged):
  97. await agen.__anext__()
  98. await agen.aclose()
  99. asyncio.run(_run())
  100. def test_raise_on_data_gap():
  101. async def _run():
  102. async with MockClient(raise_on_data_gap=True) as client:
  103. agen = client.subscribe_telemetry("hood.0")
  104. await agen.__anext__()
  105. client.inject_data_gap()
  106. with pytest.raises(DataGap):
  107. await agen.__anext__()
  108. await agen.aclose()
  109. asyncio.run(_run())
  110. def test_enter_safe_requires_new_lease_to_resume():
  111. async def _run():
  112. async with MockClient() as client:
  113. snap = await client.get_snapshot("hood.0")
  114. async with await client.acquire_lease("hood.0", "safe-holder", timedelta(seconds=5), auto_renew=False) as lease:
  115. safe = await lease.enter_safe_mode("unit-test")
  116. assert safe.accepted
  117. b = (
  118. prod2u.builder("hood.0", allow_unverified=True)
  119. .command_id("resume-same-lease")
  120. .based_on(snap.snapshot_seq, snap.control_revision)
  121. .set_fan(1, 10)
  122. .set_fan(2, 10)
  123. .set_fan(3, 10)
  124. .set_fan(4, 10)
  125. .set_ptc(1, 0)
  126. .set_ptc(2, 0)
  127. .set_ptc(3, 0)
  128. .set_ptc(4, 0)
  129. .set_door_close_percent("x", 0)
  130. .set_door_close_percent("y", 0)
  131. .set_door_close_percent("z", 0)
  132. .valid_for(timedelta(seconds=15))
  133. .build()
  134. )
  135. with pytest.raises(LeaseError):
  136. await lease.resume_from_safe(b)
  137. snap = await client.get_snapshot("hood.0")
  138. async with await client.acquire_lease("hood.0", "resume-holder", timedelta(seconds=5), auto_renew=False) as lease:
  139. b = (
  140. prod2u.builder("hood.0", allow_unverified=True)
  141. .command_id("resume-new-lease")
  142. .based_on(snap.snapshot_seq, snap.control_revision)
  143. .set_fan(1, 10)
  144. .set_fan(2, 10)
  145. .set_fan(3, 10)
  146. .set_fan(4, 10)
  147. .set_ptc(1, 0)
  148. .set_ptc(2, 0)
  149. .set_ptc(3, 0)
  150. .set_ptc(4, 0)
  151. .set_door_close_percent("x", 0)
  152. .set_door_close_percent("y", 0)
  153. .set_door_close_percent("z", 0)
  154. .valid_for(timedelta(seconds=15))
  155. .build()
  156. )
  157. out = await lease.resume_from_safe(b)
  158. assert out.accepted
  159. asyncio.run(_run())