using Microsoft.AspNetCore.SignalR; using Microsoft.Extensions.Logging.Abstractions; using PlotLine.Data;using PlotLine.Hubs;using PlotLine.Models;using PlotLine.Services;using PlotLine.Services.Context;using PlotLine.ViewModels; internal static class ContextRelationshipTests { static void Check(bool pass,string message){if(!pass)throw new Exception(message);Console.WriteLine("PASS Context relationships: "+message);} public static async Task Run() { var r=new CharacterRelationship{CharacterRelationshipID=9,ProjectID=7,CharacterAID=5,CharacterBID=6,CharacterAName="From",CharacterBName="To",RelationshipTypeID=1,RelationshipTypeName="Parent",RelationshipCategoryName="Family",IsInitialRelationship=true,InitialRelationshipStateID=1,InitialRelationshipStateName="Established",InitialIntensity=4}; var events=new List{new(){RelationshipEventID=10,CharacterRelationshipID=9,SceneID=22,RelationshipStateID=2,RelationshipStateName="Changed",Description="Explicit scene note"},new(){RelationshipEventID=11,CharacterRelationshipID=9,SceneID=23,RelationshipStateID=3,RelationshipStateName="Future"}}; RelationshipMapData Data()=>new(){Characters=[new(){CharacterID=5,CharacterName="From"},new(){CharacterID=6,CharacterName="To"}],Books=[new(){BookID=12}],Chapters=[new(){ChapterID=10,BookID=12}],Scenes=[new(){SceneID=22,ChapterID=10,SceneNumber=1},new(){SceneID=23,ChapterID=10,SceneNumber=2}],Relationships=[r],RelationshipEvents=events}; var point=RelationshipMapService.BuildAtScene(Data(),new Dictionary{{22,0},{23,1}},22).Single();Check(point.CurrentStateName=="Changed"&&point.RelationshipCategoryName=="Family"&&!point.IsReciprocal,"canonical story-point state excludes future events, preserves Family and direction"); var role="Owner";RelationshipEventCreateViewModel? saved=null;InitialRelationshipEditViewModel? initial=null; var repository=NativeWorkflowTests.Proxy((name,_)=>name switch{"GetRelationshipMapAsync"=>Task.FromResult(Data()),"GetLookupsAsync"=>Task.FromResult(new CharacterLookupData{RelationshipTypes=[new(){RelationshipTypeID=1,TypeName="Parent",RelationshipCategoryName="Family",IsActive=true}],RelationshipStates=[new(){RelationshipStateID=1,StateName="Established",IsActive=true},new(){RelationshipStateID=2,StateName="Changed",IsActive=true}]}),_=>throw new Exception(name)}); var service=NativeWorkflowTests.Proxy((name,args)=>{if(name=="UpdateRelationshipEventAsync"){saved=(RelationshipEventCreateViewModel)args![0]!;return Task.FromResult(10);}if(name=="SaveInitialRelationshipAsync"){initial=(InitialRelationshipEditViewModel)args![0]!;return Task.FromResult(9);}throw new Exception(name);}); var client=NativeWorkflowTests.Proxy((_,_)=>Task.CompletedTask);var clients=NativeWorkflowTests.Proxy((_,_)=>client);var hub=NativeWorkflowTests.Proxy>((_,_)=>clients); var edit=new ContextRelationshipEditing(repository,service,NativeWorkflowTests.Proxy((_,_)=>Task.FromResult(new(){BookId=12,Chapters=[new(){ChapterId=10,Scenes=[new(){SceneId=22}]}]})),NativeWorkflowTests.Proxy((_,_)=>Task.FromResult(new(){BookID=12,ProjectID=7})),NativeWorkflowTests.Proxy((_,_)=>Task.FromResult(new(){ProjectID=7,AccessRole=role})),NativeWorkflowTests.Proxy((_,_)=>4),new ContextWorkspaceNotifications(hub,NullLogger.Instance)); var form=await edit.Read(12,22,9,"scene");Check(form.StateID==2&&form.Notes=="Explicit scene note","focused form loads current scene event values");form.Notes="Corrected scene note";await edit.Save(form);Check(saved?.RelationshipEventID==10&&saved.CharacterRelationshipID==9&&saved.CharacterAID==5&&saved.CharacterBID==6&&saved.SceneID==22,"save corrects only selected event using existing service"); form.TargetID=5;try{await edit.Save(form);throw new Exception("invalid pair accepted");}catch(ArgumentException){Check(true,"invalid self-relationship rejected");}form.TargetID=6; form.Mutual=true;try{await edit.Save(form);throw new Exception("direction changed");}catch(ArgumentException){Check(true,"existing directional relationship cannot silently become reciprocal");}form.Mutual=false; events[0].Description="Changed elsewhere";try{await edit.Save(form);throw new Exception("stale accepted");}catch(InvalidOperationException){Check(true,"stale relationship drafts rejected");} var start=await edit.Read(12,22,9,"initial");start.Notes="Initial correction";await edit.Save(start);Check(initial?.CharacterRelationshipID==9&&initial.InitialRelationshipStateID==1&&initial.CharacterID==5&&initial.RelatedCharacterID==6,"focused initial correction reuses existing initial relationship service"); try{await edit.Read(12,22,999,"scene");throw new Exception("foreign ID accepted");}catch(UnauthorizedAccessException){Check(true,"unowned relationship ID rejected");} try{await edit.Read(12,999,9,"scene");throw new Exception("foreign scene accepted");}catch(UnauthorizedAccessException){Check(true,"cross-book scene rejected");} r.StartSceneID=23;var author=await edit.Read(12,22,9,"initial","family");Check(author.ReferenceLens=="family"&&author.Types.Any(t=>t.Value=="1"),"Family initial corrections use explicit author truth without treating a future source as current POV knowledge"); r.RelationshipCategoryName="Professional / Social";r.RelationshipTypeName="Colleague";try{await edit.Read(12,22,9,"initial","family");throw new Exception("nonfamily accepted");}catch(ArgumentException){Check(true,"Family reference mode cannot relabel a nonfamily connection");}r.RelationshipCategoryName="Family";r.RelationshipTypeName="Parent";r.StartSceneID=null; role="Viewer";try{await edit.Read(12,22,9,"scene");throw new Exception("viewer accepted");}catch(UnauthorizedAccessException){Check(true,"read-only relationship correction rejected");} } }