"""Tests del Historian DuckDB (Sprint 4).""" from __future__ import annotations import asyncio from datetime import UTC, datetime, timedelta import pytest from vmssailor.core.enums import Protocol, UnitSI from vmssailor.core.tag import Tag from vmssailor.runtime.server.historian import Historian from vmssailor.runtime.server.tag_store import TagStore def _tag(tag_id: str) -> Tag: return Tag(id=tag_id, unit_si=UnitSI.BAR, protocol=Protocol.MODBUS_RTU, address=1) @pytest.mark.asyncio async def test_historian_records_updates(): store = TagStore() store.register_tag(_tag("X")) h = Historian() # in-memory await h.start(store) try: for v in (4.0, 4.1, 4.2): await store.update("X", v) # Force flush await asyncio.sleep(0.05) await h._flush(force=True) rows = h.query( "X", since=datetime.now(UTC) - timedelta(minutes=1), ) assert len(rows) == 3 values = [r["value"] for r in rows] assert values == [4.0, 4.1, 4.2] finally: await h.stop() @pytest.mark.asyncio async def test_historian_stats(): store = TagStore() store.register_tag(_tag("X")) h = Historian() await h.start(store) try: await store.update("X", 1.0) await asyncio.sleep(0.05) await h._flush(force=True) stats = h.stats() assert stats["total_samples"] >= 1 finally: await h.stop()