▍ humdrum codex / glint v1.0.2
license AGPL-3.0
4.8 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
157
158
159
160
161
162
163
164
165
166
167
168
package editor

import (
	"testing"

	"glint/internal/theme"

	"github.com/charmbracelet/lipgloss"
)

// spanText concatenates the raw text of a line's spans. It must equal the
// original raw line exactly (markup-visible invariant).
func spanText(spans []Span) string {
	s := ""
	for _, sp := range spans {
		s += sp.Text
	}
	return s
}

func TestRenderSpansPreservesText(t *testing.T) {
	spans := []Span{
		{Text: "ab", Style: lipgloss.NewStyle()},
		{Text: "cd", Style: lipgloss.NewStyle()},
	}
	// rendering may add ANSI, but stripped content is checked elsewhere;
	// here we only assert it does not panic and is non-empty.
	if renderSpans(spans) == "" {
		t.Error("renderSpans returned empty for non-empty spans")
	}
}

func TestScanPlainTextGetsExplicitForeground(t *testing.T) {
	th := theme.FlexokiDark()
	out := ScanLines([]string{"hello world"}, th)
	if len(out) != 1 || len(out[0]) == 0 {
		t.Fatalf("expected spans for one line, got %v", out)
	}
	for _, sp := range out[0] {
		// A style with no foreground returns "" from GetForeground().
		if sp.Style.GetForeground() == lipgloss.Color("") {
			t.Errorf("plain span %q has no explicit foreground", sp.Text)
		}
	}
}

func TestScanPreservesRawTextAcrossConstructs(t *testing.T) {
	th := theme.FlexokiDark()
	lines := []string{
		"# Heading",
		"plain **bold** and *italic* and `code`",
		"- a list item with a [link](http://x) and [[wikilink]]",
		"> a quote",
		"```",
		"raw **not bold** here",
		"```",
		"---",
	}
	out := ScanLines(lines, th)
	for i, raw := range lines {
		if got := spanText(out[i]); got != raw {
			t.Errorf("line %d: span text %q != raw %q", i, got, raw)
		}
	}
}

func TestScanFencedCodeSuppressesInline(t *testing.T) {
	th := theme.FlexokiDark()
	lines := []string{"```", "**x**", "```"}
	out := ScanLines(lines, th)
	// The fence body line should be a single code-colored span, not split
	// into bold spans.
	if len(out[1]) != 1 {
		t.Errorf("fenced line split into %d spans, want 1", len(out[1]))
	}
	if out[1][0].Style.GetForeground() != th.Code {
		t.Errorf("fenced body not code-colored")
	}
}

func TestScanLeadingFrontmatter(t *testing.T) {
	th := theme.FlexokiDark()
	lines := []string{"---", "title: x", "---", "body"}
	out := ScanLines(lines, th)
	if out[0][0].Style.GetForeground() != th.Muted {
		t.Errorf("opening --- not muted")
	}
	if out[1][0].Style.GetForeground() != th.Accent {
		t.Errorf("frontmatter key not accent-colored")
	}
	if out[3][0].Style.GetForeground() != th.Text {
		t.Errorf("post-frontmatter line should be plain text")
	}
}

func TestScanEmptyLineYieldsNoSpans(t *testing.T) {
	out := ScanLines([]string{""}, theme.FlexokiDark())
	if len(out[0]) != 0 {
		t.Errorf("empty line should yield no spans, got %d", len(out[0]))
	}
}

func TestScanHeadingKeepsHashes(t *testing.T) {
	th := theme.FlexokiDark()
	out := ScanLines([]string{"### Title"}, th)
	if spanText(out[0]) != "### Title" {
		t.Errorf("heading hashes dropped: %q", spanText(out[0]))
	}
	if out[0][0].Style.GetForeground() != th.Heading {
		t.Errorf("heading not heading-colored")
	}
}

func TestScanListMarkerThenInline(t *testing.T) {
	th := theme.FlexokiDark()
	out := ScanLines([]string{"- **x**"}, th)
	if out[0][0].Style.GetForeground() != th.ListMarker {
		t.Errorf("first span should be the list marker, got fg %v", out[0][0].Style.GetForeground())
	}
	if spanText(out[0]) != "- **x**" {
		t.Errorf("list line text altered: %q", spanText(out[0]))
	}
}

func TestScanFrontmatterHighlightsKeysAndValues(t *testing.T) {
	th := theme.FlexokiDark()
	lines := []string{"---", "title: Hello", "tags:", "  - one", "  - two", "# a comment", "---", "body"}
	out := ScanLines(lines, th)

	// Char-for-char preservation across every frontmatter line.
	for i, raw := range lines {
		if got := spanText(out[i]); got != raw {
			t.Errorf("line %d: span text %q != raw %q", i, got, raw)
		}
	}
	// Opening and closing --- are muted.
	if out[0][0].Style.GetForeground() != th.Muted {
		t.Errorf("opening --- not muted")
	}
	if out[6][0].Style.GetForeground() != th.Muted {
		t.Errorf("closing --- not muted")
	}
	// "title:" key portion is Accent; the value is Text.
	if out[1][0].Style.GetForeground() != th.Accent {
		t.Errorf("key not Accent-colored, got %v", out[1][0].Style.GetForeground())
	}
	if last := out[1][len(out[1])-1]; last.Style.GetForeground() != th.Text {
		t.Errorf("value not Text-colored, got %v", last.Style.GetForeground())
	}
	// List item marker "-" is ListMarker-colored.
	foundMarker := false
	for _, sp := range out[3] {
		if sp.Style.GetForeground() == th.ListMarker {
			foundMarker = true
		}
	}
	if !foundMarker {
		t.Errorf("list item dash not ListMarker-colored")
	}
	// Comment line is muted.
	if out[5][0].Style.GetForeground() != th.Muted {
		t.Errorf("comment not muted")
	}
	// After the closing ---, normal text resumes.
	if out[7][0].Style.GetForeground() != th.Text {
		t.Errorf("post-frontmatter line should be plain text")
	}
}