feat: add clipboard file handling for Mattermost and implement batch debounce functionality
This commit is contained in:
70
scripts/iphone-photo-inbox/test_receiver.py
Normal file
70
scripts/iphone-photo-inbox/test_receiver.py
Normal file
@@ -0,0 +1,70 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import importlib.util
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch
|
||||
|
||||
|
||||
RECEIVER_PATH = Path(__file__).with_name("receiver.py")
|
||||
SPEC = importlib.util.spec_from_file_location("iphone_photo_receiver", RECEIVER_PATH)
|
||||
receiver = importlib.util.module_from_spec(SPEC)
|
||||
assert SPEC.loader is not None
|
||||
sys.modules[SPEC.name] = receiver
|
||||
SPEC.loader.exec_module(receiver)
|
||||
|
||||
|
||||
class ReceiverTests(unittest.TestCase):
|
||||
def tearDown(self) -> None:
|
||||
for batch in getattr(self, "batches", []):
|
||||
if batch.timer is not None:
|
||||
batch.timer.cancel()
|
||||
|
||||
def test_mattermost_profile_defaults_to_files_clipboard(self) -> None:
|
||||
profile = receiver.profile_defaults()["mattermost"]
|
||||
self.assertEqual(profile.clipboard, receiver.CLIPBOARD_FILES)
|
||||
|
||||
def test_batch_add_accumulates_paths_by_profile(self) -> None:
|
||||
manager = receiver.BatchManager(debounce_seconds=60)
|
||||
profile = receiver.Profile("opencode", Path("/tmp"), receiver.CLIPBOARD_TERMINAL_PATH, notify=False)
|
||||
|
||||
count1, paths1 = manager.add(profile, Path("/tmp"), Path("/tmp/one.jpg"))
|
||||
count2, paths2 = manager.add(profile, Path("/tmp"), Path("/tmp/two.jpg"))
|
||||
|
||||
self.batches = list(manager.batches.values())
|
||||
self.assertEqual(count1, 1)
|
||||
self.assertEqual(paths1, [Path("/tmp/one.jpg")])
|
||||
self.assertEqual(count2, 2)
|
||||
self.assertEqual(paths2, [Path("/tmp/one.jpg"), Path("/tmp/two.jpg")])
|
||||
|
||||
def test_files_clipboard_passes_all_paths_to_native_helper(self) -> None:
|
||||
paths = [Path("/tmp/one.jpg"), Path("/tmp/two.jpg")]
|
||||
|
||||
with tempfile.NamedTemporaryFile() as helper:
|
||||
helper_path = Path(helper.name)
|
||||
with patch.object(receiver, "COPY_FILES_HELPER_BIN", Path("/tmp/missing-helper-bin")), \
|
||||
patch.object(receiver, "COPY_FILES_HELPER", helper_path), \
|
||||
patch.object(receiver, "run_macos_action", return_value=True) as run_action:
|
||||
self.assertTrue(receiver.copy_files_to_clipboard(paths))
|
||||
|
||||
run_action.assert_called_once_with([
|
||||
str(helper_path),
|
||||
"/tmp/one.jpg",
|
||||
"/tmp/two.jpg",
|
||||
])
|
||||
|
||||
def test_terminal_paths_clipboard_contains_all_paths(self) -> None:
|
||||
paths = [Path("/tmp/one.jpg"), Path("/tmp/two with space.jpg")]
|
||||
|
||||
with patch.object(receiver, "copy_text_to_clipboard", return_value=True) as copy_text:
|
||||
self.assertTrue(receiver.apply_clipboard_mode(receiver.CLIPBOARD_TERMINAL_PATH, paths))
|
||||
|
||||
copy_text.assert_called_once_with("/tmp/one.jpg\n'/tmp/two with space.jpg'")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user