test_mock_client.py 7.0 KB

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