| 1 | using Avalonia; |
| 2 | using CascadeIDE.Cockpit.Graph; |
| 3 | using CascadeIDE.Cockpit.Graph.Layout; |
| 4 | using Xunit; |
| 5 | |
| 6 | namespace CascadeIDE.Tests; |
| 7 | |
| 8 | public sealed class StarGraphLayoutEngineTests |
| 9 | { |
| 10 | [Fact] |
| 11 | public void Layout_PlacesAnchorCenterAndSatellitesOnOrbit() |
| 12 | { |
| 13 | var engine = new StarGraphLayoutEngine(); |
| 14 | var doc = new GraphDocument |
| 15 | { |
| 16 | AnchorPath = @"D:\w\A.cs", |
| 17 | Nodes = |
| 18 | [ |
| 19 | new GraphNode |
| 20 | { |
| 21 | Id = "n0", |
| 22 | Path = @"D:\w\A.cs", |
| 23 | Kind = "anchor", |
| 24 | Label = "A.cs" |
| 25 | }, |
| 26 | new GraphNode |
| 27 | { |
| 28 | Id = "n1", |
| 29 | Path = @"D:\w\B.cs", |
| 30 | Kind = "project_peer", |
| 31 | Label = "B.cs" |
| 32 | }, |
| 33 | new GraphNode |
| 34 | { |
| 35 | Id = "n2", |
| 36 | Path = @"D:\w\C.cs", |
| 37 | Kind = "project_peer", |
| 38 | Label = "C.cs" |
| 39 | } |
| 40 | ], |
| 41 | Edges = |
| 42 | [ |
| 43 | new GraphEdge { FromId = "n0", ToId = "n1", Kind = "related_to" }, |
| 44 | new GraphEdge { FromId = "n0", ToId = "n2", Kind = "related_to" } |
| 45 | ] |
| 46 | }; |
| 47 | |
| 48 | var scene = engine.Layout(doc, 280, 120); |
| 49 | Assert.Equal(3, scene.Nodes.Count); |
| 50 | var anchor = scene.Nodes.First(n => n.IsAnchor); |
| 51 | Assert.Equal(140, anchor.Center.X, 0.5); |
| 52 | Assert.Equal(60, anchor.Center.Y, 0.5); |
| 53 | Assert.True(scene.Edges.Count >= 2); |
| 54 | Assert.All(scene.Edges, e => |
| 55 | { |
| 56 | Assert.False(string.IsNullOrWhiteSpace(e.FromNodeId)); |
| 57 | Assert.False(string.IsNullOrWhiteSpace(e.ToNodeId)); |
| 58 | Assert.True(e.ToRadius > 0); |
| 59 | }); |
| 60 | Assert.Contains(scene.Edges, e => string.Equals(e.Kind, "related_to", StringComparison.Ordinal)); |
| 61 | } |
| 62 | |
| 63 | [Fact] |
| 64 | public void Layout_ManySatellites_SpreadsBeyondCompactCenter() |
| 65 | { |
| 66 | var engine = new StarGraphLayoutEngine(); |
| 67 | var nodes = new List<GraphNode> |
| 68 | { |
| 69 | new() |
| 70 | { |
| 71 | Id = "n0", |
| 72 | Path = @"D:\w\A.cs", |
| 73 | Kind = "anchor", |
| 74 | Label = "A.cs" |
| 75 | } |
| 76 | }; |
| 77 | for (var i = 1; i <= 14; i++) |
| 78 | { |
| 79 | nodes.Add(new GraphNode |
| 80 | { |
| 81 | Id = $"n{i}", |
| 82 | Path = $@"D:\w\F{i}.cs", |
| 83 | Kind = "project_peer", |
| 84 | Label = $"File{i}.cs" |
| 85 | }); |
| 86 | } |
| 87 | |
| 88 | var doc = new GraphDocument |
| 89 | { |
| 90 | AnchorPath = @"D:\w\A.cs", |
| 91 | Nodes = nodes, |
| 92 | Edges = nodes.Skip(1).Select(n => new GraphEdge { FromId = "n0", ToId = n.Id, Kind = "related_to" }).ToList() |
| 93 | }; |
| 94 | |
| 95 | var compact = engine.Layout(doc, 400, 120); |
| 96 | var tall = engine.Layout(doc, 400, 240); |
| 97 | var maxRadiusCompact = compact.Nodes.Where(n => !n.IsAnchor).Max(n => |
| 98 | Math.Sqrt(Math.Pow(n.Center.X - 200, 2) + Math.Pow(n.Center.Y - 60, 2))); |
| 99 | var maxRadiusTall = tall.Nodes.Where(n => !n.IsAnchor).Max(n => |
| 100 | Math.Sqrt(Math.Pow(n.Center.X - 200, 2) + Math.Pow(n.Center.Y - 120, 2))); |
| 101 | Assert.True(maxRadiusTall > maxRadiusCompact + 8); |
| 102 | } |
| 103 | } |
| 104 | |