From 68864720df2fb5e6c28f4ef85b6e9472063a922d Mon Sep 17 00:00:00 2001 From: Nanahuse <109294061+Nanahuse@users.noreply.github.com> Date: Sun, 9 Aug 2026 16:36:01 +0900 Subject: [PATCH 1/3] Improve Voicepeak test coverage --- test/test_voicepeak.py | 263 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 263 insertions(+) diff --git a/test/test_voicepeak.py b/test/test_voicepeak.py index c5bc65c..7346070 100644 --- a/test/test_voicepeak.py +++ b/test/test_voicepeak.py @@ -158,3 +158,266 @@ async def test_say_textfile_accepts_paths(monkeypatch): stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE, ) + + +def test_init_raises_when_exe_path_does_not_exist(monkeypatch): + monkeypatch.setenv("ProgramFiles", os.path.dirname(__file__)) + import voicepeak_wrapper + + missing_path = os.path.join(os.path.dirname(__file__), "missing.exe") + + with pytest.raises(FileNotFoundError, match="VOICEPEAKの実行ファイルが見つかりません"): + voicepeak_wrapper.Voicepeak(exe_path=missing_path) + + +@pytest.mark.asyncio +async def test_say_textfile_accepts_str_path(monkeypatch): + client = create_client(monkeypatch) + process = ProcessResult(b"completed\n") + create_subprocess = mock_process(monkeypatch, process) + + result = await client.say_textfile("input.txt", output_path="output.wav") + + assert result == "completed\n" + create_subprocess.assert_awaited_once_with( + __file__, + "-t", + "input.txt", + "-o", + "output.wav", + stdout=asyncio.subprocess.PIPE, + stderr=asyncio.subprocess.PIPE, + ) + + +@pytest.mark.asyncio +async def test_say_textfile_uses_default_output_path(monkeypatch): + client = create_client(monkeypatch) + process = ProcessResult(b"completed\n") + create_subprocess = mock_process(monkeypatch, process) + + result = await client.say_textfile("input.txt") + + assert result == "completed\n" + create_subprocess.assert_awaited_once_with( + __file__, + "-t", + "input.txt", + "-o", + "./output.wav", + stdout=asyncio.subprocess.PIPE, + stderr=asyncio.subprocess.PIPE, + ) + + +@pytest.mark.asyncio +async def test_say_text_accepts_narrator_name(monkeypatch): + client = create_client(monkeypatch) + process = ProcessResult(b"completed\n") + create_subprocess = mock_process(monkeypatch, process) + + result = await client.say_text("本日は晴天なり", narrator="naruko") + + assert result == "completed\n" + create_subprocess.assert_awaited_once_with( + __file__, + "-s", + "本日は晴天なり", + "-n", + "naruko", + stdout=asyncio.subprocess.PIPE, + stderr=asyncio.subprocess.PIPE, + ) + + +@pytest.mark.asyncio +async def test_say_text_accepts_narrator_object(monkeypatch): + client = create_client(monkeypatch) + import voicepeak_wrapper + + process = ProcessResult(b"completed\n") + create_subprocess = mock_process(monkeypatch, process) + + result = await client.say_text("本日は晴天なり", narrator=voicepeak_wrapper.Narrator("naruko", ("happy",))) + + assert result == "completed\n" + create_subprocess.assert_awaited_once_with( + __file__, + "-s", + "本日は晴天なり", + "-n", + "naruko", + stdout=asyncio.subprocess.PIPE, + stderr=asyncio.subprocess.PIPE, + ) + + +@pytest.mark.asyncio +async def test_say_text_accepts_emotions(monkeypatch): + client = create_client(monkeypatch) + process = ProcessResult(b"completed\n") + create_subprocess = mock_process(monkeypatch, process) + + result = await client.say_text("本日は晴天なり", emotions={"happy": 100, "angry": 50}) + + assert result == "completed\n" + create_subprocess.assert_awaited_once_with( + __file__, + "-s", + "本日は晴天なり", + "-e", + "happy=100,angry=50", + stdout=asyncio.subprocess.PIPE, + stderr=asyncio.subprocess.PIPE, + ) + + +@pytest.mark.parametrize("speed", [50, 200]) +@pytest.mark.asyncio +async def test_say_text_accepts_valid_speed(monkeypatch, speed): + client = create_client(monkeypatch) + process = ProcessResult(b"completed\n") + create_subprocess = mock_process(monkeypatch, process) + + result = await client.say_text("本日は晴天なり", speed=speed) + + assert result == "completed\n" + create_subprocess.assert_awaited_once_with( + __file__, + "-s", + "本日は晴天なり", + "--speed", + str(speed), + stdout=asyncio.subprocess.PIPE, + stderr=asyncio.subprocess.PIPE, + ) + + +@pytest.mark.parametrize("speed", [49, 201, 100.5, "100"]) +@pytest.mark.asyncio +async def test_say_text_rejects_invalid_speed(monkeypatch, speed): + client = create_client(monkeypatch) + mock_process(monkeypatch, ProcessResult()) + + with pytest.raises(ValueError, match="speedは50 - 200の範囲内の整数"): + await client.say_text("本日は晴天なり", speed=speed) + + +@pytest.mark.parametrize("pitch", [-300, 300]) +@pytest.mark.asyncio +async def test_say_text_accepts_valid_pitch(monkeypatch, pitch): + client = create_client(monkeypatch) + process = ProcessResult(b"completed\n") + create_subprocess = mock_process(monkeypatch, process) + + result = await client.say_text("本日は晴天なり", pitch=pitch) + + assert result == "completed\n" + create_subprocess.assert_awaited_once_with( + __file__, + "-s", + "本日は晴天なり", + "--pitch", + str(pitch), + stdout=asyncio.subprocess.PIPE, + stderr=asyncio.subprocess.PIPE, + ) + + +@pytest.mark.parametrize("pitch", [-301, 301, 0.5, "0"]) +@pytest.mark.asyncio +async def test_say_text_rejects_invalid_pitch(monkeypatch, pitch): + client = create_client(monkeypatch) + mock_process(monkeypatch, ProcessResult()) + + with pytest.raises(ValueError, match="pitchは-300 - 300の範囲内の整数"): + await client.say_text("本日は晴天なり", pitch=pitch) + + +@pytest.mark.asyncio +async def test_say_text_with_all_options_builds_full_command(monkeypatch): + client = create_client(monkeypatch) + import voicepeak_wrapper + + process = ProcessResult(b"completed\n") + create_subprocess = mock_process(monkeypatch, process) + + result = await client.say_text( + "こんにちは", + output_path="out.wav", + narrator=voicepeak_wrapper.Narrator("Japanese Female 1", ("happy", "sad")), + emotions={"happy": 100, "sad": 50}, + speed=120, + pitch=-50, + ) + + assert result == "completed\n" + create_subprocess.assert_awaited_once_with( + __file__, + "-s", + "こんにちは", + "-o", + "out.wav", + "-n", + "Japanese Female 1", + "-e", + "happy=100,sad=50", + "--speed", + "120", + "--pitch", + "-50", + stdout=asyncio.subprocess.PIPE, + stderr=asyncio.subprocess.PIPE, + ) + + +def test_make_say_command_rejects_text_and_text_file_together(monkeypatch): + client = create_client(monkeypatch) + + with pytest.raises(ValueError, match="textかtext_fileの一方のみ指定してください"): + client._Voicepeak__make_say_command(text="text", text_file="input.txt") + + +def test_make_say_command_requires_text_or_text_file(monkeypatch): + client = create_client(monkeypatch) + + with pytest.raises(ValueError, match="textまたはtext_fileが設定されている必要があります"): + client._Voicepeak__make_say_command() + + +def test_make_say_command_rejects_invalid_text_type(monkeypatch): + client = create_client(monkeypatch) + + with pytest.raises(ValueError, match="textまたはtext_fileが不正な値です"): + client._Voicepeak__make_say_command(text=123) # type: ignore[arg-type] + + +def test_make_say_command_rejects_invalid_text_file_type(monkeypatch): + client = create_client(monkeypatch) + + with pytest.raises(ValueError, match="textまたはtext_fileが不正な値です"): + client._Voicepeak__make_say_command(text="text", text_file=123) # type: ignore[arg-type] + + +@pytest.mark.asyncio +async def test_get_emotion_list_returns_empty_tuple_for_empty_output(monkeypatch): + client = create_client(monkeypatch) + mock_process(monkeypatch, ProcessResult(b"")) + + assert await client.get_emotion_list("Japanese Female 1") == () + + +@pytest.mark.asyncio +async def test_get_narrator_name_list_handles_output_without_trailing_newline(monkeypatch): + client = create_client(monkeypatch) + mock_process(monkeypatch, ProcessResult(b"Japanese Female 1")) + + assert await client.get_narrator_name_list() == ("Japanese Female 1",) + + +@pytest.mark.asyncio +async def test_get_narrator_list_returns_empty_for_empty_output(monkeypatch): + client = create_client(monkeypatch) + mock_process(monkeypatch, ProcessResult(b"")) + + assert await client.get_narrator_list() == () From a74a5ea8520eda1913bb47f6767b53717c426e03 Mon Sep 17 00:00:00 2001 From: Nanahuse <109294061+Nanahuse@users.noreply.github.com> Date: Sun, 9 Aug 2026 16:45:38 +0900 Subject: [PATCH 2/3] Automate VOICEPEAK file generation tests --- test/test_manual_voicepeak_file_generation.py | 117 -------- test/test_voicepeak_file_generation.py | 270 ++++++++++++++++++ 2 files changed, 270 insertions(+), 117 deletions(-) delete mode 100644 test/test_manual_voicepeak_file_generation.py create mode 100644 test/test_voicepeak_file_generation.py diff --git a/test/test_manual_voicepeak_file_generation.py b/test/test_manual_voicepeak_file_generation.py deleted file mode 100644 index ff03f93..0000000 --- a/test/test_manual_voicepeak_file_generation.py +++ /dev/null @@ -1,117 +0,0 @@ -# Copyright (c) 2023 Nanahuse -# This software is released under the MIT License -# https://opensource.org/license/mit/ - -"""VOICEPEAK integration tests that create output files. - -These tests require a local VOICEPEAK installation and are marked xfail with -``run=False``, so they are reported as NOTRUN without launching VOICEPEAK. Run -them with pytest's built-in ``--runxfail`` option when file-generation coverage -is needed: - - uv run --group dev --with-editable . pytest --runxfail - -Or run just this module: - - uv run --group dev --with-editable . pytest \\ - test/test_manual_voicepeak_file_generation.py --runxfail -""" - -import asyncio -import os -from pathlib import Path - -import pytest - -pytestmark = pytest.mark.xfail(run=False, reason="requires a local VOICEPEAK installation; pass --runxfail to run") - -TEST_DIRECTORY = os.path.dirname(os.path.abspath(__file__)) -OUTPUT_PATH = os.path.join(TEST_DIRECTORY, "output") - - -@pytest.mark.asyncio -async def test_get_narrator_list(): - import voicepeak_wrapper - - client = voicepeak_wrapper.Voicepeak() - narrators = await client.get_narrator_list() - lines = [] - for narrator in narrators: - lines.append(f"{narrator.name} : {', '.join(narrator.emotions)}\n") - - await client.say_text( - "本日は晴天なり", - output_path=os.path.join(OUTPUT_PATH, f"narrator_{narrator.name}.wav"), - narrator=narrator, - emotions={narrator.emotions[1]: 100}, - ) - - await asyncio.to_thread(Path(OUTPUT_PATH, "narrators.txt").write_text, "".join(lines), encoding="UTF-8") - - -@pytest.mark.asyncio -async def test_get_narrator_name_list(): - import voicepeak_wrapper - - client = voicepeak_wrapper.Voicepeak() - narrator_names = await client.get_narrator_name_list() - await asyncio.to_thread( - Path(OUTPUT_PATH, "narrator_names.txt").write_text, - "".join(f"{name}\n" for name in narrator_names), - encoding="UTF-8", - ) - - -@pytest.mark.asyncio -async def test_get_emotion_list(): - import voicepeak_wrapper - - client = voicepeak_wrapper.Voicepeak() - - narrator_names = await client.get_narrator_name_list() - lines = [] - for name in narrator_names: - emotion_list = await client.get_emotion_list(name) - lines.append(f"{name} : {', '.join(emotion_list)}\n") - - await asyncio.to_thread(Path(OUTPUT_PATH, "emotions.txt").write_text, "".join(lines), encoding="UTF-8") - - with pytest.raises(RuntimeError): - await client.get_emotion_list("hogehoge") - - -@pytest.mark.asyncio -async def test_say_text(): - import voicepeak_wrapper - - client = voicepeak_wrapper.Voicepeak() - await client.say_text("本日は晴天なり", output_path=os.path.join(OUTPUT_PATH, "test_say_text.wav")) - await client.say_text( - "本日は晴天なり", output_path=os.path.join(OUTPUT_PATH, "test_say_text_upper.wav"), speed=200, pitch=300 - ) - await client.say_text( - "本日は晴天なり", output_path=os.path.join(OUTPUT_PATH, "test_say_text_lower.wav"), speed=50, pitch=-300 - ) - - with pytest.raises(ValueError): - await client.say_text("エラー", output_path=os.path.join(OUTPUT_PATH, "error.wav"), speed=201) - with pytest.raises(ValueError): - await client.say_text("エラー", output_path=os.path.join(OUTPUT_PATH, "error.wav"), speed=49) - with pytest.raises(ValueError): - await client.say_text("エラー", output_path=os.path.join(OUTPUT_PATH, "error.wav"), pitch=-301) - with pytest.raises(ValueError): - await client.say_text("エラー", output_path=os.path.join(OUTPUT_PATH, "error.wav"), pitch=301) - - with pytest.raises(RuntimeError): - await client.say_text("1" * 141, output_path=os.path.join(OUTPUT_PATH, "error.wav")) - - -@pytest.mark.asyncio -async def test_say_testfile(): - import voicepeak_wrapper - - client = voicepeak_wrapper.Voicepeak() - - text_file = os.path.join(TEST_DIRECTORY, "sample.txt") - - await client.say_textfile(text_file, output_path=os.path.join(OUTPUT_PATH, "say_text.wav")) diff --git a/test/test_voicepeak_file_generation.py b/test/test_voicepeak_file_generation.py new file mode 100644 index 0000000..9a92ad5 --- /dev/null +++ b/test/test_voicepeak_file_generation.py @@ -0,0 +1,270 @@ +# Copyright (c) 2023 Nanahuse +# This software is released under the MIT License +# https://opensource.org/license/mit/ + +"""Mock-based tests for the wrapper's file-generation behavior. + +These tests replace the former manual VOICEPEAK integration tests that were +xfailed (``run=False``) because they launched a local voicepeak.exe. They run +without a local VOICEPEAK installation by mocking the subprocess, and verify +the intended output paths, generated files, existing-file overwrite, long-text, +and ``text_file`` behavior using temporary directories. +""" + +import asyncio +import os +from unittest.mock import AsyncMock + +import pytest + +pytestmark = pytest.mark.asyncio + + +class ProcessResult: + def __init__(self, stdout: bytes = b"", stderr: bytes = b""): + self.communicate = AsyncMock(return_value=(stdout, stderr)) + + +def create_client(monkeypatch): + monkeypatch.setenv("ProgramFiles", os.path.dirname(__file__)) + import voicepeak_wrapper + + return voicepeak_wrapper.Voicepeak(exe_path=__file__) + + +def mock_process(monkeypatch, *results): + create_subprocess = AsyncMock(side_effect=results) + monkeypatch.setattr(asyncio, "create_subprocess_exec", create_subprocess) + return create_subprocess + + +async def test_get_narrator_list_and_generate_file(monkeypatch, tmp_path): + client = create_client(monkeypatch) + import voicepeak_wrapper + + processes = ( + ProcessResult(b"Japanese Female 1\nJapanese Male 1\n"), + ProcessResult(b"happy\nsad\n"), + ProcessResult(b"happy\nangry\n"), + ProcessResult(b"completed\n"), + ProcessResult(b"completed\n"), + ) + create_subprocess = mock_process(monkeypatch, *processes) + + narrators = await client.get_narrator_list() + assert narrators == ( + voicepeak_wrapper.Narrator("Japanese Female 1", ("happy", "sad")), + voicepeak_wrapper.Narrator("Japanese Male 1", ("happy", "angry")), + ) + + lines = [] + for narrator in narrators: + lines.append(f"{narrator.name} : {', '.join(narrator.emotions)}\n") + await client.say_text( + "本日は晴天なり", + output_path=tmp_path / f"narrator_{narrator.name}.wav", + narrator=narrator, + emotions={narrator.emotions[1]: 100}, + ) + + output_file = tmp_path / "narrators.txt" + await asyncio.to_thread(output_file.write_text, "".join(lines), encoding="UTF-8") + assert output_file.read_text(encoding="UTF-8") == "Japanese Female 1 : happy, sad\nJapanese Male 1 : happy, angry\n" + + create_subprocess.assert_any_await( + __file__, + "-s", + "本日は晴天なり", + "-o", + os.fspath(tmp_path / "narrator_Japanese Female 1.wav"), + "-n", + "Japanese Female 1", + "-e", + "sad=100", + stdout=asyncio.subprocess.PIPE, + stderr=asyncio.subprocess.PIPE, + ) + create_subprocess.assert_any_await( + __file__, + "-s", + "本日は晴天なり", + "-o", + os.fspath(tmp_path / "narrator_Japanese Male 1.wav"), + "-n", + "Japanese Male 1", + "-e", + "angry=100", + stdout=asyncio.subprocess.PIPE, + stderr=asyncio.subprocess.PIPE, + ) + + +async def test_get_narrator_name_list_and_generate_file(monkeypatch, tmp_path): + client = create_client(monkeypatch) + mock_process(monkeypatch, ProcessResult(b"Japanese Female 1\nJapanese Male 1\n")) + + narrator_names = await client.get_narrator_name_list() + assert narrator_names == ("Japanese Female 1", "Japanese Male 1") + + output_file = tmp_path / "narrator_names.txt" + await asyncio.to_thread( + output_file.write_text, + "".join(f"{name}\n" for name in narrator_names), + encoding="UTF-8", + ) + assert output_file.read_text(encoding="UTF-8") == "Japanese Female 1\nJapanese Male 1\n" + + +async def test_get_emotion_list_and_generate_file(monkeypatch, tmp_path): + client = create_client(monkeypatch) + processes = ( + ProcessResult(b"Japanese Female 1\nJapanese Male 1\n"), + ProcessResult(b"happy\nsad\n"), + ProcessResult(b"happy\nangry\n"), + ) + mock_process(monkeypatch, *processes) + + narrator_names = await client.get_narrator_name_list() + lines = [] + for name in narrator_names: + emotion_list = await client.get_emotion_list(name) + lines.append(f"{name} : {', '.join(emotion_list)}\n") + + output_file = tmp_path / "emotions.txt" + await asyncio.to_thread(output_file.write_text, "".join(lines), encoding="UTF-8") + assert output_file.read_text(encoding="UTF-8") == "Japanese Female 1 : happy, sad\nJapanese Male 1 : happy, angry\n" + + mock_process(monkeypatch, ProcessResult(stderr=b"narrator not found: hogehoge\n")) + with pytest.raises(RuntimeError, match="narrator not found"): + await client.get_emotion_list("hogehoge") + + +async def test_say_text_and_validate_speed_pitch(monkeypatch, tmp_path): + client = create_client(monkeypatch) + processes = ( + ProcessResult(b"completed\n"), + ProcessResult(b"completed\n"), + ProcessResult(b"completed\n"), + ) + create_subprocess = mock_process(monkeypatch, *processes) + + await client.say_text("本日は晴天なり", output_path=tmp_path / "test_say_text.wav") + await client.say_text("本日は晴天なり", output_path=tmp_path / "test_say_text_upper.wav", speed=200, pitch=300) + await client.say_text("本日は晴天なり", output_path=tmp_path / "test_say_text_lower.wav", speed=50, pitch=-300) + + with pytest.raises(ValueError, match="speedは50 - 200の範囲内の整数"): + await client.say_text("エラー", output_path=tmp_path / "error.wav", speed=201) + with pytest.raises(ValueError, match="speedは50 - 200の範囲内の整数"): + await client.say_text("エラー", output_path=tmp_path / "error.wav", speed=49) + with pytest.raises(ValueError, match="pitchは-300 - 300の範囲内の整数"): + await client.say_text("エラー", output_path=tmp_path / "error.wav", pitch=-301) + with pytest.raises(ValueError, match="pitchは-300 - 300の範囲内の整数"): + await client.say_text("エラー", output_path=tmp_path / "error.wav", pitch=301) + + create_subprocess.assert_any_await( + __file__, + "-s", + "本日は晴天なり", + "-o", + os.fspath(tmp_path / "test_say_text.wav"), + stdout=asyncio.subprocess.PIPE, + stderr=asyncio.subprocess.PIPE, + ) + create_subprocess.assert_any_await( + __file__, + "-s", + "本日は晴天なり", + "-o", + os.fspath(tmp_path / "test_say_text_upper.wav"), + "--speed", + "200", + "--pitch", + "300", + stdout=asyncio.subprocess.PIPE, + stderr=asyncio.subprocess.PIPE, + ) + create_subprocess.assert_any_await( + __file__, + "-s", + "本日は晴天なり", + "-o", + os.fspath(tmp_path / "test_say_text_lower.wav"), + "--speed", + "50", + "--pitch", + "-300", + stdout=asyncio.subprocess.PIPE, + stderr=asyncio.subprocess.PIPE, + ) + + +async def test_say_text_long_text_raises_runtime_error(monkeypatch, tmp_path): + client = create_client(monkeypatch) + + def long_text_process(*call_args, **kwargs): + args = call_args[1:] + text = args[args.index("-s") + 1] + if len(text) >= 141: + return ProcessResult(stderr=b"text is too long\n") + return ProcessResult(b"completed\n") + + create_subprocess = AsyncMock(side_effect=long_text_process) + monkeypatch.setattr(asyncio, "create_subprocess_exec", create_subprocess) + + await client.say_text("本日は晴天なり", output_path=tmp_path / "ok.wav") + with pytest.raises(RuntimeError, match="text is too long"): + await client.say_text("1" * 141, output_path=tmp_path / "too_long.wav") + assert create_subprocess.await_count == 2 + + +async def test_say_textfile_generates_from_text_file(monkeypatch, tmp_path): + client = create_client(monkeypatch) + text_file = tmp_path / "text.txt" + await asyncio.to_thread(text_file.write_text, "吾輩は猫である。名前はまだ無い。", encoding="UTF-8") + create_subprocess = mock_process(monkeypatch, ProcessResult(b"completed\n")) + + result = await client.say_textfile(text_file, output_path=tmp_path / "say_text.wav") + + assert result == "completed\n" + create_subprocess.assert_awaited_once_with( + __file__, + "-t", + os.fspath(text_file), + "-o", + os.fspath(tmp_path / "say_text.wav"), + stdout=asyncio.subprocess.PIPE, + stderr=asyncio.subprocess.PIPE, + ) + + +async def test_say_text_overwrites_existing_output_file(monkeypatch, tmp_path): + client = create_client(monkeypatch) + output_path = tmp_path / "existing.wav" + await asyncio.to_thread(output_path.write_bytes, b"previous generation") + create_subprocess = mock_process(monkeypatch, ProcessResult(b"completed\n")) + + result = await client.say_text("本日は晴天なり", output_path=output_path) + + assert result == "completed\n" + create_subprocess.assert_awaited_once_with( + __file__, + "-s", + "本日は晴天なり", + "-o", + os.fspath(output_path), + stdout=asyncio.subprocess.PIPE, + stderr=asyncio.subprocess.PIPE, + ) + + +async def test_generated_narrator_file_overwrites_existing_content(monkeypatch, tmp_path): + client = create_client(monkeypatch) + output_file = tmp_path / "narrators.txt" + await asyncio.to_thread(output_file.write_text, "stale line from a previous run\n", encoding="UTF-8") + mock_process(monkeypatch, ProcessResult(b"Japanese Female 1\n")) + + narrator_names = await client.get_narrator_name_list() + lines = "".join(f"{name}\n" for name in narrator_names) + await asyncio.to_thread(output_file.write_text, lines, encoding="UTF-8") + + assert output_file.read_text(encoding="UTF-8") == "Japanese Female 1\n" From 5fb97824cd18ed353a2dee778fc6e3d7fa5d5b37 Mon Sep 17 00:00:00 2001 From: Nanahuse <109294061+Nanahuse@users.noreply.github.com> Date: Sun, 9 Aug 2026 17:05:11 +0900 Subject: [PATCH 3/3] Restore real VOICEPEAK integration tests --- test/test_manual_voicepeak_file_generation.py | 171 +++++++++++ test/test_voicepeak_file_generation.py | 270 ------------------ 2 files changed, 171 insertions(+), 270 deletions(-) create mode 100644 test/test_manual_voicepeak_file_generation.py delete mode 100644 test/test_voicepeak_file_generation.py diff --git a/test/test_manual_voicepeak_file_generation.py b/test/test_manual_voicepeak_file_generation.py new file mode 100644 index 0000000..90b5f16 --- /dev/null +++ b/test/test_manual_voicepeak_file_generation.py @@ -0,0 +1,171 @@ +# Copyright (c) 2023 Nanahuse +# This software is released under the MIT License +# https://opensource.org/license/mit/ + +"""VOICEPEAK integration tests that create output files. + +These tests require a local VOICEPEAK installation and are marked xfail with +``run=False``, so they are reported as NOTRUN without launching VOICEPEAK. Run +them with pytest's built-in ``--runxfail`` option when file-generation coverage +is needed: + + uv run --group dev --with-editable . pytest --runxfail + +Or run just this module: + + uv run --group dev --with-editable . pytest \\ + test/test_manual_voicepeak_file_generation.py --runxfail +""" + +import asyncio +import os +from pathlib import Path + +import pytest + +pytestmark = pytest.mark.xfail(run=False, reason="requires a local VOICEPEAK installation; pass --runxfail to run") + +TEST_DIRECTORY = os.path.dirname(os.path.abspath(__file__)) +OUTPUT_PATH = os.path.join(TEST_DIRECTORY, "output") + + +@pytest.mark.asyncio +async def test_get_narrator_list(): + import voicepeak_wrapper + + client = voicepeak_wrapper.Voicepeak() + narrators = await client.get_narrator_list() + lines = [] + for narrator in narrators: + lines.append(f"{narrator.name} : {', '.join(narrator.emotions)}\n") + + await client.say_text( + "本日は晴天なり", + output_path=os.path.join(OUTPUT_PATH, f"narrator_{narrator.name}.wav"), + narrator=narrator, + emotions={narrator.emotions[1]: 100}, + ) + + await asyncio.to_thread(Path(OUTPUT_PATH, "narrators.txt").write_text, "".join(lines), encoding="UTF-8") + + +@pytest.mark.asyncio +async def test_get_narrator_name_list(): + import voicepeak_wrapper + + client = voicepeak_wrapper.Voicepeak() + narrator_names = await client.get_narrator_name_list() + await asyncio.to_thread( + Path(OUTPUT_PATH, "narrator_names.txt").write_text, + "".join(f"{name}\n" for name in narrator_names), + encoding="UTF-8", + ) + + +@pytest.mark.asyncio +async def test_get_emotion_list(): + import voicepeak_wrapper + + client = voicepeak_wrapper.Voicepeak() + + narrator_names = await client.get_narrator_name_list() + lines = [] + for name in narrator_names: + emotion_list = await client.get_emotion_list(name) + lines.append(f"{name} : {', '.join(emotion_list)}\n") + + await asyncio.to_thread(Path(OUTPUT_PATH, "emotions.txt").write_text, "".join(lines), encoding="UTF-8") + + with pytest.raises(RuntimeError): + await client.get_emotion_list("hogehoge") + + +@pytest.mark.asyncio +async def test_say_text(): + import voicepeak_wrapper + + client = voicepeak_wrapper.Voicepeak() + await client.say_text("本日は晴天なり", output_path=os.path.join(OUTPUT_PATH, "test_say_text.wav")) + await client.say_text( + "本日は晴天なり", output_path=os.path.join(OUTPUT_PATH, "test_say_text_upper.wav"), speed=200, pitch=300 + ) + await client.say_text( + "本日は晴天なり", output_path=os.path.join(OUTPUT_PATH, "test_say_text_lower.wav"), speed=50, pitch=-300 + ) + + with pytest.raises(ValueError): + await client.say_text("エラー", output_path=os.path.join(OUTPUT_PATH, "error.wav"), speed=201) + with pytest.raises(ValueError): + await client.say_text("エラー", output_path=os.path.join(OUTPUT_PATH, "error.wav"), speed=49) + with pytest.raises(ValueError): + await client.say_text("エラー", output_path=os.path.join(OUTPUT_PATH, "error.wav"), pitch=-301) + with pytest.raises(ValueError): + await client.say_text("エラー", output_path=os.path.join(OUTPUT_PATH, "error.wav"), pitch=301) + + with pytest.raises(RuntimeError): + await client.say_text("1" * 141, output_path=os.path.join(OUTPUT_PATH, "error.wav")) + + +@pytest.mark.asyncio +async def test_say_testfile(): + import voicepeak_wrapper + + client = voicepeak_wrapper.Voicepeak() + + text_file = os.path.join(TEST_DIRECTORY, "sample.txt") + + await client.say_textfile(text_file, output_path=os.path.join(OUTPUT_PATH, "say_text.wav")) + + +@pytest.mark.asyncio +async def test_say_text_overwrites_existing_output_wav(): + import voicepeak_wrapper + + client = voicepeak_wrapper.Voicepeak() + output_path = Path(OUTPUT_PATH) / "test_overwrite.wav" + await asyncio.to_thread(output_path.write_bytes, b"previous generation") + + await client.say_text("本日は晴天なり", output_path=output_path) + + content = await asyncio.to_thread(output_path.read_bytes) + assert len(content) > 0 + assert content != b"previous generation" + assert content[:4] == b"RIFF" + + +@pytest.mark.asyncio +async def test_say_text_with_pathlib_output_narrator_and_emotions(): + import voicepeak_wrapper + + client = voicepeak_wrapper.Voicepeak() + narrators = await client.get_narrator_list() + assert len(narrators) > 0 + narrator = narrators[0] + + output_path = Path(OUTPUT_PATH) / "test_narrator_emotions.wav" + emotions = {narrator.emotions[0]: 100} if narrator.emotions else None + await client.say_text( + "本日は晴天なり", + output_path=output_path, + narrator=narrator.name, + emotions=emotions, + ) + + content = await asyncio.to_thread(output_path.read_bytes) + assert len(content) > 0 + assert content[:4] == b"RIFF" + + +@pytest.mark.asyncio +async def test_say_textfile_with_pathlib_input_and_output(): + import voicepeak_wrapper + + client = voicepeak_wrapper.Voicepeak() + text_file = Path(TEST_DIRECTORY) / "sample.txt" + output_path = Path(OUTPUT_PATH) / "test_textfile_path.wav" + + await client.say_textfile(text_file, output_path=output_path, speed=120, pitch=-50) + + content = await asyncio.to_thread(output_path.read_bytes) + assert len(content) > 0 + assert content[:4] == b"RIFF" diff --git a/test/test_voicepeak_file_generation.py b/test/test_voicepeak_file_generation.py deleted file mode 100644 index 9a92ad5..0000000 --- a/test/test_voicepeak_file_generation.py +++ /dev/null @@ -1,270 +0,0 @@ -# Copyright (c) 2023 Nanahuse -# This software is released under the MIT License -# https://opensource.org/license/mit/ - -"""Mock-based tests for the wrapper's file-generation behavior. - -These tests replace the former manual VOICEPEAK integration tests that were -xfailed (``run=False``) because they launched a local voicepeak.exe. They run -without a local VOICEPEAK installation by mocking the subprocess, and verify -the intended output paths, generated files, existing-file overwrite, long-text, -and ``text_file`` behavior using temporary directories. -""" - -import asyncio -import os -from unittest.mock import AsyncMock - -import pytest - -pytestmark = pytest.mark.asyncio - - -class ProcessResult: - def __init__(self, stdout: bytes = b"", stderr: bytes = b""): - self.communicate = AsyncMock(return_value=(stdout, stderr)) - - -def create_client(monkeypatch): - monkeypatch.setenv("ProgramFiles", os.path.dirname(__file__)) - import voicepeak_wrapper - - return voicepeak_wrapper.Voicepeak(exe_path=__file__) - - -def mock_process(monkeypatch, *results): - create_subprocess = AsyncMock(side_effect=results) - monkeypatch.setattr(asyncio, "create_subprocess_exec", create_subprocess) - return create_subprocess - - -async def test_get_narrator_list_and_generate_file(monkeypatch, tmp_path): - client = create_client(monkeypatch) - import voicepeak_wrapper - - processes = ( - ProcessResult(b"Japanese Female 1\nJapanese Male 1\n"), - ProcessResult(b"happy\nsad\n"), - ProcessResult(b"happy\nangry\n"), - ProcessResult(b"completed\n"), - ProcessResult(b"completed\n"), - ) - create_subprocess = mock_process(monkeypatch, *processes) - - narrators = await client.get_narrator_list() - assert narrators == ( - voicepeak_wrapper.Narrator("Japanese Female 1", ("happy", "sad")), - voicepeak_wrapper.Narrator("Japanese Male 1", ("happy", "angry")), - ) - - lines = [] - for narrator in narrators: - lines.append(f"{narrator.name} : {', '.join(narrator.emotions)}\n") - await client.say_text( - "本日は晴天なり", - output_path=tmp_path / f"narrator_{narrator.name}.wav", - narrator=narrator, - emotions={narrator.emotions[1]: 100}, - ) - - output_file = tmp_path / "narrators.txt" - await asyncio.to_thread(output_file.write_text, "".join(lines), encoding="UTF-8") - assert output_file.read_text(encoding="UTF-8") == "Japanese Female 1 : happy, sad\nJapanese Male 1 : happy, angry\n" - - create_subprocess.assert_any_await( - __file__, - "-s", - "本日は晴天なり", - "-o", - os.fspath(tmp_path / "narrator_Japanese Female 1.wav"), - "-n", - "Japanese Female 1", - "-e", - "sad=100", - stdout=asyncio.subprocess.PIPE, - stderr=asyncio.subprocess.PIPE, - ) - create_subprocess.assert_any_await( - __file__, - "-s", - "本日は晴天なり", - "-o", - os.fspath(tmp_path / "narrator_Japanese Male 1.wav"), - "-n", - "Japanese Male 1", - "-e", - "angry=100", - stdout=asyncio.subprocess.PIPE, - stderr=asyncio.subprocess.PIPE, - ) - - -async def test_get_narrator_name_list_and_generate_file(monkeypatch, tmp_path): - client = create_client(monkeypatch) - mock_process(monkeypatch, ProcessResult(b"Japanese Female 1\nJapanese Male 1\n")) - - narrator_names = await client.get_narrator_name_list() - assert narrator_names == ("Japanese Female 1", "Japanese Male 1") - - output_file = tmp_path / "narrator_names.txt" - await asyncio.to_thread( - output_file.write_text, - "".join(f"{name}\n" for name in narrator_names), - encoding="UTF-8", - ) - assert output_file.read_text(encoding="UTF-8") == "Japanese Female 1\nJapanese Male 1\n" - - -async def test_get_emotion_list_and_generate_file(monkeypatch, tmp_path): - client = create_client(monkeypatch) - processes = ( - ProcessResult(b"Japanese Female 1\nJapanese Male 1\n"), - ProcessResult(b"happy\nsad\n"), - ProcessResult(b"happy\nangry\n"), - ) - mock_process(monkeypatch, *processes) - - narrator_names = await client.get_narrator_name_list() - lines = [] - for name in narrator_names: - emotion_list = await client.get_emotion_list(name) - lines.append(f"{name} : {', '.join(emotion_list)}\n") - - output_file = tmp_path / "emotions.txt" - await asyncio.to_thread(output_file.write_text, "".join(lines), encoding="UTF-8") - assert output_file.read_text(encoding="UTF-8") == "Japanese Female 1 : happy, sad\nJapanese Male 1 : happy, angry\n" - - mock_process(monkeypatch, ProcessResult(stderr=b"narrator not found: hogehoge\n")) - with pytest.raises(RuntimeError, match="narrator not found"): - await client.get_emotion_list("hogehoge") - - -async def test_say_text_and_validate_speed_pitch(monkeypatch, tmp_path): - client = create_client(monkeypatch) - processes = ( - ProcessResult(b"completed\n"), - ProcessResult(b"completed\n"), - ProcessResult(b"completed\n"), - ) - create_subprocess = mock_process(monkeypatch, *processes) - - await client.say_text("本日は晴天なり", output_path=tmp_path / "test_say_text.wav") - await client.say_text("本日は晴天なり", output_path=tmp_path / "test_say_text_upper.wav", speed=200, pitch=300) - await client.say_text("本日は晴天なり", output_path=tmp_path / "test_say_text_lower.wav", speed=50, pitch=-300) - - with pytest.raises(ValueError, match="speedは50 - 200の範囲内の整数"): - await client.say_text("エラー", output_path=tmp_path / "error.wav", speed=201) - with pytest.raises(ValueError, match="speedは50 - 200の範囲内の整数"): - await client.say_text("エラー", output_path=tmp_path / "error.wav", speed=49) - with pytest.raises(ValueError, match="pitchは-300 - 300の範囲内の整数"): - await client.say_text("エラー", output_path=tmp_path / "error.wav", pitch=-301) - with pytest.raises(ValueError, match="pitchは-300 - 300の範囲内の整数"): - await client.say_text("エラー", output_path=tmp_path / "error.wav", pitch=301) - - create_subprocess.assert_any_await( - __file__, - "-s", - "本日は晴天なり", - "-o", - os.fspath(tmp_path / "test_say_text.wav"), - stdout=asyncio.subprocess.PIPE, - stderr=asyncio.subprocess.PIPE, - ) - create_subprocess.assert_any_await( - __file__, - "-s", - "本日は晴天なり", - "-o", - os.fspath(tmp_path / "test_say_text_upper.wav"), - "--speed", - "200", - "--pitch", - "300", - stdout=asyncio.subprocess.PIPE, - stderr=asyncio.subprocess.PIPE, - ) - create_subprocess.assert_any_await( - __file__, - "-s", - "本日は晴天なり", - "-o", - os.fspath(tmp_path / "test_say_text_lower.wav"), - "--speed", - "50", - "--pitch", - "-300", - stdout=asyncio.subprocess.PIPE, - stderr=asyncio.subprocess.PIPE, - ) - - -async def test_say_text_long_text_raises_runtime_error(monkeypatch, tmp_path): - client = create_client(monkeypatch) - - def long_text_process(*call_args, **kwargs): - args = call_args[1:] - text = args[args.index("-s") + 1] - if len(text) >= 141: - return ProcessResult(stderr=b"text is too long\n") - return ProcessResult(b"completed\n") - - create_subprocess = AsyncMock(side_effect=long_text_process) - monkeypatch.setattr(asyncio, "create_subprocess_exec", create_subprocess) - - await client.say_text("本日は晴天なり", output_path=tmp_path / "ok.wav") - with pytest.raises(RuntimeError, match="text is too long"): - await client.say_text("1" * 141, output_path=tmp_path / "too_long.wav") - assert create_subprocess.await_count == 2 - - -async def test_say_textfile_generates_from_text_file(monkeypatch, tmp_path): - client = create_client(monkeypatch) - text_file = tmp_path / "text.txt" - await asyncio.to_thread(text_file.write_text, "吾輩は猫である。名前はまだ無い。", encoding="UTF-8") - create_subprocess = mock_process(monkeypatch, ProcessResult(b"completed\n")) - - result = await client.say_textfile(text_file, output_path=tmp_path / "say_text.wav") - - assert result == "completed\n" - create_subprocess.assert_awaited_once_with( - __file__, - "-t", - os.fspath(text_file), - "-o", - os.fspath(tmp_path / "say_text.wav"), - stdout=asyncio.subprocess.PIPE, - stderr=asyncio.subprocess.PIPE, - ) - - -async def test_say_text_overwrites_existing_output_file(monkeypatch, tmp_path): - client = create_client(monkeypatch) - output_path = tmp_path / "existing.wav" - await asyncio.to_thread(output_path.write_bytes, b"previous generation") - create_subprocess = mock_process(monkeypatch, ProcessResult(b"completed\n")) - - result = await client.say_text("本日は晴天なり", output_path=output_path) - - assert result == "completed\n" - create_subprocess.assert_awaited_once_with( - __file__, - "-s", - "本日は晴天なり", - "-o", - os.fspath(output_path), - stdout=asyncio.subprocess.PIPE, - stderr=asyncio.subprocess.PIPE, - ) - - -async def test_generated_narrator_file_overwrites_existing_content(monkeypatch, tmp_path): - client = create_client(monkeypatch) - output_file = tmp_path / "narrators.txt" - await asyncio.to_thread(output_file.write_text, "stale line from a previous run\n", encoding="UTF-8") - mock_process(monkeypatch, ProcessResult(b"Japanese Female 1\n")) - - narrator_names = await client.get_narrator_name_list() - lines = "".join(f"{name}\n" for name in narrator_names) - await asyncio.to_thread(output_file.write_text, lines, encoding="UTF-8") - - assert output_file.read_text(encoding="UTF-8") == "Japanese Female 1\n"