"""Round 134: Connections › Connect to Mumble… — LinTunes as a bot in a Mumble room, following trav between rooms.""" import asyncio import sqlite3 import ssl import subprocess from unittest.mock import MagicMock import numpy as np import pytest from lintunes.mumble import certs, opus, proto, servers from lintunes.mumble.client import Connection, MumbleError, ServerState from lintunes.mumble.sink import ( FRAME_MS, MumbleSink, SAMPLES, apply_gain, bitrate_for) @pytest.fixture(autouse=True) def _app(qapp): """Every test here holds the QApplication. Mixing tests that make Qt objects with ones that don't freed its wrapper mid-file now and then (sip's release_QApplication, a segfault in a later test).""" return qapp def _delete(obj): """Deleted now, not left for the end of the session: a Qt object still alive when pytest drops the QApplication segfaulted in sip.""" from PyQt6.QtCore import QCoreApplication, QEvent obj.deleteLater() QCoreApplication.sendPostedEvents(None, QEvent.Type.DeferredDelete.value) @pytest.fixture def xdg(tmp_path, monkeypatch): monkeypatch.setenv("XDG_CONFIG_HOME", str(tmp_path / "config")) monkeypatch.setenv("XDG_DATA_HOME", str(tmp_path / "data")) return tmp_path # ---- wire format ---- def test_protobuf_round_trip(): data = proto.encode([(1, 7), (3, "Rüm"), (5, True), (6, None), (9, 300), (14, b"\x00\xff")]) fields = proto.decode(data) assert fields[1] == [7] and fields[5] == [1] and fields[9] == [300] assert proto.text(fields, 3) == "Rüm" assert fields[14] == [b"\x00\xff"] assert 6 not in fields @pytest.mark.parametrize("value", [0, 1, 0x7F, 0x80, 0x3FFF, 0x4000, 0x1FFFFF, 0x200000, 0xFFFFFFF, 0x10000000, 0xFFFFFFFF]) def test_mumble_varint_round_trip(value): data = proto.mumble_varint(value) assert proto.read_mumble_varint(data) == (value, len(data)) def test_voice_packet_layout(): packet = proto.voice_packet(4, b"abc", terminator=True) assert packet[0] == 4 << 5 # Opus, normal talking seq, i = proto.read_mumble_varint(packet, 1) length, i = proto.read_mumble_varint(packet, i) assert seq == 4 and length == 3 | proto.TERMINATOR assert packet[i:] == b"abc" with pytest.raises(ValueError): proto.voice_packet(0, bytes(proto.TERMINATOR)) def test_frame_reader_reassembles_split_frames(): stream = proto.frame(proto.PING, b"x" * 10) + proto.frame(9, b"") reader = proto.FrameReader() got = [] for i in range(len(stream)): got += reader.feed(stream[i:i + 1]) assert got == [(proto.PING, b"x" * 10), (9, b"")] def test_bandwidth_caps_the_bitrate(): assert bitrate_for(128, 0) == 128000 # no limit given assert bitrate_for(128, 558000) == 128000 capped = bitrate_for(128, 72000) assert capped < 72000 and capped > 50000 def test_gain_scales_samples(): pcm = np.array([1000, -1000, 32767], dtype="