PlotDirector/PlotLine.Tests/ContextThreadsTests.cs
2026-12-06 17:57:40 +00:00

87 lines
7.0 KiB
C#

using System.Reflection;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using PlotLine.Controllers;
using PlotLine.Models;
using PlotLine.Services.Context;
using PlotLine.Services;
internal static class ContextThreadsTests
{
public static void Run()
{
var scope = new SpatialScope { ProjectID = 7, BookID = 12, SceneID = 90, CanEdit = true };
var data = new PlotLineWorkspaceData
{
PlotLines = [new() { PlotLineID = 1, ProjectID = 7, BookID = 12, PlotLineName = "Main" },
new() { PlotLineID = 2, ProjectID = 7, PlotLineName = "Shared" },
new() { PlotLineID = 3, ProjectID = 7, BookID = 13 }, new() { PlotLineID = 4, ProjectID = 8 }],
Threads = [new() { PlotThreadID = 10, PlotLineID = 1, ThreadTitle = "Question", IntroducedSceneID = 15 },
new() { PlotThreadID = 11, PlotLineID = 2, ThreadTitle = "Promise", ActualResolutionSceneID = 90 },
new() { PlotThreadID = 12, PlotLineID = 3 }, new() { PlotThreadID = 13, PlotLineID = 4 },
new() { PlotThreadID = 14, PlotLineID = 1, PlannedResolutionSceneID = 90 }],
AttributionScenes = [new() { SceneID = 15, BookID = 12, NarrativeOrder = 1 },
new() { SceneID = 7, BookID = 12, NarrativeOrder = 2 }, new() { SceneID = 90, BookID = 12, NarrativeOrder = 3 },
new() { SceneID = 2, BookID = 12, NarrativeOrder = 4 }, new() { SceneID = 44, BookID = 13, NarrativeOrder = 0 }],
Contributions = [new() { PlotThreadID = 10, SceneID = 7, BookID = 12 }, new() { PlotThreadID = 10, SceneID = 7, BookID = 12 },
new() { PlotThreadID = 10, SceneID = 44, BookID = 13 }]
};
SceneThread[] links = [new() { SceneID = 90, PlotThreadID = 10 }, new() { SceneID = 7, PlotThreadID = 10 },
new() { SceneID = 2, PlotThreadID = 10 }, new() { SceneID = 44, PlotThreadID = 10 }];
ContextThreadPlacement[] plan = [new(10, 90, null), new(14, null, 3)];
var panel = ContextThreads.Build(scope, data, links, plan);
var historyOnly = ContextThreads.Build(scope, data, links, []);
Check(historyOnly.Threads.Single(t => t.PlotThreadID == 10) is { Linked: true, CanRemove: false },
"Historical links remain visible after unassigning a thread.");
Check(panel.Threads.Single(t => t.PlotThreadID == 14).AssignedElsewhere, "A chapter assignment prevents a competing scene assignment.");
var planOnly = ContextThreads.Build(scope, data, [], plan);
Check(planOnly.Threads.Single(t => t.PlotThreadID == 10) is { Linked: true, CanRemove: true, HasManuscriptAssociation: false },
"A planning assignment appears in Context without generating a historical link.");
Check(panel.Threads.Count == 3, "Exclude other books and projects; include project-wide lines.");
var question = panel.Threads.Single(t => t.PlotThreadID == 10);
Check(question.Linked && question.CanRemove, "A scene assignment is independently editable.");
Check(question.PreviousScenes.Select(s => s.SceneID).SequenceEqual([15, 7]), "Keep every previous pickup in narrative order; deduplicate events, milestones and direct links; exclude future/other-book scenes.");
Check(panel.Threads.Single(t => t.PlotThreadID == 11) is { Linked: true, CanRemove: false }, "A resolution stays linked and cannot be removed as a direct scene link.");
Check(!panel.Threads.Single(t => t.PlotThreadID == 14).Linked, "A planned resolution alone is not an actual pickup.");
var earlier = ContextThreads.Build(new() { ProjectID = 7, BookID = 12, SceneID = 7 }, data, links, plan);
Check(earlier.Threads.Single(t => t.PlotThreadID == 10).PreviousScenes.Single().SceneID == 15, "History follows the selected scene, not IDs or latest saved scene.");
Check(!earlier.Threads.Single(t => t.PlotThreadID == 10).CanRemove, "Read-only scopes cannot offer removal.");
Check(typeof(ContextThreadsController).GetMethod("Remove")!.IsDefined(typeof(ValidateAntiForgeryTokenAttribute), true), "Thread removal requires antiforgery.");
var chapterScope = new SpatialScope { ProjectID = 7, BookID = 12, ChapterID = 3, CanEdit = true };
var chapterPanel = ContextThreads.Build(chapterScope, data, links,
[.. plan, new(11, null, null, "Requires Question", "Requires Question")]);
Check(chapterPanel.Threads.Single(t => t.PlotThreadID == 14) is
{ IncludedInChapter: true, IncludedInScene: false, CanRemove: true, AssignedElsewhere: false },
"Chapter placement is editable without a scene and does not create a manuscript association.");
Check(chapterPanel.Threads.Single(t => t.PlotThreadID == 11) is { Assigned: false, ChapterBlock: "Requires Question" },
"An unassigned prerequisite-blocked thread carries its eligibility explanation.");
var backup = new ProjectBackupExport
{
Project = new() { ProjectID = 7 },
Data = new()
{
Books = [new() { BookID = 12, ProjectID = 7 }, new() { BookID = 13, ProjectID = 7 }],
Chapters = [new() { ChapterID = 3, BookID = 12 }],
Scenes = [new() { SceneID = 90, ChapterID = 3 }],
PlotLines = [new() { PlotLineID = 1, ProjectID = 7, BookID = 12 }],
PlotThreads = [new() { PlotThreadID = 10, PlotLineID = 1 }],
SceneThreads = [new() { SceneID = 90, PlotThreadID = 10 }]
}
};
Check(ValidateBackup(backup) == null, "Scene links participate in the canonical backup graph.");
backup.Data.SceneThreads = [new() { SceneID = 90, PlotThreadID = 10 }, new() { SceneID = 90, PlotThreadID = 10 }];
Check(ValidateBackup(backup)?.Contains("duplicate scene/thread") == true, "Reject duplicate backup associations before restore.");
backup.Data.SceneThreads = [new() { SceneID = 90, PlotThreadID = 10 }];
backup.Data.PlotLines[0].BookID = 13;
Check(ValidateBackup(backup)?.Contains("another book") == true, "Reject a scene/thread association crossing books.");
backup.Data.SceneThreads = [new() { SceneID = 90, PlotThreadID = 999 }];
Check(ValidateBackup(backup)?.Contains("missing or foreign") == true, "Reject foreign threads before restore.");
Check(typeof(ContextThreadsController).IsDefined(typeof(AuthorizeAttribute), true), "Thread endpoints require authentication.");
Check(typeof(ContextThreadsController).GetMethod("Add")!.IsDefined(typeof(ValidateAntiForgeryTokenAttribute), true), "Thread saves require antiforgery.");
}
private static string? ValidateBackup(ProjectBackupExport backup) =>
(string?)typeof(ImportService).Assembly.GetType("PlotLine.Services.ProjectBackupGraphValidation")!
.GetMethod("Validate", BindingFlags.Public | BindingFlags.Static)!.Invoke(null, [backup, false]);
private static void Check(bool condition, string message) { if (!condition) throw new Exception(message); }
}