PlotDirector/PlotLine.Tests/ContextWindowsTests.cs

28 lines
3.1 KiB
C#

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");
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");}
}
}