using System.Reflection; using System.Text.Json; using PlotLine.Data; using PlotLine.Models; using PlotLine.Services; internal static class MapStoryTests { public static readonly Guid A = Guid.Parse("00000000-0000-0000-0000-000000000001"), B = Guid.Parse("00000000-0000-0000-0000-000000000002"), Child = Guid.Parse("00000000-0000-0000-0000-000000000003"); static void Check(bool ok,string message) { if(!ok) throw new InvalidOperationException(message); } public static MapStoryData Fixture() { var ids = new[] { 90, 10, 70, 20, 80, 30 }; int?[] locations = [10,20,10,null,30,40]; return new() { Maps = [new() { MapID=1,ProjectID=7,Name="Story region",Kind="Region",Revision=12 },new() {MapID=2,ProjectID=7,Name="House detail",Kind="Local Area",ParentMapID=1,ParentFeatureID=A}], Features = [new() {MapID=1,FeatureID=A,Name="Map alias A",LocationID=10,X=-200,Y=0,IsProvisional=false,FloorPlanID=101},new(){MapID=1,FeatureID=B,Name="Map alias B",LocationID=20,X=250,Y=100,IsProvisional=false},new(){MapID=1,Name="Unmapped town",LocationID=null,X=0,Y=300,IsProvisional=false},new(){MapID=2,FeatureID=Child,Name="House room",LocationID=40,IsProvisional=false}], Relationships=[new() {MapID=1,SourceID=A,TargetID=B,Direction="W",RelativeDistance="Far",MeasuredDistance=126,Unit="kilometres",TypicalTravelTime="3h 30m"}], Locations=[new(){LocationID=10,ProjectID=7,LocationName="Harbour",Description="A working harbour."},new(){LocationID=20,ProjectID=7,LocationName="Hill town"},new(){LocationID=30,ProjectID=7,LocationName="Unmapped town"},new(){LocationID=40,ParentLocationID=10,ProjectID=7,LocationName="House room"}], FloorPlans=[new(){FloorPlanID=101,ProjectID=7,LocationID=40,Name="House floor plan"}], Characters=[new(){CharacterID=1,CharacterName="Ari",CharacterImportance=9},new(){CharacterID=2,CharacterName="Bea",CharacterImportance=8},new(){CharacterID=3,CharacterName="Mentioned person"},new(){CharacterID=4,CharacterName="Unknown person"}], Scenes=ids.Select((id,i)=>new MapStoryScene{SceneID=id,BookID=9,BookTitle="The crossing",ChapterID=i<3?11:22,ChapterNumber=i<3?1:2,ChapterTitle=i<3?"Departure":"Return",NarrativeOrder=i+1,SceneNumber=i+1,SceneTitle=new[]{"Harbour morning","Hill arrival","Harbour revisit","Unknown interval","Unmapped scene","House discovery"}[i],PrimaryLocationID=locations[i],FloorPlanID=i==5?101:null,StartDateTime=new DateTime(2026,1,1,9,0,0).AddMinutes(i*30),TimeModeName="Exact DateTime",TimeConfidenceName="Confirmed"}).ToList(), Appearances=ids.Select((id,i)=>new SceneCharacter {SceneCharacterID=i+1,SceneID=id,CharacterID=1,PresenceTypeName="Present"}).Concat([ new SceneCharacter{SceneCharacterID=20,SceneID=90,CharacterID=2,LocationID=20,PresenceTypeName="Present"}, new SceneCharacter{SceneCharacterID=21,SceneID=20,CharacterID=2,PresenceTypeName="Unknown"}, new SceneCharacter{SceneCharacterID=22,SceneID=90,CharacterID=3,PresenceTypeName="Mentioned Only",LocationID=20} ]).ToList() }; } public static PlotLineWorkspaceData Plots() => new() { PlotLines=[new(){PlotLineID=5,ProjectID=7,PlotLineName="The search"},new(){PlotLineID=6,ProjectID=7,PlotLineName="The return"}], Contributions=[new(){PlotLineID=5,SceneID=90},new(){PlotLineID=5,SceneID=70},new(){PlotLineID=5,SceneID=80},new(){PlotLineID=6,SceneID=10},new(){PlotLineID=6,SceneID=30}] }; public static MapsViewModel Workspace() { var f=Fixture();return new(){Project=new(){ProjectID=7,ProjectName="Story map fixture"},Maps=f.Maps,Locations=f.Locations,FloorPlans=f.FloorPlans, Document=new(){Map=f.Maps[0],Features=f.Features.Where(x=>x.MapID==1).ToList(),Relationships=f.Relationships}}; } public static void ProjectionAndEvidence() { var input=Fixture();var geography=JsonSerializer.Serialize(new{input.Maps,input.Features,input.Relationships}); var p=MapStoryComposer.Compose(input,Plots(),1); Check(p.Scenes.Select(s=>s.SceneID).SequenceEqual([90,10,70,20,80,30]),"Scene IDs must not replace canonical manuscript order."); Check(p.Scenes[0].Position.FeatureID==A&&p.Scenes[1].Position.FeatureID==B,"Join canonical Location IDs, despite map aliases."); Check(p.Scenes[4].Position.Status=="Unmapped"&&p.Scenes[4].Position.FeatureID is null,"Matching names must never infer association."); Check(p.Scenes[3].Position.Status=="No Location","No Location differs from unmapped."); Check(p.Scenes[5].Position.FeatureID==A&&p.Scenes[5].Position.Status=="Ancestor location"&&p.Scenes[5].Position.DetailMapID==2,"Canonical ancestor and explicit detailed map must be preserved."); Check(p.Scenes[5].Position.FloorPlanID==101,"Floor Plan link must follow explicit hierarchy."); Check(p.Scenes[0].PlotLineIDs.SequenceEqual([5])&&p.Scenes[1].PlotLineIDs.SequenceEqual([6]),"Membership comes from canonical workspace contributions."); var stops=p.Stops.Where(x=>x.CharacterID==1).ToList(); Check(stops.Select(s=>s.Position.LocationID).SequenceEqual(new int?[]{10,20,10,null,30,40}),"A→B→A revisits and unknown gaps must remain in sequence."); Check(!p.Stops.Any(s=>s.CharacterID==3),"Mention-only membership is not location evidence."); Check(p.Stops.Single(s=>s.CharacterID==2&&s.SceneID==90).Position.LocationID==20,"Explicit character Location overrides scene Location."); Check(p.Stops.Single(s=>s.CharacterID==2&&s.SceneID==20).Position.LocationID is null,"Unknown appearance does not inherit a scene position."); var leg=p.Legs.First(l=>l.CharacterID==1); Check(leg.TimingObservation&&leg.EvidenceLevel==3&&leg.TypicalMinutes==210&&leg.ElapsedMinutes==30&&leg.MeasuredDistance==126,"Confirmed times and explicit duration support a gentle observation."); Check(p.Legs.Single(l=>l.CharacterID==1&&l.FromSceneID==10).Direction=="E","Reverse relationship direction is contextual and explicit."); Check(p.Legs.Where(l=>l.From.LocationID is null||l.To.LocationID is null).All(l=>!l.TimingObservation),"Unknown gaps cannot create travel claims."); Check(JsonSerializer.Serialize(new{input.Maps,input.Features,input.Relationships})==geography,"Projection must not mutate geography or revisions."); var f=Fixture();f.Relationships.Clear();f.Features[1].X=100000; Check(MapStoryComposer.Compose(f,Plots(),1).Legs.All(l=>!l.TimingObservation&&l.MeasuredDistance is null),"Canvas spacing never establishes distance or travel time."); f=Fixture();f.Relationships[0].TypicalTravelTime=null; Check(MapStoryComposer.Compose(f,Plots(),1).Legs.All(l=>!l.TimingObservation),"Distance alone is not travel duration."); f=Fixture();f.Scenes[1].TimeModeName="Exact Date"; Check(!MapStoryComposer.Compose(f,Plots(),1).Legs.First().TimingObservation,"Date-only evidence cannot support minute-level claims."); f=Fixture();f.Scenes[1].TimeConfidenceName="Estimated"; Check(!MapStoryComposer.Compose(f,Plots(),1).Legs.First().TimingObservation,"Estimated time cannot support a strong observation."); f=Fixture();f.Features[0].IsProvisional=true; Check(!MapStoryComposer.Compose(f,Plots(),1).Legs.First().TimingObservation,"Planning geography does not silently establish travel facts."); Check(MapStoryComposer.ParseMinutes("3h 30m")==210&&MapStoryComposer.ParseMinutes("2 hours")==120&&MapStoryComposer.ParseMinutes("possibly two hours") is null&&MapStoryComposer.ParseMinutes("2 hours by portal") is null,"Only explicit supported duration syntax is parsed."); f=Fixture();f.Appearances[0].ExitLocationID=20;f.Scenes[0].EndDateTime=new DateTime(2026,1,1,11,0,0); var exit=MapStoryComposer.Compose(f,Plots(),1).Stops.First();Check(exit.ObservedAt==f.Scenes[0].EndDateTime&&exit.TimePrecision=="Timestamp","An explicit exit Location is observed at scene end, never backdated to scene start."); f.Scenes[0].EndDateTime=null;Check(MapStoryComposer.Compose(f,Plots(),1).Stops.First().TimePrecision is null,"Exit Location without an end time cannot acquire a timestamp."); var resolver=MapStoryComposer.Resolver(Fixture(),2); Check(resolver(20).Status=="On ancestor map"&&resolver(20).FeatureID is null,"A detailed map must not fake ancestor positions on its own canvas."); } public static void Scale() { var input=Fixture();input.Characters=Enumerable.Range(1,30).Select(i=>new Character{CharacterID=i,CharacterName=$"Person {i}",CharacterImportance=i%10}).ToList(); input.Scenes=Enumerable.Range(1,500).Select(i=>new MapStoryScene{SceneID=1000+i,BookID=9,ChapterID=11,NarrativeOrder=i,SceneNumber=i,SceneTitle=$"Scene {i}",PrimaryLocationID=i%5==0?null:i%3==0?30:i%2==0?10:20,StartDateTime=new DateTime(2026,1,1).AddHours(i),TimeModeName="Exact DateTime",TimeConfidenceName="Confirmed"}).ToList(); input.Appearances=input.Scenes.SelectMany((scene,index)=>Enumerable.Range(0,10).Select(j=>new SceneCharacter{SceneID=scene.SceneID,CharacterID=(index+j)%30+1,PresenceTypeName="Present"})).ToList(); var clock=System.Diagnostics.Stopwatch.StartNew();var projection=MapStoryComposer.Compose(input,Plots(),1); Check(projection.Scenes.Count==500&&projection.Characters.Count==30&&projection.Stops.Count==5000&&projection.Legs.Count==4970,"A substantial manuscript retains every physical appearance and per-character transition."); Console.WriteLine($"Maps Story scale: 500 scenes, 30 characters, 5,000 appearances in {clock.ElapsedMilliseconds}ms."); } public static void SecurityAndBatching() { int calls=0; T Proxy(Func handler) where T:class {var p=DispatchProxy.Create();((StoryPointProxy)(object)p).Handler=handler;return p;} var denied=Proxy((_,_)=>Task.FromResult(false)); var repo=Proxy((_,_)=>{calls++;return Task.FromResult(Fixture());}); var plots=Proxy((_,_)=>{calls++;return Task.FromResult(Plots());}); var scenarios=Proxy((_,_)=>{calls++;return Task.FromResult>([]);}); Check(new MapStoryService(repo,plots,scenarios,denied).GetAsync(7,1,4).Result is null&&calls==0,"Denied access must short circuit every batch."); var allowed=Proxy((_,_)=>Task.FromResult(true)); Check(new MapStoryService(repo,plots,scenarios,allowed).GetAsync(7,1,4).Result?.Scenes.Count==6&&calls==3,"Query count is fixed: source, canonical memberships and scenario choices, never per-marker."); calls=0;Check(new MapStoryService(repo,plots,scenarios,allowed).GetAsync(7,1,4,999).Result is null&&calls==1,"Foreign/unknown scenario must fail before loading source or memberships."); var alternatives=Proxy((_,_)=>Task.FromResult>([new(){ScenarioID=55,ProjectID=7,ScenarioName="Alternative order"}])); var scenarioPlots=Proxy((_,args)=>{Check((int?)args![2]==55,"Scenario is passed to the canonical Plot Lines projection.");return Task.FromResult(new PlotLineWorkspaceData{AttributionScenes=[new(){SceneID=70,NarrativeOrder=1,BookID=9,ChapterID=22,ChapterNumber=2,ChapterTitle="Return"},new(){SceneID=90,NarrativeOrder=2,BookID=9,ChapterID=22,ChapterNumber=2,ChapterTitle="Return"}]});}); var alternative=new MapStoryService(repo,scenarioPlots,alternatives,allowed).GetAsync(7,1,4,55).Result!; Check(alternative.Scenes.Select(s=>s.SceneID).SequenceEqual([70,90])&&alternative.Scenes.All(s=>s.ChapterID==22),"Scenario sequence and proposed chapter footprint must reuse the workspace projection."); var controller=new PlotLine.Controllers.MapsController(null!,null!,null!,null!,denied,null!); Check(controller.Story(7,1,null,null!).Result is Microsoft.AspNetCore.Mvc.NotFoundResult,"Story controller enforces access."); Check(controller.Journey(7,null,new(),null!,null!).Result is Microsoft.AspNetCore.Mvc.NotFoundResult,"Travel annotation controller enforces access."); } }