git/orchard/main
tui/internal/ui/palette_test.go
63 lines2 KB last change raw · history
package ui

import (
	"strings"
	"testing"

	"github.com/charmbracelet/lipgloss"
)

func TestNoStyleUnderlines(t *testing.T) {
	// lipgloss emits an escape sequence per rune to underline: 200 characters
	// cost 2800 bytes against 209 plain. No style may carry it back.
	text := strings.Repeat("x", 200)
	for name, render := range map[string]func(string) string{
		"Warn":  func(s string) string { return Warn.Render(s) },
		"Alert": func(s string) string { return Alert.Render(s) },
		"Err":   func(s string) string { return Err.Render(s) },
		"OK":    func(s string) string { return OK.Render(s) },
	} {
		got := len(render(text))
		if got > 400 {
			t.Errorf("%s renders %d bytes for 200 characters; underline is back",
				name, got)
		}
	}
}

func TestWarnAndAlertStayDistinguishableFromTheirNeighbours(t *testing.T) {
	// The second channel has to survive for a theme whose slots look alike.
	for _, pair := range [][2]func(string) string{
		{func(s string) string { return Alert.Render(s) },
			func(s string) string { return Err.Render(s) }},
		{func(s string) string { return Warn.Render(s) },
			func(s string) string { return OK.Render(s) }},
	} {
		if pair[0]("x") == pair[1]("x") {
			t.Errorf("two roles render identically: %q", pair[0]("x"))
		}
	}
}

func TestAdvisoriesAreColouredBySeverityNotFixability(t *testing.T) {
	// Fixability has its own channel -- the "fixed in X" text. Colouring the
	// severity label by it made Critical and Low the same shade, which is the
	// one distinction a security list exists to make.
	seen := map[string]lipgloss.Style{}
	for _, sev := range []string{"Critical", "High", "Medium", "Low", "Unknown"} {
		seen[sev] = StylesByRole(advisoryRole(sev))
	}
	if seen["Critical"].Render("x") == seen["Low"].Render("x") {
		t.Error("Critical and Low render identically")
	}
	if seen["Critical"].Render("x") == seen["Medium"].Render("x") {
		t.Error("Critical and Medium render identically")
	}
	if seen["Medium"].Render("x") == seen["Low"].Render("x") {
		t.Error("Medium and Low render identically")
	}
	if seen["Critical"].Render("x") != Alert.Render("x") {
		t.Error("Critical is not the alert role")
	}
}