aboutsummaryrefslogtreecommitdiff
path: root/internal/tui/reader_test.go
blob: f3e43d5759f31cc73fb1537efa4ad206816f4d03 (plain) (blame)
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package tui

import (
	"strings"
	"testing"

	tea "github.com/charmbracelet/bubbletea"

	"github.com/lukaszkasprzak/lectio/internal/bible"
	"github.com/lukaszkasprzak/lectio/internal/bookmarks"
	"github.com/lukaszkasprzak/lectio/internal/config"
)

func enReader(t *testing.T) ReaderModel {
	t.Helper()
	t.Setenv("XDG_DATA_HOME", t.TempDir()) // isolate bookmarks + last-place files
	tbl, err := bible.LoadBookTable(nil)
	if err != nil {
		t.Fatal(err)
	}
	cfg := config.Default()
	cfg.UILanguage = "en"
	cfg.SiglaStyle = "english"
	cfg.Versions = []string{"bt", "wuj", "vul", "grb", "drb"}
	return NewReader(cfg, tbl, bookmarks.Open())
}

func key(m ReaderModel, k tea.KeyMsg) ReaderModel {
	nm, _ := m.Update(k)
	return nm.(ReaderModel)
}

func runes(s string) tea.KeyMsg { return tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune(s)} }

func win(m ReaderModel, w, h int) ReaderModel {
	nm, _ := m.Update(tea.WindowSizeMsg{Width: w, Height: h})
	return nm.(ReaderModel)
}

func TestReaderInit(t *testing.T) {
	m := enReader(t)
	if m.mode != modePick {
		t.Errorf("mode = %v want modePick", m.mode)
	}
	if len(m.matches) != len(m.books) || len(m.books) != 73 {
		t.Errorf("matches=%d books=%d", len(m.matches), len(m.books))
	}
	if len(m.versions) == 0 || m.versions[0] == "bt" {
		t.Errorf("versions = %v (bt must be excluded, non-empty)", m.versions)
	}
}

func TestReaderFilterAndOpen(t *testing.T) {
	m := enReader(t)
	nm, _ := m.Update(tea.WindowSizeMsg{Width: 80, Height: 24})
	m = nm.(ReaderModel)
	m = key(m, runes("jn"))
	if len(m.matches) == 0 || m.books[m.matches[0]].Canonical != "John" {
		t.Fatalf("filter 'jn' top = %v", m.books[m.matches[m.pickSel]])
	}
	m = key(m, tea.KeyMsg{Type: tea.KeyEnter})
	if m.mode != modeRead {
		t.Fatalf("did not enter read mode")
	}
	if m.books[m.bookIdx].Canonical != "John" || len(m.verses) == 0 {
		t.Errorf("opened book=%q verses=%d", m.books[m.bookIdx].Canonical, len(m.verses))
	}
	if !strings.Contains(m.View(), "1") { // chapter 1 header
		t.Errorf("read view missing chapter:\n%s", m.View())
	}
}

func TestReaderChapterAndVersion(t *testing.T) {
	m := enReader(t)
	nm, _ := m.Update(tea.WindowSizeMsg{Width: 80, Height: 24})
	m = nm.(ReaderModel)
	m = key(m, runes("jn"))
	m = key(m, tea.KeyMsg{Type: tea.KeyEnter})
	firstChap := m.chapters[m.chapPos]
	m = key(m, tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("n")}) // next chapter
	if m.chapters[m.chapPos] <= firstChap {
		t.Errorf("chapter did not advance: %d -> %d", firstChap, m.chapters[m.chapPos])
	}
	v0 := m.verIdx
	m = key(m, tea.KeyMsg{Type: tea.KeyTab})
	if m.verIdx == v0 {
		t.Errorf("version did not cycle")
	}
	// esc returns to picker
	m = key(m, tea.KeyMsg{Type: tea.KeyEsc})
	if m.mode != modePick {
		t.Errorf("esc did not return to picker")
	}
}

