97 lines
6.7 KiB
C#
97 lines
6.7 KiB
C#
using PlotLine.Data;
|
|
using Microsoft.AspNetCore.DataProtection;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.Configuration;
|
|
using PlotLine.Controllers;
|
|
using PlotLine.Models.WritingSolver;
|
|
using PlotLine.Services;
|
|
|
|
internal static class WritingSolverTests
|
|
{
|
|
private static string Root()
|
|
{
|
|
var directory=new DirectoryInfo(AppContext.BaseDirectory);
|
|
while(directory is not null && !File.Exists(Path.Combine(directory.FullName,"PlotLine.slnx"))) directory=directory.Parent;
|
|
return directory?.FullName ?? throw new Exception("Repository unavailable.");
|
|
}
|
|
private static SolverGraph Graph() => WritingSolverRepository.Parse(File.ReadAllText(Path.Combine(Root(),"PlotLine/Content/WritingSolver/demo.json")));
|
|
private static void Require(bool ok,string message) { if(!ok) throw new Exception(message); }
|
|
public static void RoutingAndRevision()
|
|
{
|
|
var graph=Graph();
|
|
var history=new[]{new SolverAnswer("welcome",["story"]),new SolverAnswer("plot",["threads","crossing"]),new SolverAnswer("focus",["weave"])};
|
|
var result=WritingSolverEngine.Evaluate(graph,history);
|
|
Require(result.Next is null && result.Solutions[0].Id=="interweaving","Plot diagnosis missing.");
|
|
Require(result.Scores["plot"]==6 && result.Scores["people"]==2,"Multi-select weights are incorrect.");
|
|
Require(result.Solutions[0].Features.Count==3 && result.Solutions[0].Features.Count(f=>f.Primary)==1,"Feature roles missing.");
|
|
var revised=WritingSolverEngine.Evaluate(graph,[new("welcome",["people"]),history[1],history[2]]);
|
|
Require(revised.Next?.Id=="broaden" && revised.Answers.Count==1 && revised.Scores.Count==0,"Revision kept stale answers/scores.");
|
|
var fallback=WritingSolverEngine.Evaluate(graph,[new("welcome",["story"]),new("plot",["other"]),new("broaden",["other"]),new("focus",["other"])]);
|
|
Require(fallback.Solutions.Any(s=>s.Id=="overview"),"Fallback did not broaden into useful advice.");
|
|
var invalid=WritingSolverEngine.Evaluate(graph,[history[0],new("plot",["threads","other"])]);
|
|
Require(invalid.Answers.Count==1,"Fallback must be exclusive.");
|
|
var repeated=WritingSolverEngine.Evaluate(graph,[history[0],new("plot",["threads","threads"])]);
|
|
Require(repeated.Scores["plot"]==3,"Duplicate selections amplified weights.");
|
|
graph.Solutions[1].Signals=new(){["plot"]=1};
|
|
Require(WritingSolverEngine.Evaluate(graph,history).Solutions.Count==2,"Multiple diagnoses were lost.");
|
|
graph=Graph();
|
|
graph.Nodes.Single(n=>n.Id=="welcome").Routes.Add(new(){Target="focus",Signal="plot",Minimum=1,Priority=100});
|
|
Require(WritingSolverEngine.Evaluate(graph,[history[0]]).Next?.Id=="focus","Signal-conditioned alternative route was ignored.");
|
|
}
|
|
|
|
private sealed class MemoryGraph(SolverGraph graph) : IWritingSolverRepository
|
|
{
|
|
public Task<SolverGraph> GetAsync() => Task.FromResult(graph);
|
|
public Task PublishAsync(string json,string editor) => throw new NotSupportedException();
|
|
}
|
|
public static void AnonymousState()
|
|
{
|
|
var provider = new EphemeralDataProtectionProvider();
|
|
var protector = provider.CreateProtector("WritingSolver.history.v1").ToTimeLimitedDataProtector();
|
|
var controller = new WritingProblemsController(new MemoryGraph(Graph()),provider,new ConfigurationBuilder().Build())
|
|
{
|
|
ControllerContext = new ControllerContext { HttpContext = new DefaultHttpContext() }
|
|
};
|
|
var valid = protector.Protect("{\"Version\":\"demo-1\",\"Answers\":[{\"Node\":\"welcome\",\"Choices\":[\"story\"]}]}",TimeSpan.FromHours(2));
|
|
var view = (ViewResult)controller.Index(valid).GetAwaiter().GetResult();
|
|
var journey = (SolverJourney)view.Model!;
|
|
Require(journey.Error is null && journey.Outcome.Next?.Id=="plot","Anonymous protected history did not resume.");
|
|
Require(controller.Response.Headers.CacheControl.ToString().Contains("no-store"),"Diagnostic state must not be cached.");
|
|
foreach(var token in new[]{valid+"tampered",protector.Protect("{}",DateTimeOffset.UtcNow.AddMinutes(-1)),protector.Protect("{\"Version\":\"obsolete\",\"Answers\":[]}",TimeSpan.FromHours(2))})
|
|
{
|
|
view=(ViewResult)controller.Index(token).GetAwaiter().GetResult();
|
|
journey=(SolverJourney)view.Model!;
|
|
Require(journey.Error is not null && journey.Outcome.Answers.Count==0,"Expired/tampered/stale-version history must restart gracefully.");
|
|
}
|
|
}
|
|
|
|
public static void Validation()
|
|
{
|
|
var graph=Graph();
|
|
Require(WritingSolverValidator.Validate(graph).Count==0,"Demo must validate.");
|
|
graph.Solutions[1].Slug=graph.Solutions[0].Slug;
|
|
Require(WritingSolverValidator.Validate(graph).Any(e=>e.Contains("Duplicate solution slug")),"Duplicate slug accepted.");
|
|
graph=Graph(); graph.Nodes[0].Routes.Add(new(){Target="missing"});
|
|
Require(WritingSolverValidator.Validate(graph).Any(e=>e.Contains("target")),"Missing reference accepted.");
|
|
graph=Graph(); graph.Nodes.Single(n=>n.Id=="focus").Routes.Add(new(){Target="welcome"});
|
|
Require(WritingSolverValidator.Validate(graph).Any(e=>e.Contains("cycle")),"Cycle accepted.");
|
|
graph=Graph(); graph.Solutions[0].Threshold=10000;
|
|
Require(WritingSolverValidator.Validate(graph).Any(e=>e.Contains("Unreachable solution")),"Impossible diagnosis accepted.");
|
|
graph=Graph(); graph.Nodes.Add(new(){Id="orphan",Title="Orphan",Choices=[new(){Id="other",Text="Other",Fallback=true}]});
|
|
var errors=WritingSolverValidator.Validate(graph);
|
|
Require(errors.Any(e=>e.Contains("Unreachable node")) && errors.Any(e=>e.Contains("Dead end")),"Orphan/dead end accepted.");
|
|
graph=Graph(); graph.Nodes[0].Choices[0].Weights["plot"]=101;
|
|
Require(WritingSolverValidator.Validate(graph).Any(e=>e.Contains("Malformed weights")),"Malformed weights accepted.");
|
|
graph=Graph();
|
|
graph.Nodes[0].Routes.Single(r=>r.Choice=="story").Signal="plot";
|
|
graph.Nodes[0].Routes.Single(r=>r.Choice=="story").Minimum=99;
|
|
Require(WritingSolverValidator.Validate(graph).Any(e=>e.Contains("Impossible signal route")),"Impossible conditional route accepted.");
|
|
graph=Graph();
|
|
graph.Nodes[0].Routes.Single(r=>r.Choice=="story").Priority=-100;
|
|
Require(WritingSolverValidator.Validate(graph).Any(e=>e.Contains("Unreachable node under")),"Structurally connected but shadowed route accepted.");
|
|
graph=Graph(); graph.Solutions[0].Features[0].Feature="missing";
|
|
Require(WritingSolverValidator.Validate(graph).Any(e=>e.Contains("feature mapping")),"Invalid feature accepted.");
|
|
}
|
|
}
|