diff --git a/PlotLine.Tests/Browser/native-import-connection.cjs b/PlotLine.Tests/Browser/native-import-connection.cjs index 90f73268..15e089bf 100644 --- a/PlotLine.Tests/Browser/native-import-connection.cjs +++ b/PlotLine.Tests/Browser/native-import-connection.cjs @@ -16,6 +16,7 @@ const {chromium}=require('playwright');const fs=require('node:fs');const path=re await state([{wordAvailable:false,wordRunning:true}]);assert.match(await label.textContent(),/Word connected.*open a manuscript/);assert.equal(await button.isDisabled(),true); const manuscript={bookId:0,wordAvailable:true,wordRunning:true,canCapture:true,active:true,documentTitle:'Title ',expires:new Date(Date.now()+45000).toISOString()}; await state([manuscript]);assert.match(await label.textContent(),/not yet associated/);assert.equal(await button.isDisabled(),false,'first-time import must not deadlock on missing association');assert.match(await page.locator('[data-word-companion-connected-copy]').textContent(),/only after your request/);assert.equal(await page.locator('script:not([src])').count(),1,'filename must not inject script markup'); + await state([{...manuscript,active:false},{...manuscript,bookId:7,active:true}]);assert.equal(await button.isDisabled(),true,'do not substitute inactive unassociated manuscript for a different active book'); await state([{...manuscript,bookId:5}]);assert.match(await label.textContent(),/ready to import/);assert.equal(await button.isDisabled(),false); await state([{...manuscript,bookId:7}]);assert.match(await label.textContent(),/another book/);assert.equal(await button.isDisabled(),true); await state([{...manuscript,expires:new Date(Date.now()-1000).toISOString()}]);assert.equal(await button.isDisabled(),true,'stale document cannot enable import'); diff --git a/PlotLine.Tests/NativeManuscriptCaptureTests.cs b/PlotLine.Tests/NativeManuscriptCaptureTests.cs index 3586c4f6..36297486 100644 --- a/PlotLine.Tests/NativeManuscriptCaptureTests.cs +++ b/PlotLine.Tests/NativeManuscriptCaptureTests.cs @@ -40,6 +40,8 @@ internal static class NativeManuscriptCaptureTests var freshId=Guid.NewGuid();var freshSession=Guid.NewGuid();var freshManuscript=Guid.NewGuid(); store.Put(7,"fresh-grant",DateTimeOffset.UtcNow.AddHours(1),freshId,freshSession,new(){BookId=0,WordRunning=true,ManuscriptId=freshManuscript,Sequence=1,CanCapture=true,Active=true,DocumentTitle="First manuscript"}); store.Remove(7,"grant",source.InstanceId,source.SessionId); + var otherActiveSession=Guid.NewGuid();store.Put(7,"fresh-grant",DateTimeOffset.UtcNow.AddHours(1),freshId,otherActiveSession,new(){BookId=99,ManuscriptId=Guid.NewGuid(),Sequence=1,CanCapture=true,Active=true,DocumentTitle="Different active book"}); + bad=false;try{await transfer.Start(7,4,1,3);}catch(InvalidOperationException){bad=true;}Check(bad,"An inactive unassociated manuscript is never substituted for an active document belonging to another book");store.Remove(7,"fresh-grant",freshId,otherActiveSession); var firstImport=await transfer.Start(7,4,1,3);var command=store.TakeNavigation(7,"fresh-grant",freshId,freshSession); Check(firstImport?.ScanID!=null&&command?.BookId==3&&command.SessionId==freshSession,"First-time import targets the active unassociated manuscript and carries the authorised destination book"); await transfer.Fail(7,"fresh-grant",command!.Id);store.Remove(7,"fresh-grant",freshId,freshSession); diff --git a/PlotLine/Services/NativeCompanion/NativeManuscriptCapture.cs b/PlotLine/Services/NativeCompanion/NativeManuscriptCapture.cs index 8b15732e..1d8ad34e 100644 --- a/PlotLine/Services/NativeCompanion/NativeManuscriptCapture.cs +++ b/PlotLine/Services/NativeCompanion/NativeManuscriptCapture.cs @@ -17,7 +17,11 @@ public sealed class NativeManuscriptCapture(NativePresenceStore presence,IManusc using var scope=scopes.CreateScope();var access=scope.ServiceProvider.GetRequiredService(); if(await access.GetBookStructureAsync(book,user)==null)throw new InvalidOperationException("The selected book is not accessible."); foreach(var expired in pending.Where(p=>p.Value.Expires<=clock.GetUtcNow()).Select(p=>p.Key))pending.TryRemove(expired,out _); - var documents=presence.Read(user,0).Where(l=>(l.Position.BookId==book||l.Position.BookId==0)&&l.Position.WordAvailable&&l.Position.CanCapture).ToArray(); + var all=presence.Read(user,0).Where(l=>l.Position.WordAvailable&&l.Position.CanCapture).ToArray(); + var linked=all.Where(l=>l.Position.BookId==book).ToArray(); + var documents=linked.Length>0?linked:all.Where(l=>l.Position.BookId==0).ToArray(); + if(linked.Length==0&&documents.Length>0&&all.Any(l=>l.Position.Active&&l.Position.BookId>0&&l.Position.BookId!=book)) + throw new InvalidOperationException("Activate the intended unassociated manuscript in Word before importing. The active document belongs to another book."); if(documents.Length==0)return null; var active=documents.Where(l=>l.Position.Active).ToArray(); var selected=active.Length==1?active[0]:documents.Length==1?documents[0]:throw new InvalidOperationException("More than one Word document is linked to this book. Activate the intended document, then retry."); diff --git a/PlotLine/wwwroot/js/word-companion-presence.js b/PlotLine/wwwroot/js/word-companion-presence.js index a8f9dd94..2888db26 100644 --- a/PlotLine/wwwroot/js/word-companion-presence.js +++ b/PlotLine/wwwroot/js/word-companion-presence.js @@ -2,7 +2,8 @@ // Uses the existing native presence stream; no extra polling or authentication flow. window.PlotDirectorNativeImportState=(leases,book,now=Date.now())=>{ const live=(leases||[]).filter(l=>!l.expires||Date.parse(l.expires)>now),words=live.filter(l=>l.wordAvailable); - const candidates=words.filter(l=>l.canCapture&&(l.bookId===0||!book||l.bookId===book)),active=candidates.filter(l=>l.active); + const linked=words.filter(l=>l.canCapture&&(!book||l.bookId===book)); + const candidates=linked.length?linked:words.some(l=>l.active&&l.bookId>0&&l.bookId!==book)?[]:words.filter(l=>l.canCapture&&l.bookId===0),active=candidates.filter(l=>l.active); const doc=active.length===1?active[0]:candidates.length===1?candidates[0]:null; const connected=live.length>0||leases?.connectionState==='Connected'; const label=doc?(doc.bookId===0?'Manuscript open · not yet associated with this book':'Manuscript connected · ready to import'):candidates.length>1?'Choose the active Word manuscript':words.length?'Manuscript belongs to another book · change its association':live.some(l=>l.wordRunning)?'Word connected · open a manuscript':connected?'Companion authenticated · Word unavailable':leases?.connectionState==='Reconnecting'?'Companion reconnecting':leases?.connectionState==='Action required'?'Companion needs authorisation':'Companion disconnected';