166 lines
4.8 KiB
Go
166 lines
4.8 KiB
Go
package tui
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
|
|
"git.a-wan.net/alan/pipelines/internal/pipeline"
|
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
|
)
|
|
|
|
func nestedForkGraph(t *testing.T) protocol.Graph {
|
|
t.Helper()
|
|
source, err := os.ReadFile("../../examples/nested-forks.md")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
graph, err := pipeline.Parse(string(source))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return graph
|
|
}
|
|
|
|
func TestNestedForkBranchesHaveSeparateLanes(t *testing.T) {
|
|
graph := nestedForkGraph(t)
|
|
layout := layoutGraph(graph)
|
|
nodes := map[string]graphNode{}
|
|
for _, node := range layout.nodes {
|
|
nodes[node.key] = node
|
|
}
|
|
for _, pair := range [][2]string{{"brief_answer", "detail_router"}, {"walkthrough", "reference"}} {
|
|
a, b := nodes[pair[0]], nodes[pair[1]]
|
|
if a.x != b.x || a.y+a.height > b.y {
|
|
t.Fatalf("fork siblings must share a column in separate lanes: %+v %+v", a, b)
|
|
}
|
|
}
|
|
if nodes["intake"].y != nodes["format_router"].y || nodes["delivery"].x <= nodes["reference"].x {
|
|
t.Fatal("trunk and merge are not horizontal/forward")
|
|
}
|
|
if nodes[protocol.FailureEnd].y <= nodes["reference"].y+nodes["reference"].height || nodes[protocol.FailureEnd].x >= nodes[protocol.SuccessEnd].x {
|
|
t.Fatal("terminals must remain below branches, failure left of success")
|
|
}
|
|
for _, edge := range layout.edges {
|
|
if !protocol.IsTerminal(edge.target) && nodes[edge.source].x >= nodes[edge.target].x {
|
|
t.Fatal("acyclic content edge runs backward")
|
|
}
|
|
}
|
|
ports := map[string]int{}
|
|
for _, edge := range layout.edges {
|
|
ports[edge.source+":"+edge.outcome] = edge.from
|
|
}
|
|
if ports["format_router:brief"] >= ports["format_router:detailed"] || ports["detail_router:walkthrough"] >= ports["detail_router:reference"] {
|
|
t.Fatal("fork ports do not follow upper/lower lane order")
|
|
}
|
|
assertForkRouteClearance(t, layout)
|
|
view := strings.Join(renderGraph(graph, "detail_router"), "\n")
|
|
if strings.Count(view, "╔") != 1 || strings.ContainsAny(view, "┼╳") {
|
|
t.Fatal("selection/crossing conventions regressed")
|
|
}
|
|
for _, stage := range graph.Stages {
|
|
if !strings.Contains(view, stage.Key) {
|
|
t.Fatal("missing rendered stage", stage.Key)
|
|
}
|
|
}
|
|
if strings.Count(view, "▼")+strings.Count(view, "▶") != len(layout.edges)+7 {
|
|
t.Fatal("missing destination or bottom failure arrows")
|
|
}
|
|
for i := 0; i < 5; i++ {
|
|
if strings.Join(renderGraph(graph, "detail_router"), "\n") != view {
|
|
t.Fatal("lane layout is unstable")
|
|
}
|
|
}
|
|
}
|
|
|
|
func assertForkRouteClearance(t *testing.T, layout graphLayout) {
|
|
t.Helper()
|
|
ports := map[graphPoint]bool{}
|
|
for _, edge := range layout.edges {
|
|
for _, port := range []graphPoint{edge.path[0], edge.path[len(edge.path)-1]} {
|
|
if ports[port] {
|
|
t.Fatal("routes share a port", port)
|
|
}
|
|
ports[port] = true
|
|
}
|
|
for i := 1; i < len(edge.path); i++ {
|
|
a, b := edge.path[i-1], edge.path[i]
|
|
dx, dy := 0, 0
|
|
if a.x != b.x && a.y != b.y {
|
|
t.Fatal("non-orthogonal route")
|
|
}
|
|
if b.x > a.x {
|
|
dx = 1
|
|
} else if b.x < a.x {
|
|
dx = -1
|
|
}
|
|
if b.y > a.y {
|
|
dy = 1
|
|
} else if b.y < a.y {
|
|
dy = -1
|
|
}
|
|
for a != b {
|
|
a.x += dx
|
|
a.y += dy
|
|
if a.x < 0 || a.y < 0 || a.x >= layout.width || a.y >= layout.height {
|
|
t.Fatal("route outside canvas", edge, a)
|
|
}
|
|
for _, node := range layout.nodes {
|
|
if node.key == edge.source || node.key == edge.target {
|
|
continue
|
|
}
|
|
if a.x >= node.x && a.x < node.x+node.width && a.y >= node.y && a.y < node.y+node.height {
|
|
t.Fatalf("route %s -> %s passes through %s at %v", edge.source, edge.target, node.key, a)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestForkLanesKeepFeedbackArcs(t *testing.T) {
|
|
graph := nestedForkGraph(t)
|
|
graph.Stages[4].Edges["retry"] = "detail_router"
|
|
layout := layoutGraph(graph)
|
|
nodes := map[string]graphNode{}
|
|
for _, node := range layout.nodes {
|
|
nodes[node.key] = node
|
|
}
|
|
if nodes["walkthrough"].y == nodes["reference"].y {
|
|
t.Fatal("feedback collapsed forward branches")
|
|
}
|
|
for _, edge := range layout.edges {
|
|
if edge.outcome == "retry" {
|
|
above := false
|
|
for _, point := range edge.path {
|
|
if point.y < nodes[edge.source].y {
|
|
above = true
|
|
}
|
|
}
|
|
if !above || !edge.left {
|
|
t.Fatal("feedback route lost its upper arc")
|
|
}
|
|
}
|
|
}
|
|
assertForkRouteClearance(t, layout)
|
|
}
|
|
|
|
func TestSelectingLaneRevealsWholeStage(t *testing.T) {
|
|
m := New(context.Background(), "")
|
|
m.width = 160
|
|
m.height = 45
|
|
m.keys.detail = true
|
|
m.detail = &protocol.PipelineDetail{Graph: nestedForkGraph(t)}
|
|
for _, key := range []string{"4", "6", "7"} {
|
|
m = press(m, key)
|
|
layout := layoutGraph(m.detail.Graph)
|
|
page := m.pipelineGeometry(m.height-4).graphHeight - 5
|
|
for _, node := range layout.nodes {
|
|
if node.key == m.selectedStage().Key && node.height <= page && (node.y < m.pipelineTop || node.y+node.height > m.pipelineTop+page) {
|
|
t.Fatal("selected lane stage is clipped", node, m.pipelineTop, page)
|
|
}
|
|
}
|
|
}
|
|
}
|