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

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

	"glint/internal/theme"
)

func TestFuzzyMatchSubsequence(t *testing.T) {
	if _, ok := fuzzyMatch("abc", "axbxc"); !ok {
		t.Error("abc should match axbxc as a subsequence")
	}
	if _, ok := fuzzyMatch("abc", "acb"); ok {
		t.Error("abc should NOT match acb (out of order)")
	}
	if _, ok := fuzzyMatch("", "anything"); !ok {
		t.Error("empty query should match anything")
	}
}

func TestFuzzyMatchCaseInsensitive(t *testing.T) {
	if _, ok := fuzzyMatch("DOC", "my-document"); !ok {
		t.Error("DOC should match my-document case-insensitively")
	}
}

func TestFuzzyMatchContiguousScoresHigher(t *testing.T) {
	tight, ok1 := fuzzyMatch("ab", "abxx")
	loose, ok2 := fuzzyMatch("ab", "axxb")
	if !ok1 || !ok2 {
		t.Fatal("both should match")
	}
	if tight <= loose {
		t.Errorf("contiguous match (%d) should score higher than gapped (%d)", tight, loose)
	}
}

func TestWalkMarkdownSkipsDotDirsAndNonMarkdown(t *testing.T) {
	root := t.TempDir()
	os.WriteFile(filepath.Join(root, "a.md"), nil, 0o644)
	os.WriteFile(filepath.Join(root, "b.txt"), nil, 0o644)
	os.MkdirAll(filepath.Join(root, ".git"), 0o755)
	os.WriteFile(filepath.Join(root, ".git", "c.md"), nil, 0o644)
	os.MkdirAll(filepath.Join(root, "sub"), 0o755)
	os.WriteFile(filepath.Join(root, "sub", "d.md"), nil, 0o644)

	entries, err := walkMarkdown(root)
	if err != nil {
		t.Fatal(err)
	}
	var got []string
	for _, e := range entries {
		got = append(got, e.path)
	}
	sort.Strings(got)
	want := []string{
		filepath.Join(root, "a.md"),
		filepath.Join(root, "sub", "d.md"),
	}
	if len(got) != len(want) {
		t.Fatalf("walkMarkdown = %v, want %v", got, want)
	}
	for i := range want {
		if got[i] != want[i] {
			t.Errorf("got[%d] = %q, want %q", i, got[i], want[i])
		}
	}
}

func TestRankEntriesMtimeDescending(t *testing.T) {
	base := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)
	all := []fileEntry{
		{path: "/v/old.md", mod: base},
		{path: "/v/new.md", mod: base.Add(48 * time.Hour)},
		{path: "/v/mid.md", mod: base.Add(24 * time.Hour)},
	}
	got := rankEntries(all, "", "")
	want := []string{"/v/new.md", "/v/mid.md", "/v/old.md"}
	for i := range want {
		if got[i] != want[i] {
			t.Errorf("got[%d] = %q, want %q", i, got[i], want[i])
		}
	}
}

func TestRankEntriesTodayFloatsToTop(t *testing.T) {
	base := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)
	all := []fileEntry{
		{path: "/v/new.md", mod: base.Add(48 * time.Hour)},
		{path: "/v/Daily/2026-06-28.md", mod: base}, // oldest, but today
	}
	got := rankEntries(all, "", "/v/Daily/2026-06-28.md")
	if got[0] != "/v/Daily/2026-06-28.md" {
		t.Errorf("today's note should float to top, got %q", got[0])
	}
}

func TestRankEntriesQueryFiltersAndTieBreaksByMtime(t *testing.T) {
	base := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)
	all := []fileEntry{
		{path: "/v/alpha.md", mod: base},
		{path: "/v/alpha-new.md", mod: base.Add(24 * time.Hour)},
		{path: "/v/zzz.md", mod: base.Add(72 * time.Hour)},
	}
	got := rankEntries(all, "alpha", "")
	// zzz filtered out; both "alpha" files present, newer first on equal score is
	// not guaranteed (scores differ), but zzz must be absent.
	for _, p := range got {
		if p == "/v/zzz.md" {
			t.Error("zzz.md should not match query 'alpha'")
		}
	}
	if len(got) != 2 {
		t.Fatalf("got %d matches, want 2", len(got))
	}
}

func TestPickerPreviewRendersSelectedFile(t *testing.T) {
	root := t.TempDir()
	p := filepath.Join(root, "note.md")
	os.WriteFile(p, []byte("# Heading\n\nbody text"), 0o644)

	m, err := New(root, theme.FlexokiDark(), "", "dark")
	if err != nil {
		t.Fatal(err)
	}
	m.SetSize(120, 30)
	m.recompute() // selects note.md
	view := m.View()
	if !strings.Contains(view, "Heading") {
		t.Errorf("preview pane should render the selected file's heading; view:\n%s", view)
	}
}

func TestNewNotePath(t *testing.T) {
	root := "/v"
	cases := map[string]string{
		"foo":        filepath.Join("/v", "foo.md"),
		"Folder/Bar": filepath.Join("/v", "Folder", "Bar.md"),
		"x.md":       filepath.Join("/v", "x.md"),
		"  spaced  ": filepath.Join("/v", "spaced.md"),
	}
	for q, want := range cases {
		if got := NewNotePath(root, q); got != want {
			t.Errorf("NewNotePath(%q) = %q, want %q", q, got, want)
		}
	}
	if NewNotePath(root, "   ") != "" {
		t.Error("blank query should yield empty path")
	}
}