editor_test.go

 1package editor
 2
 3import (
 4	"testing"
 5
 6	tea "github.com/charmbracelet/bubbletea/v2"
 7	"github.com/charmbracelet/crush/internal/app"
 8	"github.com/stretchr/testify/assert"
 9	"github.com/stretchr/testify/require"
10)
11
12func TestEditorTypingForwardSlashOpensCompletions(t *testing.T) {
13	testEditor := newEditor(&app.App{})
14	require.NotNil(t, testEditor)
15
16	// Simulate pressing the '/' key
17	keyPressMsg := tea.KeyPressMsg{
18		Text: "/",
19	}
20
21	m, cmds := testEditor.Update(keyPressMsg)
22	testEditor = m.(*editorCmp)
23	cmds()
24
25	assert.True(t, testEditor.isCompletionsOpen)
26	assert.Equal(t, "/", testEditor.textarea.Value())
27}
28
29func TestEditorAutocompletionWithEmptyInput(t *testing.T) {
30	testEditor := newEditor(&app.App{})
31	require.NotNil(t, testEditor)
32
33	// First, give the editor focus
34	testEditor.Focus()
35
36	// Simulate pressing the '/' key when the editor is empty
37	// This should trigger the completions to open
38	keyPressMsg := tea.KeyPressMsg{
39		Text: "/",
40	}
41
42	m, cmds := testEditor.Update(keyPressMsg)
43	testEditor = m.(*editorCmp)
44	cmds()
45
46	// Verify completions menu is open
47	assert.True(t, testEditor.isCompletionsOpen)
48	assert.Equal(t, "/", testEditor.textarea.Value())
49
50	// Verify the query is empty (since we just opened it)
51	assert.Equal(t, "", testEditor.currentQuery)
52}
53
54func TestEditorAutocompletionFiltering(t *testing.T) {
55	testEditor := newEditor(&app.App{})
56	require.NotNil(t, testEditor)
57
58	// First, open the completions menu by simulating a '/' key press
59	testEditor.Focus()
60	keyPressMsg := tea.KeyPressMsg{
61		Text: "/",
62	}
63
64	m, cmds := testEditor.Update(keyPressMsg)
65	testEditor = m.(*editorCmp)
66	cmds()
67
68	// Verify completions menu is open
69	assert.True(t, testEditor.isCompletionsOpen)
70	assert.Equal(t, "/", testEditor.textarea.Value())
71
72	// Now simulate typing a query to filter the completions
73	// Set the text to "/tes" and then simulate typing "t" to make "/test"
74	testEditor.textarea.SetValue("/tes")
75
76	// Simulate typing a key that would trigger filtering
77	keyPressMsg = tea.KeyPressMsg{
78		Text: "t",
79	}
80
81	m, cmds = testEditor.Update(keyPressMsg)
82	testEditor = m.(*editorCmp)
83	cmds()
84
85	// Verify the editor still has completions open
86	assert.True(t, testEditor.isCompletionsOpen)
87
88	// The currentQuery should be updated based on what we typed
89	// In this case, it would be "test" (the word after the initial '/')
90	// Note: The actual filtering is handled by the completions component,
91	// so we're just verifying the editor's state is correct
92	assert.Equal(t, "test", testEditor.currentQuery)
93}