77 lines
5.4 KiB
C#
77 lines
5.4 KiB
C#
using System.Reflection;
|
|
using Microsoft.AspNetCore.Http;
|
|
using PlotLine.Data;
|
|
using PlotLine.Models;
|
|
using PlotLine.Services;
|
|
using PlotLine.ViewModels;
|
|
|
|
internal static class ApplicationNavigationTests
|
|
{
|
|
public static void ContextAndPermissions() => Run().GetAwaiter().GetResult();
|
|
|
|
private static async Task Run()
|
|
{
|
|
var project = new Project { ProjectID = 7, ProjectName = "Navigation project" };
|
|
var book = new Book { BookID = 12, ProjectID = 7, BookTitle = "Volume two" };
|
|
var projectCalls = 0;
|
|
var bookCalls = 0;
|
|
var projects = Proxy<IProjectRepository>((name, args) =>
|
|
{
|
|
if (name != "GetForUserAsync") throw new Exception(name);
|
|
projectCalls++;
|
|
Check((int)args![1]! == 4, "Project lookup must use current user.");
|
|
return Task.FromResult((int)args[0]! == 7 ? project : null);
|
|
});
|
|
var books = Proxy<IBookRepository>((name, args) =>
|
|
{
|
|
if (name != "ListByProjectAsync") throw new Exception(name);
|
|
bookCalls++;
|
|
Check((int)args![0]! == 7, "Never load books for an inaccessible project.");
|
|
return Task.FromResult<IReadOnlyList<Book>>([book, new() { BookID = 13, ProjectID = 7, IsArchived = true }]);
|
|
});
|
|
var user = Proxy<ICurrentUserService>((_, _) => (int?)4);
|
|
var navigation = new ApplicationNavigation(projects, books, user);
|
|
var request = new DefaultHttpContext().Request;
|
|
request.RouteValues["controller"] = "Names";
|
|
var names = await navigation.BuildAsync(new NamesLibraryViewModel { Project = project, Filter = new() { BookID = 12 } }, null, request);
|
|
Check(names.Book == book && names.Project == project && names.ActiveSection == "Names", "Names should preserve selected book and active section.");
|
|
Check(names.Books.Count == 1, "Archived books must not appear in switcher.");
|
|
var all = await navigation.BuildAsync(new TimelineViewModel { Project = project }, "Timeline", request);
|
|
Check(all.Book is null, "Project-wide timeline must not invent a current book.");
|
|
request.QueryString = new QueryString("?bookId=12");
|
|
var calendar = await navigation.BuildAsync(new StoryCalendarViewModel { Project = project }, "Calendar", request);
|
|
Check(calendar.Book is null, "An all-books page model must take precedence over a URL hint.");
|
|
var point = await navigation.BuildAsync(new StoryPointViewModel { Project = project, Snapshot = new() { Book = book } }, "At This Point", request);
|
|
Check(point.Book == book, "Story Point should use the snapshot book.");
|
|
var chapter = await navigation.BuildAsync(new ChapterDetailViewModel { Project = project, Book = book }, "Overview", request);
|
|
Check(chapter.Book == book, "Chapter context should retain its book.");
|
|
var badBook = await navigation.BuildAsync(new NamesLibraryViewModel { Project = project, Filter = new() { BookID = 999 } }, null, request);
|
|
Check(badBook.Book is null, "Book from another project must not enter the menu.");
|
|
request.QueryString = QueryString.Empty;
|
|
var writing = await navigation.BuildAsync(new WritingContextPageViewModel { ProjectID = 7, BookID = 12 }, "Writer", request);
|
|
Check(writing.Project == project && writing.Book == book, "Writing window scope should resolve without changing it.");
|
|
var focus = Proxy<INarrativeFocus>((name,args)=>name=="Read"?Task.FromResult(new NarrativeFocusState{ProjectID=7,BookID=12,ChapterID=22,SceneID=32,Version=6}):throw new Exception("Navigation rendering must not commit focus"));
|
|
var shared = new ApplicationNavigation(projects,books,user,focus);
|
|
var writer = await shared.BuildAsync(new WriterDashboardViewModel{ProjectID=7,BookID=12,Destination="Write",Objective=new(){ChapterID=21,SceneID=31}},"Writer",request);
|
|
Check(writer.WordChapterID==22&&writer.WordSceneID==32,"Word intention comes from authoritative navigation, never a saved objective.");
|
|
var explorer = await shared.BuildAsync(new {ProjectID=7,BookID=12,SceneID=999,ChapterID=999},"Relationships",request);
|
|
Check(explorer.WordChapterID==22&&explorer.WordSceneID==32,"Local visualisation position cannot become a global or Word target.");
|
|
var before = bookCalls;
|
|
var denied = await navigation.BuildAsync(new WritingIntentPageViewModel { ProjectID = 88, BookID = 12 }, "Writer", request);
|
|
Check(denied.Project is null && denied.Books.Count == 0 && bookCalls == before, "Unauthorised scope must not expose books or project actions.");
|
|
var global = await navigation.BuildAsync(new object(), null, request);
|
|
Check(global.Project is null, "Global pages must not inherit a prior project's context.");
|
|
var anonymous = new ApplicationNavigation(projects, books, Proxy<ICurrentUserService>((_, _) => null));
|
|
before = projectCalls;
|
|
Check((await anonymous.BuildAsync(new NamesLibraryViewModel { Project = project }, null, request)).Project is null && before == projectCalls, "Anonymous navigation must not load project data.");
|
|
}
|
|
|
|
private static T Proxy<T>(Func<string, object?[]?, object?> handler) where T : class
|
|
{
|
|
var proxy = DispatchProxy.Create<T, StoryPointProxy>();
|
|
((StoryPointProxy)(object)proxy).Handler = handler;
|
|
return proxy;
|
|
}
|
|
private static void Check(bool condition, string message) { if (!condition) throw new InvalidOperationException(message); }
|
|
}
|