summaryrefslogtreecommitdiff
path: root/internal/ui/widgets/textviewentry_test.go
blob: bbb20efbda8879056d74045be2ab3a27715cc95b (plain)
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
package widgets

import (
	"strings"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func newTVE() *TextViewEntry {
	e := &TextViewEntry{}
	e.ExtendBaseWidget(e)
	return e
}

// ── splitRaw ─────────────────────────────────────────────────────────────────

func TestSplitRaw(t *testing.T) {
	tests := []struct {
		input string
		want  []string
	}{
		{"", []string{""}},
		{"a\nb", []string{"a", "b"}},
		{"a\r\nb", []string{"a", "b"}},
		{"a\rb", []string{"a", "b"}},
		{"a\nb\nc", []string{"a", "b", "c"}},
	}

	for _, tt := range tests {
		t.Run(tt.input, func(t *testing.T) {
			got := splitRaw(tt.input)
			assert.Equal(t, tt.want, got)
		})
	}
}

// ── buildVisualLines ─────────────────────────────────────────────────────────

func TestBuildVisualLines_NoWrap(t *testing.T) {
	lines := buildVisualLines([]string{"GET /foo HTTP/1.1", "Host: example.com"}, 80)
	assert.Len(t, lines, 2)
}

func TestBuildVisualLines_Wraps(t *testing.T) {
	long := strings.Repeat("x", 100)
	lines := buildVisualLines([]string{long}, 40)

	assert.Greater(t, len(lines), 2, "expected ≥3 visual lines for 100-char line at width 40")

	for _, vl := range lines {
		assert.Equal(t, 0, vl.logicalIdx, "all chunks should map to logicalIdx 0")
	}

	expected := 0
	for _, vl := range lines {
		assert.Equal(t, expected, vl.colOffset)
		expected += len([]rune(vl.raw))
	}
}

func TestBuildVisualLines_EmptyLine(t *testing.T) {
	lines := buildVisualLines([]string{"GET /foo HTTP/1.1", "", `{"key":"val"}`}, 80)
	assert.Len(t, lines, 3)
}

func TestBuildVisualLines_EmptyInput(t *testing.T) {
	lines := buildVisualLines([]string{""}, 80)
	assert.Len(t, lines, 1)
}

// ── logicalToVisual ──────────────────────────────────────────────────────────

func TestLogicalToVisual_Simple(t *testing.T) {
	visual := buildVisualLines([]string{"hello", "world"}, 80)

	v, col := logicalToVisual(visual, 0, 0)
	assert.Equal(t, 0, v)
	assert.Equal(t, 0, col)

	v, col = logicalToVisual(visual, 1, 3)
	assert.Equal(t, 1, visual[v].logicalIdx)
	assert.Equal(t, 3, col)
}

func TestLogicalToVisual_EndOfLine(t *testing.T) {
	visual := buildVisualLines([]string{"hello"}, 80)

	v, col := logicalToVisual(visual, 0, 5)
	assert.Equal(t, 0, v)
	assert.Equal(t, 5, col)
}

func TestLogicalToVisual_WrappedLine(t *testing.T) {
	// "01234567890123456789" wrapped at 10 → chunks [0:10] [10:20]
	visual := buildVisualLines([]string{"01234567890123456789"}, 10)

	v, col := logicalToVisual(visual, 0, 11)
	assert.Equal(t, 10, visual[v].colOffset, "col 11 should land in chunk starting at offset 10")
	assert.Equal(t, 1, col)
}

func TestLogicalToVisual_EndOfWrappedLine(t *testing.T) {
	// "01234567890123456789" wrapped at 10 → chunks [0:10] [10:20]
	visual := buildVisualLines([]string{"01234567890123456789"}, 10)

	v, _ := logicalToVisual(visual, 0, 20)
	assert.Equal(t, visual[len(visual)-1].colOffset, visual[v].colOffset, "col at end should land in last chunk")
}

// ── normaliseSelection ───────────────────────────────────────────────────────

func TestNormaliseSelection_Forward(t *testing.T) {
	e := newTVE()
	e.selAnchorLine, e.selAnchorCol = 0, 2
	e.selLine, e.selCol = 1, 5

	sl, sc, el, ec := e.normaliseSelection()
	assert.Equal(t, 0, sl)
	assert.Equal(t, 2, sc)
	assert.Equal(t, 1, el)
	assert.Equal(t, 5, ec)
}

func TestNormaliseSelection_Reversed(t *testing.T) {
	e := newTVE()
	e.selAnchorLine, e.selAnchorCol = 1, 5
	e.selLine, e.selCol = 0, 2

	sl, sc, el, ec := e.normaliseSelection()
	assert.Equal(t, 0, sl)
	assert.Equal(t, 2, sc)
	assert.Equal(t, 1, el)
	assert.Equal(t, 5, ec)
}

func TestNormaliseSelection_SamePosition(t *testing.T) {
	e := newTVE()
	e.selAnchorLine, e.selAnchorCol = 0, 3
	e.selLine, e.selCol = 0, 3

	sl, sc, el, ec := e.normaliseSelection()
	assert.Equal(t, 0, sl)
	assert.Equal(t, 3, sc)
	assert.Equal(t, 0, el)
	assert.Equal(t, 3, ec)
}

// ── selectedText ─────────────────────────────────────────────────────────────

func TestSelectedText_SameLine(t *testing.T) {
	e := newTVE()
	e.mu.Lock()
	e.rawFull = "hello world"
	e.mu.Unlock()
	e.selAnchorLine, e.selAnchorCol = 0, 0
	e.selLine, e.selCol = 0, 5
	e.hasSelection = true

	assert.Equal(t, "hello", e.selectedText())
}

func TestSelectedText_MultiLine(t *testing.T) {
	e := newTVE()
	e.mu.Lock()
	e.rawFull = "line one\nline two"
	e.mu.Unlock()
	e.selAnchorLine, e.selAnchorCol = 0, 5
	e.selLine, e.selCol = 1, 4
	e.hasSelection = true

	assert.Equal(t, "one\nline", e.selectedText())
}

func TestSelectedText_NoSelection(t *testing.T) {
	e := newTVE()
	e.mu.Lock()
	e.rawFull = "hello"
	e.mu.Unlock()
	e.hasSelection = false

	assert.Equal(t, "", e.selectedText())
}

// ── deleteSelection ──────────────────────────────────────────────────────────

func TestDeleteSelection_SameLine(t *testing.T) {
	e := newTVE()
	e.mu.Lock()
	e.rawFull = "hello world"
	e.mu.Unlock()
	e.selAnchorLine, e.selAnchorCol = 0, 6
	e.selLine, e.selCol = 0, 11
	e.hasSelection = true

	lines := e.logicalLines()
	lines = e.deleteSelection(lines)
	e.commitLines(lines)

	assert.Equal(t, "hello ", e.GetText())
	assert.Equal(t, 6, e.cursorCol)
	assert.False(t, e.hasSelection)
}

func TestDeleteSelection_MultiLine(t *testing.T) {
	e := newTVE()
	e.mu.Lock()
	e.rawFull = "line one\nline two\nline three"
	e.mu.Unlock()
	e.selAnchorLine, e.selAnchorCol = 0, 5
	e.selLine, e.selCol = 2, 4
	e.hasSelection = true

	lines := e.logicalLines()
	lines = e.deleteSelection(lines)
	e.commitLines(lines)

	assert.Equal(t, "line  three", e.GetText())
}

func TestDeleteSelection_ReversedAnchor(t *testing.T) {
	e := newTVE()
	e.mu.Lock()
	e.rawFull = "hello world"
	e.mu.Unlock()
	e.selAnchorLine, e.selAnchorCol = 0, 11
	e.selLine, e.selCol = 0, 6
	e.hasSelection = true

	lines := e.logicalLines()
	lines = e.deleteSelection(lines)
	e.commitLines(lines)

	assert.Equal(t, "hello ", e.GetText())
}

func TestDeleteSelection_NoSelection(t *testing.T) {
	e := newTVE()
	e.mu.Lock()
	e.rawFull = "hello"
	e.mu.Unlock()
	e.hasSelection = false

	lines := e.logicalLines()
	lines = e.deleteSelection(lines)
	e.commitLines(lines)

	assert.Equal(t, "hello", e.GetText())
}

// ── pushUndo / undo ──────────────────────────────────────────────────────────

func TestPushUndo_StoresCursorPosition(t *testing.T) {
	e := newTVE()
	e.pushUndo("hello", 0, 3)

	require.Len(t, e.undoStack, 1)
	assert.Equal(t, "hello", e.undoStack[0].raw)
	assert.Equal(t, 0, e.undoStack[0].cursorLine)
	assert.Equal(t, 3, e.undoStack[0].cursorCol)
}

func TestPushUndo_StackLimit(t *testing.T) {
	e := newTVE()
	for i := 0; i < 110; i++ {
		e.pushUndo("x", 0, 0)
	}
	assert.LessOrEqual(t, len(e.undoStack), 100)
}

func TestUndo_RestoresTextAndCursor(t *testing.T) {
	e := newTVE()
	e.mu.Lock()
	e.rawFull = "hello"
	e.mu.Unlock()
	e.cursorLine, e.cursorCol = 0, 5

	e.pushUndo("hello", 0, 5)

	e.mu.Lock()
	e.rawFull = "hello world"
	e.visualCacheRaw = ""
	e.cursorLine = 0
	e.cursorCol = 11
	e.mu.Unlock()

	e.mu.Lock()
	undo := e.undoStack[len(e.undoStack)-1]
	e.undoStack = e.undoStack[:len(e.undoStack)-1]
	e.rawFull = undo.raw
	e.visualCacheRaw = ""
	e.cursorLine = undo.cursorLine
	e.cursorCol = undo.cursorCol
	e.mu.Unlock()

	assert.Equal(t, "hello", e.GetText())
	assert.Equal(t, 0, e.cursorLine)
	assert.Equal(t, 5, e.cursorCol)
}

// ── visual cache ─────────────────────────────────────────────────────────────

func TestVisualCache_InvalidatedOnCommit(t *testing.T) {
	e := newTVE()
	e.mu.Lock()
	e.rawFull = "hello"
	e.mu.Unlock()

	e.mu.Lock()
	e.visualCache = buildVisualLines([]string{"hello"}, 80)
	e.visualCacheRaw = "hello"
	e.visualCacheWidth = 80
	e.mu.Unlock()

	e.commitLines([]string{"world"})

	e.mu.Lock()
	cacheRaw := e.visualCacheRaw
	e.mu.Unlock()

	assert.Equal(t, "", cacheRaw, "commitLines should invalidate visual cache")
}

func TestVisualCache_HitOnSecondCall(t *testing.T) {
	e := newTVE()
	e.mu.Lock()
	e.rawFull = "GET /foo HTTP/1.1\nHost: example.com"
	e.visualCacheRaw = ""
	e.mu.Unlock()

	// Prime the cache manually without needing a renderer.
	cpl := 80
	e.mu.Lock()
	built := buildVisualLines(splitRaw(e.rawFull), cpl)
	e.visualCache = built
	e.visualCacheRaw = e.rawFull
	e.visualCacheWidth = cpl
	e.mu.Unlock()

	e.mu.Lock()
	c1 := e.visualCache
	c2 := e.visualCache
	e.mu.Unlock()

	require.NotEmpty(t, c1)
	assert.Equal(t, &c1[0], &c2[0], "expected same slice on repeated access")
}

func TestVisualCache_MissAfterMutation(t *testing.T) {
	e := newTVE()
	e.mu.Lock()
	e.rawFull = "hello"
	cpl := 80
	e.visualCache = buildVisualLines([]string{"hello"}, cpl)
	e.visualCacheRaw = "hello"
	e.visualCacheWidth = cpl
	e.mu.Unlock()

	e.commitLines([]string{"world"})

	e.mu.Lock()
	cacheRaw := e.visualCacheRaw
	e.mu.Unlock()

	assert.Equal(t, "", cacheRaw, "cache should be invalidated after mutation")
}

// ── commitLines ──────────────────────────────────────────────────────────────

func TestCommitLines_UpdatesText(t *testing.T) {
	e := newTVE()
	e.commitLines([]string{"new text"})
	assert.Equal(t, "new text", e.GetText())
}

func TestCommitLines_MultiLine(t *testing.T) {
	e := newTVE()
	e.commitLines([]string{"line one", "line two"})
	assert.Equal(t, "line one\nline two", e.GetText())
}