turbo-editors/turbo-corepublic Fork 0
v1.0.1
Commits
Clone
git clone https://git.rickub.com/turbo-editors/turbo-core.git
git clone ssh://git@rickub.com/turbo-editors/turbo-core.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

📦 Turbo Core f3ade8d · on v1.0.1 · k33g · 8h ago
view_test.go · 399 lines · 11.2 KBGo Blame HistoryRaw
  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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
package filetree

import (
	"path/filepath"
	"strings"
	"testing"

	"github.com/gdamore/tcell/v2"

	"rickub.com/turbo-editors/turbo-core/theme"
	"rickub.com/turbo-editors/turbo-core/ui"
)

// newTestView returns a focused view on a project, sized to fit.
func newTestView(t *testing.T, root string, width, height int) *View {
	t.Helper()

	v, err := NewView(root)
	if err != nil {
		t.Fatalf("NewView(%q) error = %v", root, err)
	}
	v.SetFocused(true)
	v.SetBounds(ui.Rect{W: width, H: height})
	return v
}

// drawnRows paints a view onto a simulated screen and returns what it shows,
// trimmed of the scroll bar and trailing blanks.
func drawnRows(t *testing.T, v *View) []string {
	t.Helper()

	screen := tcell.NewSimulationScreen("UTF-8")
	if err := screen.Init(); err != nil {
		t.Fatalf("initialising the simulation screen: %v", err)
	}
	t.Cleanup(screen.Fini)

	bounds := v.Bounds()
	screen.SetSize(bounds.W, bounds.H)
	v.Draw(ui.NewPainter(screen), theme.Default(""))
	screen.Show()

	cells, width, height := screen.GetContents()
	out := make([]string, height)
	for row := range height {
		var text strings.Builder
		for col := range width - 1 { // the last column is the scroll bar
			runes := cells[row*width+col].Runes
			if len(runes) == 0 {
				text.WriteRune(' ')
				continue
			}
			text.WriteRune(runes[0])
		}
		out[row] = strings.TrimRight(text.String(), " ")
	}
	return out
}

// press sends a key to the view.
func press(v *View, key tcell.Key) bool {
	return v.HandleKey(tcell.NewEventKey(key, 0, tcell.ModNone))
}

func TestTheViewDrawsMarkersAndIndentation(t *testing.T) {
	v := newTestView(t, makeTree(t, "internal/app/app.go", "main.go"), 30, 8)
	find2(t, v, "internal").Expand()

	rows := drawnRows(t, v)

	want := []string{"▼ internal", "  ▶ app", "  main.go"}
	for i, line := range want {
		if rows[i] != line {
			t.Errorf("row %d is %q, want %q (whole view:\n%s)", i, rows[i], line, strings.Join(rows, "\n"))
		}
	}
}

func TestTheTitleIsTheProjectDirectory(t *testing.T) {
	root := makeTree(t, "main.go")
	v := newTestView(t, root, 30, 8)

	if got := v.Title(); got != filepath.Base(root) {
		t.Errorf("Title() = %q, want %q", got, filepath.Base(root))
	}
	if got := v.Root(); got != root {
		t.Errorf("Root() = %q, want %q", got, root)
	}
}

func TestTheArrowsWalkTheRows(t *testing.T) {
	v := newTestView(t, makeTree(t, "a.go", "b.go", "c.go"), 30, 8)

	press(v, tcell.KeyDown)
	press(v, tcell.KeyDown)
	if got := v.Selected().Name(); got != "c.go" {
		t.Errorf("after two downs the highlight is on %q, want c.go", got)
	}

	press(v, tcell.KeyUp)
	if got := v.Selected().Name(); got != "b.go" {
		t.Errorf("after an up the highlight is on %q, want b.go", got)
	}
}

func TestTheArrowsStopAtEitherEnd(t *testing.T) {
	v := newTestView(t, makeTree(t, "a.go", "b.go"), 30, 8)

	for range 5 {
		press(v, tcell.KeyUp)
	}
	if got := v.Selected().Name(); got != "a.go" {
		t.Errorf("the highlight ran off the top onto %q", got)
	}
	for range 5 {
		press(v, tcell.KeyDown)
	}
	if got := v.Selected().Name(); got != "b.go" {
		t.Errorf("the highlight ran off the bottom onto %q", got)
	}
}

func TestHomeAndEndGoToTheEnds(t *testing.T) {
	v := newTestView(t, makeTree(t, "a.go", "b.go", "c.go"), 30, 8)

	press(v, tcell.KeyEnd)
	if got := v.Selected().Name(); got != "c.go" {
		t.Errorf("End put the highlight on %q", got)
	}
	press(v, tcell.KeyHome)
	if got := v.Selected().Name(); got != "a.go" {
		t.Errorf("Home put the highlight on %q", got)
	}
}

