From f7a5d519fb8767ba2ea0811349a4f7280c8a7b08 Mon Sep 17 00:00:00 2001 From: "suhird.singh" Date: Wed, 24 Jun 2026 13:41:17 -0400 Subject: [PATCH] gui: test main panel goto-top/bottom helpers Cover the gotoBottomOriginY origin math (scroll-past-bottom on/off, content shorter than the view, empty content) and the isDoublePress timing used to detect the vim-style "gg" double press. --- pkg/gui/main_panel_test.go | 100 +++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 pkg/gui/main_panel_test.go diff --git a/pkg/gui/main_panel_test.go b/pkg/gui/main_panel_test.go new file mode 100644 index 00000000..0600779e --- /dev/null +++ b/pkg/gui/main_panel_test.go @@ -0,0 +1,100 @@ +package gui + +import ( + "testing" + "time" + + "github.com/stretchr/testify/assert" +) + +func TestGotoBottomOriginY(t *testing.T) { + type scenario struct { + name string + totalLines int + viewHeight int + scrollPastBottom bool + expected int + } + + scenarios := []scenario{ + { + name: "keeps the last page on screen when not scrolling past bottom", + totalLines: 100, + viewHeight: 20, + scrollPastBottom: false, + expected: 80, + }, + { + name: "scrolls all content off the top when scrolling past bottom", + totalLines: 100, + viewHeight: 20, + scrollPastBottom: true, + expected: 100, + }, + { + name: "never returns a negative origin when content fits the view", + totalLines: 5, + viewHeight: 20, + scrollPastBottom: false, + expected: 0, + }, + { + name: "handles empty content", + totalLines: 0, + viewHeight: 20, + scrollPastBottom: true, + expected: 0, + }, + } + + for _, s := range scenarios { + t.Run(s.name, func(t *testing.T) { + assert.Equal(t, s.expected, gotoBottomOriginY(s.totalLines, s.viewHeight, s.scrollPastBottom)) + }) + } +} + +func TestIsDoublePress(t *testing.T) { + now := time.Unix(1000, 0) + timeout := 250 * time.Millisecond + + type scenario struct { + name string + previous time.Time + now time.Time + expected bool + } + + scenarios := []scenario{ + { + name: "no previous press is not a double press", + previous: time.Time{}, + now: now, + expected: false, + }, + { + name: "second press within the timeout is a double press", + previous: now.Add(-100 * time.Millisecond), + now: now, + expected: true, + }, + { + name: "second press exactly on the timeout is a double press", + previous: now.Add(-timeout), + now: now, + expected: true, + }, + { + name: "second press after the timeout is not a double press", + previous: now.Add(-timeout - time.Millisecond), + now: now, + expected: false, + }, + } + + for _, s := range scenarios { + t.Run(s.name, func(t *testing.T) { + assert.Equal(t, s.expected, isDoublePress(s.previous, s.now, timeout)) + }) + } +}