Files
whisper-local/tests/test_inserter.py
T

105 lines
3.9 KiB
Python
Raw Normal View History

import asyncio
import sys
from unittest.mock import AsyncMock, patch, call
import pytest
from whisper_local.inserter._wayland import WaylandInserter
class TestWaylandInserter:
@pytest.mark.asyncio
@patch("whisper_local.inserter._wayland.asyncio.sleep", new_callable=AsyncMock)
@patch("whisper_local.inserter._wayland.asyncio.create_subprocess_exec")
async def test_insert_text_calls_tools_in_order(self, mock_exec, mock_sleep):
mock_proc = AsyncMock()
mock_proc.communicate.return_value = (b"alter clipboard", b"")
mock_proc.returncode = 0
mock_proc.wait = AsyncMock()
mock_exec.return_value = mock_proc
inserter = WaylandInserter()
await inserter.insert("Hallo Welt")
calls = mock_exec.call_args_list
assert len(calls) == 4
assert calls[0][0] == ("wl-paste", "--no-newline")
assert calls[1][0] == ("wl-copy", "--", "Hallo Welt")
assert calls[2][0][0] == "ydotool"
assert calls[3][0] == ("wl-copy", "--", "alter clipboard")
@pytest.mark.asyncio
async def test_insert_empty_text_does_nothing(self):
inserter = WaylandInserter()
await inserter.insert("")
@pytest.mark.asyncio
@patch("whisper_local.inserter._wayland.asyncio.sleep", new_callable=AsyncMock)
@patch("whisper_local.inserter._wayland.asyncio.create_subprocess_exec")
async def test_clipboard_restore_on_paste_failure(self, mock_exec, mock_sleep):
call_count = 0
async def mock_create_proc(*args, **kwargs):
nonlocal call_count
call_count += 1
mock_proc = AsyncMock()
mock_proc.wait = AsyncMock()
if call_count == 1: # wl-paste
mock_proc.communicate.return_value = (b"original", b"")
mock_proc.returncode = 0
elif call_count == 3: # ydotool — fails
mock_proc.wait.side_effect = OSError("ydotool not found")
else: # wl-copy calls
mock_proc.returncode = 0
return mock_proc
mock_exec.side_effect = mock_create_proc
inserter = WaylandInserter()
with pytest.raises(OSError):
await inserter.insert("Test")
assert call_count == 4
class TestWin32Inserter:
@pytest.mark.asyncio
@patch("whisper_local.inserter._win32.keyboard_controller")
@patch("whisper_local.inserter._win32.Win32Inserter._get_clipboard", return_value="alter clipboard")
@patch("whisper_local.inserter._win32.Win32Inserter._set_clipboard")
async def test_insert_text(self, mock_set, mock_get, mock_kb):
from whisper_local.inserter._win32 import Win32Inserter
inserter = Win32Inserter()
await inserter.insert("Hallo Welt")
# Clipboard wurde gesichert
mock_get.assert_called_once()
# Text wurde in Clipboard gesetzt, dann Ctrl+V, dann Restore
assert mock_set.call_count == 2
mock_set.assert_any_call("Hallo Welt")
mock_set.assert_any_call("alter clipboard")
@pytest.mark.asyncio
async def test_insert_empty_does_nothing(self):
from whisper_local.inserter._win32 import Win32Inserter
inserter = Win32Inserter()
await inserter.insert("")
@pytest.mark.asyncio
@patch("whisper_local.inserter._win32.keyboard_controller")
@patch("whisper_local.inserter._win32.Win32Inserter._get_clipboard", return_value="original")
@patch("whisper_local.inserter._win32.Win32Inserter._set_clipboard")
async def test_clipboard_restored_on_error(self, mock_set, mock_get, mock_kb):
from whisper_local.inserter._win32 import Win32Inserter
mock_kb.press.side_effect = OSError("keyboard error")
inserter = Win32Inserter()
with pytest.raises(OSError):
await inserter.insert("Test")
# Clipboard muss trotzdem wiederhergestellt werden
mock_set.assert_called_with("original")