▍ humdrum codex / glint v1.1.2
license AGPL-3.0
3.5 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
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package dialog

import (
	"strings"
	"testing"

	"glint/internal/theme"

	"github.com/charmbracelet/bubbles/cursor"
	tea "github.com/charmbracelet/bubbletea"
	"github.com/charmbracelet/lipgloss"
)

func newModel() *Model { return New(theme.FlexokiDark()) }

func typeRunes(m *Model, s string) {
	for _, r := range s {
		m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{r}})
	}
}

func TestOpenSetsTitleAndPrefill(t *testing.T) {
	m := newModel()
	m.Open("Rename", "~/Notes/", "my-note")
	if m.Title() != "Rename" {
		t.Errorf("Title() = %q, want Rename", m.Title())
	}
	if m.Value() != "my-note" {
		t.Errorf("Value() = %q, want my-note", m.Value())
	}
}

func TestTypingAppendsAfterPrefill(t *testing.T) {
	m := newModel()
	m.Open("New note", "~/Notes/", "draft")
	typeRunes(m, "-2")
	if m.Value() != "draft-2" {
		t.Errorf("Value() = %q, want draft-2 (cursor must start at end of prefill)", m.Value())
	}
}

func TestEnterConfirms(t *testing.T) {
	m := newModel()
	m.Open("New note", "~/Notes/", "")
	typeRunes(m, "hello")
	res, _ := m.Update(tea.KeyMsg{Type: tea.KeyEnter})
	if res != ResultConfirm {
		t.Errorf("Enter gave %v, want ResultConfirm", res)
	}
	if m.Value() != "hello" {
		t.Errorf("Value() = %q, want hello", m.Value())
	}
}

func TestEscCancels(t *testing.T) {
	m := newModel()
	m.Open("New note", "~/Notes/", "")
	res, _ := m.Update(tea.KeyMsg{Type: tea.KeyEsc})
	if res != ResultCancel {
		t.Errorf("Esc gave %v, want ResultCancel", res)
	}
}

func TestOrdinaryKeysReturnNone(t *testing.T) {
	m := newModel()
	m.Open("New note", "~/Notes/", "")
	res, _ := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'x'}})
	if res != ResultNone {
		t.Errorf("rune key gave %v, want ResultNone", res)
	}
}

func TestValueTrimsSurroundingSpace(t *testing.T) {
	m := newModel()
	m.Open("New note", "~/Notes/", "")
	typeRunes(m, "  spaced  ")
	if m.Value() != "spaced" {
		t.Errorf("Value() = %q, want spaced", m.Value())
	}
}

func TestOpenClearsPreviousError(t *testing.T) {
	m := newModel()
	m.Open("New note", "~/Notes/", "")
	m.SetError("Name taken")
	m.Open("New note", "~/Notes/", "")
	if m.err != "" {
		t.Errorf("err = %q after reopen, want empty", m.err)
	}
}

func TestTypingClearsError(t *testing.T) {
	m := newModel()
	m.Open("New note", "~/Notes/", "")
	m.SetError("Type a name first")
	typeRunes(m, "a")
	if m.err != "" {
		t.Errorf("err = %q after typing, want empty", m.err)
	}
}

func TestErrorSurvivesNonKeyMessages(t *testing.T) {
	m := newModel()
	m.Open("New note", "~/Notes/", "")
	m.SetError("Type a name first")
	m.Update(cursor.BlinkMsg{})
	if m.err == "" {
		t.Error("err cleared by a cursor blink; only typing should clear it")
	}
}

func TestViewShowsTitleDirAndFooter(t *testing.T) {
	m := newModel()
	m.SetWidth(60)
	m.Open("New note", "~/Humdrum/Inbox/", "")
	out := m.View()
	for _, want := range []string{"New note", "~/Humdrum/Inbox/", "Enter", "Esc"} {
		if !strings.Contains(out, want) {
			t.Errorf("View() missing %q:\n%s", want, out)
		}
	}
}

func TestViewShowsErrorWhenSet(t *testing.T) {
	m := newModel()
	m.SetWidth(60)
	m.Open("New note", "~/Notes/", "")
	if strings.Contains(m.View(), "Name taken") {
		t.Fatal("View() shows the error before SetError")
	}
	m.SetError("Name taken")
	if !strings.Contains(m.View(), "Name taken") {
		t.Errorf("View() missing the error line:\n%s", m.View())
	}
}

func TestViewFitsContentWidth(t *testing.T) {
	m := newModel()
	m.SetWidth(40)
	m.Open("New note", "~/Notes/", "")
	for i, ln := range strings.Split(m.View(), "\n") {
		if w := lipgloss.Width(ln); w > 40 {
			t.Errorf("line %d width = %d, want <= 40", i, w)
		}
	}
}