PlotDirector/PlotLine.Tests/WriterNavigationTests.cs

60 lines
7.0 KiB
C#

using Microsoft.AspNetCore.Mvc;
using PlotLine.Controllers;
using PlotLine.Data;
using PlotLine.Models;
using PlotLine.Services;
using PlotLine.ViewModels;
internal static class WriterNavigationTests
{
public static async Task Run()
{
void Check(bool ok,string why){if(!ok)throw new Exception(why);Console.WriteLine("PASS Writer navigation "+why);}
Check(PlotLine.Services.Writer.WriterFocusActivities.SessionKind("Drafting")=="Writing" && PlotLine.Services.Writer.WriterFocusActivities.SessionKind("Researching")=="Planning" && PlotLine.Services.Writer.WriterFocusActivities.SessionKind("Revising")=="Editing", "objective activity mappings use supported session types");
Check(PlotLine.Services.Writer.WriterFocusActivities.SessionKind("Proofreading")==null && PlotLine.Services.Writer.WriterFocusActivities.SessionKind("Unknown")==null, "ambiguous or unsupported recording activity requires explicit compatible choice");
Check(!PlotLine.Services.Writer.WriterFocusActivities.ObjectiveKinds.Contains("AudioReview") && PlotLine.Services.Writer.WriterFocusActivities.SessionKind("AudioReview")=="AudioReview", "session-only activities never become objective types");
var allowed=true;int reads=0;
var workspace=NativeWorkflowTests.Proxy<IWriterWorkspaceService>((n,a)=>
{
if(n!="GetDashboardAsync")throw new Exception("Navigation attempted a write or workflow initialisation: "+n);
reads++;
return Task.FromResult(new WriterDashboardViewModel{ProjectID=7,Objective=(int?)a![1]==12?new(){BookID=12,ChapterID=21,SceneID=31,SceneTitle="Saved scene",Activity="Revising"}:null});
});
var books=NativeWorkflowTests.Proxy<IBookRepository>((n,a)=>n switch{
"ListByProjectAsync"=>Task.FromResult<IReadOnlyList<Book>>([new(){BookID=12,ProjectID=7,BookTitle="First"},new(){BookID=13,ProjectID=7,BookTitle="Second"}]),
"GetAsync"=>Task.FromResult<Book?>((int)a![0]! is 12 or 13?new(){BookID=(int)a[0]!,ProjectID=7}:null),
_=>throw new Exception(n)});
var schedules=NativeWorkflowTests.Proxy<IWritingScheduleService>((n,a)=>n=="GetTodayDashboardAsync"?Task.FromResult(new WritingScheduleDashboardViewModel()):throw new Exception("Navigation attempted schedule mutation "+n));
var access=NativeWorkflowTests.Proxy<IProjectAccessService>((n,a)=>Task.FromResult(allowed));
var focus=NativeWorkflowTests.Proxy<INarrativeFocus>((name,args)=>name=="Read"?Task.FromResult(new NarrativeFocusState{ProjectID=7,BookID=(int)args![2]!,ChapterID=(int)args[2]! ==12?22:null,SceneID=(int)args[2]! ==12?99:null,Version=5}):throw new Exception("Page display must not write focus: "+name));
var controller=new WriterController(workspace,schedules,books,access,narrativeFocus:focus){ControllerContext=new(){HttpContext=new Microsoft.AspNetCore.Http.DefaultHttpContext{User=new System.Security.Claims.ClaimsPrincipal(new System.Security.Claims.ClaimsIdentity([new System.Security.Claims.Claim(System.Security.Claims.ClaimTypes.NameIdentifier,"4")],"Fixture"))}}};
var routes=new Dictionary<string,Func<int,Task<IActionResult>>>{
["Home"]=b=>controller.Index(7,b),["Write"]=b=>controller.Write(7,b),["Explore"]=b=>controller.Explore(7,b),
["Plan"]=b=>controller.Plan(7,b),["Review"]=b=>controller.Review(7,b),["Reflect"]=b=>controller.Reflect(7,b),["Dashboard"]=b=>controller.Dashboard(7,b)};
foreach(var (destination,load) in routes){
var first=(ViewResult)await load(12);var model=(WriterDashboardViewModel)first.Model!;
Check(model.Destination==destination&&model.BookID==12&&model.WorkingContext?.Working?.SceneID==99&&model.WorkingContext.SavedObjective?.SceneID==31&&model.Objective?.SceneID==31,"destination "+destination+" uses shared current location while preserving the independent objective");
var second=(ViewResult)await load(13);model=(WriterDashboardViewModel)second.Model!;
Check(model.BookID==13&&model.Objective==null&&model.WorkingContext?.Working?.SceneID==null&&model.WorkingContext?.SavedObjective==null,"destination "+destination+" never carries a focus into another book");
Check(await load(999) is NotFoundResult,"destination "+destination+" refuses unavailable book");
}
Check((await controller.Index(7,12,workspace:"Revision") is RedirectToActionResult queueAlias&&queueAlias.ActionName=="Workspaces"&&(string?)queueAlias.RouteValues!["workspace"]=="Revision")&&(await controller.Index(7,12,journey:true) is RedirectToActionResult reflection&&reflection.ActionName=="Reflect"),"legacy query routes still reach their actual workspaces/calendar");
Check(((WriterDashboardViewModel)((ViewResult)await controller.Index(7,12,setup:true)).Model!).Destination=="Write","old objective setup URL reaches Write");
Check(((ViewResult)await controller.Workspaces(7,12)).ViewName=="Legacy"&&(controller.Journey(7,12) is RedirectToActionResult journey&&journey.ActionName=="Reflect")&&((ViewResult)await controller.Overview(7,12)).ViewName=="Legacy","legacy desk and queues remain available; Journey redirects to Reflect");
allowed=false;Check(await controller.Write(null,12) is NotFoundResult&&await controller.Explore(null,12) is NotFoundResult,"book-only new route checks access before metadata");
foreach(var destination in WriterNavigation.Destinations){
Check(WriterNavigation.BookSwitchAction(destination)==destination,"book switching retains "+destination+" without chapter parameters");
var groups=WriterNavigation.RibbonGroups(destination);
Check(!groups.Any(g=>g.Label=="Utilities"&&g.Commands.Any(c=>c.Action=="Workspaces"))&&!groups.SelectMany(g=>g.Commands).Any(c=>c.Label=="Writing desk"),"no duplicate desk/workspace destinations on "+destination);
Check(groups[0].Commands.Select(x=>x.Action).SequenceEqual(new[]{"Index","Write","Explore","Plan","Review","Reflect"}),"single existing ribbon exposes all destinations on "+destination);
}
Check(WriterNavigation.BookSwitchAction("write")=="Write" && WriterNavigation.DestinationForAction("schedulepreview")=="Plan","case-insensitive bookmarked routes retain destination context");
Check(WriterNavigation.BookSwitchAction("SaveObjective")=="Index","book menu never navigates to a mutation action");
var noBooks=NativeWorkflowTests.Proxy<IWriterWorkspaceService>((n,a)=>n=="GetDashboardAsync"?Task.FromResult(new WriterDashboardViewModel()):throw new Exception("Empty-book navigation attempted a write"));
var empty=new WriterController(noBooks,schedules);
Check(((WriterDashboardViewModel)((ViewResult)await empty.Index(null)).Model!).BookID==null,"new author Home has no fabricated book selection");
Check(((WriterDashboardViewModel)((ViewResult)await empty.Explore(null)).Model!).Destination=="Explore","exploration does not require scenes, plans, project or Word");
Check(reads>0,"real read paths exercised without calling author-data writes");
}
}