func TestRightOpensAClosedDirectoryAndThenStepsIntoIt(t *testing.T) {
	v := newTestView(t, makeTree(t, "internal/app.go"), 30, 8)

	press(v, tcell.KeyRight)
	if !v.Selected().Expanded() {
		t.Fatal("right did not open the directory")
	}

	press(v, tcell.KeyRight)
	if got := v.Selected().Name(); got != "app.go" {
		t.Errorf("a second right put the highlight on %q, want app.go", got)
	}
}

func TestLeftClosesAnOpenDirectoryAndThenStepsOut(t *testing.T) {
	v := newTestView(t, makeTree(t, "internal/app.go"), 30, 8)
	press(v, tcell.KeyRight) // open internal
	press(v, tcell.KeyRight) // step onto app.go

	press(v, tcell.KeyLeft)
	if got := v.Selected().Name(); got != "internal" {
		t.Fatalf("left from a file put the highlight on %q, want its directory", got)
	}

	press(v, tcell.KeyLeft)
	if v.Selected().Expanded() {
		t.Error("a second left did not close the directory")
	}
}

func TestChoosingAFileHandsItsPathOver(t *testing.T) {
	root := makeTree(t, "main.go")
	v := newTestView(t, root, 30, 8)

	var opened []string
	v.OnOpen = func(path string) { opened = append(opened, path) }
	press(v, tcell.KeyEnter)

	if len(opened) != 1 || opened[0] != filepath.Join(root, "main.go") {
		t.Errorf("OnOpen saw %v, want the one file's absolute path", opened)
	}
}

func TestChoosingADirectoryOpensItRatherThanTheFile(t *testing.T) {
	v := newTestView(t, makeTree(t, "internal/app.go"), 30, 8)

	opened := 0
	v.OnOpen = func(string) { opened++ }
	press(v, tcell.KeyEnter)

	if opened != 0 {
		t.Error("choosing a directory was reported as opening a file")
	}
	if !v.Selected().Expanded() {
		t.Error("choosing a directory did not open it")
	}
}

func TestChoosingWithNoOnOpenDoesNotPanic(t *testing.T) {
	v := newTestView(t, makeTree(t, "main.go"), 30, 8)

	press(v, tcell.KeyEnter) // nobody is listening
}

func TestAnEmptyProjectHandlesKeysWithoutPanicking(t *testing.T) {
	v := newTestView(t, t.TempDir(), 30, 8)

	for _, key := range []tcell.Key{tcell.KeyDown, tcell.KeyUp, tcell.KeyLeft, tcell.KeyRight, tcell.KeyEnter, tcell.KeyEnd} {
		press(v, key)
	}
	if v.Selected() != nil {
		t.Error("an empty project reported a highlighted node")
	}
}

func TestCollapsingABranchKeepsTheHighlightOnARow(t *testing.T) {
	// The highlight was below the branch that just closed, so it would
	// otherwise be left pointing past the last row.
	v := newTestView(t, makeTree(t, "internal/a.go", "internal/b.go", "internal/c.go"), 30, 8)
	press(v, tcell.KeyRight) // open internal
	press(v, tcell.KeyEnd)   // onto c.go, the last row

	v.Selected() // c.go
	press(v, tcell.KeyHome)
	press(v, tcell.KeyLeft) // close internal

	if got := v.Selected().Name(); got != "internal" {
		t.Errorf("the highlight is on %q after collapsing", got)
	}
}

func TestF5AndCtrlRRefreshTheTree(t *testing.T) {
	for _, key := range []tcell.Key{tcell.KeyF5, tcell.KeyCtrlR} {
		t.Run(tcell.KeyNames[key], func(t *testing.T) {
			root := makeTree(t, "main.go")
			v := newTestView(t, root, 30, 8)
			writeFile(t, filepath.Join(root, "other.go"))

			press(v, key)

			if len(v.tree.Rows()) != 2 {
				t.Errorf("the tree shows %d rows after a refresh, want 2", len(v.tree.Rows()))
			}
		})
	}
}

func TestRefreshKeepsTheHighlightOnTheSameFile(t *testing.T) {
	root := makeTree(t, "b.go", "c.go")
	v := newTestView(t, root, 30, 8)
	press(v, tcell.KeyDown) // onto c.go

	writeFile(t, filepath.Join(root, "a.go")) // sorts before both
	v.Refresh()

	if got := v.Selected().Name(); got != "c.go" {
		t.Errorf("the highlight moved to %q; a new file above it must not carry it along", got)
	}
}

func TestRefreshKeepsTheHighlightInRangeWhenItsFileHasGone(t *testing.T) {
	root := makeTree(t, "a.go", "b.go")
	v := newTestView(t, root, 30, 8)
	press(v, tcell.KeyEnd) // onto b.go

	removeFile(t, filepath.Join(root, "b.go"))
	v.Refresh()

	if got := v.Selected(); got == nil || got.Name() != "a.go" {
		t.Errorf("after its file went the highlight is on %v, want the row that is left", got)
	}
}

