Skip to content

Commit e7fa644

Browse files
committed
feat: zoxide tests
1 parent e684f77 commit e7fa644

File tree

3 files changed

+558
-0
lines changed

3 files changed

+558
-0
lines changed

src/internal/model_test.go

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"runtime"
99
"testing"
1010

11+
tea "github.com/charmbracelet/bubbletea"
1112
zoxidelib "github.com/lazysegtree/go-zoxide"
1213
"github.com/stretchr/testify/assert"
1314
"github.com/stretchr/testify/require"
@@ -248,6 +249,7 @@ func TestChooserFile(t *testing.T) {
248249
}
249250
}
250251

252+
//nolint:gocognit,maintidx // Integration test with multiple subtests for comprehensive zoxide functionality
251253
func TestZoxide(t *testing.T) {
252254
zoxideDataDir := t.TempDir()
253255
zClient, err := zoxidelib.New(zoxidelib.WithDataDir(zoxideDataDir))
@@ -354,4 +356,156 @@ func TestZoxide(t *testing.T) {
354356
results := m.zoxideModal.GetResults()
355357
assert.Empty(t, results, "Zoxide modal should show no results when ZoxideSupport is disabled")
356358
})
359+
360+
t.Run("Zoxide modal size on window resize", func(t *testing.T) {
361+
common.Config.ZoxideSupport = true
362+
m := defaultTestModelWithZClient(zClient, dir1)
363+
p := NewTestTeaProgWithEventLoop(t, m)
364+
365+
p.SendKey(common.Hotkeys.OpenZoxide[0])
366+
assert.Eventually(t, func() bool {
367+
return p.getModel().zoxideModal.IsOpen()
368+
}, DefaultTestTimeout, DefaultTestTick, "Zoxide modal should open")
369+
370+
initialWidth := p.getModel().zoxideModal.GetWidth()
371+
initialMaxHeight := p.getModel().zoxideModal.GetMaxHeight()
372+
373+
newWidth := 4 * common.MinimumWidth
374+
newHeight := 4 * common.MinimumHeight
375+
p.Send(tea.WindowSizeMsg{Width: newWidth, Height: newHeight})
376+
377+
assert.Eventually(t, func() bool {
378+
updatedWidth := p.getModel().zoxideModal.GetWidth()
379+
updatedMaxHeight := p.getModel().zoxideModal.GetMaxHeight()
380+
return updatedWidth != initialWidth && updatedMaxHeight != initialMaxHeight
381+
}, DefaultTestTimeout, DefaultTestTick, "Zoxide modal dimensions should update on window resize")
382+
383+
updatedWidth := p.getModel().zoxideModal.GetWidth()
384+
updatedMaxHeight := p.getModel().zoxideModal.GetMaxHeight()
385+
assert.Greater(t, updatedWidth, initialWidth, "Width should increase with larger window")
386+
assert.Greater(t, updatedMaxHeight, initialMaxHeight, "MaxHeight should increase with larger window")
387+
})
388+
389+
t.Run("Zoxide 'z' key suppression on open", func(t *testing.T) {
390+
common.Config.ZoxideSupport = true
391+
m := defaultTestModelWithZClient(zClient, dir1)
392+
p := NewTestTeaProgWithEventLoop(t, m)
393+
394+
p.SendKey(common.Hotkeys.OpenZoxide[0])
395+
assert.Eventually(t, func() bool {
396+
return p.getModel().zoxideModal.IsOpen()
397+
}, DefaultTestTimeout, DefaultTestTick, "Zoxide modal should open")
398+
399+
assert.Eventually(t, func() bool {
400+
return p.getModel().zoxideModal.GetTextInputValue() == ""
401+
}, DefaultTestTimeout, DefaultTestTick, "The 'z' key should not be added to textInput")
402+
403+
p.SendKey("a")
404+
p.SendKey("b")
405+
p.SendKey("c")
406+
407+
assert.Eventually(t, func() bool {
408+
return p.getModel().zoxideModal.GetTextInputValue() == "abc"
409+
}, DefaultTestTimeout, DefaultTestTick, "Subsequent keys should be added to textInput")
410+
})
411+
412+
t.Run("Multi-space directory name navigation", func(t *testing.T) {
413+
common.Config.ZoxideSupport = true
414+
multiSpaceDir := filepath.Join(curTestDir, "test dir")
415+
utils.SetupDirectories(t, multiSpaceDir)
416+
defer os.RemoveAll(multiSpaceDir)
417+
418+
m := defaultTestModelWithZClient(zClient, dir1)
419+
p := NewTestTeaProgWithEventLoop(t, m)
420+
421+
err := p.getModel().updateCurrentFilePanelDir(multiSpaceDir)
422+
require.NoError(t, err, "Failed to navigate to multi-space directory")
423+
424+
err = p.getModel().updateCurrentFilePanelDir(dir1)
425+
require.NoError(t, err, "Failed to navigate back to dir1")
426+
427+
p.SendKey(common.Hotkeys.OpenZoxide[0])
428+
assert.Eventually(t, func() bool {
429+
return p.getModel().zoxideModal.IsOpen()
430+
}, DefaultTestTimeout, DefaultTestTick, "Zoxide modal should open")
431+
432+
for _, char := range "test dir" {
433+
p.SendKey(string(char))
434+
}
435+
436+
assert.Eventually(t, func() bool {
437+
results := p.getModel().zoxideModal.GetResults()
438+
for _, result := range results {
439+
if result.Path == multiSpaceDir {
440+
return true
441+
}
442+
}
443+
return false
444+
}, DefaultTestTimeout, DefaultTestTick, "Multi-space directory should be found by zoxide")
445+
446+
results := p.getModel().zoxideModal.GetResults()
447+
multiSpaceDirIndex := -1
448+
for i, result := range results {
449+
if result.Path == multiSpaceDir {
450+
multiSpaceDirIndex = i
451+
break
452+
}
453+
}
454+
require.NotEqual(t, -1, multiSpaceDirIndex, "Multi-space directory should be in results")
455+
456+
for range multiSpaceDirIndex {
457+
p.SendKey(common.Hotkeys.ListDown[0])
458+
}
459+
460+
p.SendKey(common.Hotkeys.ConfirmTyping[0])
461+
assert.Eventually(t, func() bool {
462+
return p.getModel().getFocusedFilePanel().location == multiSpaceDir
463+
}, DefaultTestTimeout, DefaultTestTick, "Should navigate to multi-space directory")
464+
})
465+
466+
t.Run("Zoxide escape key closes modal", func(t *testing.T) {
467+
common.Config.ZoxideSupport = true
468+
m := defaultTestModelWithZClient(zClient, dir1)
469+
p := NewTestTeaProgWithEventLoop(t, m)
470+
471+
p.SendKey(common.Hotkeys.OpenZoxide[0])
472+
assert.Eventually(t, func() bool {
473+
return p.getModel().zoxideModal.IsOpen()
474+
}, DefaultTestTimeout, DefaultTestTick, "Zoxide modal should open")
475+
476+
p.SendKey(common.Hotkeys.CancelTyping[0])
477+
assert.Eventually(t, func() bool {
478+
return !p.getModel().zoxideModal.IsOpen()
479+
}, DefaultTestTimeout, DefaultTestTick, "Zoxide modal should close on escape key")
480+
})
481+
482+
t.Run("Zoxide with invalid/non-existent directory", func(t *testing.T) {
483+
common.Config.ZoxideSupport = true
484+
m := defaultTestModelWithZClient(zClient, dir1)
485+
p := NewTestTeaProgWithEventLoop(t, m)
486+
487+
currentLocation := p.getModel().getFocusedFilePanel().location
488+
489+
p.SendKey(common.Hotkeys.OpenZoxide[0])
490+
assert.Eventually(t, func() bool {
491+
return p.getModel().zoxideModal.IsOpen()
492+
}, DefaultTestTimeout, DefaultTestTick, "Zoxide modal should open")
493+
494+
for _, char := range "uniquexyzabc123" {
495+
p.SendKey(string(char))
496+
}
497+
498+
assert.Eventually(t, func() bool {
499+
results := p.getModel().zoxideModal.GetResults()
500+
return len(results) == 0
501+
}, DefaultTestTimeout, DefaultTestTick, "Should have no results for unique search")
502+
503+
p.SendKey(common.Hotkeys.ConfirmTyping[0])
504+
assert.Eventually(t, func() bool {
505+
return !p.getModel().zoxideModal.IsOpen()
506+
}, DefaultTestTimeout, DefaultTestTick, "Zoxide modal should close")
507+
508+
assert.Equal(t, currentLocation, p.getModel().getFocusedFilePanel().location,
509+
"Should stay in current location when confirming with no results")
510+
})
357511
}

src/internal/ui/zoxide/utils.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ func (m *Model) GetResults() []zoxidelib.Result {
6464
return out
6565
}
6666

67+
func (m *Model) GetTextInputValue() string {
68+
return m.textInput.Value()
69+
}
70+
6771
func isKeyAlphaNum(msg tea.KeyMsg) bool {
6872
r := []rune(msg.String())
6973
if len(r) != 1 {

0 commit comments

Comments
 (0)