using PlotLine.Services.Context; internal static class ContextWindowsTests { private sealed class Clock:TimeProvider {public DateTimeOffset Now=DateTimeOffset.UtcNow;public override DateTimeOffset GetUtcNow()=>Now;} private static void Check(bool value,string message){if(!value)throw new Exception(message);Console.WriteLine("PASS My Windows: "+message);} public static void Run() { var clock=new Clock();var store=new ContextWindows(clock);var first=Guid.NewGuid();var second=Guid.NewGuid(); ContextWindowRegistration Registration(Guid id,int book,string action,bool available=true)=>new(1,book,id,[new("0","reference",[action],available)]); store.Register(7,"origin",Registration(first,12,"scene.focus"));store.Register(7,"remote-device",Registration(second,12,"scene.focus")); store.Register(8,"other-user",Registration(Guid.NewGuid(),12,"scene.focus"));store.Register(7,"other-book",Registration(Guid.NewGuid(),13,"scene.focus")); var command=new ContextInteraction(1,12,"0","scene.focus",StoryPoint:new(10,22)); Check(store.List(7,12).Length==2,"discovery is isolated by account and book"); var local=store.Route(7,"origin",command)!.Value;Check(local.Window.Connection=="origin","suitable local pane is preferred"); var remote=store.Route(7,"origin",command with{TargetWindowID=second})!.Value;Check(remote.Window.Connection=="remote-device","explicit recipient is targeted without broadcasting"); Check(store.Complete(7,"origin",remote.CommandID)==null,"another connection cannot acknowledge a recipient's command"); Check(store.Complete(7,"remote-device",remote.CommandID)=="origin","only the selected recipient acknowledges delivery"); Check(store.Route(7,"origin",command with{Action="location.focus",LocationID=20})==null,"no-handler is explicit and does not replace a tool"); try{store.Route(7,"origin",command with{TargetUserID=8});throw new Exception("Cross-user request accepted");}catch(ArgumentException){Check(true,"cross-user commands are disabled");} try{store.Route(7,"origin",command with{BookID=13});throw new Exception("Cross-book request accepted");}catch(ArgumentException){Check(true,"origin registration cannot cross books");} store.Register(7,"origin",Registration(first,12,"scene.focus",false));Check(store.Route(7,"origin",command)!.Value.Window.Connection=="remote-device","busy local pane is bypassed without mutation"); store.Remove("remote-device");Check(store.Route(7,"origin",command)==null,"disconnect removes eligible destinations"); clock.Now+=TimeSpan.FromSeconds(61);Check(store.List(7,12).Length==0,"missed disconnects expire on authoritative discovery"); store.Register(7,"reconnected",Registration(first,12,"scene.focus"));Check(store.List(7,12).Length==1,"reconnection registers current capability state"); var environment=Guid.NewGuid();store.Register(7,"reconnected",Registration(first,12,"scene.focus") with{EnvironmentID=environment}); store.Register(7,"main-remote",new(1,12,Guid.NewGuid(),[new("0","main",["workspace.open"])],Guid.NewGuid(),"main")); var open=new ContextInteraction(1,12,"0","workspace.open",Workspace:new("characters",5));Check(store.Route(7,"reconnected",open)==null,"main navigation never targets another browser environment/device"); store.Register(7,"main-local",new(1,13,Guid.NewGuid(),[new("0","main",["workspace.open"])],environment,"main",[12,13])); Check(store.Route(7,"reconnected",open)?.Window.Connection=="main-local","same-environment main shell can receive authorised navigation for its project books"); Check(store.Route(7,"reconnected",open with{TargetSessionID="main-remote"})==null,"explicit target cannot bypass same-environment restriction"); store.Remove("main-local");store.Remove("main-remote");Check(store.TryLaunch(7,"reconnected",12,environment),"one browser environment can reserve a missing main-window launch");Check(!store.TryLaunch(7,"reconnected",12,environment),"concurrent fallback launches are suppressed");clock.Now+=TimeSpan.FromSeconds(11);Check(store.TryLaunch(7,"reconnected",12,environment),"failed or blocked main launches can be retried after the short lease"); try{store.Register(7,"bad",new(1,12,Guid.NewGuid(),[new("0","fake",["word.navigate"])]));throw new Exception("Word action accepted");}catch(ArgumentException){Check(true,"interaction contract cannot invoke Word navigation");} var launchStore=new ContextWindows(clock);var browser=Guid.NewGuid(); launchStore.Register(7,"launcher",new(1,12,Guid.NewGuid(),[],browser)); launchStore.Register(8,"foreign-user",Registration(Guid.NewGuid(),12,"scene.focus") with{EnvironmentID=browser}); launchStore.Register(7,"foreign-book",Registration(Guid.NewGuid(),13,"scene.focus") with{EnvironmentID=browser}); launchStore.Register(7,"foreign-browser",Registration(Guid.NewGuid(),12,"scene.focus") with{EnvironmentID=Guid.NewGuid()}); Check(launchStore.OpenContext(7,"launcher",12).Status=="open","ribbon launch cannot reuse a foreign account, book or browser window"); Check(launchStore.OpenContext(7,"launcher",12).Status=="pending","repeated ribbon launches reserve only one new Context window"); launchStore.Register(7,"held-context",Registration(Guid.NewGuid(),12,"scene.focus",false) with{EnvironmentID=browser}); Check(launchStore.OpenContext(7,"launcher",12)==("reused","held-context"),"existing Context is reused without replacing busy tools or its independent held position"); try{launchStore.OpenContext(8,"launcher",12);throw new Exception("Cross-user launch accepted");}catch(ArgumentException){Check(true,"Context launch validates its authenticated origin registration");} launchStore.Remove("held-context");clock.Now+=TimeSpan.FromSeconds(11); Check(launchStore.OpenContext(7,"launcher",12).Status=="open","failed launch reservations expire for an explicit retry"); } }