PlotDirector/PlotLine.Tests/SharedFocusNativeDeliveryTests.cs

61 lines
6.2 KiB
C#

using System.Data;
using Dapper;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Data.SqlClient;
using Microsoft.Extensions.Logging.Abstractions;
using PlotDirector.Companion.Protocol;
using PlotLine.Data;
using PlotLine.Hubs;
using PlotLine.Services;
using PlotLine.Services.NativeCompanion;
// Real coordinator/SQL and the existing native queue, with a recording transport instead of Word.
internal static class SharedFocusNativeDeliveryTests
{
private sealed class Factory(string value):ISqlConnectionFactory
{public IDbConnection CreateConnection()=>new SqlConnection(value);}
public static async Task Run(string value,SharedFocusDatabaseTests.Fixture f)
{
void Check(bool ok,string name){if(!ok)throw new Exception(name);Console.WriteLine("PASS shared native delivery: "+name);}
var packets=new List<(string Name,object? Packet)>();
var client=NativeWorkflowTests.Proxy<IClientProxy>((_,args)=>{packets.Add(((string)args![0]!,((object?[])args[1]!)[0]));return Task.CompletedTask;});
var clients=NativeWorkflowTests.Proxy<IHubClients>((_,args)=>{Check((string)args![0]! ==NativeCompanionPresenceHub.UserGroup(f.user),"notifications use only the authenticated author group");return client;});
var hub=NativeWorkflowTests.Proxy<IHubContext<NativeCompanionPresenceHub>>((_,_)=>clients);
var store=new NativePresenceStore(TimeProvider.System);var instance=Guid.NewGuid();var window=Guid.NewGuid();var mode=Guid.NewGuid();
var service=new SharedManuscriptFocus(new Factory(value),hub,store,NullLogger<SharedManuscriptFocus>.Instance);
NativePresence Position(long sequence,bool held=false,bool saved=true)=>new(){BookId=f.book,ManuscriptId=f.manuscript,Revision=1,Sequence=sequence,WordSaved=saved,WordAvailable=true,WordRunning=true,CanAcknowledgeNavigation=true,CanSharedFocus=true,FocusHeld=held,AnyFollowingWindows=!held,FocusWindowID=window,FocusModeRequest=mode,CursorStart=12,DocumentTitle="Synthetic shared-focus fixture"};
NativeLease Put(NativePresence position){Check(store.Put(f.user,"synthetic-focus-grant",DateTimeOffset.UtcNow.AddMinutes(10),instance,f.session,position),"exact native lease accepted");return store.Read(f.user,f.book).Single(l=>l.Instance==instance&&l.Session==f.session);}
AdapterCommand? Take()=>store.TakeNavigation(f.user,"synthetic-focus-grant",instance,f.session);
var first=Position(1);first.DeliberateFocusSequence=1;
await service.NativeObservation(f.user,f.project,Put(first),false);
var current=await service.Run(f.user,new(){ProjectID=f.project,BookID=f.book},"read");
Check(current.ParagraphID==f.paragraph1&&current.OriginWindowID==window,"Word alone publishes the verified canonical paragraph");
var command=Take();Check(command?.BackgroundFocus==true&&command.Chapter?.Start==10&&command.FocusVersion==current.Version,"shared navigation uses the existing exact-session background command");
var ack=Position(2);ack.NavigationAcknowledgedId=command!.Id;ack.NavigationResult="completed";Put(ack);
var count=packets.Count;
await service.NativeObservation(f.user,f.project,Put(Position(3)),false);
Check(packets.Count==count&&(await service.Run(f.user,new(){ProjectID=f.project,BookID=f.book},"read")).Version==current.Version,"passive native presence neither publishes nor polls shared focus");
var browserWindow=Guid.NewGuid();
var next=await service.Run(f.user,new(){ProjectID=f.project,BookID=f.book,WindowID=browserWindow,Sequence=1,ParagraphID=f.paragraph3},"publish");
command=Take();Check(command?.BackgroundFocus==true&&command.Chapter?.Start==40&&command.FocusVersion==next.Version&&command.FocusOriginWindowID==browserWindow,"browser focus queues the exact verified paragraph across chapters without activation semantics");
ack=Position(4);ack.NavigationAcknowledgedId=command!.Id;ack.NavigationResult="completed";Put(ack);
mode=Guid.NewGuid();var held=await service.NativeObservation(f.user,f.project,Put(Position(5,true)),true);
Check(held=="held"&&Take()==null,"held Word consumes no shared navigation command");
mode=Guid.NewGuid();await service.NativeObservation(f.user,f.project,Put(Position(6)),true);command=Take();
Check(command?.BackgroundFocus==true&&command.FocusVersion==next.Version&&(await service.Run(f.user,new(){ProjectID=f.project,BookID=f.book},"read")).Version==next.Version,"Word release queues latest revision without publishing its old cursor");
var failed=Position(7);failed.NavigationAcknowledgedId=command!.Id;failed.NavigationResult="failed";Check(await service.NativeObservation(f.user,f.project,Put(failed),true)=="word-busy"&&Take()==null,"failed background receipt is reported as busy rather than successful following");
Check(await service.NativeObservation(f.user,f.project,Put(Position(8,saved:false)),true)=="save-required","unsaved Word reports its limitation while the coordinator stays available");
var otherInstance=Guid.NewGuid();var otherSession=Guid.NewGuid();var other=Position(1);other.Sequence=1;
Check(store.Put(f.user,"synthetic-other-session",DateTimeOffset.UtcNow.AddMinutes(10),otherInstance,otherSession,other),"independent native session accepted");
var unmapped=await service.NativeObservation(f.user,f.project,store.Read(f.user,f.book).Single(l=>l.Instance==otherInstance),true);
Check(unmapped=="word-unmapped"&&store.TakeNavigation(f.user,"synthetic-other-session",otherInstance,otherSession)==null,"unmapped sessions receive an explicit limitation and never another session's offsets");
await using var validation=new SqlConnection(value);await validation.OpenAsync();
try{
await validation.ExecuteAsync("UPDATE dbo.BookChapterValidations SET WordRevision=2 WHERE BookID=@book AND UserID=@user AND SessionID=@session",new{book=f.book,user=f.user,session=f.session});
var saved=Position(9);saved.Revision=2;saved.ParagraphRevision=2;
await service.NativeObservation(f.user,f.project,Put(saved),true);var renewed=Take();
Check(renewed?.Revision==2&&renewed.Id!=command!.Id&&renewed.FocusVersion==next.Version,"saved Word revision and accepted snapshot receive a fresh delivery identity without manufacturing shared focus");
}finally{await validation.ExecuteAsync("UPDATE dbo.BookChapterValidations SET WordRevision=1 WHERE BookID=@book AND UserID=@user AND SessionID=@session AND WordRevision=2",new{book=f.book,user=f.user,session=f.session});}
}
}