package tui import ( "fmt" "math/rand" "strings" "testing" "git.a-wan.net/alan/pipelines/internal/pipeline" "git.a-wan.net/alan/pipelines/internal/protocol" ) func TestRenderGraphWithCyclesAndDefaults(t *testing.T) { source := pipeline.Template([]string{"implementer", "reviewer"}, true) source = strings.Replace(source, "success -> #success", "bad_implementation -> implementer\nsuccess -> reviewer\nerror -> #failure", 1) // The replacement above adds an explicit error; remove the template's duplicate. source = strings.Replace(source, "error -> #failure\nerror -> #failure", "error -> #failure", 1) source = strings.ReplaceAll(source, "error -> #failure\n", "") graph, err := pipeline.Parse(source) if err != nil { t.Fatal(err) } view := strings.Join(renderGraph(graph), "\n") for _, text := range []string{"implementer", "reviewer", "bad_implementation", "success", "error*", "SUCCESS", "FAILURE", "├→", "▼", "│"} { if !strings.Contains(view, text) { t.Fatalf("missing %q:\n%s", text, view) } } if strings.Count(view, "├→") != 3 { t.Fatalf("expected every edge to have an outgoing arrow:\n%s", view) } for i := 0; i < 20; i++ { if strings.Join(renderGraph(graph), "\n") != view { t.Fatal("unstable graph layout") } } } func TestGraphRoutesHaveSeparatePortsAndNonOverlappingLanes(t *testing.T) { source := pipeline.Template([]string{"implementer", "reviewer", "tester"}, true) source = strings.Replace(source, "success -> tester", "bad_implementation -> implementer\nsuccess -> tester", 1) graph, err := pipeline.Parse(source) if err != nil { t.Fatal(err) } layout := layoutGraph(graph) ports := map[string]bool{} for i, a := range layout.edges { for _, port := range []string{fmt.Sprint(a.path[0]), fmt.Sprint(a.path[len(a.path)-1])} { if ports[port] { t.Fatalf("edges share a source/destination port %s", port) } ports[port] = true } if a.outcome == "bad_implementation" && !a.left { t.Fatal("backward edge must use left-hand routing") } for _, b := range layout.edges[i+1:] { if a.x == b.x && min(a.from, a.to) <= max(b.from, b.to) && min(b.from, b.to) <= max(a.from, a.to) { t.Fatal("edges overlap along a vertical lane") } } } view := strings.Join(renderGraph(graph), "\n") if strings.Contains(view, "┼") { t.Fatal("crossings should not look like connected junctions") } bottom := 0 for _, edge := range layout.edges { if edge.bottomExit { bottom++ } } if strings.Count(view, "▼")+strings.Count(view, "▶") != len(layout.edges)+bottom { t.Fatal("every edge needs its own destination arrow") } } func TestHorizontalGraphStageOrder(t *testing.T) { graph, err := pipeline.Parse(pipeline.Template([]string{"implementer", "reviewer", "tester", "publisher"}, true)) if err != nil { t.Fatal(err) } layout := layoutGraph(graph) for i := 1; i < len(graph.Stages); i++ { if layout.nodes[i].y != layout.nodes[0].y || layout.nodes[i].x <= layout.nodes[i-1].x { t.Fatal("stages must run left to right") } } for _, e := range layout.edges { if protocol.IsTerminal(e.target) && (!e.topEntry || e.to <= layout.nodes[0].y+layout.nodes[0].height) { t.Fatal("terminals must receive arrows from above, below stages") } } } func TestAdjacentFlowStaysBetweenBoxes(t *testing.T) { source := pipeline.Template([]string{"planner", "implementer", "reviewer"}, true) source = strings.Replace(source, "success -> #success", "changes_requested -> implementer\nsuccess -> #success", 1) graph, err := pipeline.Parse(source) if err != nil { t.Fatal(err) } layout := layoutGraph(graph) nodes := map[string]graphNode{} for _, node := range layout.nodes { nodes[node.key] = node } for _, edge := range layout.edges { if edge.outcome == "success" && !edge.topEntry { from, to := nodes[edge.source], nodes[edge.target] for _, point := range edge.path { if point.x < from.x+from.width-1 || point.x > to.x || point.y < from.y+2 || point.y >= from.y+from.height-1 { t.Fatalf("normal flow leaves the gap: %+v", edge) } } } if edge.outcome == "changes_requested" { above := false for _, point := range edge.path { if point.y < nodes[edge.source].y { above = true } } if !above { t.Fatal("backward routes should retain their upper arc") } } } linear, err := pipeline.Parse(pipeline.Template([]string{"a", "b", "c"}, true)) if err != nil { t.Fatal(err) } if layoutGraph(linear).nodes[0].y != 0 { t.Fatal("normal flow should not reserve empty upper lanes") } } func TestSelectedStageFrameAndNoBlankPadding(t *testing.T) { graph, err := pipeline.Parse(pipeline.Template([]string{"planner", "implementer", "reviewer"}, true)) if err != nil { t.Fatal(err) } for _, selected := range []string{"planner", "implementer", "reviewer"} { rows := renderGraph(graph, selected) view := strings.Join(rows, "\n") if strings.Count(view, "╔") != 1 || strings.Count(view, "╚") != 1 || strings.Contains(view, "▸") { t.Fatal("selected stage needs a distinct frame without a title arrow") } for _, row := range rows { if strings.TrimSpace(row) == "" { t.Fatal("graph has a blank padding row") } } } } func TestOrderedPortsAndBottomFailureExits(t *testing.T) { source := pipeline.Template([]string{"planner", "implementer", "reviewer"}, true) source = strings.Replace(source, "success -> #success", "changes_requested -> implementer\nsuccess -> #success", 1) graph, err := pipeline.Parse(source) if err != nil { t.Fatal(err) } layout := layoutGraph(graph) nodes := map[string]graphNode{} for _, node := range layout.nodes { nodes[node.key] = node } if nodes[protocol.FailureEnd].x >= nodes[protocol.SuccessEnd].x { t.Fatal("failure must be left of success") } for _, edge := range layout.edges { if edge.outcome == "error" { node := nodes[edge.source] if !edge.bottomExit || edge.from != node.y+node.height-2 || edge.path[0].y != node.y+node.height-1 { t.Fatal("failure errors must be last, exiting the bottom border") } } } for _, node := range layout.nodes { if protocol.IsTerminal(node.key) { continue } incoming := 0 for _, edge := range layout.edges { if edge.target == node.key { incoming++ } } if incoming == 0 { continue } title := node.y + 1 for _, edge := range layout.edges { if edge.target == node.key && (edge.to <= title || edge.to >= node.y+node.height-1) { t.Fatal("inputs must stay within compact port rows") } if edge.source == node.key && edge.from <= title { t.Fatal("outcomes must follow the title") } } } view := renderGraph(graph) for row := 0; row < nodes["planner"].y+nodes["planner"].height; row++ { if strings.Contains(view[row], "+") { t.Fatalf("ordered stage ports should avoid crossings in the stage row: %s", view[row]) } } // The existing error may route to a recovery stage: only failure-bound errors // are bottom exits, even though the outcome name is still error. graph.Stages[2].Edges["error"] = "implementer" for _, edge := range layoutGraph(graph).edges { if edge.source == "reviewer" && edge.outcome == "error" && edge.bottomExit { t.Fatal("recovery errors must retain stage-to-stage routing") } } } func TestBothSystemTerminalsAreAlwaysRendered(t *testing.T) { graph, err := pipeline.Parse(strings.Replace(pipeline.Template([]string{"worker"}, true), "success -> #success", "success -> #failure", 1)) if err != nil { t.Fatal(err) } layout := layoutGraph(graph) if len(layout.nodes) != len(graph.Stages)+2 { t.Fatal("system terminals must not be user stages") } for _, target := range []string{protocol.SuccessEnd, protocol.FailureEnd} { if _, ok := layout.positions[target]; !ok { t.Fatalf("missing %s terminal", target) } } view := strings.Join(renderGraph(graph), "\n") if !strings.Contains(view, "SUCCESS") || !strings.Contains(view, "FAILURE") { t.Fatal("both endings should appear even if one is unused") } if layout.positions[protocol.SuccessEnd] != layout.positions[protocol.FailureEnd] { t.Fatal("system terminal nodes must share a row") } found := false for _, line := range renderGraph(graph) { if strings.Contains(line, "SUCCESS") && strings.Contains(line, "FAILURE") { found = true } } if !found { t.Fatal("success and failure titles must render side by side") } } func TestRoutingInvariantsAcrossCyclicGraphs(t *testing.T) { random := rand.New(rand.NewSource(7)) for sample := 0; sample < 40; sample++ { count := 2 + random.Intn(12) graph := protocol.Graph{} for i := 0; i < count; i++ { next := "#" if i+1 < count { next = fmt.Sprintf("stage_%d", i+1) } edges := map[string]string{"success": next, "error": "#"} for branch := 0; branch < 3; branch++ { edges[fmt.Sprintf("route_%d", branch)] = fmt.Sprintf("stage_%d", random.Intn(count)) } graph.Stages = append(graph.Stages, protocol.Stage{Key: fmt.Sprintf("stage_%d", i), Edges: edges}) } layout := layoutGraph(graph) ports := map[string]bool{} for i, a := range layout.edges { for _, port := range []string{fmt.Sprint(a.path[0]), fmt.Sprint(a.path[len(a.path)-1])} { if ports[port] { t.Fatalf("sample %d shares a port", sample) } ports[port] = true } for _, b := range layout.edges[i+1:] { if a.x == b.x && min(a.from, a.to) <= max(b.from, b.to) && min(b.from, b.to) <= max(a.from, a.to) { t.Fatalf("sample %d has overlapping lane segments", sample) } } } view := strings.Join(renderGraph(graph), "\n") if strings.ContainsAny(view, "╳+") { t.Fatal("crossings should retain a route rather than a marker") } bottom := 0 for _, edge := range layout.edges { if edge.bottomExit { bottom++ } } if strings.Count(view, "▼")+strings.Count(view, "▶") != len(layout.edges)+bottom { t.Fatalf("sample %d lost a destination port", sample) } } } func TestLargeGraphRenderingBounded(t *testing.T) { graph := protocol.Graph{} for i := 0; i < 201; i++ { graph.Stages = append(graph.Stages, protocol.Stage{Key: "stage", Edges: map[string]string{"success": "#"}}) } view := strings.Join(renderGraph(graph), "\n") if strings.Count(view, "→ [SUCCESS]") != 201 { t.Fatal("large-graph fallback lost routes") } }