โ– humdrum codex / glint v1.1.2
license AGPL-3.0
8.2 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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package editor

import (
	"testing"

	tea "github.com/charmbracelet/bubbletea"
)

// enter sends an Enter key through the full HandleKey path (undo-aware), the
// same way the app drives the editor.
func enter(e *Editor) { e.HandleKey(tea.KeyMsg{Type: tea.KeyEnter}) }
func tab(e *Editor)   { e.HandleKey(tea.KeyMsg{Type: tea.KeyTab}) }
func shiftTab(e *Editor) {
	e.HandleKey(tea.KeyMsg{Type: tea.KeyShiftTab})
}

func TestEnterContinuesBulletMarker(t *testing.T) {
	e := New()
	e.SetContent([]byte("- first"))
	e.Cursor = Position{Row: 0, Col: len("- first")}
	enter(e)
	if got := e.Lines[1]; got != "- " {
		t.Fatalf("new line = %q, want %q", got, "- ")
	}
	if e.Cursor.Row != 1 || e.Cursor.Col != 2 {
		t.Fatalf("cursor = %+v, want row 1 col 2", e.Cursor)
	}
}

func TestEnterContinuesStarAndPlus(t *testing.T) {
	for _, m := range []string{"* ", "+ "} {
		e := New()
		e.SetContent([]byte(m + "x"))
		e.Cursor = Position{Row: 0, Col: len(m + "x")}
		enter(e)
		if got := e.Lines[1]; got != m {
			t.Fatalf("marker %q: new line = %q, want %q", m, got, m)
		}
	}
}

func TestEnterIncrementsOrderedNumber(t *testing.T) {
	e := New()
	e.SetContent([]byte("1. first"))
	e.Cursor = Position{Row: 0, Col: len("1. first")}
	enter(e)
	if got := e.Lines[1]; got != "2. " {
		t.Fatalf("new line = %q, want %q", got, "2. ")
	}
}

func TestEnterOrderedParenDelimiter(t *testing.T) {
	e := New()
	e.SetContent([]byte("3) third"))
	e.Cursor = Position{Row: 0, Col: len("3) third")}
	enter(e)
	if got := e.Lines[1]; got != "4) " {
		t.Fatalf("new line = %q, want %q", got, "4) ")
	}
}

func TestEnterContinuesCheckboxUnchecked(t *testing.T) {
	e := New()
	e.SetContent([]byte("- [x] done"))
	e.Cursor = Position{Row: 0, Col: len("- [x] done")}
	enter(e)
	if got := e.Lines[1]; got != "- [ ] " {
		t.Fatalf("new line = %q, want %q", got, "- [ ] ")
	}
}

func TestEnterPreservesIndentation(t *testing.T) {
	e := New()
	e.SetContent([]byte("  - nested"))
	e.Cursor = Position{Row: 0, Col: len("  - nested")}
	enter(e)
	if got := e.Lines[1]; got != "  - " {
		t.Fatalf("new line = %q, want %q", got, "  - ")
	}
}

func TestEnterSplitsContentMidItem(t *testing.T) {
	e := New()
	e.SetContent([]byte("- foobar"))
	e.Cursor = Position{Row: 0, Col: len("- foo")}
	enter(e)
	if e.Lines[0] != "- foo" || e.Lines[1] != "- bar" {
		t.Fatalf("lines = %q / %q, want %q / %q", e.Lines[0], e.Lines[1], "- foo", "- bar")
	}
}

func TestEnterOnEmptyItemExitsList(t *testing.T) {
	e := New()
	e.SetContent([]byte("- "))
	e.Cursor = Position{Row: 0, Col: 2}
	enter(e)
	if len(e.Lines) != 1 {
		t.Fatalf("line count = %d, want 1 (no new line)", len(e.Lines))
	}
	if e.Lines[0] != "" {
		t.Fatalf("line = %q, want empty (marker removed)", e.Lines[0])
	}
	if e.Cursor.Col != 0 {
		t.Fatalf("cursor col = %d, want 0", e.Cursor.Col)
	}
}

