▍ humdrum codex / glint v1.0.2
license AGPL-3.0
2.3 KB raw
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package app

import (
	"strings"
	"testing"

	"glint/internal/editor"

	tea "github.com/charmbracelet/bubbletea"
)

// altSemicolon is the Alt+; key event that opens the spell popup.
var altSemicolon = tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune(";"), Alt: true}

func editorPos(row, col int) editor.Position { return editor.Position{Row: row, Col: col} }

func TestAltSemicolonOpensPopupOnMisspelling(t *testing.T) {
	a := newApp()
	a.setSize(100, 24)
	a.editor.SetContent([]byte("a recieve here"))
	a.editor.SetCursor(editorPos(0, 4)) // inside "recieve"

	a.handleKey(altSemicolon)
	if a.mode != ModeSpell {
		t.Fatalf("mode = %d, want ModeSpell", a.mode)
	}
	if a.spell.word != "recieve" {
		t.Errorf("popup word = %q, want recieve", a.spell.word)
	}
	if len(a.spell.options) < 3 {
		t.Errorf("want suggestions + add + ignore, got %d options", len(a.spell.options))
	}
}

func TestAltSemicolonNoMisspellingShowsStatus(t *testing.T) {
	a := newApp()
	a.setSize(100, 24)
	a.editor.SetContent([]byte("all correct words"))
	a.editor.SetCursor(editorPos(0, 1))
	a.handleKey(altSemicolon)
	if a.mode == ModeSpell {
		t.Error("popup opened with no misspelling under the cursor")
	}
}

func TestSpellPopupNumberKeyReplaces(t *testing.T) {
	a := newApp()
	a.setSize(100, 24)
	a.editor.SetContent([]byte("a recieve here"))
	a.editor.SetCursor(editorPos(0, 4))
	a.handleKey(altSemicolon)

	// The first suggestion for "recieve" is "receive"; press "1" to apply it.
	if a.spell.options[0].value != "receive" {
		t.Fatalf("first suggestion = %q, want receive", a.spell.options[0].value)
	}
	a.handleKey(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("1")})
	if a.mode != ModeEditor {
		t.Errorf("popup did not close after applying; mode = %d", a.mode)
	}
	if got := a.editor.Lines[0]; got != "a receive here" {
		t.Errorf("line after replace = %q, want \"a receive here\"", got)
	}
}

func TestSpellPopupAddKey(t *testing.T) {
	a := newApp()
	a.setSize(100, 24)
	a.editor.SetContent([]byte("my zzplonk word"))
	a.editor.SetCursor(editorPos(0, 4))
	a.handleKey(altSemicolon)
	if a.mode != ModeSpell {
		t.Fatalf("popup did not open on zzplonk")
	}
	a.handleKey(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("i")}) // ignore for session
	if a.mode != ModeEditor {
		t.Errorf("popup did not close after ignore")
	}
	if strings.Contains(a.status, "failed") {
		t.Errorf("ignore reported failure: %q", a.status)
	}
}