turbo-editors/turbo-corepublic Fork 0
v1.0.0
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.0 · k33g · 11h ago
dialogs_test.go · 496 lines · 14.9 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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
package app

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

	"github.com/gdamore/tcell/v2"

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

// testDirectory builds a directory holding the given entries; a name ending in
// a slash becomes a subdirectory.
func testDirectory(t *testing.T, names ...string) string {
	t.Helper()

	root := t.TempDir()
	for _, name := range names {
		path := filepath.Join(root, strings.TrimSuffix(name, "/"))
		var err error
		if strings.HasSuffix(name, "/") {
			err = os.Mkdir(path, 0o755)
		} else {
			err = os.WriteFile(path, nil, 0o644)
		}
		if err != nil {
			t.Fatalf("creating %s: %v", name, err)
		}
	}
	return root
}

func TestTheFileDialogListsDirectoriesFirst(t *testing.T) {
	root := testDirectory(t, "zeta.go", "alpha/", "beta.go", ".hidden")

	dialog := NewFileDialog("Open", root, ui.Rect{W: 80, H: 24})

	want := []string{".." + string(filepath.Separator), "alpha" + string(filepath.Separator), "beta.go", "zeta.go"}
	got := dialog.list.Items()
	if len(got) != len(want) {
		t.Fatalf("the list holds %v, want %v", got, want)
	}
	for i, name := range want {
		if got[i] != name {
			t.Errorf("entry %d is %q, want %q", i, got[i], name)
		}
	}
}

func TestTheFileDialogHidesDotFiles(t *testing.T) {
	root := testDirectory(t, ".git/", ".env", "main.go")

	dialog := NewFileDialog("Open", root, ui.Rect{W: 80, H: 24})

	for _, entry := range dialog.list.Items() {
		if strings.HasPrefix(entry, ".") && entry != ".."+string(filepath.Separator) {
			t.Errorf("the list shows %q, want dot entries hidden", entry)
		}
	}
}

func TestTheFileDialogOpensBesideTheGivenFile(t *testing.T) {
	root := testDirectory(t, "main.go")
	path := filepath.Join(root, "main.go")

	dialog := NewFileDialog("Save as", path, ui.Rect{W: 80, H: 24})

	if dialog.directory != root {
		t.Errorf("the dialog opened in %q, want %q", dialog.directory, root)
	}
	if got := dialog.name.Text(); got != "main.go" {
		t.Errorf("the name field holds %q, want the file's name", got)
	}
	if got := dialog.Path(); got != path {
		t.Errorf("Path() = %q, want %q", got, path)
	}
}

func TestTheFileDialogOpensInADirectoryGivenDirectly(t *testing.T) {
	root := testDirectory(t, "main.go")

	dialog := NewFileDialog("Open", root, ui.Rect{W: 80, H: 24})

	if dialog.directory != root {
		t.Errorf("the dialog opened in %q, want %q", dialog.directory, root)
	}
	if got := dialog.name.Text(); got != "" {
		t.Errorf("the name field holds %q, want it empty", got)
	}
}

func TestTheFileDialogPathJoinsTheDirectoryAndTheName(t *testing.T) {
	root := testDirectory(t)
	dialog := NewFileDialog("Save as", root, ui.Rect{W: 80, H: 24})

	dialog.name.SetText("new.go")

	if got := dialog.Path(); got != filepath.Join(root, "new.go") {
		t.Errorf("Path() = %q", got)
	}
}

func TestTheFileDialogAcceptsAnAbsolutePath(t *testing.T) {
	dialog := NewFileDialog("Open", testDirectory(t), ui.Rect{W: 80, H: 24})

	dialog.name.SetText("/etc/hosts")

	if got := dialog.Path(); got != filepath.Clean("/etc/hosts") {
		t.Errorf("Path() = %q, want the absolute path used as it is", got)
	}
}

func TestAnEmptyNameGivesNoPath(t *testing.T) {
	dialog := NewFileDialog("Open", testDirectory(t), ui.Rect{W: 80, H: 24})

	dialog.name.SetText("   ")

	if got := dialog.Path(); got != "" {
		t.Errorf("Path() = %q, want nothing for a blank name", got)
	}
}

func TestChoosingADirectoryBrowsesIntoIt(t *testing.T) {
	root := testDirectory(t, "sub/")
	dialog := NewFileDialog("Open", root, ui.Rect{W: 80, H: 24})

	dialog.list.Select(1) // "sub/"
	dialog.choose()

	if dialog.directory != filepath.Join(root, "sub") {
		t.Errorf("the dialog is in %q, want it inside sub", dialog.directory)
	}
	if dialog.Dialog().Done() {
		t.Error("browsing into a directory closed the dialog")
	}
}

