Pad/internal/editor/cursor_test.go
Greg Pomerantz 0dac354f2a fix(editor): wire key events to cursor movement
- Replace gtx.Event(nil) with key.Filter{Focus: focusedID} and
  key.FocusFilter{Target: focusedID} so Gio correctly routes key
  and edit events to the focused editor text field.

- Convert EditorState.CursorPosition (byte offset) to line/column
  coordinates for accurate cursor rendering.

- Add debug logging in HandleKeyDown and HandleCursorMove.
2026-06-03 07:41:18 -04:00

53 lines
1.3 KiB
Go

package editor
import (
"testing"
"pad/internal/ui"
)
// TestCursorExistsInLayout checks if the EditorLayout produces a cursor element.
func TestCursorExistsInLayout(t *testing.T) {
state := NewState()
TheState = state
// Set to EditorPage
state.page = EditorPage
elems := EditorLayout(ui.Dp(800), ui.Dp(600), false)
cursorFound := false
for _, e := range elems {
// Assuming we add a Type() method to ui.Element interface later.
// For now, this will fail to compile if Type() doesn't exist.
// Let's assume we can cast or inspect the element.
if e.Type() == "cursor" {
cursorFound = true
break
}
}
if !cursorFound {
t.Error("Expected cursor element in EditorLayout")
}
}
// TestKeyDownMove verifies key down events update CursorPosition.
func TestKeyDownMove(t *testing.T) {
state := NewState()
TheState = state
state.Editor.Buffer = "Hello"
state.Editor.CursorPosition = 1
// Simulate Right Arrow
HandleKeyDown("Right")
if state.Editor.CursorPosition != 2 {
t.Errorf("Expected cursor position 2, got %d", state.Editor.CursorPosition)
}
// Simulate Left Arrow
HandleKeyDown("Left")
if state.Editor.CursorPosition != 1 {
t.Errorf("Expected cursor position 1, got %d", state.Editor.CursorPosition)
}
}