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 }; 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 }]; var panel = ContextThreads.Build(scope, data, links); 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, "A direct scene link needs no event."); 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).Linked, "An actual resolution is a recorded association."); 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); Check(earlier.Threads.Single(t => t.PlotThreadID == 10).PreviousScenes.Single().SceneID == 15, "History follows the selected scene, not IDs or latest saved scene."); 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); } }