diff --git a/test/test_manual_voicepeak_file_generation.py b/test/test_manual_voicepeak_file_generation.py index ff03f93..90b5f16 100644 --- a/test/test_manual_voicepeak_file_generation.py +++ b/test/test_manual_voicepeak_file_generation.py @@ -115,3 +115,57 @@ async def test_say_testfile(): 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.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() == ()