func TestChoosingAFileClosesTheDialog(t *testing.T) {
	root := testDirectory(t, "main.go")
	dialog := NewFileDialog("Open", root, ui.Rect{W: 80, H: 24})

	dialog.list.Select(1) // "main.go"
	dialog.choose()

	if !dialog.Dialog().Done() {
		t.Fatal("choosing a file did not close the dialog")
	}
	if got := dialog.Path(); got != filepath.Join(root, "main.go") {
		t.Errorf("Path() = %q", got)
	}
}

func TestConfirmingADirectoryNameBrowsesRatherThanClosing(t *testing.T) {
	root := testDirectory(t, "sub/")
	dialog := NewFileDialog("Save as", root, ui.Rect{W: 80, H: 24})

	dialog.name.SetText("sub")
	dialog.confirm()

	if dialog.Dialog().Done() {
		t.Error("the dialog closed on a directory name")
	}
	if dialog.directory != filepath.Join(root, "sub") {
		t.Errorf("the dialog is in %q, want it inside sub", dialog.directory)
	}
}

func TestAnUnreadableDirectoryStillOffersTheParent(t *testing.T) {
	dialog := NewFileDialog("Open", filepath.Join(t.TempDir(), "does-not-exist"), ui.Rect{W: 80, H: 24})

	items := dialog.list.Items()

	if len(items) != 1 || items[0] != ".."+string(filepath.Separator) {
		t.Errorf("the list holds %v, want just the parent entry", items)
	}
}

func TestTheDialogTitleShowsTheDirectory(t *testing.T) {
	root := testDirectory(t)

	dialog := NewFileDialog("Open", root, ui.Rect{W: 80, H: 24})

	if !strings.HasPrefix(dialog.Dialog().Title(), "Open") {
		t.Errorf("the title is %q, want it to start with the dialog's name", dialog.Dialog().Title())
	}
	if !strings.Contains(dialog.Dialog().Title(), filepath.Base(root)) {
		t.Errorf("the title is %q, want the directory in it", dialog.Dialog().Title())
	}
}

func TestShortenPath(t *testing.T) {
	tests := []struct {
		name  string
		path  string
		width int
		want  string
	}{
		{"already short enough", "/tmp/x", 20, "/tmp/x"},
		{"trimmed from the left", "/a/very/long/path/indeed", 10, "…th/indeed"},
		{"a width too small to trim to", "/a/b/c", 2, "/a/b/c"},
	}

	for _, tc := range tests {
		t.Run(tc.name, func(t *testing.T) {
			got := shortenPath(tc.path, tc.width)
			if len([]rune(got)) > max(tc.width, len([]rune(tc.path))) {
				t.Errorf("shortenPath() = %q, which is longer than %d", got, tc.width)
			}
			if tc.want != "" && got != tc.want {
				t.Errorf("shortenPath() = %q, want %q", got, tc.want)
			}
		})
	}
}

func TestTheConfirmDialogOffersThreeAnswers(t *testing.T) {
	dialog := NewConfirmDialog("Close", "Save changes?", ui.Rect{W: 80, H: 24})

	if got := len(dialog.Controls()); got != 4 {
		t.Errorf("the dialog holds %d controls, want a label and three buttons", got)
	}

	dialog.HandleKey(tcell.NewEventKey(tcell.KeyRune, 'n', tcell.ModAlt))
	if got := dialog.Result(); got != ui.ResultNo {
		t.Errorf("Result() = %v, want ResultNo", got)
	}
}

func TestTheMessageDialogShowsEveryLine(t *testing.T) {
	dialog := NewMessageDialog("Help", "first\nsecond\nthird", ui.Rect{W: 80, H: 24})

	if got := len(dialog.Controls()); got != 4 {
		t.Errorf("the dialog holds %d controls, want three labels and a button", got)
	}
	if got := dialog.Bounds().H; got != 9 {
		t.Errorf("the dialog is %d rows tall, want it to grow with the message", got)
	}
}

func TestThePromptDialogReturnsWhatWasTyped(t *testing.T) {
	dialog, field := NewPromptDialog("Go to line", "Line:", "12", ui.Rect{W: 80, H: 24})

	dialog.HandleKey(tcell.NewEventKey(tcell.KeyRune, '3', tcell.ModNone))

	if got := field.Text(); got != "123" {
		t.Errorf("the field holds %q, want %q", got, "123")
	}
}

func TestTheFindDialogCarriesItsOptions(t *testing.T) {
	dialog := NewFindDialog("func", true, ui.Rect{W: 80, H: 24})

	if got := dialog.Needle(); got != "func" {
		t.Errorf("Needle() = %q", got)
	}
	if !dialog.MatchCase() {
		t.Error("MatchCase() = false, want the option carried over")
	}
}

