using System.Reflection; using System.Text.Json; using PlotLine.Models; using PlotLine.Services; internal static class MapTopologyTests { static void Check(bool ok,string message){if(!ok)throw new InvalidOperationException(message);} public static MapDocument Document() { var v1=Guid.NewGuid().ToString();var v2=Guid.NewGuid().ToString();var edge=Guid.NewGuid().ToString(); var points=new double[][]{[-.5,-.5],[.5,-.5],[.5,.5],[-.5,.5]}; var a=new MapFeature{MapID=1,Name="Boundary fixture A",Type="County",GeometryClass="Area",Layer="Regions",Width=200,Height=200}; var b=new MapFeature{MapID=1,Name="Boundary fixture B",Type="County",GeometryClass="Area",Layer="Regions",X=200,Width=200,Height=200}; var store=new{version=1,vertices=new Dictionary{{v1,[100,-100]},{v2,[100,100]}},edges=new Dictionary{{edge,new{a=v1,b=v2,shape=new double[][][]{[[1d/3,0],[2d/3,0],[1,0]]},coast="Natural",baseCoast="Natural",style=new{theme="region",borderColor="#80699b",borderStyle="Solid"}}}}}; a.GeometryJson=JsonSerializer.Serialize(new{points,coast="Natural",vertexRefs=new string?[]{null,v1,v2,null},edgeRefs=new string?[]{null,edge,null,null},topologyStore=store}); b.GeometryJson=JsonSerializer.Serialize(new{points,coast="Natural",vertexRefs=new string?[]{v1,null,null,v2},edgeRefs=new string?[]{null,null,null,edge}}); return new(){Map=new(){MapID=1,ProjectID=7,Name="Shared boundaries",Kind="Region"},Features=[a,b]}; } public static void Run() { var d=Document();Check(MapValidation.Validate(d) is null,"Two adjoining areas share valid authoritative vertices/edge."); var original=d.Features[0].GeometryJson;foreach(var invalid in new[]{original.Replace("\"version\":1","\"version\":2"),original.Replace("\"Natural\"","\"unsupported\""),original.Replace("#80699b","url(unsafe)"),original.Replace("[100,-100]","[100,100]"),original.Replace("\"theme\":\"region\"","\"theme\":\"unsafe\"")}){d.Features[0].GeometryJson=invalid;Check(MapValidation.Validate(d) is not null,"Malformed topology, zero-length edges and unsafe styles must fail.");} d.Features[0].GeometryJson=original;d.Features[1].GeometryJson="{}";Check(MapValidation.Validate(d) is not null,"Single-user/orphan definitions cannot be saved."); d=Document();d.Features[1].GeometryJson=d.Features[0].GeometryJson;Check(MapValidation.Validate(d) is not null,"Duplicate definitions cannot be copied into another area."); d=Document();var backup=new ProjectBackupExport{Project=new(){ProjectID=7},Data=new(){StoryMaps=[d.Map],MapFeatures=d.Features}};var imported=JsonSerializer.Deserialize(JsonSerializer.Serialize(backup))!; var validate=typeof(ProjectRestoreService).GetMethod("ValidateBackup",BindingFlags.Static|BindingFlags.NonPublic)!;Check(validate.Invoke(null,[7,imported]) is null,"Project export/import/restore validation accepts the entire shared graph."); Check(imported.Data.MapFeatures[0].GeometryJson==d.Features[0].GeometryJson&&imported.Data.MapFeatures[1].GeometryJson==d.Features[1].GeometryJson,"Shared references and definitions round-trip byte-for-byte."); imported.Data.MapFeatures=imported.Data.MapFeatures.Take(1).ToList();Check(validate.Invoke(null,[7,imported]) is string,"Import/restore refuses incomplete topology."); Console.WriteLine("PASS shared boundary server integrity, versions/refs/appearance, duplicate/orphan rejection and project import/export/restore contracts."); } }