Files
LGCompanion/tests/test_surround.py
xpamych f069010250 Автоприменение HRIR: генерация PipeWire-конфига и перезапуск без ручных шагов
- Новый модуль surround.py: генерация конфига в ~/.config/lgcompanion/,
  симлинк в pipewire.conf.d, перезапуск PipeWire, удаление устаревшего
  симлинка headset-companion
- CLI: команда 'hrir apply [--no-restart]'; автоприменение в hrir add/
  set-default/remove
- install.sh использует 'lgcompanion hrir apply --no-restart' вместо
  inline-генерации в .generated/
- Тесты tests/test_surround.py
2026-09-14 21:29:18 +03:00

122 lines
4.5 KiB
Python

"""Tests for surround config generation and activation."""
from __future__ import annotations
from pathlib import Path
from unittest.mock import patch
from lgcompanion.config import HrirConfig, HrirProfile
from lgcompanion.surround import (
apply_hrir_config,
ensure_pipewire_link,
generate_surround_config,
remove_pipewire_link,
)
def _make_template(tmp_path: Path) -> Path:
template = tmp_path / "template.conf"
template.write_text('filename = "{hrir_path}"\n')
return template
def test_generate_surround_config(tmp_path: Path) -> None:
template = _make_template(tmp_path)
out = tmp_path / "out" / "virtual-surround.conf"
result = generate_surround_config(Path("/tmp/hrir.wav"), out_path=out, template_path=template)
assert result == out
assert out.read_text() == 'filename = "/tmp/hrir.wav"\n'
def test_ensure_pipewire_link(tmp_path: Path) -> None:
target = tmp_path / "virtual-surround.conf"
target.write_text("config")
link = tmp_path / "conf.d" / "lgcompanion-virtual-surround.conf"
result = ensure_pipewire_link(target, link_path=link)
assert result == link
assert link.is_symlink()
assert link.resolve() == target
def test_ensure_pipewire_link_replaces_existing(tmp_path: Path) -> None:
target = tmp_path / "new.conf"
target.write_text("new")
old = tmp_path / "old.conf"
old.write_text("old")
link = tmp_path / "conf.d" / "lgcompanion-virtual-surround.conf"
ensure_pipewire_link(old, link_path=link)
ensure_pipewire_link(target, link_path=link)
assert link.resolve() == target
def test_ensure_pipewire_link_removes_legacy(tmp_path: Path) -> None:
target = tmp_path / "virtual-surround.conf"
target.write_text("config")
conf_dir = tmp_path / "conf.d"
conf_dir.mkdir()
legacy = conf_dir / "headset-companion-virtual-surround.conf"
legacy.symlink_to(tmp_path / "stale.conf")
ensure_pipewire_link(target, link_path=conf_dir / "lgcompanion-virtual-surround.conf")
assert not legacy.exists()
def test_remove_pipewire_link(tmp_path: Path) -> None:
target = tmp_path / "virtual-surround.conf"
target.write_text("config")
link = tmp_path / "conf.d" / "lgcompanion-virtual-surround.conf"
ensure_pipewire_link(target, link_path=link)
remove_pipewire_link(link_path=link)
assert not link.exists()
# Повторное удаление не должно падать
remove_pipewire_link(link_path=link)
def test_apply_hrir_config_success(tmp_path: Path) -> None:
hrir_file = tmp_path / "hrir.wav"
hrir_file.write_text("wav")
generated = tmp_path / "gen" / "virtual-surround.conf"
link = tmp_path / "conf.d" / "lgcompanion-virtual-surround.conf"
template = _make_template(tmp_path)
hrir = HrirConfig(file=str(hrir_file), profiles=[HrirProfile(name="test", path=hrir_file)])
with patch("lgcompanion.surround.GENERATED_CONFIG_PATH", generated), patch(
"lgcompanion.surround.PIPEWIRE_CONF_LINK", link
), patch("lgcompanion.surround.DEFAULT_PIPEWIRE_CONFIG_TEMPLATE", template), patch(
"lgcompanion.surround.restart_pipewire"
) as mock_restart:
assert apply_hrir_config(hrir) is True
mock_restart.assert_called_once()
assert generated.read_text() == f'filename = "{hrir_file}"\n'
assert link.resolve() == generated
def test_apply_hrir_config_no_restart(tmp_path: Path) -> None:
hrir_file = tmp_path / "hrir.wav"
hrir_file.write_text("wav")
hrir = HrirConfig(file=str(hrir_file))
with patch("lgcompanion.surround.GENERATED_CONFIG_PATH", tmp_path / "gen.conf"), patch(
"lgcompanion.surround.PIPEWIRE_CONF_LINK", tmp_path / "conf.d" / "link.conf"
), patch("lgcompanion.surround.DEFAULT_PIPEWIRE_CONFIG_TEMPLATE", _make_template(tmp_path)), patch(
"lgcompanion.surround.restart_pipewire"
) as mock_restart:
assert apply_hrir_config(hrir, restart=False) is True
mock_restart.assert_not_called()
def test_apply_hrir_config_missing_hrir_removes_link(tmp_path: Path) -> None:
target = tmp_path / "virtual-surround.conf"
target.write_text("config")
link = tmp_path / "conf.d" / "lgcompanion-virtual-surround.conf"
ensure_pipewire_link(target, link_path=link)
hrir = HrirConfig(file=str(tmp_path / "deleted.wav"))
with patch("lgcompanion.surround.PIPEWIRE_CONF_LINK", link), patch(
"lgcompanion.surround.restart_pipewire"
) as mock_restart:
assert apply_hrir_config(hrir) is False
mock_restart.assert_called_once()
assert not link.exists()