Bug fixes.

This commit is contained in:
gmp
2026-06-03 10:38:29 -04:00
parent 0dac354f2a
commit bb55b4ab81
5 changed files with 144 additions and 57 deletions
+22 -4
View File
@@ -51,13 +51,29 @@ func clampScrollOffset(s *BrowserState) {
}
}
// resolveTapIndex maps a ListView row index to the actual sorted entry index.
// When search is active, the row index is into SearchResults, so we must
// look through SearchResults to find the real entry index.
func resolveTapIndex(s *BrowserState, index int) int {
if s.Query != "" && len(s.SearchResults) > 0 {
if index < 0 || index >= len(s.SearchResults) {
return -1
}
return s.SearchResults[index]
}
return index
}
// HandleBrowserTap processes a tap on the ListView at the given index.
func HandleBrowserTap(bm *BrowserManager, s *BrowserState, index int) {
if index < 0 || index >= s.TotalEntries {
// Resolve the ListView row index to the actual sorted entry index.
// When search is active, the row is an index into SearchResults.
entryIndex := resolveTapIndex(s, index)
if entryIndex < 0 || entryIndex >= s.TotalEntries {
return
}
entry, ok := getEntryByIndex(s, index)
entry, ok := getEntryByIndex(s, entryIndex)
if !ok {
return // Page not loaded
}
@@ -76,9 +92,11 @@ func HandleBrowserTap(bm *BrowserManager, s *BrowserState, index int) {
bm.NavigateTo(newPath)
} else {
// Open file
s.SelectedIndex = index
s.SelectedIndex = entryIndex
fmt.Printf("HandleBrowserTap: Opening file %s\n", entry.Path)
ui.OpenFile(entry.Path)
if ui.OpenFile != nil {
ui.OpenFile(entry.Path)
}
// Dispatch ReadFileTask to the worker pool
task := pool.NewReadFileTask(entry.Path, bm.fs)
fmt.Printf("HandleBrowserTap: Dispatching ReadFileTask for %s\n", entry.Path)
+72
View File
@@ -58,3 +58,75 @@ func TestTapUp(t *testing.T) {
t.Errorf("expected CurrentPath='/root', got %s", state.CurrentPath)
}
}
// TestTapWithSearchResults verifies that tapping a file in the search
// results list opens the correct file, not the file at the same index
// in the full directory listing.
func TestTapWithSearchResults(t *testing.T) {
state := NewBrowserState()
fs := mock.NewFileSystem()
wp := pool.NewWorkerPool(1)
bm, _ := NewBrowserManager(state, wp, fs)
state.CurrentPath = "/root"
// Mock entries: 5 files, sorted by name
entries := []Entry{
NewEntry("/root/alpha.txt", "alpha.txt", 100, time.Time{}, false),
NewEntry("/root/beta.txt", "beta.txt", 200, time.Time{}, false),
NewEntry("/root/gamma.txt", "gamma.txt", 300, time.Time{}, false),
NewEntry("/root/delta.txt", "delta.txt", 400, time.Time{}, false),
NewEntry("/root/epsilon.txt", "epsilon.txt", 500, time.Time{}, false),
}
state.Pages[0] = NewPage(0, entries)
state.TotalEntries = 5
// Simulate a search for "beta" that matches only index 1
state.Query = "beta"
state.SearchResults = []int{1}
// Tapping row 0 of the search results should open beta.txt (entry index 1),
// NOT alpha.txt (entry index 0).
HandleBrowserTap(bm, state, 0)
// Verify SelectedIndex was set to the resolved entry index (1), not the row index (0).
if state.SelectedIndex != 1 {
t.Errorf("expected SelectedIndex=1, got %d", state.SelectedIndex)
}
}
// TestTapWithMultipleSearchResults verifies tapping different rows of
// search results resolves to the correct entries.
func TestTapWithMultipleSearchResults(t *testing.T) {
state := NewBrowserState()
fs := mock.NewFileSystem()
wp := pool.NewWorkerPool(1)
bm, _ := NewBrowserManager(state, wp, fs)
state.CurrentPath = "/root"
// Mock entries: 5 files
entries := []Entry{
NewEntry("/root/aaa.txt", "aaa.txt", 100, time.Time{}, false),
NewEntry("/root/zzz.txt", "zzz.txt", 200, time.Time{}, false),
NewEntry("/root/aaa2.txt", "aaa2.txt", 300, time.Time{}, false),
NewEntry("/root/zzz2.txt", "zzz2.txt", 400, time.Time{}, false),
NewEntry("/root/mid.txt", "mid.txt", 500, time.Time{}, false),
}
state.Pages[0] = NewPage(0, entries)
state.TotalEntries = 5
// Simulate a search for "zzz" that matches indices 1 and 3
state.Query = "zzz"
state.SearchResults = []int{1, 3}
// Tapping row 0 should resolve to entry index 1 (zzz.txt)
HandleBrowserTap(bm, state, 0)
if state.SelectedIndex != 1 {
t.Errorf("row 0: expected SelectedIndex=1, got %d", state.SelectedIndex)
}
// Tapping row 1 should resolve to entry index 3 (zzz2.txt)
HandleBrowserTap(bm, state, 1)
if state.SelectedIndex != 3 {
t.Errorf("row 1: expected SelectedIndex=3, got %d", state.SelectedIndex)
}
}