func TestEnterOnEmptyCheckboxExitsList(t *testing.T) {
	e := New()
	e.SetContent([]byte("- [ ] "))
	e.Cursor = Position{Row: 0, Col: len("- [ ] ")}
	enter(e)
	if len(e.Lines) != 1 || e.Lines[0] != "" {
		t.Fatalf("lines = %v, want one empty line", e.Lines)
	}
}

func TestEnterOnNonListInsertsPlainNewline(t *testing.T) {
	e := New()
	e.SetContent([]byte("hello"))
	e.Cursor = Position{Row: 0, Col: 5}
	enter(e)
	if len(e.Lines) != 2 || e.Lines[1] != "" {
		t.Fatalf("lines = %v, want plain split", e.Lines)
	}
}

func TestTabIndentsListItem(t *testing.T) {
	e := New()
	e.SetContent([]byte("- item"))
	e.Cursor = Position{Row: 0, Col: 2}
	tab(e)
	if e.Lines[0] != "  - item" {
		t.Fatalf("line = %q, want %q", e.Lines[0], "  - item")
	}
	if e.Cursor.Col != 4 {
		t.Fatalf("cursor col = %d, want 4", e.Cursor.Col)
	}
}

func TestTabOnNonListInsertsTab(t *testing.T) {
	e := New()
	e.SetContent([]byte("plain"))
	e.Cursor = Position{Row: 0, Col: 0}
	tab(e)
	if e.Lines[0] != "\tplain" {
		t.Fatalf("line = %q, want %q", e.Lines[0], "\tplain")
	}
}

func TestShiftTabOutdentsListItem(t *testing.T) {
	e := New()
	e.SetContent([]byte("  - item"))
	e.Cursor = Position{Row: 0, Col: 4}
	shiftTab(e)
	if e.Lines[0] != "- item" {
		t.Fatalf("line = %q, want %q", e.Lines[0], "- item")
	}
	if e.Cursor.Col != 2 {
		t.Fatalf("cursor col = %d, want 2", e.Cursor.Col)
	}
}

func TestShiftTabOutdentTabIndent(t *testing.T) {
	e := New()
	e.SetContent([]byte("\t- item"))
	e.Cursor = Position{Row: 0, Col: 3}
	shiftTab(e)
	if e.Lines[0] != "- item" {
		t.Fatalf("line = %q, want %q", e.Lines[0], "- item")
	}
}

func TestShiftTabAtZeroIndentNoop(t *testing.T) {
	e := New()
	e.SetContent([]byte("- item"))
	e.Cursor = Position{Row: 0, Col: 2}
	shiftTab(e)
	if e.Lines[0] != "- item" {
		t.Fatalf("line = %q, want unchanged", e.Lines[0])
	}
}

func TestListContinuationIsUndoable(t *testing.T) {
	e := New()
	e.SetContent([]byte("- first"))
	e.Cursor = Position{Row: 0, Col: len("- first")}
	enter(e)
	e.Undo()
	if len(e.Lines) != 1 || e.Lines[0] != "- first" {
		t.Fatalf("after undo lines = %v, want [- first]", e.Lines)
	}
}

func TestParseListItemRejectsNonList(t *testing.T) {
	for _, s := range []string{"", "plain text", "-nodash", "1.no space", "   ", "-"} {
		if _, ok := parseListItem(s); ok {
			t.Errorf("parseListItem(%q) = ok, want not a list", s)
		}
	}
}

// --- TASK-023: checkbox toggle ---

func TestToggleCheckboxFlipsUnchecked(t *testing.T) {
	e := New()
	e.SetContent([]byte("- [ ] task"))
	e.Cursor = Position{Row: 0, Col: 8} // cursor mid-content, not on brackets
	if !e.ToggleCheckbox() {
		t.Fatal("ToggleCheckbox returned false on a checkbox line")
	}
	if got := e.Lines[0]; got != "- [x] task" {
		t.Fatalf("line = %q, want %q", got, "- [x] task")
	}
	if !e.Dirty {
		t.Fatal("toggle did not mark the buffer dirty")
	}
}

