Files
FWS-Kitty/kitty_tests/clipboard_history.py
T
BlackAngelTV 42c097c69a feat(clipboard): show the paste target and navigate the history picker
The clipboard history picker listed the entries without saying when they
were copied or which window would receive the paste, and the only way to
pick an entry was to read its hint label and type the letters, which the
picker never showed.

Each entry now carries the time it was copied and the window that will be
pasted into is named above the list, with a footer documenting the keys.
Pressing Up/Down moves the highlighted entry and Enter pastes it, through a
new --pick-from-list mode of the hints kitten that keeps the hint letters
working as before.

Fixes #49
2026-12-03 11:01:11 +01:00

147 lines
5.3 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_entries_carry_the_time_they_were_copied(self) -> None:
h = ClipboardHistory()
self.assertTrue(h.add('first', timestamp=100.0))
self.assertTrue(h.add('second', timestamp=200.0))
self.ae(h.entries(), ('second', 'first'))
self.ae(h.entries_with_times(), (('second', 200.0), ('first', 100.0)))
# A repeated copy moves the text and its new time to the top
self.assertTrue(h.add('first', timestamp=300.0))
self.ae(h.entries(), ('first', 'second'))
self.ae(h.entries_with_times(), (('first', 300.0), ('second', 200.0)))
self.ae(h.search_with_times('sec'), (('second', 200.0),))
self.ae(h.search('sec'), ('second',))
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'))