using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Abstractions; using Microsoft.AspNetCore.Mvc.Filters; using Microsoft.AspNetCore.Routing; using PlotLine.Services; internal static class NarrativeNavigationTests { public static async Task Run() { int writes=0; var focus=NativeWorkflowTests.Proxy((name,args)=>{ if(name!="Open"||args![0] is not int actor||actor!=4)throw new Exception("Unexpected focus operation"); writes++;return Task.FromResult(new(){ProjectID=7,BookID=12,ChapterID=21,SceneID=(int?)args[2],Version=2}); }); var user=NativeWorkflowTests.Proxy((_,_) => (int?)4); foreach(var item in new[]{ ("Scenes","Edit","GET","",true,true), ("Chapters","Details","GET","",true,true), ("Writer","Chapter","GET","",true,true), ("RelationshipMap","State","GET","?sceneId=31",true,false), ("Maps","Story","GET","?sceneId=31",true,false), ("FloorPlans","Edit","GET","?sceneId=31",true,false), ("Timeline","Index","GET","?SelectedSceneID=31",true,false), ("Timeline","Index","GET","?SelectedSceneID=31&navigateFocus=true",true,true), ("Scenes","Edit","GET","?retainNarrativeFocus=true",true,false), ("Scenes","Edit","GET","?ajax",true,false), ("Scenes","Edit","GET","?prefetch",true,false), ("Scenes","Edit","GET","",false,false), ("Scenes","Edit","POST","",true,false)}) { var http=new DefaultHttpContext();http.Request.Method=item.Item3;http.Request.QueryString=new(item.Item4); if(item.Item4=="?ajax")http.Request.Headers["Sec-Fetch-Mode"]="cors";if(item.Item4=="?prefetch")http.Request.Headers["Sec-Purpose"]="prefetch"; var route=new RouteData();route.Values["controller"]=item.Item1;route.Values["action"]=item.Item2; var action=new ActionContext(http,route,new ActionDescriptor());var filter=new NarrativeNavigationFilter(user); var executing=new ActionExecutingContext(action,[],new Dictionary{{"id",31}},new object()); var executed=new ActionExecutedContext(action,[],new object()){Result=item.Item5?new ViewResult():new NotFoundResult()}; var before=writes;await filter.OnActionExecutionAsync(executing,()=>Task.FromResult(executed)); if(writes-before!=0)throw new Exception($"Narrative navigation classification failed: {item}"); } var savedContext=new DefaultHttpContext();savedContext.Request.Method="POST";var savedRoute=new RouteData();savedRoute.Values["controller"]="Scenes"; var saveAction=new ActionContext(savedContext,savedRoute,new ActionDescriptor());var redirect=new RedirectToActionResult("Edit","Scenes",new{id=31}); var savedResult=new ActionExecutedContext(saveAction,[],new object()){Result=redirect}; await new NarrativeNavigationFilter(user).OnActionExecutionAsync(new(saveAction,[],new Dictionary(),new object()),()=>Task.FromResult(savedResult)); if(redirect.RouteValues?["retainNarrativeFocus"] is not true)throw new Exception("Saving an editor must preserve independently navigated focus"); foreach(var navigationController in new Controller[]{new PlotLine.Controllers.NarrativeFocusController(focus,user),new PlotLine.Controllers.SharedFocusController(null!,user)}) { var navigationAction=new ActionExecutingContext(saveAction,[],new Dictionary(),navigationController); bool allowedNavigation=false; await new SubscriptionEnforcementFilter(user,NativeWorkflowTests.Proxy((_,_)=>throw new Exception("Narrative navigation must not require content-edit entitlement"))) .OnActionExecutionAsync(navigationAction,()=>{allowedNavigation=true;return Task.FromResult(new ActionExecutedContext(saveAction,[],navigationController));}); if(!allowedNavigation)throw new Exception("Read-only accounts must retain narrative navigation"); } Console.WriteLine("PASS page loads, refreshes, local inspection and editor return redirects never publish shared focus"); } }