using PlotLine.Models; using PlotLine.Services; using System.Text.Json; internal static class PlotHealthTests { public static void RolesAndClocks() { foreach (var role in new[] { "Introduced", "Developed", "Complicated", "Clue Planted", "Question Raised", "Question Answered", "False Lead", "Revealed", "Payoff", "Resolved", "Contradicted", "Retconned" }) Check(PlotHealthProgressPolicy.IsMeaningful(role), $"Meaningful role omitted: {role}"); Check(!PlotHealthProgressPolicy.IsMeaningful("Mentioned") && !PlotHealthProgressPolicy.IsMeaningful("Note"), "Non-progress policy changed"); var f = new Fixture(); var empty = f.Run(); Check(Find(empty, "H001").All(x => x.Level == PlotHealthLevel.Information), "Unstarted threads are informational"); Check(!Find(empty, "H002").Any(), "Planned unstarted thread must not be called quiet"); f.Add(11, 1, "Introduced"); f.Add(11, 5, "Developed"); f.Add(11, 25, "Mentioned"); f.Add(11, 29, "Note"); var before = JsonSerializer.Serialize(f.Data()); var result = f.Run(); var thread = result.Lines[0].Threads[0]; Check(thread.ScenesSinceTouch == 1 && thread.ScenesSinceProgress == 25, "Mention and Note must change touch, not progress"); Check(thread.LastProgress?.SceneID == 5 && thread.LastTouch?.SceneID == 29, "Clock evidence must identify its own event"); Check(Find(result, "H002").Any(x => x.ScenesWithoutProgress == 25), "Trailing progress silence missing"); Check(Find(result, "H007").Any(), "Planned activity mismatch missing"); Check(JsonSerializer.Serialize(f.Data()) == before, "Evaluation must not mutate canonical data"); f.Add(11, 28, "Developed"); Check(f.Run().Lines[0].Threads[0].ScenesSinceProgress == 2, "Developed must reset progress clock"); f = new Fixture(); f.Add(11, 1, "Introduced"); Check(!Find(f.Run(cutoff: 10), "H003").Any(), "Recent introduction should not need review"); Check(Find(f.Run(cutoff: 11), "H003").Any(), "Introduction without development should use the same 10-scene threshold"); f.Add(11, 20, "Introduced"); Check(Find(f.Run(), "H003").Any(), "Repeated introduction is not development"); f = new Fixture(); f.Add(11, 1, "Introduced"); f.Add(11, 1, "Resolved"); Check(!Find(f.Run(), "H002").Any(), "Same-scene resolution should stop trailing progress silence"); Check(!Find(f.Run(), "H003").Any(), "Same-scene closure must not be labelled undeveloped merely because event order is unknown"); f = new Fixture(); f.Add(11, 1, "Mentioned"); f.Add(11, 28, "Note"); Check(Find(f.Run(), "H002").Any(), "Non-progress touches must not hide absence of meaningful progress"); } public static void QuestionsClosureAndStatus() { var f = new Fixture(); f.Add(11, 3, "Question Raised"); Check(Find(f.Run(), "H004").Any(), "Open question missing"); f.Add(11, 6, "Revealed"); Check(Find(f.Run(), "H004").Single().Evidence.Any(e => e.Signal.Contains("Revealed")), "Reveal evidence must be shown without treating it as an answer"); f.Add(11, 8, "Question Answered"); Check(!Find(f.Run(), "H004").Any(), "Later answer should satisfy coarse question check"); f.Add(11, 9, "Question Raised"); Check(Find(f.Run(), "H004").Any(), "Earlier answer must not answer a later question"); foreach (var closure in new[] { "Payoff", "Revealed", "Question Answered", "Resolved" }) { f = new Fixture(); f.Add(11, 2, "Clue Planted"); Check(Find(f.Run(), "H005").Any(), "Unclosed clue missing"); f.Add(11, 3, closure); Check(!Find(f.Run(), "H005").Any(), $"Clue closure role ignored: {closure}"); } f = new Fixture(); f.Threads[0].ThreadStatusName = "Ready For Payoff"; f.Add(11, 1, "Developed"); Check(Find(f.Run(), "H006").Any(), "Ready status without payoff missing"); f.Add(11, 4, "Payoff"); Check(!Find(f.Run(), "H006").Any(), "Payoff evidence ignored"); f = new Fixture(); f.Threads[0].ThreadStatusName = "Resolved"; f.Add(11, 2, "Resolved"); f.Add(11, 3, "Mentioned"); f.Add(11, 5, "Developed"); Check(Find(f.Run(), "H008").Any(x => x.Level == PlotHealthLevel.Warning), "Development after explicit resolution should be visible"); Check(Find(f.Run(), "H007").Any(), "Closed status with positioned later activity should be reviewed"); f = new Fixture(); f.Threads[0].ThreadStatusName = "Abandoned"; f.Add(11, 2, "Note", "Abandon"); f.Add(11, 4, "Developed"); Check(Find(f.Run(), "H007").Any() && Find(f.Run(), "H008").Any(), "Positioned abandonment and later progress missing"); f = new Fixture(); f.Threads[0].ThreadStatusName = "Introduced"; f.Add(11, 2, "Introduced"); f.Add(11, 3, "Developed"); Check(Find(f.Run(), "H007").Any(), "Introduced/later development mismatch missing"); f = new Fixture(); f.Add(11, 2, "Payoff"); f.Add(11, 5, "Introduced"); Check(Find(f.Run(), "H009").Single().Level == PlotHealthLevel.Warning, "Payoff before introduction missing"); f = new Fixture(); f.Threads[0].IntroducedSceneID = 5; f.Threads[0].ActualResolutionSceneID = 10; f.Add(11, 2, "Introduced"); f.Add(11, 12, "Developed"); Check(Find(f.Run(), "H010").Single().Evidence.Any(e => e.SceneID == 12), "Outside-boundary evidence missing"); f.Add(11, 8, "Resolved"); Check(Find(f.Run(), "H008").Any(x => x.Title == "Resolution/boundary review"), "Competing resolution positions should be shown"); // Discovery-style contradictions are fixtures only; evaluator has no manuscript-specific paths. f = new Fixture(); f.Threads[0].ThreadTitle = "Proof and Belonging"; f.Threads[0].IntroducedSceneID = 8; f.Threads[0].ActualResolutionSceneID = 9; f.Add(11, 6, "Developed"); f.Add(11, 8, "Revealed"); f.Add(11, 9, "Payoff"); Check(Find(f.Run(), "H007").Any() && Find(f.Run(), "H010").Any(), "Discovery Planned/payoff/boundary contradiction lost"); } public static void TargetsScopeAndRollup() { var f = new Fixture(); var branch = f.Add(11, 2, "Developed", "Branch"); f.Targets.Add(new() { ThreadEventID = branch.ThreadEventID, TargetPlotLineID = 2, SourcePlotLineID = 1, StructuralType = "Branch" }); Check(Find(f.Run(), "H011").Any(), "Missing target continuation missing"); f.Add(22, 4, "Mentioned"); Check(!Find(f.Run(), "H011").Any(), "Recorded target continuation should satisfy branch check"); f.Lines.Add(new() { PlotLineID = 3, ProjectID = 7, PlotLineName = "Third route" }); branch.CanonicalStructuralType = "Split"; f.Targets.Add(new() { ThreadEventID = branch.ThreadEventID, TargetPlotLineID = 3, SourcePlotLineID = 1, StructuralType = "Split" }); Check(Find(f.Run(), "H011").Single().Explanation.Contains("Third route"), "Each split target must be checked separately"); branch.CanonicalStructuralType = "Merge"; f.Targets.Clear(); f.Targets.Add(new() { ThreadEventID = branch.ThreadEventID, TargetPlotLineID = 999, SourcePlotLineID = 1, StructuralType = "Merge" }); Check(Find(f.Run(), "H011").Single().Level == PlotHealthLevel.Warning, "Missing explicit merge target must be distinguished from hidden target"); f.Targets.Clear(); Check(Find(f.Run(), "H011").Any(x => x.Level == PlotHealthLevel.Warning), "Absent target record must be detected"); f = new Fixture(); f.Add(11, 2, "Developed"); f.Add(22, 2, "Developed"); f.Add(22, 25, "Developed"); var book = f.Run(book: 10); Check(Find(book, "H012").Single(x => x.PlotLineID == 1).Level == PlotHealthLevel.Review, "Book-owned open thread should be review"); Check(Find(book, "H012").Single(x => x.PlotLineID == 2).Level == PlotHealthLevel.Information, "Project thread at book end should be informational despite later-book activity"); Check(Find(f.Run(), "H012").All(x => x.Level == PlotHealthLevel.Information), "Current project end is not intended final ending"); Check(f.Run(selected: [1]).Lines.Count == 1 && f.Run(selected: [1, 2]).Lines.Count == 2, "Focus/Weave must evaluate selected lines"); var weave = f.Run(selected: [1, 2]); Check(weave.Lines.Sum(l => l.Findings.Count) == weave.Findings.Count, "Line rollup should aggregate findings, not invent status"); f.Add(11, 2, "Note"); f.Add(11, 2, "Complicated"); Check(f.Run().Lines[0].Threads[0].ScenesSinceProgress == 28, "Shared scenes and multiple same-scene events must not inflate narrative distance"); } public static void OrderingAndCoverage() { var f = new Fixture(); f.Add(11, 19, "Developed"); var thread = f.Run(cutoff: 21).Lines[0].Threads[0]; Check(thread.ChaptersSinceProgress == 1, "Cross-book restarted chapter numbers must count chapter identities"); f.Scenes[20].ChapterNumber = 1000; f.Scenes[18].ChapterNumber = 9999; Check(f.Run(cutoff: 21).Lines[0].Threads[0].ChaptersSinceProgress == 1, "Unusual chapter numbers must not affect distance"); f = new Fixture(); f.Add(11, 1, "Introduced"); f.Add(11, 3, "Resolved"); Check(!Find(f.Run(), "H009").Any(), "Canonical resolution follows introduction"); f.Threads[0].ThreadStatusName = "Resolved"; Check(Find(f.Run(cutoff: 2), "H012").Any(), "Global closed status must not leak back before positioned resolution"); var reordered = f.Run(order: Enumerable.Range(1, 30).Reverse().ToArray(), scenario: 5); Check(Find(reordered, "H009").Any(), "Scenario ordering must drive rule positions"); Check(reordered.Findings.All(x => x.Scope.ScenarioID == 5), "Order labels must identify scenario"); f = new Fixture(); f.Add(11, 1, "Question Raised"); f.Add(11, 2, "Question Answered"); var partial = f.Run(order: [1, 3, 20], scenario: 5); Check(!partial.Scope.EvidenceComplete && partial.Scope.MissingSceneCount == 27, "Scenario omissions must be explicit"); Check(Find(partial, "H004").Single().Level == PlotHealthLevel.Information && !Find(partial, "H004").Single().EvidenceComplete, "Incomplete scenario must not manufacture a definitive absence claim"); Check(partial.Findings.SelectMany(x => x.Evidence).All(x => x.Position <= 3), "Never substitute canonical positions into scenario evidence"); f = new Fixture(); f.Add(11, 1, "Introduced"); f.Add(11, 2, "Payoff"); f.Add(11, 18, "Developed"); Check(Find(f.Run(), "H002").Any(x => x.ScenesWithoutProgress == 15), "Internal progress silence should remain inspectable after a later event"); } public static void DiscoveryContradictions() { var f = new Fixture(); f.Threads[0].ThreadTitle = "First Sparks: Recognition Before Proof"; f.Threads[0].ThreadStatusName = "Introduced"; f.Threads[0].IntroducedSceneID = 5; f.Threads[0].ActualResolutionSceneID = 10; f.Add(11, 1, "Introduced"); f.Add(11, 10, "Payoff"); f.Add(11, 15, "Developed"); f.Add(11, 25, "Introduced"); Check(Find(f.Run(), "H007").Any() && Find(f.Run(), "H008").Any() && Find(f.Run(), "H010").Any(), "First Sparks evidence must remain separate from stored status"); f = new Fixture(); f.Threads[0].ThreadTitle = "Edges of Tenderness"; f.Threads[0].ThreadStatusName = "Resolved"; f.Threads[0].IntroducedSceneID = 5; f.Threads[0].ActualResolutionSceneID = 29; f.Add(11, 1, "Introduced"); f.Add(11, 15, "Developed"); f.Add(11, 29, "Resolved"); Check(Find(f.Run(), "H010").Any() && !Find(f.Run(), "H009").Any(), "Earlier introduction boundary mismatch is distinct from closure-before-introduction"); f = new Fixture(); f.Threads[0].ThreadTitle = "Bent Lines, Straight Cop"; f.Add(11, 5, "Developed"); f.Add(11, 8, "Clue Planted"); f.Add(11, 16, "Developed"); Check(Find(f.Run(), "H002").Any(x => x.ScenesWithoutProgress == 14) && Find(f.Run(), "H005").Any() && Find(f.Run(), "H007").Any(), "Bent Lines should show quiet/clue/status evidence without modifying Planned"); Check(f.Threads[0].ThreadStatusName == "Planned", "Stored author status must never be auto-changed"); } public static void GeneratedScale() { var lines = Enumerable.Range(1, 40).Select(id => new PlotLineWorkspaceLineRow { PlotLineID = id, ProjectID = 7 }).ToList(); var threads = lines.Select(l => new PlotLineWorkspaceThreadRow { PlotThreadID = l.PlotLineID, PlotLineID = l.PlotLineID, ThreadStatusName = "Developing" }).ToList(); var scenes = Enumerable.Range(1, 1200).Select(i => new PlotLineWorkspaceAttributionRow { SceneID = i, NarrativeOrder = i, BookID = 10, ChapterID = (i-1)/10+1 }).ToList(); var events = lines.SelectMany(l => Enumerable.Range(0, 300).Select(i => new PlotLineWorkspaceContributionRow { ThreadEventID = l.PlotLineID * 1000 + i, PlotLineID = l.PlotLineID, PlotThreadID = l.PlotLineID, SceneID = (i * 7 + l.PlotLineID * 13) % 1200 + 1, CanonicalRole = "Developed", CanonicalStructuralType = "Progress" })).ToList(); var data = new PlotLineWorkspaceData { PlotLines = lines, Threads = threads, AttributionScenes = scenes, Contributions = events }; var result = new PlotHealthEvaluator().Evaluate(new(7, null, null, lines.Select(l => l.PlotLineID).ToArray(), data, data)); Check(result.Lines.Count == 40 && result.Findings.SelectMany(f => f.Evidence).All(e => e.Position <= 1200), "12,000-contribution weave must use scene positions, not event counts"); } internal sealed class Fixture { public List Lines = [new() { PlotLineID = 1, ProjectID = 7, BookID = 10, PlotLineName = "Book route" }, new() { PlotLineID = 2, ProjectID = 7, PlotLineName = "Project route" }]; public List Threads = [new() { PlotThreadID = 11, PlotLineID = 1, ThreadTitle = "Book thread", ThreadStatusName = "Planned" }, new() { PlotThreadID = 22, PlotLineID = 2, ThreadTitle = "Project thread", ThreadStatusName = "Developing" }]; public List Events = []; public List Targets = []; public List Scenes = Enumerable.Range(1, 30).Select(i => new PlotLineWorkspaceAttributionRow { SceneID = i, NarrativeOrder = i, SceneTitle = $"Scene {i}", BookID = i <= 20 ? 10 : 20, ChapterID = ((i - 1) / 5) + 1, ChapterNumber = ((i - 1) % 20) / 5 + 1 }).ToList(); public PlotLineWorkspaceContributionRow Add(int thread, int scene, string role, string structure = "Progress") { var s = Scenes.Single(x => x.SceneID == scene); var e = new PlotLineWorkspaceContributionRow { ThreadEventID = Events.Count + 1, PlotThreadID = thread, PlotLineID = Threads.Single(t => t.PlotThreadID == thread).PlotLineID, SceneID = scene, BookID = s.BookID, ChapterID = s.ChapterID, ChapterNumber = s.ChapterNumber, SceneNumber = scene, NarrativeOrder = s.NarrativeOrder, ThreadTitle = Threads.Single(t => t.PlotThreadID == thread).ThreadTitle, SceneTitle = s.SceneTitle, CanonicalRole = role, CanonicalStructuralType = structure }; Events.Add(e); return e; } public PlotLineWorkspaceData Data(IReadOnlyList? order = null) => new() { PlotLines = Lines, Threads = Threads, Contributions = Events, Targets = Targets, AttributionScenes = order ?? Scenes }; public PlotHealthResult Run(int? cutoff = null, int? book = null, int[]? selected = null, int[]? order = null, int? scenario = null) { var ordered = (order is null ? Scenes : order.Select(id => Scenes.Single(s => s.SceneID == id))).Where(s => !book.HasValue || s.BookID == book).Select((s, i) => new PlotLineWorkspaceAttributionRow { SceneID = s.SceneID, BookID = s.BookID, ChapterID = s.ChapterID, NarrativeOrder = i + 1, SceneTitle = s.SceneTitle }).ToList(); return new PlotHealthEvaluator().Evaluate(new(7, book, scenario, selected ?? [1, 2], Data(ordered), Data(), cutoff)); } } private static IEnumerable Find(PlotHealthResult result, string rule) => result.Findings.Where(x => x.RuleID == rule); private static void Check(bool condition, string message) { if (!condition) throw new InvalidOperationException(message); } }