Files
glassMxrph/tests/clipboard_test.py
T
mxandClaude Opus 5.5 5a10469efe Add Clipboard History as a launcher mode backed by cliphist
The launcher gains a clipboard mode with the same bar, position and size:
the latest five entries show at once and the rest scroll, search filters
in recency order, Enter copies, Shift+Delete removes an entry. >clipboard
switches in place without closing or animating; glassmxrph clipboard
toggles it directly. History stays with the user's cliphist watcher.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-15 15:16:14 +02:00

34 lines
1.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
import json,subprocess,sys,tempfile,unittest
from pathlib import Path
from unittest.mock import patch
ROOT=Path(__file__).resolve().parents[1]
sys.path.insert(0,str(ROOT/'quickshell/helpers'))
import clipboard
class ClipboardHelper(unittest.TestCase):
def setUp(self):
self.tmp=tempfile.TemporaryDirectory();self.cache=patch.object(clipboard,'CACHE',Path(self.tmp.name));self.cache.start()
def tearDown(self):self.cache.stop();self.tmp.cleanup()
def test_history_parses_text_and_images_newest_first(self):
listing=b'3\t[[ binary data 2 KiB png 640x480 ]]\n2\thello\tworld\n1\tplain\n'
def fake(*args,data=None,timeout=5):return listing if args==('list',) else b'PNGDATA'
with patch.object(clipboard,'cliphist',side_effect=fake):
entries=clipboard.history()
self.assertEqual([e['id'] for e in entries],[3,2,1])
self.assertTrue(entries[0]['image']);self.assertEqual(entries[0]['text'],'Image 640×480')
self.assertEqual(Path(entries[0]['thumb']).read_bytes(),b'PNGDATA')
self.assertEqual(entries[1]['text'],'hello\tworld')
def test_stale_thumbnails_are_pruned(self):
(Path(self.tmp.name)/'9.png').write_bytes(b'old')
with patch.object(clipboard,'cliphist',return_value=b'1\ttext\n'):clipboard.history()
self.assertFalse((Path(self.tmp.name)/'9.png').exists())
def test_ids_are_validated_before_cliphist_runs(self):
with patch.object(clipboard,'cliphist') as run:
for bad in [None,'1;rm',-1,True,1.5]:
with self.assertRaises(ValueError):clipboard.main({'op':'delete','id':bad})
run.assert_not_called()
def test_cli_errors_are_json(self):
p=subprocess.run([sys.executable,str(ROOT/'quickshell/helpers/clipboard.py'),'{"op":"unknown"}'],capture_output=True,text=True)
self.assertFalse(json.loads(p.stdout)['ok']);self.assertEqual(p.returncode,1)
if __name__=='__main__':unittest.main()