summaryrefslogtreecommitdiff
path: root/internal/ui/help/help_test.go
blob: 79c3ec06a33c21298797bd791c58967d1b0d022b (plain)
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
package help

import (
	"regexp"
	"strings"
	"testing"

	"github.com/charmbracelet/x/ansi"
)

func TestLinesFlattensSectionsForSearch(t *testing.T) {
	sections := []Section{
		{
			Title: "Navigation",
			Items: []Item{
				{Key: "j", Desc: "move down"},
				{Key: "k", Desc: "move up"},
			},
		},
		{
			Title: "General",
			Items: []Item{
				{Key: "q", Desc: "quit"},
			},
		},
	}

	got := Lines(sections)
	want := []string{
		"Navigation",
		"j: move down",
		"k: move up",
		"",
		"General",
		"q: quit",
	}

	if strings.Join(got, "\n") != strings.Join(want, "\n") {
		t.Fatalf("Lines() mismatch\nwant:\n%s\ngot:\n%s", strings.Join(want, "\n"), strings.Join(got, "\n"))
	}
}

func TestRenderHighlightsSearchMatches(t *testing.T) {
	sections := []Section{
		{
			Title: "Search",
			Items: []Item{
				{Key: "/", Desc: "search tasks"},
				{Key: "n", Desc: "next match"},
			},
		},
	}
	palette := Palette{
		HeaderFG: "15",
		HeaderBG: "8",
		KeyFG:    "14",
		DescFG:   "250",
		SearchFG: "0",
		SearchBG: "11",
	}

	base := Render(sections, palette, nil)
	got := Render(sections, palette, regexp.MustCompile("search"))
	if !strings.Contains(ansi.Strip(got), "search tasks") {
		t.Fatalf("rendered help omitted matching item: %q", got)
	}
	if got == base {
		t.Fatalf("search highlight did not change rendered help")
	}

	noMatch := Render(sections, palette, regexp.MustCompile("not-present"))
	if noMatch != base {
		t.Fatalf("non-matching search changed rendered help\nbase: %q\nnoMatch: %q", base, noMatch)
	}
}