PlotDirector/PlotLine.Tests/ContextWarningsTests.cs
2026-12-02 15:41:28 +00:00

74 lines
5.4 KiB
C#

using System.Reflection;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using PlotLine.Controllers;
using PlotLine.Services;
using PlotLine.Data;
using PlotLine.Models;
using PlotLine.Services.Context;
internal static class ContextWarningsTests
{
public static void Run()
{
var started = new DateTime(2026, 12, 2, 12, 0, 0, DateTimeKind.Utc);
var data = new TimelineData { Books = [new(){BookID=1,BookNumber=1},new(){BookID=2,BookNumber=2}],
Chapters = [new(){ChapterID=10,BookID=1,ChapterNumber=1},new(){ChapterID=20,BookID=2,ChapterNumber=1}],
Scenes = [new(){SceneID=3,ChapterID=20,SceneNumber=2},new(){SceneID=1,ChapterID=10,SceneNumber=1},new(){SceneID=2,ChapterID=20,SceneNumber=1}] };
var scope = new SpatialScope { ProjectID=1,BookID=2,ChapterID=20,SceneID=2 };
ContinuityWarning[] findings = [new(){SceneID=2,ContinuityWarningID=1,SeverityName="Info"},
new(){SceneID=2,ContinuityWarningID=2,WarningTypeName="Scene Date Order Conflict"},
new(){SceneID=3,ContinuityWarningID=3,IsIntentional=true},new(){SceneID=2,ContinuityWarningID=4,IsDismissed=true}];
var checkedFrame = ContextWarnings.Build(scope,data,findings,new(started,started.AddSeconds(5)));
Check(checkedFrame.Scenes.Select(s=>s.SceneID).SequenceEqual(new[]{2,3}),"Chapter strip follows canonical manuscript order");
Check(checkedFrame.Scenes[0].PreviousSceneID==1,"Date evidence preserves predecessor across book/chapter boundaries");
Check(checkedFrame.Scenes[0].Findings.Select(w=>w.ContinuityWarningID).SequenceEqual(new[]{2,1}),"Date evidence first; dismissed excluded; information retained for toggle");
Check(checkedFrame.Scenes[1].Findings.Count==0,"Intentional review removes active finding counts");
Check(!checkedFrame.Stale && checkedFrame.LastChecked==started.AddSeconds(5),"Completed checkpoint covers zero-finding scenes");
data.Scenes[1].UpdatedDate=started.AddSeconds(1);
Check(ContextWarnings.Build(scope,data,findings,new(started,started.AddSeconds(5))).Stale,"Cross-book predecessor edit during validation conservatively marks stale");
Check(ContextWarnings.Build(scope,data,[],null).Stale,"No completed checkpoint never claims a clean/current check");
Check(ContextWorkspaceNavigation.IsAllowed("/Warnings?projectId=1&sceneId=2"),"Main window accepts scene-filtered warning route");
Check(ContextWorkspaceNavigation.IsAllowed("/Timeline?projectId=1&selectedSceneId=2"),"Main window accepts evidence scene route");
Check(!ContextWorkspaceNavigation.IsAllowed("/Warnings/ValidateProject?projectId=1"),"Validation actions are never delegated as read navigation");
ValidationConcurrency().GetAwaiter().GetResult();
Console.WriteLine("PASS Context warnings ordering, recorded decisions, checkpoint coverage and stale evidence");
}
private static async Task ValidationConcurrency()
{
var pending = new TaskCompletionSource<ContinuityValidationSummary>();
var validation = DispatchProxy.Create<IContinuityValidationService, ValidationProxy>();
var proxy = (ValidationProxy)(object)validation; proxy.Pending = pending.Task;
var collection = new ServiceCollection().AddLogging().AddSingleton<ICurrentUserService>(new NoCurrentUser());
collection.AddControllersWithViews();
var services = collection.BuildServiceProvider();
WarningsController Controller() => new(validation, null!, null!) { ControllerContext = new() { HttpContext = new DefaultHttpContext { RequestServices = services } } };
var first = Controller().ValidateProject(991122, null);
var duplicate = await Controller().ValidateBook(991122, 1);
Check(duplicate is ConflictObjectResult && proxy.Calls==1,"Overlapping project/book checks share a server gate");
pending.SetResult(new(){WarningCount=2});
Check(await first is ViewResult,"First validation returns completed dashboard");
proxy.Pending = Task.FromException<ContinuityValidationSummary>(new InvalidOperationException("Synthetic failure"));
Check(await Controller().ValidateProject(991122,null) is ObjectResult { StatusCode:500 },"Validation failures produce explicit failure response");
proxy.Pending = Task.FromResult(new ContinuityValidationSummary());
Check(await Controller().ValidateProject(991122,null) is ViewResult,"Failed checks release the gate for deliberate retry");
}
public class ValidationProxy : DispatchProxy
{
public Task<ContinuityValidationSummary> Pending = null!;
public int Calls;
protected override object? Invoke(MethodInfo? method, object?[]? args)
{
if(method!.Name is "ValidateProjectAsync" or "ValidateBookAsync") { Calls++; return Pending; }
if(method.Name=="GetDashboardAsync") return Task.FromResult<PlotLine.ViewModels.WarningDashboardViewModel?>(new());
throw new NotSupportedException(method.Name);
}
}
private sealed class NoCurrentUser : ICurrentUserService
{
public int? UserId=>null;public string? Email=>null;public string? DisplayName=>null;public bool IsAuthenticated=>false;
}
private static void Check(bool value,string message) { if(!value) throw new InvalidOperationException(message); }
}