56 lines
1.9 KiB
Go
56 lines
1.9 KiB
Go
package tui
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
"testing"
|
|
|
|
"git.a-wan.net/alan/pipelines/internal/pipeline"
|
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
|
"github.com/charmbracelet/x/ansi"
|
|
)
|
|
|
|
func assertCenteredGraphBody(t *testing.T, view string, graph []string, width, height int) {
|
|
t.Helper()
|
|
widest := 0
|
|
for _, line := range graph {
|
|
widest = max(widest, ansi.StringWidth(line))
|
|
}
|
|
inner, rows := width-4, height-5
|
|
left, top := max(0, (inner-widest)/2), max(0, (rows-len(graph))/2)
|
|
actual := strings.Split(ansi.Strip(view), "\n")
|
|
for i, line := range graph {
|
|
want := " " + strings.Repeat(" ", left) + ansi.Strip(line)
|
|
if strings.TrimRight(actual[3+top+i], " ") != strings.TrimRight(want, " ") {
|
|
t.Fatalf("graph not centered at row %d:\nactual %q\nwant %q", i, actual[3+top+i], want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestPipelineAndRunGraphsCenterWhenTheyFit(t *testing.T) {
|
|
graph, err := pipeline.Parse(pipeline.Template([]string{"worker"}, true))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
m := New(context.Background(), "")
|
|
m.width, m.height = 160, 60
|
|
m.detail = &protocol.PipelineDetail{Graph: graph}
|
|
m.keys.detail = true
|
|
// Stale panning/scroll values from a larger graph must not prevent centering.
|
|
m.pipelineTop, m.graphPan = 99, 99
|
|
for _, focused := range []bool{false, true} {
|
|
m.stageFocus = focused
|
|
assertCenteredGraphBody(t, m.graphPanel(m.width, 40), m.graphLines(), m.width, 40)
|
|
}
|
|
m.runDetail = &protocol.RunDetail{Graph: graph}
|
|
assertCenteredGraphBody(t, m.runGraphPanel(40, 99, 99), m.runGraphLines(m.width-4), m.width, 40)
|
|
}
|
|
|
|
func TestGraphViewportKeepsOversizedDiagramPannable(t *testing.T) {
|
|
lines := []string{"0123456789abcdefghij", "ABCDEFGHIJKLMNOPQRST"}
|
|
view := strings.Split(ansi.Strip(graphViewport("Graph", lines, 14, 7, 99, 99, true, "pan")), "\n")
|
|
if strings.TrimSpace(view[3]) != "abcdefghij" || strings.TrimSpace(view[4]) != "KLMNOPQRST" {
|
|
t.Fatal("oversized graph should clamp pan without centering/cropping the wrong region", view)
|
|
}
|
|
}
|