Skip to content

Commit 9e4f93f

Browse files
claudelazysegtree
andcommitted
Use consistent zoxidelib alias and enhance zoxide test
- Replace all instances of 'github.com/lazysegtree/go-zoxide' imports with 'zoxidelib' alias for consistency across codebase - Add comprehensive zoxide UI navigation test that types "dir2" to search and presses Enter to navigate - Fix zoxide modal close behavior by adding m.Close() call in handleConfirm action - Add GetResults() method to zoxide UI component for test access - Create defaultTestModelWithZClient() helper function for zoxide testing Co-Authored-By: Nitin Kumar <59679977+lazysegtree@users.noreply.github.com>
1 parent 9272b00 commit 9e4f93f

File tree

5 files changed

+86
-4
lines changed

5 files changed

+86
-4
lines changed

src/internal/config_function.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"reflect"
77
"runtime"
88

9-
"github.com/lazysegtree/go-zoxide"
9+
zoxidelib "github.com/lazysegtree/go-zoxide"
1010

1111
"github.com/yorukot/superfile/src/internal/ui/processbar"
1212
"github.com/yorukot/superfile/src/internal/ui/rendering"
@@ -25,7 +25,7 @@ import (
2525

2626
// This is the only usecase of named returns, distinguish between multiple return values
2727
func initialConfig(firstFilePanelDirs []string) (toggleDotFile bool, //nolint: nonamedreturns // See above
28-
toggleFooter bool, zClient *zoxide.Client) {
28+
toggleFooter bool, zClient *zoxidelib.Client) {
2929
// Open log stream
3030
file, err := os.OpenFile(variable.LogFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
3131

@@ -73,7 +73,7 @@ func initialConfig(firstFilePanelDirs []string) (toggleDotFile bool, //nolint: n
7373
}
7474

7575
if common.Config.ZoxideSupport {
76-
zClient, err = zoxide.New()
76+
zClient, err = zoxidelib.New()
7777
if err != nil {
7878
slog.Error("Error initializing zoxide client", "error", err)
7979
}
@@ -90,7 +90,7 @@ func initialConfig(firstFilePanelDirs []string) (toggleDotFile bool, //nolint: n
9090
return toggleDotFile, toggleFooter, zClient
9191
}
9292

93-
func updateFirstFilePanelDirs(firstFilePanelDirs []string, cwd string, zClient *zoxide.Client) {
93+
func updateFirstFilePanelDirs(firstFilePanelDirs []string, cwd string, zClient *zoxidelib.Client) {
9494
for i := range firstFilePanelDirs {
9595
if firstFilePanelDirs[i] == "" {
9696
firstFilePanelDirs[i] = common.Config.DefaultDirectory

src/internal/model_test.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ import (
55
"fmt"
66
"os"
77
"path/filepath"
8+
"runtime"
89
"testing"
910

11+
zoxidelib "github.com/lazysegtree/go-zoxide"
1012
"github.com/stretchr/testify/assert"
1113
"github.com/stretchr/testify/require"
1214

@@ -245,3 +247,70 @@ func TestChooserFile(t *testing.T) {
245247
})
246248
}
247249
}
250+
251+
252+
func TestZoxide(t *testing.T) {
253+
zoxideDataDir := t.TempDir()
254+
zClient, err := zoxidelib.New(zoxidelib.WithDataDir(zoxideDataDir))
255+
if err != nil {
256+
if runtime.GOOS != utils.OsLinux {
257+
t.Skipf("Skipping zoxide tests in non-Linux because zoxide client cannot be initialized")
258+
} else {
259+
t.Fatalf("zoxide initialization failed")
260+
}
261+
}
262+
263+
originalZoxideSupport := common.Config.ZoxideSupport
264+
defer func() {
265+
common.Config.ZoxideSupport = originalZoxideSupport
266+
}()
267+
268+
curTestDir := filepath.Join(testDir, "TestZoxide")
269+
dir1 := filepath.Join(curTestDir, "dir1")
270+
dir2 := filepath.Join(curTestDir, "dir2")
271+
dir3 := filepath.Join(curTestDir, "dir3")
272+
utils.SetupDirectories(t, curTestDir, dir1, dir2, dir3)
273+
274+
t.Run("Zoxide tracking and navigation", func(t *testing.T) {
275+
common.Config.ZoxideSupport = true
276+
m := defaultTestModelWithZClient(zClient, dir1)
277+
278+
err := m.updateCurrentFilePanelDir(dir2)
279+
require.NoError(t, err, "Failed to navigate to dir2")
280+
assert.Equal(t, dir2, m.getFocusedFilePanel().location, "Should be in dir2 after navigation")
281+
282+
err = m.updateCurrentFilePanelDir(dir3)
283+
require.NoError(t, err, "Failed to navigate to dir3")
284+
assert.Equal(t, dir3, m.getFocusedFilePanel().location, "Should be in dir3 after navigation")
285+
286+
TeaUpdateWithErrCheck(m, utils.TeaRuneKeyMsg(common.Hotkeys.OpenZoxide[0]))
287+
assert.True(t, m.zoxideModal.IsOpen(), "Zoxide modal should open when pressing 'z' key")
288+
289+
// Type "dir2" to search for it
290+
for _, char := range "dir2" {
291+
TeaUpdateWithErrCheck(m, utils.TeaRuneKeyMsg(string(char)))
292+
}
293+
294+
results := m.zoxideModal.GetResults()
295+
assert.GreaterOrEqual(t, len(results), 1, "Should have at least 1 directory found by zoxide UI search")
296+
297+
resultPaths := make([]string, len(results))
298+
for i, result := range results {
299+
resultPaths[i] = result.Path
300+
}
301+
assert.Contains(t, resultPaths, dir2, "dir2 should be found by zoxide UI search")
302+
303+
// Press enter to navigate to dir2
304+
TeaUpdateWithErrCheck(m, utils.TeaRuneKeyMsg(common.Hotkeys.ConfirmTyping[0]))
305+
assert.False(t, m.zoxideModal.IsOpen(), "Zoxide modal should close after navigation")
306+
assert.Equal(t, dir2, m.getFocusedFilePanel().location, "Should navigate back to dir2 after zoxide selection")
307+
})
308+
309+
t.Run("Zoxide disabled shows no results", func(t *testing.T) {
310+
common.Config.ZoxideSupport = false
311+
m := defaultTestModelWithZClient(zClient, dir1)
312+
313+
TeaUpdateWithErrCheck(m, utils.TeaRuneKeyMsg(common.Hotkeys.OpenZoxide[0]))
314+
assert.True(t, m.zoxideModal.IsOpen(), "Zoxide modal should open even when ZoxideSupport is disabled")
315+
})
316+
}

src/internal/test_utils.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"time"
88

99
tea "github.com/charmbracelet/bubbletea"
10+
zoxidelib "github.com/lazysegtree/go-zoxide"
1011
"github.com/stretchr/testify/assert"
1112
"github.com/stretchr/testify/require"
1213

@@ -26,6 +27,13 @@ func defaultTestModel(dirs ...string) *model {
2627
return m
2728
}
2829

30+
func defaultTestModelWithZClient(zClient *zoxidelib.Client, dirs ...string) *model {
31+
m := defaultModelConfig(false, false, false, dirs, zClient)
32+
m.disableMetatdata = true
33+
_, _ = TeaUpdate(m, tea.WindowSizeMsg{Width: 2 * common.MinimumWidth, Height: 2 * common.MinimumHeight})
34+
return m
35+
}
36+
2937
// Helper function to setup panel mode and selection
3038
func setupPanelModeAndSelection(t *testing.T, m *model, useSelectMode bool, itemName string, selectedItems []string) {
3139
t.Helper()

src/internal/ui/zoxide/model.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ func (m *Model) HandleUpdate(msg tea.Msg) (common.ModelAction, tea.Cmd) {
6060
switch {
6161
case slices.Contains(common.Hotkeys.ConfirmTyping, msg.String()):
6262
action = m.handleConfirm()
63+
m.Close()
6364
case slices.Contains(common.Hotkeys.CancelTyping, msg.String()):
6465
m.Close()
6566
case slices.Contains(common.Hotkeys.ListUp, msg.String()):

src/internal/ui/zoxide/utils.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,7 @@ func (m *Model) SetMaxHeight(maxHeight int) {
5252
}
5353
m.maxHeight = maxHeight
5454
}
55+
56+
func (m *Model) GetResults() []zoxidelib.Result {
57+
return m.results
58+
}

0 commit comments

Comments
 (0)