PlotDirector/PlotLine.Tests/ParagraphViewTests.cs

43 lines
3.7 KiB
C#

using System.Security.Claims;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Abstractions;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.AspNetCore.Mvc.Razor;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.DataProtection;
using PlotLine.Controllers;
using PlotLine.Models;
using PlotLine.Services.Manuscript;
using PlotDirector.Companion.Protocol.Manuscript;
internal static class ParagraphViewTests
{
public static async Task Run()
{
var builder=WebApplication.CreateBuilder();builder.Logging.ClearProviders();builder.WebHost.UseUrls("http://127.0.0.1:0");builder.Services.AddSingleton<IDataProtectionProvider>(new EphemeralDataProtectionProvider());builder.Services.AddControllersWithViews().AddApplicationPart(typeof(ParagraphManuscriptController).Assembly);
await using var app=builder.Build();app.MapControllers();app.MapControllerRoute("default","{controller}/{action}/{id?}");
app.MapGet("/fixture",async (HttpContext http)=>{
http.User=new ClaimsPrincipal(new ClaimsIdentity([new Claim(ClaimTypes.NameIdentifier,"7")],"fixture"));
var rows=Enumerable.Range(1,210).Select(i=>new ParagraphOccurrence{Text=i==1?"<script>unsafe()</script>\r":$"Saved narrative paragraph {i}.\r",Ordinal=i-1,Start=(i-1)*50,End=i*50}).ToArray();
var state=ParagraphManuscriptEngine.Bootstrap(3,1,Guid.NewGuid(),rows,[new(){SceneId=11,ChapterId=4,Title="A long scene title",Start=1,End=210}]);
var route=new RouteData();route.Values["controller"]="ParagraphManuscript";route.Values["action"]="Index";var action=new ActionContext(http,route,new ActionDescriptor());
var engine=http.RequestServices.GetRequiredService<IRazorViewEngine>();var view=engine.GetView(null,"/Views/ParagraphManuscript/Index.cshtml",false);if(!view.Success)throw new Exception("Compiled paragraph view missing");
using var output=new StringWriter();var data=new ViewDataDictionary(new EmptyModelMetadataProvider(),new ModelStateDictionary()){Model=new ParagraphWorkspaceModel{State=state,BookTitle="Synthetic book",Events=[new(){Kind="Knowledge",Id=9,SceneId=11,Description="The saved event"}]}};
var temp=http.RequestServices.GetRequiredService<ITempDataDictionaryFactory>().GetTempData(http);await view.View.RenderAsync(new ViewContext(action,view.View,data,temp,output,new HtmlHelperOptions()));
return Results.Content(output.ToString(),"text/html");
});
await app.StartAsync();try{
var server=app.Services.GetRequiredService<Microsoft.AspNetCore.Hosting.Server.IServer>();var address=server.Features.Get<Microsoft.AspNetCore.Hosting.Server.Features.IServerAddressesFeature>()!.Addresses.Single();using var client=new HttpClient();var html=await client.GetStringAsync(address+"/fixture");
if(!html.Contains("__RequestVerificationToken")||!html.Contains("/manuscript/paragraphs/change")||!html.Contains("Knowledge:9")||html.Contains("<script>unsafe()")||html.Contains("Saved narrative paragraph 101."))throw new Exception("Compiled review view lost scoped routes, antiforgery, typed identity, escaping or pagination");
var path=Path.Combine(Path.GetTempPath(),"pd-paragraph-review-fixture.html");await File.WriteAllTextAsync(path,html);
Console.WriteLine("PASS actual compiled Razor workspace: scoped form routes, antiforgery, typed event choice, encoded prose and bounded pagination (synthetic fixture only)");
}finally{await app.StopAsync();}
}
}