Spaces:
Sleeping
Sleeping
Download tests/test_capture.py from aadi-joshi/omninpc: direct link, hf CLI and curl.
- Browser
- Download file 3.81 kB
-
https://huggingface.co/spaces/aadi-joshi/omninpc/resolve/main/tests/test_capture.py
- Command line
-
hf download hf://spaces/aadi-joshi/omninpc/tests/test_capture.py
-
curl -L -o test_capture.py https://huggingface.co/spaces/aadi-joshi/omninpc/resolve/main/tests/test_capture.py
3.81 kB
| """Unit tests for capture modules. | |
| Tests: | |
| 1. FrameCapture NamedTuple has correct fields | |
| 2. AudioChunk NamedTuple has correct fields | |
| 3. frame_to_b64 returns valid base64 string | |
| 4. Queue drop-oldest: when queue full, new frame replaces oldest | |
| 5. AudioCapturer VAD: silent chunk → is_silent=True | |
| """ | |
| from __future__ import annotations | |
| import asyncio | |
| import base64 | |
| import time | |
| from io import BytesIO | |
| from unittest.mock import MagicMock, patch | |
| import numpy as np | |
| import pytest | |
| from PIL import Image | |
| from capture.frame_capture import FrameCapture, FrameCapturer | |
| from capture.audio_capture import AudioChunk | |
| class TestFrameCapture: | |
| def test_namedtuple_fields(self): | |
| """FrameCapture has correct fields.""" | |
| frame = FrameCapture( | |
| frame_bytes=b"fake_jpeg", | |
| timestamp=1234.5, | |
| width=1280, | |
| height=720, | |
| ) | |
| assert frame.frame_bytes == b"fake_jpeg" | |
| assert frame.timestamp == 1234.5 | |
| assert frame.width == 1280 | |
| assert frame.height == 720 | |
| def test_frame_to_b64(self): | |
| """frame_to_b64 returns a valid base64-encoded string.""" | |
| config = MagicMock() | |
| config.capture.frame_width = 640 | |
| config.capture.frame_height = 360 | |
| config.capture.frame_queue_maxsize = 2 | |
| config.capture.fps_target = 3 | |
| capturer = FrameCapturer(config) | |
| # Create a small JPEG in memory | |
| img = Image.new("RGB", (64, 36), color=(255, 0, 0)) | |
| buf = BytesIO() | |
| img.save(buf, format="JPEG", quality=85) | |
| jpeg_bytes = buf.getvalue() | |
| frame = FrameCapture(frame_bytes=jpeg_bytes, timestamp=time.perf_counter(), width=64, height=36) | |
| b64 = capturer.frame_to_b64(frame) | |
| assert isinstance(b64, str) | |
| decoded = base64.b64decode(b64) | |
| assert decoded == jpeg_bytes | |
| async def test_queue_drop_oldest(self): | |
| """When queue is full, new frame replaces oldest.""" | |
| config = MagicMock() | |
| config.capture.frame_width = 640 | |
| config.capture.frame_height = 360 | |
| config.capture.frame_queue_maxsize = 2 | |
| config.capture.fps_target = 3 | |
| capturer = FrameCapturer(config) | |
| q = capturer._queue | |
| # Fill queue to capacity | |
| frame_a = FrameCapture(frame_bytes=b"a", timestamp=1.0, width=640, height=360) | |
| frame_b = FrameCapture(frame_bytes=b"b", timestamp=2.0, width=640, height=360) | |
| await q.put(frame_a) | |
| await q.put(frame_b) | |
| # Now push a new frame (should drop oldest) | |
| frame_c = FrameCapture(frame_bytes=b"c", timestamp=3.0, width=640, height=360) | |
| capturer._push(frame_c) | |
| # Queue should contain b and c (a was dropped) | |
| items = [] | |
| while not q.empty(): | |
| items.append(await q.get()) | |
| timestamps = [f.timestamp for f in items] | |
| assert 1.0 not in timestamps # frame_a dropped | |
| assert 3.0 in timestamps # frame_c present | |
| class TestAudioChunk: | |
| def test_namedtuple_fields(self): | |
| """AudioChunk has correct fields.""" | |
| chunk = AudioChunk( | |
| audio_bytes=b"wav_data", | |
| timestamp=100.0, | |
| is_silent=False, | |
| rms_energy=0.05, | |
| ) | |
| assert chunk.audio_bytes == b"wav_data" | |
| assert chunk.timestamp == 100.0 | |
| assert chunk.is_silent is False | |
| assert chunk.rms_energy == pytest.approx(0.05) | |
| def test_silent_chunk_detection(self): | |
| """A chunk of zeros should be flagged as silent.""" | |
| import numpy as np | |
| from capture.audio_capture import _SILENCE_RMS | |
| data = np.zeros(8000, dtype=np.float32) | |
| rms = float(np.sqrt(np.mean(data ** 2) + 1e-10)) | |
| is_silent = rms < _SILENCE_RMS | |
| assert is_silent is True | |