PlotDirector/PlotLine.Tests/WritingSolverNeutralTests.cs

65 lines
4.9 KiB
C#

using System.Text.Json;
using PlotLine.Data;
using PlotLine.Models.WritingSolver;
using PlotLine.Services;
internal static class WritingSolverNeutralTests
{
private static void Check(bool condition,string message) { if(!condition) throw new Exception(message); }
private static SolverGraph Empty(bool declared=false) => new()
{
Version="neutral-test",EntryNode="question",MinAnswers=1,MaxAnswers=6,
Nodes=[new() {Id="question",Title="What would help?",NeutralEndpoint=declared,
Reassurance=declared ? new(){Title="Take your time.",Summary="You can leave with this reassurance."} : null,
Choices=[new(){Id="other",Text="Something else",Fallback=true}]}]
};
public static void Validation()
{
var ordinary=Empty();Check(WritingSolverValidator.Validate(ordinary).Any(e=>e.Contains("Dead end without diagnostic evidence")),"Undeclared evidence-free dead end passed.");
var valid=Empty(true);Check(WritingSolverValidator.Validate(valid).Count==0,"Explicit neutral endpoint failed.");
var outcome=WritingSolverEngine.Evaluate(valid,[new("question",["other"])]);
Check(outcome.Next is null && outcome.Solutions.Count==0 && outcome.Scores.Count==0 && outcome.Reassurance?.Title=="Take your time.","Neutral endpoint manufactured evidence or a solution.");
foreach(var change in new Action<SolverNode>[] {
n=>n.Routes.Add(new(){Target="question",AllowCycle=true}),n=>n.Multiple=true,
n=>n.Choices[0].Weights["knowledge"]=1,n=>n.Affinity["knowledge"]=1,
n=>n.Reassurance=null,n=>n.Reassurance!.Title="",n=>n.Reassurance!.Feature="missing",
n=>n.Reassurance!.Advice=["One","Two","Three","Four"]})
{
var malformed=Empty(true);change(malformed.Nodes[0]);Check(WritingSolverValidator.Validate(malformed).Count>0,"Malformed neutral configuration passed.");
}
ordinary.Nodes[0].Reassurance=new(){Title="Hidden",Summary="Not declared"};
Check(WritingSolverValidator.Validate(ordinary).Any(e=>e.Contains("requires an explicit")),"Undeclared reassurance passed.");
var demo=WritingSolverRepository.Parse(File.ReadAllText(Path.Combine(WritingSolverCuratedV2Tests.Root(),"PlotLine/Content/WritingSolver/demo.json")));
Check(demo.Nodes.All(n=>!n.NeutralEndpoint),"Old graph compatibility changed.");
Check(WritingSolverValidator.Validate(WritingSolverCuratedV2Tests.Graph()).Count==0,"Curated V2 does not pass the real validator.");
}
private static SolverAnswer[] Prefix => [new("entry",["unsure"]),new("rescue-happening",["reread-cant-see"]),new("rescue-confidence",["using-tool-concern"]),new("existing-stage",["draft-done"])];
public static void ReassuranceAndRevision()
{
var graph=WritingSolverCuratedV2Tests.Graph();var before=WritingSolverEngine.Evaluate(graph,Prefix);
foreach(var choice in graph.Nodes.Single(n=>n.NeutralEndpoint).Choices.Where(c=>c.Active))
{
var after=WritingSolverEngine.Evaluate(graph,[..Prefix,new("existing-barrier",[choice.Id])]);
Check(after.Next is null && after.Solutions.Count==0 && after.Reassurance is not null,"Neutral concern did not receive reassurance.");
Check(after.Scores.OrderBy(s=>s.Key).SequenceEqual(before.Scores.OrderBy(s=>s.Key)),"Adoption added diagnostic evidence.");
var expected=choice.Reassurance ?? graph.Nodes.Single(n=>n.NeutralEndpoint).Reassurance;
Check(after.Reassurance==expected,"The selected choice did not determine the reassurance.");
var replay=WritingSolverEngine.Evaluate(graph,after.Answers);
Check(replay.Reassurance==after.Reassurance && replay.Solutions.Count==0,"Forward/replay changed reassurance.");
var revised=after.Answers.ToArray();revised[0]=new("entry",["feels-wrong"]);
var changed=WritingSolverEngine.Evaluate(graph,revised);
Check(changed.Reassurance is null && changed.Answers.Count==1 && changed.Next?.Id=="feel-where","Revised answer retained an adoption outcome.");
}
var assertive=WritingSolverCuratedV2Tests.Graph();assertive.MinAnswers=6;
assertive.Nodes.Single(n=>n.Id=="entry").Choices.Single(c=>c.Id=="unsure").Weights["overwhelm"]=100;
var neutral=WritingSolverEngine.Evaluate(assertive,[..Prefix,new("existing-barrier",["discovery"])]);
Check(neutral.Solutions.Count==0 && neutral.Reassurance is not null && neutral.Scores["overwhelm"]==100,"A neutral outcome must not become a diagnosis because of prior signals.");
foreach(var id in new[]{13,58})
{
var result=WritingSolverCuratedV2Tests.Replay(graph).Single(c=>c.Id==id);
var ordinary=WritingSolverEngine.Evaluate(graph,result.AcceptedPath);
Check(ordinary.Reassurance is null && ordinary.Solutions.Count==0,"Ordinary insufficient evidence was reclassified as adoption.");
}
}
}