#!/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()