Files
FWS-Kitty/kitty_tests/clipboard_history.py
T
BlackAngelTV 44b9aa166e fix(clipboard): record streamed clipboard writes in the history
OSC 52 writes are committed as a chunker factory rather than bytes, so the
history silently dropped every text copy made by a program. Extract the text
from either representation, bounded so a huge write is not materialized.
2026-11-11 21:32:09 +01:00

134 lines
4.6 KiB
Python

#!/usr/bin/env python
# License: GPL v3 Copyright: 2026, Kovid Goyal <kovid at kovidgoyal.net>
from kitty.clipboard_history import ClipboardHistory, match_score, text_from_mime_data
from .base import BaseTest
class TestClipboardHistory(BaseTest):
def test_newest_first(self) -> None:
h = ClipboardHistory()
self.assertFalse(h)
self.assertTrue(h.add('one'))
self.assertTrue(h.add('two'))
self.ae(h.entries(), ('two', 'one'))
self.ae(list(h), ['two', 'one'])
self.ae(len(h), 2)
self.ae(h[0], 'two')
def test_empty_and_blank_text_is_ignored(self) -> None:
h = ClipboardHistory()
self.assertFalse(h.add(''))
self.assertFalse(h.add(' \n\t'))
self.ae(len(h), 0)
def test_private_entries_are_never_recorded(self) -> None:
h = ClipboardHistory()
self.assertFalse(h.add('hunter2', private=True))
self.ae(len(h), 0)
self.assertTrue(h.add('public'))
self.assertFalse(h.add('hunter2', private=True))
self.ae(h.entries(), ('public',))
def test_duplicates_move_to_the_top(self) -> None:
h = ClipboardHistory()
for x in ('a', 'b', 'a'):
h.add(x)
self.ae(h.entries(), ('a', 'b'))
def test_entry_count_is_bounded(self) -> None:
h = ClipboardHistory(max_entries=3)
for x in ('a', 'b', 'c', 'd', 'e'):
h.add(x)
self.ae(h.entries(), ('e', 'd', 'c'))
def test_total_size_is_bounded(self) -> None:
h = ClipboardHistory(max_entries=100, max_total_chars=10)
h.add('12345')
h.add('67890')
h.add('abcde')
self.assertLessEqual(sum(len(x) for x in h.entries()), 10)
self.ae(h.entries()[0], 'abcde')
def test_oversized_entries_are_ignored(self) -> None:
h = ClipboardHistory(max_entry_chars=5)
self.assertFalse(h.add('123456'))
self.assertTrue(h.add('12345'))
self.ae(h.entries(), ('12345',))
def test_clear(self) -> None:
h = ClipboardHistory()
h.add('a')
h.clear()
self.ae(len(h), 0)
self.assertFalse(h)
# clearing resets the accounting, so new entries are kept even after large ones
self.assertTrue(h.add('b'))
self.ae(h.entries(), ('b',))
def test_search(self) -> None:
h = ClipboardHistory()
for x in ('make test', 'make build', 'git status', 'echo make'):
h.add(x)
self.ae(h.search(''), h.entries())
self.ae(h.search(' '), h.entries())
# An earlier substring match ranks first, ties keep the newest entry first
self.ae(h.search('make'), ('make build', 'make test', 'echo make'))
self.ae(h.search('mkb'), ('make build',))
self.ae(h.search('zzz'), ())
def test_substring_matches_rank_before_subsequences(self) -> None:
h = ClipboardHistory()
h.add('xkcd')
h.add('kcd')
self.ae(h.search('kcd'), ('kcd', 'xkcd'))
def test_match_score(self) -> None:
self.ae(match_score('', 'anything'), 0)
self.ae(match_score('abc', 'xxabc'), 2)
self.assertGreater(match_score('abc', 'a-x-b-x-c'), 2)
self.ae(match_score('abc', 'cba'), -1)
self.ae(match_score('Abc', 'xxabc'), 2) # matching is case insensitive
def test_text_from_bytes(self) -> None:
self.ae(text_from_mime_data(b'hello'), 'hello')
self.ae(text_from_mime_data('not bytes'), '')
self.ae(text_from_mime_data(None), '')
self.ae(text_from_mime_data(b'a\xffb'), 'a\ufffdb')
def test_text_from_chunker(self) -> None:
# OSC 52 writes are streamed, so the clipboard stores a chunker factory not bytes
chunks = [b'first ', b'second', b'']
def factory():
it = iter(chunks)
def chunker():
return next(it, b'')
return chunker
self.ae(text_from_mime_data(factory), 'first second')
def test_text_from_chunker_is_bounded(self) -> None:
def factory():
remaining = [b'x' * 1000] * 1000
it = iter(remaining)
def chunker():
return next(it, b'')
return chunker
ans = text_from_mime_data(factory, max_chars=10)
self.assertLessEqual(len(ans), 40)
self.assertTrue(ans)
def test_no_persistence_api(self) -> None:
# The history is deliberately memory only: there is no save/load entry point to call
h = ClipboardHistory()
self.assertFalse(hasattr(h, 'save'))
self.assertFalse(hasattr(h, 'load'))
self.assertFalse(hasattr(h, 'path'))