paste_coalescer_test.go RawHistoryFolder

1package client
2
3import (
4	"bytes"
5	"strconv"
6	"sync"
7	"testing"
8	"time"
9
10	"github.com/stretchr/testify/require"
11
12	"github.com/bnema/vev/internal/ports"
13)
14
15// pcFakeClock hands out fake timers the test fires by hand. The coalescer only
16// ever has one timer live at a time (a flush timer in normal mode or a safety
17// timer while buffering), so fireLast drives whichever is pending.
18type pcFakeClock struct {
19	mu     sync.Mutex
20	timers []*pcFakeTimer
21}
22
23func (c *pcFakeClock) Now() time.Time { return time.Time{} }
24
25func (c *pcFakeClock) NewTimer(d time.Duration) ports.Timer {
26	c.mu.Lock()
27	defer c.mu.Unlock()
28	t := &pcFakeTimer{ch: make(chan time.Time, 1), d: d}
29	c.timers = append(c.timers, t)
30	return t
31}
32
33func (c *pcFakeClock) fireLast() {
34	c.mu.Lock()
35	defer c.mu.Unlock()
36	for i := len(c.timers) - 1; i >= 0; i-- {
37		if !c.timers[i].stopped && !c.timers[i].fired {
38			c.timers[i].fired = true
39			c.timers[i].ch <- time.Time{}
40			return
41		}
42	}
43}
44
45type pcFakeTimer struct {
46	ch      chan time.Time
47	d       time.Duration
48	stopped bool
49	fired   bool
50}
51
52func (t *pcFakeTimer) C() <-chan time.Time        { return t.ch }
53func (t *pcFakeTimer) Reset(d time.Duration) bool { t.d = d; return !t.stopped }
54func (t *pcFakeTimer) Stop() bool                 { s := t.stopped; t.stopped = true; return !s }
55
56// pcCollector records emit calls under a mutex so a timer goroutine's emit is
57// safe to observe from the test goroutine.
58type pcCollector struct {
59	mu    sync.Mutex
60	emits [][]byte
61}
62
63func (c *pcCollector) emit(data []byte) {
64	c.mu.Lock()
65	defer c.mu.Unlock()
66	c.emits = append(c.emits, append([]byte(nil), data...))
67}
68
69func (c *pcCollector) snapshot() [][]byte {
70	c.mu.Lock()
71	defer c.mu.Unlock()
72	out := make([][]byte, len(c.emits))
73	copy(out, c.emits)
74	return out
75}
76
77func (c *pcCollector) waitEmits(t *testing.T, n int) {
78	t.Helper()
79	require.Eventually(t, func() bool {
80		c.mu.Lock()
81		defer c.mu.Unlock()
82		return len(c.emits) >= n
83	}, time.Second, time.Millisecond)
84}
85
86func newTestCoalescer() (*pasteCoalescer, *pcFakeClock, *pcCollector) {
87	clk := &pcFakeClock{}
88	col := &pcCollector{}
89	return newPasteCoalescer(clk, col.emit), clk, col
90}
91
92// samplePaste is a multiline bracketed paste whose content deliberately embeds
93// sequences that would otherwise be interpreted (a lone ESC+letter, an Alt
94// arrow CSI, a marker-looking prefix) to prove content is carried verbatim.
95var samplePaste = []byte("\x1b[200~line one\nline\x1btwo\n\x1b[1;3Athree\x1b[20not-a-marker\x1b[201~")
96
97func TestPasteCoalescerSplitAtEveryOffsetTwoReads(t *testing.T) {
98	for s := 0; s <= len(samplePaste); s++ {
99		t.Run("split_"+strconv.Itoa(s), func(t *testing.T) {
100			pc, _, col := newTestCoalescer()
101			defer pc.Close()
102			pc.Scan(samplePaste[:s])
103			pc.Scan(samplePaste[s:])
104			require.Equal(t, [][]byte{samplePaste}, col.snapshot(),
105				"paste split at offset %d must emit exactly once, byte-identical", s)
106		})
107	}
108}
109
110func TestPasteCoalescerByteByByte(t *testing.T) {
111	pc, _, col := newTestCoalescer()
112	defer pc.Close()
113	for i := 0; i < len(samplePaste); i++ {
114		pc.Scan(samplePaste[i : i+1])
115	}
116	require.Equal(t, [][]byte{samplePaste}, col.snapshot(),
117		"paste fed one byte at a time must still emit as one contiguous paste")
118}
119
120func TestPasteCoalescerBackToBackPastesInOneRead(t *testing.T) {
121	pc, _, col := newTestCoalescer()
122	defer pc.Close()
123
124	first := []byte("\x1b[200~first\x1b[201~")
125	second := []byte("\x1b[200~second\x1b[201~")
126	pc.Scan(append(append([]byte(nil), first...), second...))
127
128	require.Equal(t, [][]byte{first, second}, col.snapshot(),
129		"back-to-back pastes in one read must emit as two intact paste frames")
130}
131
132func TestPasteCoalescerLoneEscapeFlushedAfterTimer(t *testing.T) {
133	pc, clk, col := newTestCoalescer()
134	defer pc.Close()
135
136	pc.Scan([]byte{0x1b})
137	require.Empty(t, col.snapshot(), "a trailing ESC must be held, not emitted immediately")
138
139	clk.fireLast()
140	col.waitEmits(t, 1)
141	require.Equal(t, [][]byte{{0x1b}}, col.snapshot(), "held ESC must flush unchanged after the timer")
142}
143
144func TestPasteCoalescerEscapeThenOpenMarkerNextRead(t *testing.T) {
145	pc, _, col := newTestCoalescer()
146	defer pc.Close()
147
148	pc.Scan([]byte{0x1b})
149	require.Empty(t, col.snapshot(), "ESC held pending the rest of the marker")
150
151	pc.Scan(samplePaste[1:]) // "[200~...\x1b[201~"
152	require.Equal(t, [][]byte{samplePaste}, col.snapshot(),
153		"ESC recombined with the following marker must emit a single paste")
154}
155
156func TestPasteCoalescerPlainTextPassthrough(t *testing.T) {
157	pc, _, col := newTestCoalescer()
158	defer pc.Close()
159
160	pc.Scan([]byte("hello world"))
161	pc.Scan([]byte("second read"))
162	require.Equal(t, [][]byte{[]byte("hello world"), []byte("second read")}, col.snapshot(),
163		"plain text must pass through unchanged at the same call granularity")
164}
165
166func TestPasteCoalescerTextPasteTextInOneRead(t *testing.T) {
167	pc, _, col := newTestCoalescer()
168	defer pc.Close()
169
170	paste := []byte("\x1b[200~pasted\x1b[201~")
171	read := append(append([]byte("AB"), paste...), []byte("CD")...)
172	pc.Scan(read)
173	require.Equal(t, [][]byte{[]byte("AB"), paste, []byte("CD")}, col.snapshot(),
174		"text + paste + text in one read must emit three chunks in order")
175}
176
177func TestPasteCoalescerBufferCapOverflowFlushesVerbatim(t *testing.T) {
178	pc, _, col := newTestCoalescer()
179	defer pc.Close()
180
181	big := append([]byte("\x1b[200~"), bytes.Repeat([]byte("x"), maxPasteBuffer+16)...)
182	pc.Scan(big)
183	require.Equal(t, [][]byte{big}, col.snapshot(),
184		"an oversized unterminated paste must flush verbatim without dropping bytes")
185
186	// The coalescer must be back in normal mode and keep working.
187	pc.Scan([]byte("after"))
188	require.Equal(t, [][]byte{big, []byte("after")}, col.snapshot())
189}
190
191func TestPasteCoalescerSafetyTimeoutFlushesVerbatim(t *testing.T) {
192	pc, clk, col := newTestCoalescer()
193	defer pc.Close()
194
195	partial := []byte("\x1b[200~unterminated paste")
196	pc.Scan(partial)
197	require.Empty(t, col.snapshot(), "an open paste with no closing marker is buffered, not emitted")
198
199	clk.fireLast() // safety timer
200	col.waitEmits(t, 1)
201	require.Equal(t, [][]byte{partial}, col.snapshot(),
202		"the safety timeout must flush buffered paste bytes verbatim")
203
204	pc.Scan([]byte("normal"))
205	require.Equal(t, [][]byte{partial, []byte("normal")}, col.snapshot(),
206		"after the safety flush the coalescer must resume normal scanning")
207}
208
209func TestTrailingOpenPrefixLen(t *testing.T) {
210	cases := []struct {
211		in   string
212		want int
213	}{
214		{"", 0},
215		{"abc", 0},
216		{"abc\x1b", 1},
217		{"abc\x1b[", 2},
218		{"\x1b[2", 3},
219		{"\x1b[20", 4},
220		{"\x1b[200", 5},
221		{"\x1b[200~", 0}, // full marker is not a strict prefix
222		{"[200", 0},      // no ESC: not a marker start
223	}
224	for _, tc := range cases {
225		require.Equal(t, tc.want, trailingOpenPrefixLen([]byte(tc.in)), "input %q", tc.in)
226	}
227}

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 12

Tags 11

↑↓ moveenter openesc close