func TestToggleCheckboxFlipsChecked(t *testing.T) {
	e := New()
	e.SetContent([]byte("  1. [X] done"))
	e.Cursor = Position{Row: 0, Col: 0}
	e.ToggleCheckbox()
	if got := e.Lines[0]; got != "  1. [ ] done" {
		t.Fatalf("line = %q, want %q", got, "  1. [ ] done")
	}
}

func TestToggleCheckboxNoOpOnPlainLine(t *testing.T) {
	e := New()
	e.SetContent([]byte("- a bullet, no box"))
	e.Cursor = Position{Row: 0, Col: 3}
	if e.ToggleCheckbox() {
		t.Fatal("ToggleCheckbox returned true on a non-checkbox line")
	}
	if got := e.Lines[0]; got != "- a bullet, no box" {
		t.Fatalf("line mutated to %q", got)
	}
	if e.Dirty {
		t.Fatal("no-op toggle marked the buffer dirty")
	}
}

func TestOnCheckboxBracketDetectsGlyph(t *testing.T) {
	e := New()
	e.SetContent([]byte("- [ ] task"))
	// "- [ ] task": brackets at rune cols 2,3,4.
	e.Cursor = Position{Row: 0, Col: 3}
	if !e.OnCheckboxBracket() {
		t.Fatal("cursor on the box glyph not detected")
	}
	e.Cursor = Position{Row: 0, Col: 8}
	if e.OnCheckboxBracket() {
		t.Fatal("cursor in the content wrongly detected as on the box")
	}
}

// --- TASK-039: checkboxes inside blockquotes / callouts (> - [ ]) ---

func TestToggleCheckboxInBlockquote(t *testing.T) {
	e := New()
	e.SetContent([]byte("> - [ ] Call Costco"))
	e.Cursor = Position{Row: 0, Col: 10} // mid-content
	if !e.ToggleCheckbox() {
		t.Fatal("ToggleCheckbox returned false on a blockquote checkbox line")
	}
	if got := e.Lines[0]; got != "> - [x] Call Costco" {
		t.Fatalf("line = %q, want %q", got, "> - [x] Call Costco")
	}
}

func TestToggleCheckboxNestedBlockquote(t *testing.T) {
	e := New()
	e.SetContent([]byte("> > 1. [x] deep"))
	e.Cursor = Position{Row: 0, Col: 0}
	if !e.ToggleCheckbox() {
		t.Fatal("ToggleCheckbox returned false on a nested blockquote checkbox")
	}
	if got := e.Lines[0]; got != "> > 1. [ ] deep" {
		t.Fatalf("line = %q, want %q", got, "> > 1. [ ] deep")
	}
}

func TestOnCheckboxBracketInBlockquote(t *testing.T) {
	e := New()
	e.SetContent([]byte("> - [ ] task"))
	// "> - [ ] task": '[' at rune col 4.
	e.Cursor = Position{Row: 0, Col: 5}
	if !e.OnCheckboxBracket() {
		t.Fatal("cursor on the box glyph inside a blockquote not detected")
	}
}

func TestBlockquoteListContinuationKeepsQuote(t *testing.T) {
	e := New()
	e.SetContent([]byte("> - first"))
	e.Cursor = Position{Row: 0, Col: len("> - first")}
	enter(e)
	if got := e.Lines[1]; got != "> - " {
		t.Fatalf("continued line = %q, want %q", got, "> - ")
	}
}

func TestCalloutHeaderIsNotAListItem(t *testing.T) {
	// The callout header line itself has no marker after the '>' โ€” not a list.
	for _, s := range []string{"> [!Tasks]-", "> just a quote", ">"} {
		if _, ok := parseListItem(s); ok {
			t.Errorf("parseListItem(%q) = ok, want not a list", s)
		}
	}
}