From 2ef566975a4b5b73b2a0f053c36ed59017377028 Mon Sep 17 00:00:00 2001 From: tauraamui Date: Tue, 16 Sep 2025 17:50:44 +0100 Subject: [PATCH] test(editor): improve tests for existing behaviour inserting full path --- .../tui/components/chat/editor/editor_test.go | 184 +++++------------- 1 file changed, 45 insertions(+), 139 deletions(-) diff --git a/internal/tui/components/chat/editor/editor_test.go b/internal/tui/components/chat/editor/editor_test.go index 7a49385355ccf78cbe1392c7aa683fd09e2d1be0..ca9c26755226e8bcb6161523588a101bc5c4047d 100644 --- a/internal/tui/components/chat/editor/editor_test.go +++ b/internal/tui/components/chat/editor/editor_test.go @@ -111,6 +111,26 @@ func mockDirLister(paths []string) fsext.DirectoryListerResolver { } } +type noopEvent struct{} + +type updater interface { + Update(msg tea.Msg) (tea.Model, tea.Cmd) +} + +func simulateUpdate(up updater, msg tea.Msg) (updater, tea.Msg) { + up, cmd := up.Update(msg) + if cmd != nil { + return up, cmd() + } + return up, noopEvent{} +} + +var pngMagicNumberData = []byte("\x89PNG\x0D\x0A\x1A\x0A") + +func mockResolveAbs(path string) (string, error) { + return path, nil +} + func TestEditorTypingForwardSlashOpensCompletions(t *testing.T) { testEditor := newEditor(&app.App{}, mockDirLister([]string{})) require.NotNil(t, testEditor) @@ -153,8 +173,9 @@ func TestEditorAutocompletionWithEmptyInput(t *testing.T) { assert.Equal(t, "", testEditor.currentQuery) } -func TestEditorAutoCompletion_WIP(t *testing.T) { - testEditor := newEditor(&app.App{}, mockDirLister([]string{"file1.txt", "file2.txt"})) +func TestEditorAutoCompletion_OnNonImageFileFullPathInsertedFromQuery(t *testing.T) { + entriesForAutoComplete := mockDirLister([]string{"image.png", "random.txt"}) + testEditor := newEditor(&app.App{}, entriesForAutoComplete) require.NotNil(t, testEditor) // open the completions menu by simulating a '/' key press @@ -170,33 +191,39 @@ func TestEditorAutoCompletion_WIP(t *testing.T) { if batchMsg, ok := msg.(tea.BatchMsg); ok { // Use our enhanced helper to check for OpenCompletionsMsg with specific completions var found bool - openCompletionsMsg, found = assertBatchContainsOpenCompletionsMsg(t, batchMsg, []string{"file1.txt", "file2.txt"}) + openCompletionsMsg, found = assertBatchContainsOpenCompletionsMsg(t, batchMsg, []string{"image.png", "random.txt"}) assert.True(t, found, "Expected to find OpenCompletionsMsg with specific completions in batched messages") } else { t.Fatal("Expected BatchMsg from cmds()") } assert.NotNil(t, openCompletionsMsg) -} + require.True(t, testEditor.IsCompletionsOpen()) -type noopEvent struct{} + testEditor.textarea.SetValue("/random.tx") -type updater interface { - Update(msg tea.Msg) (tea.Model, tea.Cmd) -} + keyPressMsg = tea.KeyPressMsg{ + Text: "t", + } + m, msg = simulateUpdate(testEditor, keyPressMsg) + testEditor = m.(*editorCmp) -func simulateUpdate(up updater, msg tea.Msg) (updater, tea.Msg) { - up, cmd := up.Update(msg) - if cmd != nil { - return up, cmd() + + selectMsg := completions.SelectCompletionMsg{ + Value: FileCompletionItem{ + "./root/project/random.txt", + }, + Insert: true, } - return up, noopEvent{} -} -var pngMagicNumberData = []byte("\x89PNG\x0D\x0A\x1A\x0A") + m, msg = simulateUpdate(testEditor, selectMsg) + testEditor = m.(*editorCmp) -func mockResolveAbs(path string) (string, error) { - return path, nil + if _, ok := msg.(noopEvent); !ok { + t.Fatal("Expected noopEvent from cmds()") + } + + assert.Equal(t, "./root/project/random.txt", testEditor.textarea.Value()) } func TestEditor_OnPastePathToImageEmitsAttachFileMessage(t *testing.T) { @@ -226,7 +253,7 @@ func TestEditor_OnPastePathToImageEmitsAttachFileMessage(t *testing.T) { FilePath: "image.png", FileName: "image.png", MimeType: "image/png", - Content: pngMagicNumberData, + Content: pngMagicNumberData, }, attachmentMsg) } @@ -247,127 +274,6 @@ func TestEditor_OnPastePathToNonImageEmitsAttachFileMessage(t *testing.T) { assert.Nil(t, cmd) } -/* -func TestEditorAutocompletion_StartFilteringOpens(t *testing.T) { - testEditor := newEditor(&app.App{}, mockDirLister([]string{"file1.txt", "file2.txt"})) - require.NotNil(t, testEditor) - - // open the completions menu by simulating a '/' key press - testEditor.Focus() - keyPressMsg := tea.KeyPressMsg{ - Text: "/", - } - - m, cmds := testEditor.Update(keyPressMsg) - testEditor = m.(*editorCmp) - - msg := cmds() - var openCompletionsMsg *completions.OpenCompletionsMsg - if batchMsg, ok := msg.(tea.BatchMsg); ok { - // Use our enhanced helper to check for OpenCompletionsMsg with specific completions - var found bool - openCompletionsMsg, found = assertBatchContainsOpenCompletionsMsg(t, batchMsg, []string{"file1.txt", "file2.txt"}) - assert.True(t, found, "Expected to find OpenCompletionsMsg with specific completions in batched messages") - } else { - t.Fatal("Expected BatchMsg from cmds()") - } - - assert.NotNil(t, openCompletionsMsg) - m, cmds = testEditor.Update(openCompletionsMsg) - - msg = cmds() - testEditor = m.(*editorCmp) - - if batchMsg, ok := msg.(tea.BatchMsg); ok { - assertBatchContainsExactMessage(t, batchMsg, completions.CompletionsOpenedMsg{}) - } else { - t.Fatal("Expected BatchMsg from cmds()") - } - - // Verify completions menu is open - assert.True(t, testEditor.isCompletionsOpen) - assert.Equal(t, "/", testEditor.textarea.Value()) - - // Now simulate typing a query to filter the completions - // Set the text to "/tes" and then simulate typing "t" to make "/test" - testEditor.textarea.SetValue("/tes") - - // Simulate typing a key that would trigger filtering - keyPressMsg = tea.KeyPressMsg{ - Text: "t", - } - - m, cmds = testEditor.Update(keyPressMsg) - msg = cmds() - testEditor = m.(*editorCmp) - - // Verify the editor still has completions open - assert.True(t, testEditor.isCompletionsOpen) - - // The currentQuery should be updated based on what we typed - // In this case, it would be "test" (the word after the initial '/') - // Note: The actual filtering is handled by the completions component, - // so we're just verifying the editor's state is correct - assert.Equal(t, "test", testEditor.currentQuery) - - keyPressMsg = tea.KeyPressMsg{ - Code: tea.KeyEnter, - } - - m, cmds = testEditor.Update(keyPressMsg) - msg = cmds() - testEditor = m.(*editorCmp) - - if batchMsg, ok := msg.(tea.BatchMsg); ok { - assertBatchContainsExactMessage(t, batchMsg, completions.CompletionsOpenedMsg{}) - } else { - t.Fatal("Expected BatchMsg from cmds()") - } - - m, cmds = testEditor.Update(msg) - msg = cmds() - testEditor = m.(*editorCmp) - // Verify the editor still has completions open - assert.True(t, testEditor.isCompletionsOpen) -} -*/ - -func TestEditorAutocompletion_SelectionOfNormalPathAddsToTextAreaClosesCompletion(t *testing.T) { - testEditor := newEditor(&app.App{}, mockDirLister([]string{"example_test.go", "file1.txt", "file2.txt"})) - require.NotNil(t, testEditor) - - // open the completions menu by simulating a '/' key press - testEditor.Focus() - keyPressMsg := tea.KeyPressMsg{ - Text: "/", - } - - m, cmds := testEditor.Update(keyPressMsg) - testEditor = m.(*editorCmp) - - msg := cmds() - assert.NotNil(t, msg) - m, cmds = testEditor.Update(msg) - - // Now simulate typing a query to filter the completions - // Set the text to "/tes" and then simulate typing "t" to make "/test" - testEditor.textarea.SetValue("/tes") - - // Simulate typing a key that would trigger filtering - keyPressMsg = tea.KeyPressMsg{ - Text: "t", - } - - m, cmds = testEditor.Update(keyPressMsg) - testEditor = m.(*editorCmp) - - // The currentQuery should be updated based on what we typed - // In this case, it would be "test" (the word after the initial '/') - // Note: The actual filtering is handled by the completions component, - // so we're just verifying the editor's state is correct - assert.Equal(t, "test", testEditor.currentQuery) -} - // TestHelperFunctions demonstrates how to use the batch message helpers func TestHelperFunctions(t *testing.T) { testEditor := newEditor(&app.App{}, mockDirLister([]string{"file1.txt", "file2.txt"}))