77 lines
4.0 KiB
JavaScript
77 lines
4.0 KiB
JavaScript
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; });
|