37 lines
3.5 KiB
C#
37 lines
3.5 KiB
C#
using System.Reflection;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using PlotLine.Controllers;
|
|
using PlotLine.Data;
|
|
using PlotLine.Models;
|
|
using PlotLine.Services;
|
|
using PlotLine.ViewModels;
|
|
internal static class WriterDashboardTests
|
|
{
|
|
public static async Task Run()
|
|
{
|
|
void Check(bool value,string message){if(!value)throw new Exception(message);Console.WriteLine("PASS Writer dashboard "+message);}
|
|
int? scheduledBook=null;var allowed=true;
|
|
var workspace=Proxy<IWriterWorkspaceService>((method,args)=>method=="GetDashboardAsync"?Task.FromResult(new WriterDashboardViewModel{ProjectID=1,WriteNext=[new(){BookID=2,SceneID=20},new(){BookID=3,SceneID=30}]}):throw new Exception(method));
|
|
var schedule=Proxy<IWritingScheduleService>((method,args)=>{if(method=="GetScheduleSetupAsync")return Task.FromResult(new WritingScheduleSetupViewModel{ProjectID=(int?)args![0]});if(method=="PopulateScheduleSetupOptionsAsync")return Task.CompletedTask;if(method!="GetTodayDashboardAsync")throw new Exception(method);scheduledBook=(int?)args![1];return Task.FromResult(new WritingScheduleDashboardViewModel{TodayTasks=[new(){BookID=2,SceneID=20},new(){BookID=3,SceneID=30}]});});
|
|
var books=Proxy<IBookRepository>((method,args)=>method switch{"ListByProjectAsync"=>Task.FromResult<IReadOnlyList<Book>>([new(){BookID=2,ProjectID=1},new(){BookID=3,ProjectID=1}]),"GetAsync"=>Task.FromResult<Book?>(new(){BookID=(int)args![0]!,ProjectID=1}),_=>throw new Exception(method)});
|
|
var access=Proxy<IProjectAccessService>((method,args)=>Task.FromResult(allowed));
|
|
var controller=new WriterController(workspace,schedule,books,access);
|
|
var result=(ViewResult)await controller.Index(1,3,"Revision");var model=(WriterDashboardViewModel)result.Model!;
|
|
Check(model.BookID==3&&model.WriteNext.Single().SceneID==30&&model.WritingSchedule.TodayTasks.Single().SceneID==30,"book selection filters queues and tasks consistently");
|
|
Check(scheduledBook==3&&model.Workspace=="Revision","book scope reaches schedule service before selection and active workspace persists");
|
|
Check(await controller.Index(1,999) is NotFoundResult,"unknown book cannot fall back to another book");
|
|
var setup = (ViewResult)await controller.ScheduleSetup(1, 3);
|
|
Check(((WritingScheduleSetupViewModel)setup.Model!).BookID == 3, "plan setup retains selected book instead of All Books");
|
|
Check(await controller.ScheduleSetup(99, 3) is NotFoundResult, "plan setup refuses conflicting project and book");
|
|
allowed=false;Check(await controller.Index(null,3) is NotFoundResult,"book-only deep link checks access before metadata");
|
|
Check(controller.LegacyWrite(1,3) is RedirectToActionResult redirect&&(int?)redirect.RouteValues!["bookId"]==3,"legacy Write redirect preserves book scope");
|
|
var ribbon=ApplicationRibbon.Categories.Single(c=>c.Key=="writer");Check(ribbon.Groups.SelectMany(g=>g.Commands).Any(c=>c.Action=="Workspaces")&&ribbon.Groups.SelectMany(g=>g.Commands).Any(c=>c.Controller=="WritingActivity"),"Writer contextual commands target implemented workspaces and evidence");
|
|
}
|
|
private static T Proxy<T>(Func<string,object?[]?,object?> action)where T:class {var value=DispatchProxy.Create<T,WriterProxy>();((WriterProxy)(object)value).Action=action;return value;}
|
|
public class WriterProxy:DispatchProxy
|
|
{
|
|
public Func<string,object?[]?,object?> Action {get;set;}=null!;
|
|
protected override object? Invoke(MethodInfo? targetMethod,object?[]? args)=>Action(targetMethod!.Name,args);
|
|
}
|
|
}
|