func TestReaderFuzzyScore(t *testing.T) {
	if s, ok := fuzzyScore("gen", "Gen Genesis"); !ok || s < 500 {
		t.Errorf("substring score = %d,%v", s, ok)
	}
	if _, ok := fuzzyScore("xyz", "Gen Genesis"); ok {
		t.Error("xyz should not match Genesis")
	}
	// subsequence: g..s..s across the string
	if _, ok := fuzzyScore("gss", "Gen Genesis"); !ok {
		t.Error("gss should subsequence-match")
	}
}

func TestReaderBookmarkFlow(t *testing.T) {
	m := enReader(t)
	m = win(m, 80, 24)
	m = key(m, runes("jn"))
	m = key(m, tea.KeyMsg{Type: tea.KeyEnter}) // open John 1
	if m.mode != modeRead {
		t.Fatal("did not enter reading")
	}
	// mark: m -> pick a verse -> note box -> enter
	m = key(m, runes("m"))
	if m.mode != modeMarkVerse {
		t.Fatal("m did not enter the verse picker")
	}
	m = key(m, runes("j")) // move the cursor down to John 1:2
	m = key(m, tea.KeyMsg{Type: tea.KeyEnter})
	if m.mode != modeMark {
		t.Fatal("enter did not open the note box")
	}
	m = key(m, runes("hi"))
	m = key(m, tea.KeyMsg{Type: tea.KeyEnter})
	if m.mode != modeRead {
		t.Fatal("enter did not save + return to reading")
	}
	// list: b
	m = key(m, runes("b"))
	if m.mode != modeBookmarks || len(m.marks) != 1 {
		t.Fatalf("bookmarks mode=%v n=%d", m.mode, len(m.marks))
	}
	if m.marks[0].Book != "John" || m.marks[0].Verse != 2 || m.marks[0].Note != "hi" {
		t.Errorf("mark = %+v (want John 1:2 note hi)", m.marks[0])
	}
	if !strings.Contains(m.View(), "John") {
		t.Errorf("bookmarks view missing John:\n%s", m.View())
	}
	// open the bookmark
	m = key(m, tea.KeyMsg{Type: tea.KeyEnter})
	if m.mode != modeRead || m.books[m.bookIdx].Canonical != "John" {
		t.Errorf("jump failed: mode=%v book=%q", m.mode, m.books[m.bookIdx].Canonical)
	}
	// delete via list, with confirmation
	m = key(m, runes("b"))
	m = key(m, runes("d"))
	if !m.confirmDelete {
		t.Fatal("d did not ask for confirmation")
	}
	m = key(m, runes("n")) // cancel
	if m.confirmDelete || len(m.marks) != 1 {
		t.Fatalf("n did not cancel (confirm=%v n=%d)", m.confirmDelete, len(m.marks))
	}
	m = key(m, runes("d"))
	m = key(m, runes("y")) // confirm
	if len(m.marks) != 0 {
		t.Errorf("delete failed, %d remaining", len(m.marks))
	}
}

func TestReaderRemembersPlace(t *testing.T) {
	dir := t.TempDir()
	t.Setenv("XDG_DATA_HOME", dir)
	tbl, _ := bible.LoadBookTable(nil)
	cfg := config.Default()
	cfg.UILanguage = "en"
	cfg.SiglaStyle = "english"
	cfg.Versions = []string{"wuj", "vul", "grb", "drb"}

	m := NewReader(cfg, tbl, bookmarks.Open())
	m = win(m, 80, 24)
	m = key(m, runes("jn"))
	m = key(m, tea.KeyMsg{Type: tea.KeyEnter}) // John 1 (openBook saves place)
	m = key(m, runes("n"))                     // -> John 2 (chapterStep saves place)

	m2 := NewReader(cfg, tbl, bookmarks.Open())
	if m2.mode != modeRead {
		t.Fatalf("did not restore reading mode")
	}
	if m2.books[m2.bookIdx].Canonical != "John" {
		t.Errorf("restored book = %q want John", m2.books[m2.bookIdx].Canonical)
	}
	if m2.currentChapter() != 2 {
		t.Errorf("restored chapter = %d want 2", m2.currentChapter())
	}
}