"""后台 board 缓存:版本递增与快照."""
from __future__ import annotations

import asyncio
import sys
import unittest
from pathlib import Path

ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT))
sys.path.insert(0, str(ROOT / "manual_trading_hub"))

from hub_board_cache import MonitorBoardStore  # noqa: E402


class TestHubBoardStore(unittest.TestCase):
    def test_snapshot_and_version(self) -> None:
        store = MonitorBoardStore()
        store.version = 2
        store.payload = {"ok": True, "rows": [{"id": "0"}], "updated_at": "2026-01-01T00:00:00"}
        snap = store.snapshot_dict()
        self.assertEqual(snap["board_version"], 2)
        self.assertEqual(len(snap["rows"]), 1)

    def test_aggregate_increments_version(self) -> None:
        async def run() -> None:
            store = MonitorBoardStore()
            n = 0

            async def build():
                nonlocal n
                n += 1
                return {"ok": True, "rows": [{"n": n}], "updated_at": "t"}

            await store.start(build)
            await asyncio.sleep(0.05)
            self.assertGreaterEqual(store.version, 1)
            await store.stop()

        asyncio.run(run())


if __name__ == "__main__":
    unittest.main()
