// The tab keeps its DOM between visits (app.js loads a component once and marks // it data-loaded), so whatever the last run rendered stays on screen. An // illustration from a previous generation sat under an empty form as though it // were output for a topic nobody had typed, and only a full page refresh // cleared it. const test = require('node:test'); const assert = require('node:assert/strict'); const fs = require('node:fs'); const path = require('node:path'); const ui = fs.readFileSync(path.join(__dirname, '..', 'public/js/myResources.js'), 'utf8'); test('a new generation starts from a clean result area', () => { assert.match(ui, /function clearResults\(\)/); const gen = ui.slice(ui.indexOf('function runGenerate'), ui.indexOf('function reportSearches')); assert.match(gen, /clearResults\(\);/, 'generate clears before it runs'); }); test('a modification clears it too', () => { const start = ui.indexOf('function runModify'); const mod = ui.slice(start, ui.indexOf('function renderRow', start)); assert.match(mod, /clearResults\(\);/); }); test('reopening the tab clears it, but the first visit does not need to', () => { const tab = ui.slice(ui.indexOf("e.detail.tab !== 'myresources'"), ui.indexOf('var available')); assert.match(tab, /else clearResults\(\);/, 'only on a revisit — on the first visit there is nothing to clear and init() has not run'); }); test('clearing covers every container a run writes into', () => { const fn = ui.slice(ui.indexOf('function clearResults'), ui.indexOf('function runGenerate')); for (const id of ['mr-images', 'mr-searches', 'mr-image-failures']) { assert.ok(fn.includes(id), id + ' is not cleared'); } assert.match(fn, /status\(''\)/, 'the status line is part of the debris'); // textContent, not innerHTML: these hold model-derived text and image nodes. assert.match(fn, /el\.textContent = ''/); });