func TestAnUnfocusedTreeTakesNoKeys(t *testing.T) {
	v := newTestView(t, makeTree(t, "a.go", "b.go"), 30, 8)
	v.SetFocused(false)

	if press(v, tcell.KeyDown) {
		t.Error("an unfocused tree consumed a key")
	}
	if got := v.Selected().Name(); got != "a.go" {
		t.Errorf("an unfocused tree moved its highlight onto %q", got)
	}
}

func TestClickingARowSelectsItAndClickingAgainChoosesIt(t *testing.T) {
	root := makeTree(t, "a.go", "b.go")
	v := newTestView(t, root, 30, 8)

	var opened []string
	v.OnOpen = func(path string) { opened = append(opened, path) }

	v.HandleMouse(clickAt(1, 1)) // the second row
	if got := v.Selected().Name(); got != "b.go" {
		t.Fatalf("clicking row 1 selected %q", got)
	}
	if len(opened) != 0 {
		t.Error("the first click already opened the file")
	}

	v.HandleMouse(clickAt(1, 1))
	if len(opened) != 1 || opened[0] != filepath.Join(root, "b.go") {
		t.Errorf("the second click gave %v", opened)
	}
}

func TestTheWheelScrollsTheHighlight(t *testing.T) {
	v := newTestView(t, makeTree(t, "a.go", "b.go", "c.go", "d.go", "e.go"), 30, 8)

	v.HandleMouse(wheelAt(0, 0, tcell.WheelDown))
	if got := v.Selected().Name(); got != "d.go" {
		t.Errorf("a wheel notch moved onto %q, want three rows down", got)
	}
	v.HandleMouse(wheelAt(0, 0, tcell.WheelUp))
	if got := v.Selected().Name(); got != "a.go" {
		t.Errorf("a wheel notch back moved onto %q", got)
	}
}

func TestAClickOutsideTheViewIsNotClaimed(t *testing.T) {
	v := newTestView(t, makeTree(t, "a.go"), 30, 8)
	v.SetBounds(ui.Rect{X: 5, Y: 5, W: 10, H: 4})

	if v.HandleMouse(clickAt(0, 0)) {
		t.Error("the view claimed a click that landed outside it")
	}
}

func TestALongTreeScrollsToKeepTheHighlightVisible(t *testing.T) {
	var files []string
	for _, name := range []string{"a", "b", "c", "d", "e", "f", "g", "h"} {
		files = append(files, name+".go")
	}
	v := newTestView(t, makeTree(t, files...), 30, 3) // only three rows fit

	press(v, tcell.KeyEnd)
	rows := drawnRows(t, v)

	if !strings.Contains(strings.Join(rows, "\n"), "h.go") {
		t.Errorf("the highlighted row is not on screen:\n%s", strings.Join(rows, "\n"))
	}
}

func TestTheHighlightIsVisibleInEveryShippedTheme(t *testing.T) {
	// These keys exist because list.selected is coloured against a dialog, and
	// on a window body it would be navy on navy. A selection nobody can see is
	// the exact failure this guards.
	const minimumDistance = 64

	for _, name := range theme.Available("") {
		t.Run(name, func(t *testing.T) {
			th, err := theme.Load(name, "")
			if err != nil {
				t.Fatalf("Load(%q) error = %v", name, err)
			}

			_, text, _ := th.Style(theme.KeyTreeText).Decompose()
			_, selected, _ := th.Style(theme.KeyTreeSelected).Decompose()
			if got := channelDistance(text, selected); got < minimumDistance {
				t.Errorf("tree.selected is %d channel values from tree.text, want at least %d",
					got, minimumDistance)
			}
		})
	}
}

// channelDistance returns how far apart two colours are on their furthest
// channel, in 0-255 values.
func channelDistance(a, b tcell.Color) int32 {
	ar, ag, ab := a.RGB()
	br, bg, bb := b.RGB()

	distance := int32(0)
	for _, pair := range [][2]int32{{ar, br}, {ag, bg}, {ab, bb}} {
		if difference := max(pair[0]-pair[1], pair[1]-pair[0]); difference > distance {
			distance = difference
		}
	}
	return distance
}

// clickAt returns a left-button press at a position.
func clickAt(x, y int) *tcell.EventMouse {
	return tcell.NewEventMouse(x, y, tcell.Button1, tcell.ModNone)
}

// wheelAt returns a wheel event at a position.
func wheelAt(x, y int, buttons tcell.ButtonMask) *tcell.EventMouse {
	return tcell.NewEventMouse(x, y, buttons, tcell.ModNone)
}

// find2 returns the visible node with a name, for tests that need to reach
// into the tree behind the view.
func find2(t *testing.T, v *View, name string) *Node {
	t.Helper()

	for _, row := range v.tree.Rows() {
		if row.Node.Name() == name {
			return row.Node
		}
	}
	t.Fatalf("no visible row is called %q", name)
	return nil
}