func TestTheChoiceDialogStartsOnTheCurrentItem(t *testing.T) {
	_, list := NewChoiceDialog("Theme", []string{"a", "b", "c"}, 2, ui.Rect{W: 80, H: 24})

	if got := list.Selected(); got != 2 {
		t.Errorf("Selected() = %d, want 2", got)
	}
}

func TestChoosingFromAChoiceDialogClosesIt(t *testing.T) {
	dialog, list := NewChoiceDialog("Theme", []string{"a", "b"}, 0, ui.Rect{W: 80, H: 24})

	list.Choose()

	if !dialog.Done() || dialog.Result() != ui.ResultOK {
		t.Errorf("Result() = %v, want the dialog accepted", dialog.Result())
	}
}

func TestSelectingAFileInTheListFillsTheNameField(t *testing.T) {
	root := testDirectory(t, "main.go")
	dialog := NewFileDialog("Open", root, ui.Rect{W: 80, H: 24})

	dialog.list.Select(1) // "main.go"

	if got := dialog.name.Text(); got != "main.go" {
		t.Errorf("the name field holds %q, want the highlighted entry", got)
	}
}

func TestSelectingADirectoryFillsTheFieldWithoutItsSlash(t *testing.T) {
	root := testDirectory(t, "sub/")
	dialog := NewFileDialog("Open", root, ui.Rect{W: 80, H: 24})

	dialog.list.Select(1) // "sub/"

	if got := dialog.name.Text(); got != "sub" {
		t.Errorf("the name field holds %q, want sub without the separator", got)
	}
}

func TestSelectingTheParentEntryClearsTheNameField(t *testing.T) {
	// ".." is not a name anyone would type into a field labelled "Name:".
	root := testDirectory(t, "main.go")
	dialog := NewFileDialog("Open", root, ui.Rect{W: 80, H: 24})
	dialog.list.Select(1)

	dialog.list.Select(0) // "../"

	if got := dialog.name.Text(); got != "" {
		t.Errorf("the name field holds %q, want it cleared", got)
	}
}

func TestOKActsOnTheHighlightedFile(t *testing.T) {
	// The bug this covers: OK did nothing at all on a freshly opened dialog,
	// because highlighting a file never reached the name field and Path() was
	// therefore empty.
	root := testDirectory(t, "main.go")
	dialog := NewFileDialog("Open", root, ui.Rect{W: 80, H: 24})

	dialog.list.Select(1) // "main.go"
	dialog.confirm()      // what the OK button does

	if !dialog.Dialog().Done() {
		t.Fatal("OK did nothing with a file highlighted in the list")
	}
	if got := dialog.Path(); got != filepath.Join(root, "main.go") {
		t.Errorf("Path() = %q, want the highlighted file", got)
	}
}

func TestOKOnAnUntouchedDialogBrowsesUpRatherThanDoingNothing(t *testing.T) {
	// Nothing has been highlighted, so the field is empty and the highlight is
	// still on the parent entry. OK must act on that rather than appear dead.
	root := testDirectory(t, "main.go")
	dialog := NewFileDialog("Open", root, ui.Rect{W: 80, H: 24})

	dialog.confirm()

	if dialog.Dialog().Done() {
		t.Fatal("OK closed the dialog with no file chosen")
	}
	if dialog.directory != filepath.Dir(root) {
		t.Errorf("the dialog is in %q, want the parent of %q", dialog.directory, root)
	}
}

func TestOKOnAHighlightedDirectoryBrowsesIntoIt(t *testing.T) {
	root := testDirectory(t, "sub/")
	dialog := NewFileDialog("Open", root, ui.Rect{W: 80, H: 24})

	dialog.list.Select(1) // "sub/"
	dialog.confirm()

	if dialog.Dialog().Done() {
		t.Error("OK on a directory closed the dialog instead of browsing into it")
	}
	if dialog.directory != filepath.Join(root, "sub") {
		t.Errorf("the dialog is in %q, want it inside sub", dialog.directory)
	}
}

func TestBrowsingIntoADirectoryClearsTheNameField(t *testing.T) {
	// Refilling the list moves the highlight back to the parent entry, and the
	// field follows it — the name from the directory just left must not linger.
	root := testDirectory(t, "sub/")
	dialog := NewFileDialog("Open", root, ui.Rect{W: 80, H: 24})
	dialog.list.Select(1)

	dialog.confirm() // browses into sub

	if got := dialog.name.Text(); got != "" {
		t.Errorf("the name field still holds %q after entering a directory", got)
	}
}

