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}