From 60f05cebd8e7968621a0ce015cf72987ec7577c7 Mon Sep 17 00:00:00 2001 From: "Somhairle H. Marisol" Date: Tue, 29 Sep 2026 10:50:12 +0800 Subject: feat(recorder): add offline voice clip pipeline MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit [变更性质] - 本提交新增本机离线的语音片段录音能力,不涉及网络服务或语音识别。 [新增功能] - 通过 Pulse 默认输入持续采集音频,以本地 Silero VAD 触发片段。 - 以私有目录和权限写入 24 kbps Ogg/Opus 录音,并提供 systemd 用户服务安装器。 [实现方案] - 使用有限前置缓冲、静音封段和最长段滚动状态机,避免静音落盘及内存无限增长。 - 增加真实 Opus 编解码、服务 dry-run 和纯逻辑状态转换测试;记录模型归属和部署前置条件。 [影响范围] - 新增 Python CLI、运行时模块、中文运维文档和自动测试。 - 未安装、启用或启动任何用户 systemd 服务;不删除或修改录音数据。 --- tests/test_service.py | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 tests/test_service.py (limited to 'tests/test_service.py') diff --git a/tests/test_service.py b/tests/test_service.py new file mode 100644 index 0000000..2bda2ab --- /dev/null +++ b/tests/test_service.py @@ -0,0 +1,60 @@ +from pathlib import Path + +from mic_clipper.service import install, render_unit, uninstall + + +def test_rendered_service_waits_for_audio_services_and_restarts_only_on_failure(): + unit = render_unit(Path("/runtime/python"), Path("/project")) + + assert "After=pipewire.service wireplumber.service pipewire-pulse.service" in unit + assert "Restart=on-failure" in unit + assert "RestartMaxDelaySec=60s" in unit + assert "ExecStart=/runtime/python -m mic_clipper run" in unit + assert "UMask=0077" in unit + + +def test_install_and_uninstall_only_manage_the_tool_unit(tmp_path): + unit_path = tmp_path / "systemd" / "mic-clipper.service" + clips = tmp_path / "Mic Clips" / "2026-09-29" / "keep.opus" + clips.parent.mkdir(parents=True) + clips.write_bytes(b"recording") + commands: list[list[str]] = [] + + install( + unit_path=unit_path, + python=Path("/runtime/python"), + project_root=Path("/project"), + run_command=commands.append, + ) + install( + unit_path=unit_path, + python=Path("/runtime/python"), + project_root=Path("/project"), + run_command=commands.append, + ) + uninstall(unit_path=unit_path, run_command=commands.append) + + assert not unit_path.exists() + assert clips.read_bytes() == b"recording" + assert commands == [ + ["systemctl", "--user", "daemon-reload"], + ["systemctl", "--user", "enable", "--now", "mic-clipper.service"], + ["systemctl", "--user", "daemon-reload"], + ["systemctl", "--user", "enable", "--now", "mic-clipper.service"], + ["systemctl", "--user", "disable", "--now", "mic-clipper.service"], + ["systemctl", "--user", "daemon-reload"], + ] + + +def test_dry_run_does_not_write_a_unit(tmp_path): + unit_path = tmp_path / "mic-clipper.service" + + commands = install( + unit_path=unit_path, + python=Path("/runtime/python"), + project_root=Path("/project"), + dry_run=True, + ) + + assert not unit_path.exists() + assert commands[-1] == ["systemctl", "--user", "enable", "--now", "mic-clipper.service"] -- cgit v1.2.3