func TestATypedNameWinsOverTheHighlight(t *testing.T) {
	// Someone who typed a name meant that name, whatever the list is showing.
	root := testDirectory(t, "main.go", "other.go")
	dialog := NewFileDialog("Open", root, ui.Rect{W: 80, H: 24})
	dialog.list.Select(1) // "main.go", which fills the field

	dialog.name.SetText("typed.go")
	dialog.confirm()

	if got := dialog.Path(); got != filepath.Join(root, "typed.go") {
		t.Errorf("Path() = %q, want the name that was typed", got)
	}
}

func TestSaveAsKeepsTheNameItOpenedWith(t *testing.T) {
	// Building the dialog must not let the list's initial highlight overwrite
	// the file name Save As put in the field.
	root := testDirectory(t, "other.go")
	dialog := NewFileDialog("Save as", filepath.Join(root, "main.go"), ui.Rect{W: 80, H: 24})

	if got := dialog.name.Text(); got != "main.go" {
		t.Errorf("the name field holds %q, want main.go", got)
	}
}

func TestOKWithAnEmptyDirectoryListingDoesNotCrash(t *testing.T) {
	root := t.TempDir()
	dialog := NewFileDialog("Open", root, ui.Rect{W: 80, H: 24})

	dialog.confirm() // only "../" is listed

	if dialog.directory != filepath.Dir(root) {
		t.Errorf("the dialog is in %q, want the parent", dialog.directory)
	}
}

// findOnScreen returns the screen position of a piece of text, so a test can
// click it the way a user would rather than calling the action behind it.
//
// The column is counted in runes. strings.Index would give a byte offset, and
// a drawn row is full of ░ and ║ at three bytes each — clicking a byte offset
// lands about thirty columns to the right of the thing you meant.
func findOnScreen(t *testing.T, lines []string, text string) (x, y int) {
	t.Helper()

	for row, line := range lines {
		at := strings.Index(line, text)
		if at < 0 {
			continue
		}
		return len([]rune(line[:at])), row
	}
	t.Fatalf("%q is not on the screen:\n%s", text, strings.Join(lines, "\n"))
	return 0, 0
}

func TestClickingOKInTheOpenDialogOpensTheHighlightedFile(t *testing.T) {
	a, screen := newTestApp(t)
	root := testDirectory(t, "main.go")
	t.Chdir(root)

	a.OpenFile()
	// The dialog opens with the focus on the Name field, so the first Down
	// moves it to the list and the second highlights the first real entry.
	press(a, tcell.KeyDown, 0, tcell.ModNone)
	press(a, tcell.KeyDown, 0, tcell.ModNone)

	lines := render(t, a, screen)
	x, y := findOnScreen(t, lines, "OK")
	click(a, x, y)

	if a.Modals() != 0 {
		t.Fatalf("Modals() = %d; clicking OK left the dialog up", a.Modals())
	}
	if a.Desktop().Count() != 1 {
		t.Fatalf("Count() = %d, want the file opened", a.Desktop().Count())
	}
	if got := a.Desktop().Active().Title(); got != "main.go" {
		t.Errorf("the window is called %q, want main.go", got)
	}
}

func TestTabbingToOKAndPressingEnterOpensTheHighlightedFile(t *testing.T) {
	a, _ := newTestApp(t)
	root := testDirectory(t, "main.go")
	t.Chdir(root)

	a.OpenFile()
	press(a, tcell.KeyDown, 0, tcell.ModNone) // focus the list
	press(a, tcell.KeyDown, 0, tcell.ModNone) // highlight main.go
	press(a, tcell.KeyTab, 0, tcell.ModNone)  // move the focus off the list
	press(a, tcell.KeyEnter, 0, tcell.ModNone)

	if a.Modals() != 0 {
		t.Fatalf("Modals() = %d; the dialog is still up", a.Modals())
	}
	if a.Desktop().Count() != 1 {
		t.Fatalf("Count() = %d, want the file opened", a.Desktop().Count())
	}
	if got := a.Desktop().Active().Title(); got != "main.go" {
		t.Errorf("the window is called %q, want main.go", got)
	}
}

func TestPressingAltOInTheOpenDialogOpensTheHighlightedFile(t *testing.T) {
	a, _ := newTestApp(t)
	root := testDirectory(t, "main.go")
	t.Chdir(root)

	a.OpenFile()
	press(a, tcell.KeyDown, 0, tcell.ModNone)  // focus the list
	press(a, tcell.KeyDown, 0, tcell.ModNone)  // highlight main.go
	press(a, tcell.KeyRune, 'o', tcell.ModAlt) // the OK button's hot key

	if a.Desktop().Count() != 1 {
		t.Fatalf("Count() = %d, want the file opened", a.Desktop().Count())
	}
}