import asyncio import math import os import tempfile import uuid import numpy as np from tail2.core import BroadcastHub, LatestQueue, inverse_letterbox, normalize_bbox, normalize_keypoints def test_bbox_normalization_and_no_flip(): assert normalize_bbox([10, 20, 90, 80], 100, 100) == [.1, .2, .9, .8] def test_letterbox_inverse(): assert np.allclose(inverse_letterbox(np.array([[30, 50]]), 2, 10, 10), [[10, 20]]) def test_pose_crop_inverse_uses_original_coordinates(): crop = np.array([[5, 7]], dtype=float); crop[:, 0] += 20; crop[:, 1] += 30 assert crop.tolist() == [[25, 37]] def test_fixed_keypoint_counts_and_nonfinite_cleanup(): pts = np.array([[math.nan, math.inf, math.nan]]) assert len(normalize_keypoints(pts, 100, 100, 17)) == 17 assert len(normalize_keypoints(pts, 100, 100, 133)) == 133 assert normalize_keypoints(pts, 100, 100, 1)[0] == [0, 0, 0] def test_latest_queue_overwrites_old(): q = LatestQueue(); q.put(1); q.put(2) assert q.qsize() == 1 and q.get() == 2 def test_empty_people_contract(): assert {"people": []}["people"] == [] def test_session_uuid_changes_and_frame_monotonicity(): assert uuid.uuid4() != uuid.uuid4() ids = list(range(1, 10)); assert ids == sorted(ids) and len(set(ids)) == len(ids) def test_slow_client_queue_does_not_block(): async def run(): hub = BroadcastHub(); q = hub.add(); q.put_nowait({"n": 1}) if q.full(): q.get_nowait() q.put_nowait({"n": 2}); assert (await q.get())["n"] == 2 asyncio.run(run()) def test_disconnected_state_does_not_replay_old_people(): disconnected = {"source_alive": False, "people": []} assert disconnected["people"] == []