1package daemon
2
3import (
4 "testing"
5 "time"
6
7 "github.com/bnema/vev/internal/domain"
8 "github.com/stretchr/testify/require"
9)
10
11func TestCopyScrollAnimationEasesAndPreservesDistance(t *testing.T) {
12 for _, burst := range []int{1, 6} {
13 t.Run(map[int]string{1: "notch", 6: "burst"}[burst], func(t *testing.T) {
14 f := newPerformanceFixture(t, performanceConfig{size: domain.Size{Cols: 80, Rows: 24}, panes: 1, historyRows: 200})
15 f.d.enterCopyMode(f.sess, f.ac)
16 f.d.copyWheel(f.sess, f.ac, -50)
17 f.ac.ackOutputState(f.ac.output.currentEpoch(), f.ac.output.next)
18 clock := newCoordinatorMockClock(t, 16)
19 f.d.clock = clock.clock
20 emitted := make(chan struct{}, 16)
21 f.ac.renderStages.emit = func() { emitted <- struct{}{} }
22 await := func() {
23 select {
24 case <-emitted:
25 case <-time.After(time.Second):
26 t.Fatal("scroll frame not emitted")
27 }
28 // Wait for publication and capture the ACK coordinates under
29 // the same lock that protects the in-flight output transaction.
30 f.ac.sendMu.Lock()
31 epoch, state := f.ac.output.currentEpoch(), f.ac.output.next
32 f.ac.sendMu.Unlock()
33 f.ac.ackOutputState(epoch, state)
34 }
35 rt := f.ac.overlays
36 start := rt.copyMode.ViewportTop
37 for range burst {
38 f.d.smoothCopyWheel(f.sess, f.ac, -3)
39 }
40 await()
41 require.Equal(t, start-1, rt.copyMode.ViewportTop, "first row responds immediately; bursts wait for the next frame")
42 for frame := 0; ; frame++ {
43 rt.copyMu.Lock()
44 pending := rt.copyScroll.remaining
45 rt.copyMu.Unlock()
46 if pending == 0 {
47 break
48 }
49 require.Less(t, frame, 16, "animation must settle")
50 timer := awaitCoordinatorScheduledTimer(t, clock)
51 require.Equal(t, copyScrollFrame, timer.duration)
52 timer.ch <- time.Time{}
53 await()
54 }
55 require.Equal(t, start-3*burst, rt.copyMode.ViewportTop)
56 })
57 }
58}
59
60func TestCopyScrollAnimationReversalAndCancellation(t *testing.T) {
61 f := newPerformanceFixture(t, performanceConfig{size: domain.Size{Cols: 80, Rows: 24}, panes: 1, historyRows: 200})
62 f.d.enterCopyMode(f.sess, f.ac)
63 f.d.copyWheel(f.sess, f.ac, -50)
64 f.ac.ackOutputState(f.ac.output.currentEpoch(), f.ac.output.next)
65 clock := newCoordinatorMockClock(t, 8)
66 f.d.clock = clock.clock
67 rt := f.ac.overlays
68 start := rt.copyMode.ViewportTop
69 f.d.smoothCopyWheel(f.sess, f.ac, -3)
70 old := awaitCoordinatorScheduledTimer(t, clock)
71 f.d.smoothCopyWheel(f.sess, f.ac, 3)
72 require.Equal(t, start, rt.copyMode.ViewportTop, "reversal responds without finishing the old tail")
73 select {
74 case <-old.stopped:
75 default:
76 t.Fatal("old direction timer not canceled")
77 }
78 current := awaitCoordinatorScheduledTimer(t, clock)
79 rt.copyMu.Lock()
80 rt.invalidateCopyPointerLocked(true)
81 require.Zero(t, rt.copyScroll.remaining)
82 require.Nil(t, rt.copyScroll.timer.timer)
83 rt.copyMu.Unlock()
84 select {
85 case <-current.stopped:
86 default:
87 t.Fatal("pointer lifecycle must stop inertia")
88 }
89}
90
91func TestCopyScrollAnimationTailDeadline(t *testing.T) {
92 f := newPerformanceFixture(t, performanceConfig{size: domain.Size{Cols: 80, Rows: 24}, panes: 1, historyRows: 200})
93 f.d.enterCopyMode(f.sess, f.ac)
94 clock := newCoordinatorMockClock(t, 8)
95 f.d.clock = clock.clock
96 rt := f.ac.overlays
97 rt.copyMu.Lock()
98 start := rt.copyMode.ViewportTop
99 rt.copyScroll.remaining = -60
100 rt.copyScroll.lastInput = time.Time{}.Add(-copyScrollTail)
101 changed, exit := f.d.advanceCopyScrollLocked(f.sess, f.ac)
102 require.True(t, changed)
103 require.False(t, exit)
104 require.Equal(t, start-60, rt.copyMode.ViewportTop)
105 require.Zero(t, rt.copyScroll.remaining)
106 require.Nil(t, rt.copyScroll.timer.timer)
107 rt.copyMu.Unlock()
108}
109
110func TestCopyScrollReducedMotionAndKeyboardCancellation(t *testing.T) {
111 f := newPerformanceFixture(t, performanceConfig{size: domain.Size{Cols: 80, Rows: 24}, panes: 1, historyRows: 200})
112 f.d.enterCopyMode(f.sess, f.ac)
113 f.ac.ackOutputState(f.ac.output.currentEpoch(), f.ac.output.next)
114 clock := newCoordinatorMockClock(t, 8)
115 f.d.clock = clock.clock
116 updateRuntimeConfig(f.d, func(rc *runtimeConfig) { rc.copy = domain.CopyConfig{ReduceMotion: true} })
117 rt := f.ac.overlays
118 start := rt.copyMode.ViewportTop
119 f.d.smoothCopyWheel(f.sess, f.ac, -3)
120 require.Equal(t, start-3, rt.copyMode.ViewportTop)
121 require.Empty(t, clock.timers, "reduced motion must not arm an animation")
122 updateRuntimeConfig(f.d, func(rc *runtimeConfig) { rc.copy = domain.CopyConfig{} })
123 f.d.smoothCopyWheel(f.sess, f.ac, -3)
124 timer := awaitCoordinatorScheduledTimer(t, clock)
125 require.True(t, rt.HandleInput(f.d, []byte("k")))
126 rt.copyMu.Lock()
127 require.Zero(t, rt.copyScroll.remaining)
128 require.Nil(t, rt.copyScroll.timer.timer)
129 rt.copyMu.Unlock()
130 select {
131 case <-timer.stopped:
132 default:
133 t.Fatal("keyboard navigation must cancel inertia")
134 }
135}
136
137// TestCopyScrollAnimationFullOutputWindow covers a remote link whose output
138// window is full: the animation holds still, drains the backlog in capped
139// steps once the window reopens, eases freely again once that backlog is
140// drained, and gives up after about a second of hold.
141func TestCopyScrollAnimationFullOutputWindow(t *testing.T) {
142 tests := []struct {
143 name string
144 heldFrames int
145 reopen bool
146 // drainThenFlick drains the held backlog, then starts a new 60-row
147 // flick past the tail deadline; wantMoved counts only that flick.
148 drainThenFlick bool
149 wantMoved int
150 wantActive bool
151 }{
152 {name: "full window holds the viewport", heldFrames: 1, wantMoved: 0, wantActive: true},
153 {name: "reopened window drains in capped steps", heldFrames: 1, reopen: true, wantMoved: copyScrollMaxStep, wantActive: true},
154 {name: "drained backlog lifts the cap for the next flick", heldFrames: 1, reopen: true, drainThenFlick: true, wantMoved: 60, wantActive: false},
155 {name: "long hold abandons the scroll", heldFrames: copyScrollMaxHeldFrames + 1, wantMoved: 0, wantActive: false},
156 }
157 for _, tt := range tests {
158 t.Run(tt.name, func(t *testing.T) {
159 f := newPerformanceFixture(t, performanceConfig{size: domain.Size{Cols: 80, Rows: 24}, panes: 1, historyRows: 200})
160 f.ac.output.setWindow(1)
161 f.d.enterCopyMode(f.sess, f.ac)
162 clock := newCoordinatorMockClock(t, 2*copyScrollMaxHeldFrames)
163 f.d.clock = clock.clock
164 require.True(t, f.ac.output.atCapacity(), "entering copy mode fills a one-frame window")
165 rt := f.ac.overlays
166 rt.copyMu.Lock()
167 defer rt.copyMu.Unlock()
168 start := rt.copyMode.ViewportTop
169 // Past the tail deadline the whole backlog is due at once.
170 rt.copyScroll.remaining = -60
171 rt.copyScroll.lastInput = time.Time{}.Add(-copyScrollTail)
172 for range tt.heldFrames {
173 // Each frame is driven directly, as its timer callback would:
174 // retire the previous frame timer before advancing.
175 rt.copyScroll.timer.stop()
176 changed, exit := f.d.advanceCopyScrollLocked(f.sess, f.ac)
177 require.False(t, changed || exit, "a held frame must not move or exit")
178 }
179 if tt.reopen {
180 f.ac.ackOutputState(f.ac.output.currentEpoch(), f.ac.output.next)
181 rt.copyScroll.timer.stop()
182 changed, exit := f.d.advanceCopyScrollLocked(f.sess, f.ac)
183 require.True(t, changed)
184 require.False(t, exit)
185 }
186 if tt.drainThenFlick {
187 for rt.copyScroll.remaining != 0 {
188 f.ac.ackOutputState(f.ac.output.currentEpoch(), f.ac.output.next)
189 rt.copyScroll.timer.stop()
190 changed, exit := f.d.advanceCopyScrollLocked(f.sess, f.ac)
191 require.True(t, changed)
192 require.False(t, exit)
193 }
194 start = rt.copyMode.ViewportTop
195 rt.copyScroll.remaining = -60
196 changed, exit := f.d.advanceCopyScrollLocked(f.sess, f.ac)
197 require.True(t, changed)
198 require.False(t, exit)
199 }
200 require.Equal(t, tt.wantMoved, start-rt.copyMode.ViewportTop)
201 require.Equal(t, tt.wantActive, rt.copyScroll.remaining != 0)
202 require.Equal(t, tt.wantActive, rt.copyScroll.timer.timer != nil, "the frame timer stays armed only while the scroll is active")
203 rt.copyScroll.timer.stop()
204 })
205 }
206}