@@ -3,6 +3,7 @@ package internal
33import (
44 "flag"
55 "fmt"
6+ tea "github.com/charmbracelet/bubbletea"
67 "os"
78 "path/filepath"
89 "testing"
@@ -116,6 +117,55 @@ func TestInitialFilePathPositionsCursor(t *testing.T) {
116117 assert .Equal (t , m .fileModel .filePanels [1 ].getSelectedItem ().location , file2 )
117118}
118119
120+ func TestInitialFilePathPositionsCursorWindow (t * testing.T ) {
121+ curTestDir := filepath .Join (testDir , "TestInitialFilePathPositionsCursorWindow" )
122+ dir1 := filepath .Join (curTestDir , "dir1" )
123+
124+ utils .SetupDirectories (t , curTestDir , dir1 )
125+ t .Cleanup (func () { _ = os .RemoveAll (curTestDir ) })
126+
127+ // Create 20 files with zero-padded names so lexicographic == numeric order.
128+ var targetFile string
129+ for i := 1 ; i <= 20 ; i ++ {
130+ f := filepath .Join (dir1 , fmt .Sprintf ("file%02d.txt" , i ))
131+ utils .SetupFiles (t , f )
132+ if i == 8 {
133+ targetFile = f
134+ }
135+ }
136+
137+ // Initialize model with the file path; cursor should land on file08
138+ m := defaultTestModel (targetFile )
139+
140+ // Make panel show exactly 5 entries: mainPanelHeight = 8 => 8-3 = 5
141+ TeaUpdate (m , tea.WindowSizeMsg {Width : common .MinimumWidth , Height : 10 })
142+
143+ // Populate elements and apply target cursor
144+ m .getFilePanelItems ()
145+
146+ panel := m .getFocusedFilePanel ()
147+ require .Equal (t , dir1 , panel .location , "Panel should open on parent directory" )
148+ require .Len (t , panel .element , 20 , "Should have 20 elements" )
149+
150+ // Find expected target index for file08
151+ targetIdx := - 1
152+ for i , el := range panel .element {
153+ if el .name == "file08.txt" {
154+ targetIdx = i
155+ break
156+ }
157+ }
158+ require .NotEqual (t , - 1 , targetIdx , "file08.txt should exist in elements" )
159+
160+ // Ensure cursor is on file08 and target is within visible window
161+ assert .Equal (t , targetIdx , panel .cursor , "Cursor should be on file08" )
162+
163+ visStart := panel .render
164+ visEnd := min (panel .render + panelElementHeight (m .mainPanelHeight ), len (panel .element ))
165+ assert .GreaterOrEqual (t , targetIdx , visStart , "Target file must be visible (>= start)" )
166+ assert .Less (t , targetIdx , visEnd , "Target file must be visible (< end)" )
167+ }
168+
119169func TestQuit (t * testing.T ) {
120170 // Test
121171 // 1 - Normal quit
0 commit comments