diff --git a/PlotLine.Tests/Browser/README.md b/PlotLine.Tests/Browser/README.md index 55ec45e3..99876fc1 100644 --- a/PlotLine.Tests/Browser/README.md +++ b/PlotLine.Tests/Browser/README.md @@ -1,5 +1,7 @@ # Timeline metric browser regressions +Character detail live-update regressions: run `npm run test:characters`. This executes the production script with mocked DOM, SignalR, and timers using Node only. It covers historical completion replay, a single refresh for new completion, reconnects, completion during initial connection, stale/other-character events, failed analysis, and manual reanalysis. + Run from this directory: ```sh diff --git a/PlotLine.Tests/Browser/character-enrichment-detail.cjs b/PlotLine.Tests/Browser/character-enrichment-detail.cjs new file mode 100644 index 00000000..fff1ea9a --- /dev/null +++ b/PlotLine.Tests/Browser/character-enrichment-detail.cjs @@ -0,0 +1,76 @@ +const assert = require('node:assert/strict'); +const fs = require('node:fs'); +const path = require('node:path'); +const vm = require('node:vm'); +const source = fs.readFileSync(path.join(__dirname, '../../PlotLine/wwwroot/js/character-enrichment-detail.js'), 'utf8'); +const snapshot = (run, status = 'Completed', character = 42) => ({ + characterID: character, characterEnrichmentRunID: run, status +}); + +async function fixture(run, status, initialSnapshot) { + const elements = Object.fromEntries(['panel', 'status', 'last', 'message', 'summary'].map(name => + [`[data-character-analysis-${name}]`, { textContent: '', classList: { add() {} } }])); + elements['[data-character-analysis-panel]'].dataset = { + characterId: '42', analysisRunId: String(run), analysisStatus: status + }; + elements['[data-character-analysis-last]'].textContent = '10 September 2026, 12:00'; + const button = elements['[data-character-reanalyse-button]'] = {}; + let submit; + elements['[data-character-reanalyse-form]'] = { action: '/Characters/Reanalyse', addEventListener: (_, fn) => { submit = fn; } }; + let progress, reconnect, currentSnapshot = initialSnapshot; + const timers = []; + let reloads = 0; + const connection = { + start: async () => {}, invoke: async () => currentSnapshot, + on: (_, fn) => { progress = fn; }, onreconnected: fn => { reconnect = fn; } + }; + const fetch = async () => ({ ok: true, json: async () => snapshot(run + 1, 'Queued') }); + vm.runInNewContext(source, { + document: { querySelector: selector => elements[selector] }, + signalR: { HubConnectionBuilder: class { + withUrl() { return this; } withAutomaticReconnect() { return this; } build() { return connection; } + } }, + window: { setTimeout: fn => timers.push(fn), location: { reload: () => reloads++ }, fetch }, + fetch, FormData: class {} + }); + await new Promise(resolve => setImmediate(resolve)); + return { elements, button, timers, progress, submit, + reloads: () => reloads, + reconnect: async value => { currentSnapshot = value; reconnect(); await new Promise(resolve => setImmediate(resolve)); } + }; +} + +(async () => { + const saved = await fixture(10, 'Completed', snapshot(10)); + assert.equal(saved.timers.length, 0, 'Opening a completed profile must not reload'); + await saved.reconnect(snapshot(10)); + saved.progress(snapshot(10)); + assert.equal(saved.timers.length, 0, 'Replayed completions must not reload'); + assert.equal(saved.elements['[data-character-analysis-last]'].textContent, '10 September 2026, 12:00'); + saved.progress(snapshot(11, 'Completed', 99)); + assert.equal(saved.timers.length, 0, 'Other characters must not affect this page'); + await saved.submit({ preventDefault() {} }); + assert.equal(saved.button.disabled, true); + saved.progress(snapshot(11, 'Running')); + saved.progress(snapshot(10)); + assert.equal(saved.button.disabled, true, 'Older run must not overwrite an active run'); + saved.progress(snapshot(11)); + saved.progress(snapshot(11)); + await saved.reconnect(snapshot(11)); + assert.equal(saved.timers.length, 1, 'A new completion should schedule exactly one reload'); + saved.timers[0](); + assert.equal(saved.reloads(), 1); + const reloaded = await fixture(11, 'Completed', snapshot(11)); + assert.equal(reloaded.timers.length, 0, 'The refreshed page must remain stable'); + for (const status of ['Queued', 'Running']) { + const active = await fixture(12, status, snapshot(12)); + assert.equal(active.timers.length, 1, 'Completion during connection setup must refresh an active page'); + } + const missed = await fixture(10, 'Completed', snapshot(10)); + await missed.reconnect(snapshot(12)); + assert.equal(missed.timers.length, 1, 'Reconnect must pick up a newer completed run'); + const failed = await fixture(12, 'Running', snapshot(12, 'Failed')); + assert.equal(failed.timers.length, 0); + assert.equal(failed.button.disabled, false); + console.log('PASS character detail: saved/replayed completions, real completion, reconnect, stale events, failure, and reanalysis'); +})().catch(error => { console.error(error); process.exitCode = 1; }); diff --git a/PlotLine.Tests/Browser/package.json b/PlotLine.Tests/Browser/package.json index f2fa2ba9..22fd3b32 100644 --- a/PlotLine.Tests/Browser/package.json +++ b/PlotLine.Tests/Browser/package.json @@ -1,5 +1,5 @@ { "private": true, - "scripts": { "test": "node timeline-metric-layout.cjs", "test:locations": "node location-reparent.cjs" }, + "scripts": { "test": "node timeline-metric-layout.cjs", "test:locations": "node location-reparent.cjs", "test:characters": "node character-enrichment-detail.cjs" }, "devDependencies": { "chart.js": "4.4.9", "playwright": "1.58.2" } } diff --git a/PlotLine/Views/Characters/Details.cshtml b/PlotLine/Views/Characters/Details.cshtml index 63eef350..424ab7e3 100644 --- a/PlotLine/Views/Characters/Details.cshtml +++ b/PlotLine/Views/Characters/Details.cshtml @@ -101,7 +101,7 @@ }
-
+

