using PlotLine.Services.Context; using static PlotLine.Services.Context.ContextIntentDraft; internal static class ContextIntentDraftTests { public static void Run() { static void Check(bool condition, string message) { if (!condition) throw new Exception(message); } static void Reject(Action action) { try { action(); } catch (ArgumentException) { return; } throw new Exception("Expected rejected Intent operation."); } var chapter = new Scope(10, 20); var draft = new ContextIntentDraft(chapter); var first = draft.Add("Reveal the disagreement"); draft.AddPreview(ContextIntentText.Parse("- [x] Keep the promise\n- [ ] Reveal the disagreement")); Check(draft.Items.Count == 3 && draft.Items[1].Completed && !draft.Items[2].Completed, "Reviewed preview preserves explicit checked choices and duplicate author objectives."); var last = draft.Items[2].Key; draft.Complete(first, true); Check(draft.Items[0].Key == first && draft.Items[0].Completed, "Manual completion works without prose and does not reorder."); draft.Edit(first, " Revise the disagreement ", "Author note", true); Check(draft.Items[0] is { Text: "Revise the disagreement", Notes: "Author note", Changed: true, Completed: true }, "Editing/changed flag preserves manual completion."); draft.Complete(first, false); ContextIntentText.Entity[] palette = [ new("character:1", "Captain", [], ["Recorded appearance"]), new("candidate:2", "Captain", [], ["Candidate ยท unconfirmed"]) ]; var ambiguous = ContextIntentText.Match("Captain", palette).Single(); Check(ambiguous.Ambiguous && draft.Items[0].Links.Count == 0, "Matching an ambiguous occurrence does not mutate the draft."); Reject(() => draft.LinkEntity(first, "Captain", palette)); Reject(() => draft.LinkEntity(first, "character:99", palette)); draft.LinkEntity(first, "candidate:2", palette); draft.LinkEntity(first, "candidate:2", palette); Check(draft.Items[0].Links.Count == 1 && !draft.Items[0].Links[0].Covered && !draft.Items[0].Completed, "Linking a selected candidate is idempotent and never confirms/marks covered/completes."); draft.CoverEntity(first, "candidate:2", true); Check(draft.Items[0].Links[0].Covered && !draft.Items[0].Completed, "Explicit entity coverage remains independent of item completion."); Reject(() => draft.CoverEntity(first, "character:1", true)); draft.LinkEntity(first, "character:1", palette); Check(draft.Items[0].Links.Count == 2, "Different identities sharing a label remain distinct."); var passage = new Evidence(new Scope(10, 20, 30), "An author-selected passage.", "accepted-revision-1", "paragraph-1"); draft.AddEvidence(first, passage, false); Check(draft.Items[0].Evidence.Count == 1 && !draft.Items[0].Completed, "Attaching evidence requires an independent completion choice."); draft.AddEvidence(first, passage with { Passage = "A second passage." }, true); Check(draft.Items[0].Completed && draft.Items[0].Evidence[0] == passage, "Explicit evidence completion retains source identity and previous quote."); Reject(() => draft.AddEvidence(first, passage with { Source = new Scope(11, 20, 30) }, true)); Reject(() => draft.AddEvidence(first, passage with { Source = new Scope(10, 21, 30) }, true)); Reject(() => draft.AddEvidence(first, passage with { Passage = " " }, true)); Check(draft.Items[0].Evidence.Count == 2, "Rejected evidence is atomic."); draft.RemoveEvidence(first, 0); Check(draft.Items[0].Completed, "Removing evidence does not silently undo manual completion."); draft.Move(last, 0); Check(draft.Items[0].Key == last && draft.Items[1].Key == first, "Drag/keyboard move uses stable identity and author order."); Reject(() => draft.Move(first, 3)); Reject(() => draft.Move(Guid.NewGuid(), 0)); draft.Move(last, 2); Check(draft.Items[2].Key == last, "Downward movement uses final destination index."); var before = draft.Items.ToArray(); Reject(() => draft.AddPreview([new("Valid first item", false), new(new string('x', 301), false)])); Check(draft.Items.SequenceEqual(before), "An invalid paste cannot add an earlier valid item."); Reject(() => draft.Edit(first, " ", "", false)); Reject(() => draft.Edit(first, "Valid", new string('x', MaximumNotesLength + 1), false)); Check(draft.Items.SequenceEqual(before), "Rejected edits retain all local work."); draft.UnlinkEntity(first, "candidate:2"); Check(draft.Items[0].Completed && draft.Items[0].Links.Single().EntityKey == "character:1", "Removing a link does not affect completion or another identity."); var sceneDraft = new ContextIntentDraft(new Scope(10, 20, 31)); var sceneItem = sceneDraft.Add("Scene objective"); Reject(() => sceneDraft.AddEvidence(sceneItem, passage, true)); Check(!sceneDraft.Items.Single().Completed, "Evidence from another scene cannot complete a scene-scoped item."); sceneDraft.AddEvidence(sceneItem, passage with { Source = new Scope(10, 20, 31) }, true); for (var i = 1; i < MaximumEvidence; i++) sceneDraft.AddEvidence(sceneItem, passage with { Source = new Scope(10, 20, 31) }, false); Reject(() => sceneDraft.AddEvidence(sceneItem, passage with { Source = new Scope(10, 20, 31) }, false)); Check(sceneDraft.Items.Single().Evidence.Count == MaximumEvidence, "Attachment count is bounded without losing existing evidence."); var links = new List { new("character:1", false) }; var evidence = new List { passage }; var imported = new Item(Guid.NewGuid(), 5, "Saved objective", false, false, "", links, evidence); var loaded = new ContextIntentDraft(chapter, [imported]); links.Clear(); evidence.Clear(); Check(loaded.Items.Single().Links.Count == 1 && loaded.Items.Single().Evidence.Count == 1, "Loaded collections are defensively copied."); var readOnly = false; try { ((IList)loaded.Items.Single().Links).Add(new("asset:3", false)); } catch (NotSupportedException) { readOnly = true; } Check(readOnly, "Presentation callers cannot change attachment collections outside explicit operations."); Reject(() => new ContextIntentDraft(chapter, [imported, imported])); Reject(() => new ContextIntentDraft(chapter, [imported, imported with { Key = Guid.NewGuid() }])); Reject(() => new ContextIntentDraft(new Scope(0, 20))); Reject(() => new ContextIntentDraft(new Scope(10, 20, 0))); var full = new ContextIntentDraft(chapter); full.AddPreview(Enumerable.Repeat(new ContextIntentText.PreviewItem("Objective", false), ContextIntentText.MaximumItems)); Reject(() => full.Add("One too many")); Check(full.Items.Count == ContextIntentText.MaximumItems, "Checklist bounds retain every reviewed objective."); draft.Remove(last); Check(draft.Items.Count == 2, "Explicit removal affects only the selected item."); } }