PlotDirector/PlotLine.Tests/WritingSolverResultTests.cs

98 lines
8.2 KiB
C#

using System.Reflection;
using System.Security.Claims;
using System.Text.Json;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.FileProviders;
using PlotLine.Controllers;
using PlotLine.Data;
using PlotLine.Models;
using PlotLine.Models.WritingSolver;
using PlotLine.Services;
internal static class WritingSolverResultTests
{
private static string Root=>WritingSolverCuratedV2Tests.Root();
public static WritingSolverResults Content()=>new(Path.Combine(Root,"PlotLine/Content/WritingSolver/Presentation/results-v1.json"));
private static void Check(bool condition,string message){if(!condition)throw new Exception(message);}
public static void EditorialCoverage()
{
var results=Content();var graph=WritingSolverStage1Tests.Graph();
Check(results.Catalog.Results.Select(r=>r.Solution).Order().SequenceEqual(graph.Solutions.Select(s=>s.Id).Order()),"Every diagnosis needs exactly one presentation.");
var help=new HelpService(new EnvironmentFixture());
foreach(var result in results.Catalog.Results)
{
Check(graph.Features.Any(f=>f.Id==results.Capability(result.Capability).Feature),"Unknown product feature.");
if(result.ArticleKey is not null)Check(help.GetByContextKey(result.ArticleKey) is not null,"Missing reviewed advice article.");
}
var evidenceGraphs=graph.Nodes.Concat(WritingSolverDeadEndTests.Graph().Nodes).ToArray();
foreach(var evidence in results.Catalog.Evidence)
Check(evidenceGraphs.Any(n=>n.Id==evidence.Node && n.Choices.Any(c=>c.Id==evidence.Choice)),"Evidence copy refers to an answer that cannot be supplied.");
var exploration=WritingSolverCuratedV2Tests.Explore(graph,outcome=>
{
var rendered=results.Present(outcome);
if(outcome.Next is not null || outcome.Reassurance is not null || outcome.Solutions.Count==0){Check(rendered is null,"Non-diagnostic outcome was styled as a diagnosis.");return;}
Check(rendered is not null,"Missing result presentation.");
foreach(var solution in outcome.Solutions)
{
var evidence=results.Evidence(outcome,solution.Id);
Check(evidence.Count is >0 and <=3,"A result has no supplied evidence or is too verbose.");
Check(results.Catalog.Evidence.Any(e=>e.Priority==100 && e.Solutions.Contains(solution.Id) && outcome.Answers.Any(a=>a.Node==e.Node && a.Choices.Contains(e.Choice))),"A result lacks a matched corroborating observation.");
}
});
Check(exploration.Complete && exploration.StateCount==85140 && exploration.PrimarySolutions.Length==52,"Presentation altered Stage 1 reachability.");
Check(results.Catalog.Results.Count(r=>r.Principle is not null)<26,"Principles should not be padded across every result.");
foreach(var capability in results.Catalog.Capabilities)Check(!results.HasImage(capability),"This stage must not manufacture final screenshot assets.");
var unknown=results.Catalog.Capabilities[0];unknown.Image="/images/writing-solver/not-yet-captured.webp";Check(!results.HasImage(unknown),"A missing configured image must retain a placeholder.");
}
public static void ListeningAndVariants()
{
var results=Content();var graph=WritingSolverStage1Tests.Graph();
SolverOutcome Dominance(string observation)=>WritingSolverEngine.Evaluate(graph,[new("entry",["story-not-working"]),new("story-problem",["takes-over"]),new("dominance-observation",[observation]),new("dominance-intention",["even"])]);
var expanding=results.Present(Dominance("space"))!;var gaps=results.Present(Dominance("gaps"))!;var unsure=results.Present(Dominance("unsure"))!;
Check(expanding.Solution.Id=="d04" && gaps.Solution.Id=="d04" && unsure.Solution.Id=="d04","Variant test no longer exercises one diagnosis.");
Check(expanding.Evidence.Any(t=>t.Contains("expanding")) && gaps.Evidence.Any(t=>t.Contains("vanish")),"Materially different evidence was flattened into generic prose.");
Check(!gaps.Evidence.Any(t=>t.Contains("expanding")) && !unsure.Evidence.Any(t=>t.Contains("expanding") || t.Contains("vanish")),"A result invented an observation not supplied.");
var deliberate=WritingSolverEngine.Evaluate(graph,[new("entry",["story-not-working"]),new("story-problem",["takes-over"]),new("dominance-observation",["space"]),new("dominance-intention",["deliberate"])]);
Check(results.Present(deliberate) is null && deliberate.Reassurance is not null,"Deliberate working choice must retain reassurance.");
Check(results.Present(WritingSolverEngine.Evaluate(graph,[new("entry",["unsure"]),new("rescue-happening",["reread-cant-see"]),new("rescue-confidence",["other"])])) is null,"Insufficient evidence became a diagnosis.");
var history=Dominance("space").Answers.ToArray();history[0]=new("entry",["losing-track"]);
Check(results.Present(WritingSolverEngine.Evaluate(graph,history)) is null,"Stale evidence survived earlier-answer revision.");
}
public static void WorkspaceRouting()=>WorkspaceRoutingAsync().GetAwaiter().GetResult();
private static async Task WorkspaceRoutingAsync()
{
Check(typeof(WritingSolverWorkspaceController).GetCustomAttribute<AuthorizeAttribute>() is not null,"Workspace selector must require authentication.");
var results=Content();var context=new WorkspaceFixture();
var controller=new WritingSolverWorkspaceController(results,context){ControllerContext=new ControllerContext{HttpContext=new DefaultHttpContext()}};
var initial=await controller.Open("plot");Check(initial is ViewResult {Model:SolverWorkspaceChoice {ProjectId:null}},"No project must show a graceful selector.");
var selected=await controller.Open("plot",77);Check(selected is LocalRedirectResult {Url:"/PlotLineWorkspace?projectId=77"},"Plot Lines CTA must open the real workspace.");
Check(await controller.Open("plot",999) is NotFoundResult,"Foreign project context must not be accepted.");
Check(await controller.Open("knowledge",77) is ViewResult {Model:SolverWorkspaceChoice {ProjectId:77}},"Knowledge requires a character, not an arbitrary ID.");
Check(await controller.Open("knowledge",77,999) is NotFoundResult,"Foreign character context must not be accepted.");
Check(await controller.Open("knowledge",77,88) is LocalRedirectResult {Url:"/Dynamics/Knowledge/88?projectId=77"},"Knowledge CTA must open the real character timeline.");
context.Empty=true;Check(await controller.Open("plot") is ViewResult {Model:SolverWorkspaceChoice {Projects.Count:0}},"No-project state must remain usable.");
foreach(var capability in results.Catalog.Capabilities)
{
var route=new Uri("https://fixture.invalid"+results.WorkspaceUrl(capability,77,88));var parts=route.AbsolutePath.Split('/',StringSplitOptions.RemoveEmptyEntries);
var type=typeof(WritingProblemsController).Assembly.GetType("PlotLine.Controllers."+parts[0]+"Controller");
var action=parts.Length>1?parts[1]:"Index";
Check(type?.GetMethods().Any(m=>m.Name==action)==true,"Recommendation route does not name an existing application action.");
}
}
private sealed class WorkspaceFixture : IWritingSolverWorkspaceContext
{
public bool Empty;
public Task<IReadOnlyList<Project>> ProjectsAsync()=>Task.FromResult<IReadOnlyList<Project>>(Empty?[]:[new(){ProjectID=77,ProjectName="Novel"}]);
public Task<IReadOnlyList<Character>> CharactersAsync(int id)=>Task.FromResult<IReadOnlyList<Character>>([new(){CharacterID=88,ProjectID=77,CharacterName="Character"}]);
}
private sealed class EnvironmentFixture : IWebHostEnvironment
{
public string ApplicationName{get;set;}="PlotLine";public string EnvironmentName{get;set;}="Development";
public string ContentRootPath{get;set;}=Path.Combine(Root,"PlotLine");public IFileProvider ContentRootFileProvider{get;set;}=new NullFileProvider();
public string WebRootPath{get;set;}=Path.Combine(Root,"PlotLine/wwwroot");public IFileProvider WebRootFileProvider{get;set;}=new NullFileProvider();
}
}