Profile

Preferred name: @Model.Character.CharacterName

Sex: @Model.Character.Sex

diff --git a/PlotLine/wwwroot/js/character-enrichment-detail.js b/PlotLine/wwwroot/js/character-enrichment-detail.js index bcc48264..174fda11 100644 --- a/PlotLine/wwwroot/js/character-enrichment-detail.js +++ b/PlotLine/wwwroot/js/character-enrichment-detail.js @@ -5,12 +5,14 @@ } const characterId = Number(panel.dataset.characterId || 0); - const initialBookId = Number(panel.dataset.bookId || 0); + const renderedRunId = Number(panel.dataset.analysisRunId || 0); + const renderedStatus = panel.dataset.analysisStatus || ""; + let latestRunId = renderedRunId; + let reloadScheduled = false; const form = document.querySelector("[data-character-reanalyse-form]"); const button = document.querySelector("[data-character-reanalyse-button]"); const status = document.querySelector("[data-character-analysis-status]"); const message = document.querySelector("[data-character-analysis-message]"); - const last = document.querySelector("[data-character-analysis-last]"); const summary = document.querySelector("[data-character-analysis-summary]"); if (!characterId) { return; @@ -36,6 +38,11 @@ } const nextStatus = snapshot.status || snapshot.Status || ""; + const runId = Number(snapshot.characterEnrichmentRunID || snapshot.CharacterEnrichmentRunID || 0); + if (!runId || runId < latestRunId) { + return; + } + latestRunId = runId; const nextMessage = snapshot.currentMessage || snapshot.CurrentMessage || ""; if (status) { status.textContent = nextStatus || "Not analysed"; @@ -56,10 +63,12 @@ if (nextStatus === "Completed") { setReady(); - if (last) { - last.textContent = "Just now"; + // Initial subscriptions and reconnects replay completed runs. Only reload + // when the saved page does not already contain this completed analysis. + if (!reloadScheduled && (runId > renderedRunId || renderedStatus !== "Completed")) { + reloadScheduled = true; + window.setTimeout(() => window.location.reload(), 700); } - window.setTimeout(() => window.location.reload(), 700); return; }