diff --git a/Docs/Sprints/2026-11-03-phase2.md b/Docs/Sprints/2026-11-03-phase2.md index e217589b..a6daebdf 100644 --- a/Docs/Sprints/2026-11-03-phase2.md +++ b/Docs/Sprints/2026-11-03-phase2.md @@ -80,3 +80,14 @@ The exact reported whole-page PWA navigation lock has not been reproduced separa ### Guard interaction check — in-page Help Before deployment, an added real PWA test reproduced an unnecessary save dialog when the ribbon Help link opened its in-page drawer. The opt-in guard now excludes declared Help/Bootstrap in-page actions; links leaving the page and browser unload remain protected. Extended chapter-goal suite passed in browser/PWA, including Help opening without a save prompt and unchanged draft text. This small static-script correction follows the passing 405-check broader gate; it is not deployed yet. + +### Writer book-scope repair — reproduced and verified + +The expanded real-Hub test reproduced two additional failures before repair: + +- After selecting a second book, Open Context reused the first book's window: three total pages remained instead of opening the fourth, correctly scoped Context page. Client window references were keyed only by role. References now use the existing stable role/book window name. +- A real heartbeat from the first book changed the launcher's selected book from 13 back to 12. User-wide presence broadcasts were unconditionally adopted as local scope. Other-book/project updates now leave the selected scope and role state intact, while still updating user-level Word connectivity. Canonical story-position changes continue to select the intended scope explicitly. + +Verification: build passed (0 warnings/errors); full-layout Writer browser/PWA test passed with real heartbeat injection, second-book window creation, first-book preservation, restoration to the first scope, reconnect, identity/reload/focus tests and repeated navigation/header commands. Chapter-goal suite, including Help and concurrent edits, passed again. Fixture book data now honours the requested scope; no production routing or database schema changes. + +Urgent Writer release is still pending this final broader gate; an earlier publish was superseded before installation. Next: publish committed verified fixes, run the idle-work gate immediately before Development restart, smoke-check, then continue the rendered visual inventory. diff --git a/PlotLine.Tests/Browser/writer-workspace.cjs b/PlotLine.Tests/Browser/writer-workspace.cjs index 62280a50..4f46e17f 100644 --- a/PlotLine.Tests/Browser/writer-workspace.cjs +++ b/PlotLine.Tests/Browser/writer-workspace.cjs @@ -46,6 +46,31 @@ const base = 'http://127.0.0.1:5238'; await page.locator('[data-focus-writing-context]').click(); await page.locator('[data-focus-writing-intent]').click(); assert.equal(context.pages().length,3,'Focus commands reuse the existing writing windows'); + const oldContext=windows[0]; + const oldContextUrl=oldContext.url(); + await page.evaluate(()=>document.dispatchEvent(new CustomEvent('plotdirector:story-position-changed',{detail:{projectId:7,bookId:13}}))); + await page.waitForFunction(()=>document.querySelector('[data-writing-workspace-book]').textContent==='Book 13'); + await page.evaluate(async()=>{ + await window.PlotDirectorWritingWorkspace.watchWorkspace({projectID:7,bookID:13}); + document.addEventListener('plotdirector:writing-workspace-status',event=>{ + if((event.detail?.bookID||event.detail?.BookID)===12)window.__labelDuringOtherBookUpdate=document.querySelector('[data-writing-workspace-book]').textContent; + }); + }); + await oldContext.evaluate(()=>window.__writerConnections.at(-1).invoke('WritingWindowHeartbeat',{ + browserWindowID:window.PlotDirectorWritingWorkspace.getBrowserWindowId(),projectID:7,bookID:12,writingRole:'Context',currentRoute:location.pathname + })); + await page.waitForFunction(()=>window.__labelDuringOtherBookUpdate!==undefined); + assert.equal(await page.evaluate(()=>window.__labelDuringOtherBookUpdate),'Book 13','Another book heartbeat cannot change the selected workspace'); + await page.locator('[data-open-writing-context]').click(); + await page.waitForFunction(()=>!document.querySelector('[data-open-writing-context]').disabled); + assert.equal(context.pages().length,4,'Changing books opens a separately scoped Context window'); + assert.equal(oldContext.url(),oldContextUrl,'The first book context is preserved'); + const secondContext=context.pages().find(tab=>tab!==page&&!windows.includes(tab)); + await secondContext.waitForLoadState(); + assert.equal(new URL(secondContext.url()).searchParams.get('bookId'),'13'); + await secondContext.close(); + await page.evaluate(()=>document.dispatchEvent(new CustomEvent('plotdirector:story-position-changed',{detail:{projectId:7,bookId:12}}))); + await page.waitForFunction(()=>document.querySelector('[data-writing-workspace-book]').textContent==='Book 12'); const beforeRestart=negotiations; await page.evaluate(()=>window.__writerConnections.at(-1).stop()); await start.click(); diff --git a/PlotLine.Tests/ToolbarBrowserHost.cs b/PlotLine.Tests/ToolbarBrowserHost.cs index f34d27e0..85c0f1a2 100644 --- a/PlotLine.Tests/ToolbarBrowserHost.cs +++ b/PlotLine.Tests/ToolbarBrowserHost.cs @@ -123,7 +123,7 @@ internal static class ToolbarBrowserHost FixtureProxy((name, args) => name == "GetForUserAsync" ? Task.FromResult(new Project { ProjectID = 7, ProjectName = "The Glass Harbour", AccessRole = "Owner" }) : throw new NotSupportedException(name)), FixtureProxy((name, args) => name == "ListByProjectAsync" - ? Task.FromResult>([new() { BookID = 12, ProjectID = 7, BookTitle = "A Distant Shore" }]) : throw new NotSupportedException(name)), + ? Task.FromResult>([new() { BookID = 12, ProjectID = 7, BookTitle = "A Distant Shore" }, new() { BookID = 13, ProjectID = 7, BookTitle = "The Return" }]) : throw new NotSupportedException(name)), services.GetRequiredService())); builder.Services.AddScoped(services => FixtureProxy((name, args) => name switch { "GetDashboardAsync" => Task.FromResult(WriterFixture()), @@ -238,8 +238,8 @@ internal static class ToolbarBrowserHost context.Response.ContentType = "text/html; charset=utf-8"; await context.Response.WriteAsync(writer.ToString()); }); - app.MapGet("/Writing/Context", context => RenderFullAsync(context, "Writing", "Context", new WritingContextPageViewModel { ProjectID = 7, BookID = 12 })).WithOrder(-1); - app.MapGet("/Writing/Intent", context => RenderFullAsync(context, "Writing", "Intent", new WritingIntentPageViewModel { ProjectID = 7, BookID = 12 })).WithOrder(-1); + app.MapGet("/Writing/Context", context => RenderFullAsync(context, "Writing", "Context", new WritingContextPageViewModel { ProjectID = 7, BookID = int.TryParse(context.Request.Query["bookId"].ToString(), out var bookId) ? bookId : 12 })).WithOrder(-1); + app.MapGet("/Writing/Intent", context => RenderFullAsync(context, "Writing", "Intent", new WritingIntentPageViewModel { ProjectID = 7, BookID = int.TryParse(context.Request.Query["bookId"].ToString(), out var bookId) ? bookId : 12 })).WithOrder(-1); app.MapGet("/fixture", async context => { if (context.Request.Query["page"] == "writer" && context.Request.Query["layout"] == "full") diff --git a/PlotLine/wwwroot/js/writing-workspace-launcher.js b/PlotLine/wwwroot/js/writing-workspace-launcher.js index 26fe0303..46580d48 100644 --- a/PlotLine/wwwroot/js/writing-workspace-launcher.js +++ b/PlotLine/wwwroot/js/writing-workspace-launcher.js @@ -95,6 +95,14 @@ }; const render = (status = latestStatus) => { + const incomingScope = workspace.statusScope(status); + if (status && ((activeScope.projectID && incomingScope.projectID && activeScope.projectID !== incomingScope.projectID) + || (activeScope.bookID && incomingScope.bookID && activeScope.bookID !== incomingScope.bookID))) { + // Presence broadcasts cover all this author's books. They must not change + // the workspace chosen here by a canonical position or explicit command. + text(statusNodes.word, status.wordCompanionConnected || status.WordCompanionConnected ? "Connected" : "Not connected"); + return; + } latestStatus = status; activeScope = workspace.statusScope(status, activeScope); text(statusNodes.book, activeScope.bookID ? `Book ${activeScope.bookID}` : "Waiting for book"); diff --git a/PlotLine/wwwroot/js/writing-workspace.js b/PlotLine/wwwroot/js/writing-workspace.js index 5ed8a3a1..a782a7ef 100644 --- a/PlotLine/wwwroot/js/writing-workspace.js +++ b/PlotLine/wwwroot/js/writing-workspace.js @@ -307,7 +307,7 @@ const name = stableWindowName(role, scope); const opened = window.open(url, name, popupFeatures(display)); if (opened) { - windowRefs.set(role, opened); + windowRefs.set(stableWindowName(role, scope), opened); try { opened.focus(); } catch { @@ -318,7 +318,7 @@ }; const focusRole = async (role, scope, display = null) => { - const ref = windowRefs.get(role); + const ref = windowRefs.get(stableWindowName(role, scope)); if (ref && !ref.closed) { ref.focus(); return true; @@ -326,7 +326,7 @@ const opened = window.open(buildWritingUrl(role, scope), stableWindowName(role, scope), popupFeatures(display)); if (opened) { - windowRefs.set(role, opened); + windowRefs.set(stableWindowName(role, scope), opened); opened.focus(); return true; }