PlotDirector/PlotLine.Tests/SceneCreationTests.cs

51 lines
2.8 KiB
C#

using System.Reflection;
using Microsoft.AspNetCore.Mvc;
using PlotLine.Controllers;
using PlotLine.Services;
using PlotLine.ViewModels;
internal static class SceneCreationTests
{
public static async Task Run()
{
var saves = 0;
var limited = false;
var model = new SceneEditViewModel { ChapterID = 42, SceneNumber = 3, SceneTitle = "Draft" };
var service = DispatchProxy.Create<ISceneService, StoryPointProxy>();
((StoryPointProxy)(object)service).Handler = (method, args) => method switch
{
"GetCreateAsync" => Task.FromResult<SceneEditViewModel?>((int)args![0]! == 42
? new SceneEditViewModel { ChapterID = 42, SceneNumber = 3 } : null),
"GetEditAsync" => Task.FromResult<SceneEditViewModel?>(model),
"SaveAsync" => Save(),
_ => throw new InvalidOperationException(method)
};
Task<int> Save()
{
saves++;
return limited ? Task.FromException<int>(new SubscriptionLimitException("Scene limit reached")) : Task.FromResult(81);
}
ScenesController Controller() => new(service, null!, null!, null!, null!, null!, null!, null!, null!);
void Check(bool value, string message) { if (!value) throw new Exception(message); }
var controller = Controller();
Check(await controller.Create(999) is NotFoundResult, "Unknown chapter must not show creation form");
Check(await controller.Create(42) is ViewResult { ViewName: "Create", Model: SceneEditViewModel { ChapterID: 42, SceneNumber: 3 } }, "Creation must use dedicated form and chapter defaults");
controller.ModelState.AddModelError("SceneTitle", "Required");
var invalid = (ViewResult)await controller.Save(model);
Check(invalid.ViewName == "Create" && ((SceneEditViewModel)invalid.Model!).SceneTitle == "Draft" && saves == 0, "Invalid creation must preserve fields without saving");
limited = true;
controller = Controller();
var limit = (ViewResult)await controller.Save(model);
Check(limit.ViewName == "Create" && !controller.ModelState.IsValid, "Creation limit must return usable form with error");
limited = false;
controller = Controller();
var saved = (RedirectToActionResult)await controller.Save(model);
Check(saved.ActionName == "Edit" && (int)saved.RouteValues!["id"]! == 81, "New scene must open persisted workspace");
model.SceneID = 81;
controller = Controller();
controller.ModelState.AddModelError("SceneTitle", "Required");
Check(await controller.Save(model) is ViewResult { ViewName: "Edit" }, "Invalid existing edits must retain workspace");
Console.WriteLine("PASS scene creation routes, chapter defaults, validation preservation, limits and persisted workspace redirect");
}
}