from __future__ import annotations import asyncio from datetime import timedelta import pytest from hth_sdk import ActionBatch, MockClient from hth_sdk.exceptions import ActionRejected, DataGap, DeviceEpochChanged, LeaseExpired from hth_sdk.profiles import prod2u def test_mock_connect_without_daemon(): async def _run(): async with MockClient() as client: devices = await client.list_devices() assert devices[0].backend_kind == "mock" caps = await client.get_capabilities("hood.0") assert caps.api_version == "hth.api.v1" assert caps.ack_supported is False assert caps.rpm_feedback_supported is False assert caps.firmware_version_quality == "NOT_SUPPORTED" snap = await client.get_snapshot("hood.0") assert snap.device_confirmation == "UNCONFIRMED" invalid = snap.measurement("temperature.channel.26") assert invalid is not None assert invalid.quality == "INVALID" assert invalid.value is None assert invalid.raw_code == 255 fan = snap.target("fan.chamber.1") assert fan is not None assert fan.actual_quality == "NOT_SUPPORTED" asyncio.run(_run()) def test_mock_validate_and_apply_unconfirmed(): async def _run(): async with MockClient() as client: snap = await client.get_snapshot("hood.0") batch = ( prod2u.builder("hood.0", allow_unverified=True) .command_id("idem-1") .based_on(snap.snapshot_seq, snap.control_revision) .set_fan(1, 15) .valid_for(timedelta(seconds=15)) .build() ) validated = await client.validate(batch) assert validated.accepted assert validated.confirmation == "UNCONFIRMED" assert validated.dispatched is None async with await client.acquire_lease("hood.0", "test", timedelta(seconds=5), auto_renew=False) as lease: result = await lease.apply(batch) assert result.confirmation == "UNCONFIRMED" assert result.dispatched is not None again = await lease.apply(batch) assert again.command_id == "idem-1" asyncio.run(_run()) def test_mock_rejects_unknown_and_range(): async def _run(): async with MockClient() as client: async with await client.acquire_lease("hood.0", "test", timedelta(seconds=5), auto_renew=False) as lease: bad = ActionBatch.builder("hood.0", allow_unverified=True).set("no.such", 1).valid_for(timedelta(seconds=15)).build() with pytest.raises(ActionRejected): await lease.apply(bad) asyncio.run(_run()) def test_lease_required_for_apply(): async def _run(): async with MockClient() as client: batch = prod2u.builder("hood.0", allow_unverified=True).set_fan(2, 10).valid_for(timedelta(seconds=15)).build() with pytest.raises(LeaseExpired): await client.apply(batch) asyncio.run(_run()) def test_epoch_and_gap_notifications(): async def _run(): epochs = [] gaps = [] client = MockClient( on_device_epoch_changed=lambda old, new, snap: epochs.append((old, new)), on_data_gap=lambda snap: gaps.append(snap.dropped_telemetry_count), ) async with client: agen = client.subscribe_telemetry("hood.0") first = await agen.__anext__() assert first.epoch_changed is False client.bump_epoch() second = await agen.__anext__() assert second.epoch_changed is True assert epochs client.inject_data_gap(2) third = await agen.__anext__() assert third.data_gap is True assert gaps await agen.aclose() asyncio.run(_run()) def test_raise_on_epoch_change(): async def _run(): async with MockClient(raise_on_epoch_change=True) as client: agen = client.subscribe_telemetry("hood.0") await agen.__anext__() client.bump_epoch() with pytest.raises(DeviceEpochChanged): await agen.__anext__() await agen.aclose() asyncio.run(_run()) def test_raise_on_data_gap(): async def _run(): async with MockClient(raise_on_data_gap=True) as client: agen = client.subscribe_telemetry("hood.0") await agen.__anext__() client.inject_data_gap() with pytest.raises(DataGap): await agen.__anext__() await agen.aclose() asyncio.run(_run())