using Microsoft.AspNetCore.Mvc; using PlotLine.Controllers; using PlotLine.Data; using PlotLine.Models; using PlotLine.Services; using PlotLine.ViewModels; using Microsoft.Extensions.Logging.Abstractions; internal static class WritingContextPlotLinesTests { public static async Task Run() { var data=new PlotLineWorkspaceData { PlotLines=[new(){PlotLineID=1,ProjectID=7,BookID=12,PlotLineName="Saved name",Description="Saved description"},new(){PlotLineID=2,ProjectID=7},new(){PlotLineID=3,ProjectID=7,BookID=13}], AttributionScenes=[new(){SceneID=90,BookID=12,NarrativeOrder=20,ChapterNumber=3,SceneNumber=2},new(){SceneID=15,BookID=12,NarrativeOrder=10,ChapterNumber=1,SceneNumber=4},new(){SceneID=44,BookID=13,NarrativeOrder=1}], Contributions=[new(){ThreadEventID=1,PlotLineID=1,SceneID=15,BookID=12,CanonicalTitle="Saved title",CanonicalDescription="Saved detail"},new(){ThreadEventID=2,PlotLineID=1,SceneID=15,BookID=12,CanonicalTitle="Second development"},new(){ThreadEventID=3,PlotLineID=3,SceneID=44,BookID=13}], Threads=[new(){PlotLineID=2,IntroducedSceneID=90,PlannedResolutionSceneID=44}], Targets=[new(){ThreadEventID=1,SourcePlotLineID=1,TargetPlotLineID=2}] }; var model=WritingContextPlotLines.Build(data,7,12); Check(model.AssociationScenes.Select(s=>s.SceneID).SequenceEqual([15,90]),"Use saved book manuscript order, never IDs or invented chapter sizes"); Check(model.PlotLines.Count==2&&model.PlotLines[0].Line.Description=="Saved description","Keep saved identities and exclude other-book lines"); var current=model.Scenes.Single(s=>s.SceneID==15); Check(current.Memberships.Single(m=>m.PlotLineID==1).Contributions.Count==2,"Keep every canonical development in the same scene"); Check(current.Memberships.Single(m=>m.PlotLineID==1).Contributions[0].CanonicalDescription=="Saved detail","Do not replace saved prose with inferred context"); Check(current.Memberships.Single(m=>m.PlotLineID==2).Contributions.Count==0,"Cross-line targets remain associations without invented target developments"); Check(model.Scenes.Single(s=>s.SceneID==90).Memberships.Single().Contributions.Count==0,"Saved milestones remain visible without pretending they contain prose"); var reads=0;var allowed=false; var repository=NativeWorkflowTests.Proxy((n,a)=>{reads++;return Task.FromResult(data);}); var permissions=NativeWorkflowTests.Proxy((n,a)=>Task.FromResult(allowed)); var book= new Book{BookID=12,ProjectID=7}; var books=NativeWorkflowTests.Proxy((n,a)=>Task.FromResult(book)); var controller=new WritingController(null!,null!,null!,null!,NullLogger.Instance); Check(await controller.ContextPlotLines(7,12,repository,permissions,books) is NotFoundResult&&reads==0,"Unauthorised requests must not read saved plot data"); allowed=true;book.ProjectID=8; Check(await controller.ContextPlotLines(7,12,repository,permissions,books) is NotFoundResult&&reads==0,"A book in another project must not leak saved developments"); book.ProjectID=7; Check(await controller.ContextPlotLines(7,12,repository,permissions,books) is PartialViewResult{Model:PlotLineWorkspaceViewModel}&&reads==1,"One read-only book request; no workspace-wide health or second query"); } static void Check(bool value,string message){if(!value)throw new Exception(message);} }