▍ humdrum codex / glint v1.0.2
license AGPL-3.0
2.5 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
package app

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

	"glint/internal/editor"

	tea "github.com/charmbracelet/bubbletea"
)

func TestGotoLineJumps(t *testing.T) {
	a := statusApp()
	a.editor.SetContent([]byte("one\ntwo\nthree\nfour"))
	a.Update(tea.KeyMsg{Type: tea.KeyCtrlL})
	if a.mode != ModeGotoLine {
		t.Fatalf("Ctrl+L did not open go-to-line: mode = %v", a.mode)
	}
	a.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("3")})
	a.Update(tea.KeyMsg{Type: tea.KeyEnter})
	if a.mode != ModeEditor {
		t.Errorf("after Enter mode = %v, want ModeEditor", a.mode)
	}
	if a.editor.Cursor.Row != 2 {
		t.Errorf("cursor row = %d, want 2 (line 3)", a.editor.Cursor.Row)
	}
}

func TestGotoLineEscCloses(t *testing.T) {
	a := statusApp()
	a.Update(tea.KeyMsg{Type: tea.KeyCtrlL})
	a.Update(tea.KeyMsg{Type: tea.KeyEsc})
	if a.mode != ModeEditor {
		t.Errorf("Esc did not close go-to-line: mode = %v", a.mode)
	}
}

func TestPerFileCursorMemory(t *testing.T) {
	dir := t.TempDir()
	pa := filepath.Join(dir, "a.md")
	pb := filepath.Join(dir, "b.md")
	os.WriteFile(pa, []byte("a1\na2\na3"), 0o644)
	os.WriteFile(pb, []byte("b1\nb2"), 0o644)

	a := statusApp()
	a.Load(pa)
	a.editor.SetCursor(editor.Position{Row: 2, Col: 1}) // remember this in a.md
	a.Load(pb)                                          // switch away
	a.Load(pa)                                          // return
	if got := (a.editor.Cursor); got.Row != 2 {
		t.Errorf("returning to a.md cursor = %+v, want row 2 restored", got)
	}
}

func TestPasteURLOverSelectionMakesLink(t *testing.T) {
	a := statusApp()
	a.editor.SetContent([]byte("click here"))
	a.editor.SetCursor(editor.Position{Row: 0, Col: 0})
	// select "click"
	for i := 0; i < 5; i++ {
		a.editor.HandleKey(tea.KeyMsg{Type: tea.KeyShiftRight})
	}
	a.pasteText("https://example.com")
	if got := a.editor.Lines[0]; got != "[click](https://example.com) here" {
		t.Errorf("line = %q, want [click](https://example.com) here", got)
	}
}

func TestPasteNonURLOverSelectionReplaces(t *testing.T) {
	a := statusApp()
	a.editor.SetContent([]byte("click here"))
	a.editor.SetCursor(editor.Position{Row: 0, Col: 0})
	for i := 0; i < 5; i++ {
		a.editor.HandleKey(tea.KeyMsg{Type: tea.KeyShiftRight})
	}
	a.pasteText("tap")
	if got := a.editor.Lines[0]; got != "tap here" {
		t.Errorf("line = %q, want tap here", got)
	}
}

func TestPasteURLNoSelectionInsertsLiterally(t *testing.T) {
	a := statusApp()
	a.editor.SetContent([]byte(""))
	a.pasteText("https://example.com")
	if got := a.editor.Lines[0]; got != "https://example.com" {
		t.Errorf("line = %q, want literal URL", got)
	}
}