89 lines
6.5 KiB
C#
89 lines
6.5 KiB
C#
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.AspNetCore.Mvc.ViewFeatures;
|
|
using PlotLine.Controllers;
|
|
using PlotLine.Data;
|
|
using PlotLine.Models;
|
|
using PlotLine.Services;
|
|
using PlotLine.Services.Writer;
|
|
using PlotLine.ViewModels;
|
|
|
|
internal static class WriterFoundationTests
|
|
{
|
|
public static async Task Run()
|
|
{
|
|
void Check(bool ok, string text) { if (!ok) throw new Exception(text); Console.WriteLine("PASS Writer foundation " + text); }
|
|
var working = new WriterSelection(1, 2, 10, 20);
|
|
var context = new WriterContext(1, 2).Select(WriterFocusRole.Working, working)
|
|
.Select(WriterFocusRole.SavedObjective, working)
|
|
.Select(WriterFocusRole.Exploration, new(1, 2, 11, 21))
|
|
.Select(WriterFocusRole.PinnedReference, new(1, 2, 12, 22))
|
|
.Select(WriterFocusRole.Reader, new(1, 2, 13))
|
|
.Select(WriterFocusRole.Session, new(1, 2, 14));
|
|
context = context.WithVerifiedWord(new(new(1, 2, 15, 25), Guid.NewGuid(), Guid.NewGuid(), Guid.NewGuid(), 4, 3));
|
|
Check(context.Working == working && context.SavedObjective == working && context.Reader!.ChapterID == 13 && context.Session!.ChapterID == 14 && context.VerifiedWord!.Selection.SceneID == 25,
|
|
"reference, Reader, session and verified Word selections never overwrite working focus");
|
|
var refused = false;
|
|
try { context.Select(WriterFocusRole.Exploration, new(1, 3, 11)); } catch (ArgumentException) { refused = true; }
|
|
Check(refused, "cross-book selections require a separate authorised context");
|
|
refused = false; try { _ = new WriterSelection(1, 2, null, 20); } catch (ArgumentException) { refused = true; }
|
|
Check(refused, "a scene cannot be projected without its chapter");
|
|
refused = false; try { context.WithVerifiedWord(new(working, Guid.Empty, Guid.NewGuid(), Guid.NewGuid(), 4, 3)); } catch (ArgumentException) { refused = true; }
|
|
Check(refused, "incomplete native identity cannot be projected as verified");
|
|
|
|
T Proxy<T>(Func<string, object?[]?, object?> f) where T : class => NativeWorkflowTests.Proxy<T>(f);
|
|
var user = Proxy<ICurrentUserService>((n,a) => n == "get_UserId" ? (int?)7 : throw new Exception(n));
|
|
var projects = Proxy<IProjectRepository>((n,a) => n == "ListForUserAsync" ? Task.FromResult<IReadOnlyList<Project>>([new(){ProjectID=1}]) : throw new Exception(n));
|
|
var books = Proxy<IBookRepository>((n,a) => throw new Exception(n));
|
|
var chapters = Proxy<IChapterRepository>((n,a) => throw new Exception(n));
|
|
var scopes = new List<int?>();
|
|
var repository = Proxy<IWriterWorkspaceRepository>((n,a) => n switch
|
|
{
|
|
"GetWriterDashboardScenesAsync" => Queue(a),
|
|
"GetWriterDashboardRemindersAsync" => Task.FromResult<IReadOnlyList<StoryBibleAttentionItem>>([]),
|
|
_ => throw new Exception(n)
|
|
});
|
|
Task<IReadOnlyList<WriterDashboardScene>> Queue(object?[]? args)
|
|
{
|
|
scopes.Add((int?)args![2]);
|
|
return Task.FromResult<IReadOnlyList<WriterDashboardScene>>([new(){BookID=3, SceneID=99}]);
|
|
}
|
|
await new WriterWorkspaceService(projects, books, chapters, repository, user).GetDashboardAsync(1, 3);
|
|
Check(scopes.Count == 4 && scopes.All(x => x == 3), "selected book reaches every queue repository before any result limit");
|
|
scopes.Clear();
|
|
await new WriterWorkspaceService(projects, books, chapters, repository, user).GetDashboardAsync(999, 3);
|
|
Check(scopes.Count == 0, "inaccessible project never reaches a queue repository");
|
|
|
|
var missingStory = Proxy<IStoryContextService>((n,a) => Task.FromResult<StoryContextViewModel?>(null));
|
|
var references = new WritingController(missingStory, null!, null!, Microsoft.Extensions.Logging.Abstractions.NullLogger<WritingController>.Instance);
|
|
Check(await references.Context(1,2,null,999,null,pinned:true) is ForbidResult, "unresolved or unauthorized scope cannot become a pinned selection");
|
|
|
|
var plans = new WritingPlanSummary[] { new(){WritingPlanID=1,ProjectID=1,BookID=2}, new(){WritingPlanID=2,ProjectID=1,BookID=3}, new(){WritingPlanID=3,ProjectID=1,BookID=null}, new(){WritingPlanID=4,ProjectID=9,BookID=99} };
|
|
var scheduleRepo = Proxy<IWritingScheduleRepository>((n,a) => n switch
|
|
{
|
|
"GetWritingPlanSummariesByUserAsync" => Task.FromResult<IReadOnlyList<WritingPlanSummary>>(plans),
|
|
"GetPlansRequiringRebalanceAsync" => Task.FromResult<IReadOnlyList<WritingPlanSummary>>([]),
|
|
"GetSchedulePreviewItemsAsync" => Task.FromResult<IReadOnlyList<WritingSchedulePreviewItem>>([]),
|
|
_ => throw new Exception(n)
|
|
});
|
|
var service = new WritingScheduleService(scheduleRepo, projects, books, user);
|
|
var preview = await service.GetSchedulePreviewAsync(null, 1, 3);
|
|
Check(preview.SelectedWritingPlanID == 2 && preview.Plans.Select(x=>x.WritingPlanID).SequenceEqual([2,3]) && preview.BookID == 3,
|
|
"Preview chooses within the selected book and preserves All Books plans");
|
|
Check((await service.GetSchedulePreviewAsync(1, 1, 3)).SelectedPlan == null, "explicit out-of-scope plan cannot silently select another book");
|
|
Check((await service.GetSchedulePreviewAsync(null, 1, 88)).Plans.Single().WritingPlanID == 3, "new book may still use its project-wide plan");
|
|
|
|
var schedule = Proxy<IWritingScheduleService>((n,a) => n == "RegenerateScheduleAsync" ? Task.FromResult<IReadOnlyList<WritingScheduleItemViewModel>>([]) : Task.CompletedTask);
|
|
var controller = new WriterController(Proxy<IWriterWorkspaceService>((n,a)=>throw new Exception(n)), schedule)
|
|
{ TempData = new TempDataDictionary(new DefaultHttpContext(), new MemoryTempData()) };
|
|
foreach (var response in new[] { await controller.ActivateWritingPlan(2,1,3), await controller.DeactivateWritingPlan(2,1,3), await controller.DeleteWritingPlan(2,1,3), await controller.RegenerateWritingPlanSchedule(2,1,3) })
|
|
Check(response is RedirectToActionResult redirect && (int?)redirect.RouteValues!["bookId"] == 3 && (int?)redirect.RouteValues["projectId"] == 1,
|
|
"plan management action retains the requesting list scope");
|
|
}
|
|
private sealed class MemoryTempData : ITempDataProvider
|
|
{
|
|
public IDictionary<string,object> LoadTempData(HttpContext context) => new Dictionary<string,object>();
|
|
public void SaveTempData(HttpContext context, IDictionary<string,object> values) { }
|
|
}
|
|
}
|