"use strict"; // Regression tests for the Escape-precedence fix (finding 13). // // bot.js's document-level keydown chain checked progressPopup LAST: // // if (imageViewerModal...) ... // ... // if (filesModal && filesModal.classList.contains('show')) { closeFilesModal(); return; } // ... // if (progressPopup && progressPopup.style.display === 'block') { closeProgressPopup(); return; } // // But the progress popup only opens from inside the Files page, where // filesModal always has .show — so the progressPopup branch was unreachable. // Pressing Esc during an upload closed the whole Files page and left the // top-level popup orphaned over whatever view resulted; worse, // closeProgressPopup() is the upload-cancel affordance (it calls // requestUploadCancel() to abort the in-flight batch), so Esc never cancelled // a running operation. // // The fix checks progressPopup FIRST: at z-index 100000 it is the top-most // overlay in the chain (imageViewerModal sits at 10002) and can be visible // over any of the modals below it. These tests pin the order, the completeness // of the chain, and the stacking rationale. const test = require("node:test"); const assert = require("node:assert"); const fs = require("node:fs"); const path = require("node:path"); const ROOT = path.join(__dirname, ".."); const BOT_JS = path.join(ROOT, "public", "js", "bot.js"); const BOT_CSS = path.join(ROOT, "public", "css", "bot.css"); const botSrc = fs.readFileSync(BOT_JS, "utf8"); const botCss = fs.readFileSync(BOT_CSS, "utf8"); // The one keydown chain that routes Esc through every Files-page modal. It is // identified by its unique first handler (imageViewer) followed by the // progressPopup check that must precede it. const PROGRESS_PREDICATE = "if (progressPopup && progressPopup.style.display === 'block') { closeProgressPopup(); return; }"; function escapeChainWindow() { const at = botSrc.indexOf(PROGRESS_PREDICATE); assert.notStrictEqual(at, -1, "the Esc chain must still contain the progressPopup check"); // Take a window around the progressPopup check that is guaranteed to cover // the whole branch: from the start of the keydown handler it lives in to // past the last modal check. The handler opens with "if (e.key === 'Escape') {". const handlerStart = botSrc.lastIndexOf("if (e.key === 'Escape') {", at); assert.notStrictEqual(handlerStart, -1, "progressPopup check must live inside an Escape branch"); return botSrc.slice(handlerStart, at + 4000); } test("Esc checks the progress popup before any modal in the Files-page keydown chain", () => { const chain = escapeChainWindow(); // Order within the branch: each predicate's offset must strictly increase. const order = [ ["progressPopup", PROGRESS_PREDICATE], ["imageViewerModal", "if (imageViewerModal && imageViewerModal.classList.contains('show')) { closeImageViewer(); return; }"], // Inline-capable pages (aiHelp, files, store, activity, backups) are gated // through isPageOpen(.show OR .adpanel-page-active) so Esc still works when // the inline stylesheet's display:flex !important desyncs the two classes. ["aiHelpModal", "if (aiHelpModal && isPageOpen(aiHelpModal)) { closeAiHelpModal(); return; }"], ["editorModal", "if (editorModal && editorModal.classList.contains('show')) { closeEditorModal(); return; }"], ["filesModal", "if (filesModal && isPageOpen(filesModal)) { closeFilesModal(); return; }"], ["taskModal", "if (taskModal && taskModal.classList.contains('show')) { closeTaskModal(); return; }"], ]; let prev = -1; for (const [name, predicate] of order) { const at = chain.indexOf(predicate); assert.notStrictEqual(at, -1, `the Esc chain must still handle ${name}`); assert.ok(at > prev, `${name} must be checked after the element before it (progress popup is top-most and must win Esc)`); prev = at; } }); test("the progressPopup predicate is checked exactly once in bot.js (no duplicate left at the chain tail)", () => { const count = botSrc.split(PROGRESS_PREDICATE).length - 1; assert.equal(count, 1, `expected exactly one progressPopup Esc check, found ${count}`); }); test("the popup visibility predicate is unchanged (style.display === 'block')", () => { // Same test the wheel toggle uses, so Esc, the toggle and the close button // all agree on what "visible" means. const openFn = botSrc.indexOf("function openProgressPopup()"); assert.notStrictEqual(openFn, -1, "openProgressPopup must still exist"); const openBody = botSrc.slice(openFn, openFn + 600); assert.match(openBody, /progressPopup\.style\.display = "block"/); assert.ok( botSrc.includes("if (progressPopup.style.display === 'block') closeProgressPopup();"), "the wheel toggle's open/close test must keep matching the Esc predicate" ); }); test("closeProgressPopup remains the upload-cancel affordance (calls requestUploadCancel)", () => { const at = botSrc.indexOf("function closeProgressPopup()"); assert.notStrictEqual(at, -1, "closeProgressPopup must still exist"); const body = botSrc.slice(at, at + 500); assert.match( body, /requestUploadCancel\(\)/, "Esc on the popup must abort the in-flight batch — that is the whole point of giving the popup Esc precedence" ); }); test("the full original modal chain survived the reorder (no handler dropped)", () => { const chain = escapeChainWindow(); // closeInfoModal() is intentionally absent: the info modal (element, // declaration and open/close handlers) was removed from bot.ejs/bot.js, and // its leftover Esc branch referenced the now-undeclared infoModal // identifier — an uncaught ReferenceError on every Esc that reached it. for (const closer of [ "closeImageViewer()", "closeAiHelpModal()", "closeEditorModal()", "closeFilesModal()", "closeTaskModal()", "closeModrinthPluginModal()", "closeStoreModal()", "closeActivityModal()", "closeBackupsModal()", "closeBackupCreateModal()", "closeResourceStatsPopup()", ]) { assert.ok(chain.includes(closer), `Esc chain lost a handler: ${closer}`); } }); test("stacking rationale: .progress-popup renders above every modal in the chain", () => { // The reorder is correct because the popup is the top-most overlay. Pin the // numbers so a restyle that changes stacking forces a re-examination of the // Esc order too. const popupBlock = botCss.slice(botCss.indexOf(".progress-popup {"), botCss.indexOf("}", botCss.indexOf(".progress-popup {"))); const popupZ = parseInt((popupBlock.match(/z-index:\s*(\d+)/) || [])[1], 10); assert.equal(popupZ, 100000, ".progress-popup z-index drifted — re-derive the Esc precedence if it is no longer top-most"); const viewerBlock = botCss.slice(botCss.indexOf("#imageViewerModal {"), botCss.indexOf("}", botCss.indexOf("#imageViewerModal {"))); const viewerZ = parseInt((viewerBlock.match(/z-index:\s*(\d+)/) || [])[1], 10); assert.ok( popupZ > viewerZ, `.progress-popup (${popupZ}) must stack above #imageViewerModal (${viewerZ}) — the first Esc check must always be the visually top-most element` ); }); // F1 regression (install-modal-teardown): startInstallingOverlay strips .show // from open modals, but in inline mode the planner page stays visible via // .adpanel-page-active (the inline stylesheet's display:flex !important rule // in bot.ejs). The planner's own keydown guard keyed only on .show went dead // in that state, so Esc silently did nothing and closePlannerModal's full // inline teardown (animateClose) never ran. The guard must keep checking // .show first and fall through to .adpanel-page-active. test("the planner Esc guard also closes the page once the install overlay has stripped .show (adpanel-page-active branch)", () => { const showBranch = "if (e.key === 'Escape' && plannerModal?.classList.contains('show')) {"; const at = botSrc.indexOf(showBranch); assert.notStrictEqual(at, -1, "the planner Esc guard must keep its .show branch"); // The planner guard is its own keydown handler (separate from the Files-page // chain above); take a window from its handler opening past the fallback. const handlerStart = botSrc.lastIndexOf("document.addEventListener('keydown', (e) => {", at); assert.notStrictEqual(handlerStart, -1, "the planner .show branch must live inside a document keydown handler"); const guard = botSrc.slice(handlerStart, at + 900); const fallback = "plannerModal?.classList.contains('adpanel-page-active')"; const fallbackAt = guard.indexOf(fallback); assert.notStrictEqual( fallbackAt, -1, "the planner Esc guard must also fire when only .adpanel-page-active remains (install-overlay teardown desync)" ); // Precedence within the guard: the .show branch's close call must come // before the .adpanel-page-active fallback condition. const showCloseAt = guard.indexOf("closePlannerModal();"); assert.ok(showCloseAt !== -1 && showCloseAt < fallbackAt, "the .show branch must keep precedence over the adpanel-page-active fallback"); assert.match( guard.slice(fallbackAt), /closePlannerModal\(\);/, "the adpanel-page-active fallback must route through the same closePlannerModal teardown, not a partial close" ); // The teardown lives in animateClose's inline-page block; closePlannerModal // must keep delegating there or the fallback closes nothing that matters. const closeFn = botSrc.indexOf("function closePlannerModal()"); assert.notStrictEqual(closeFn, -1, "closePlannerModal must still exist"); assert.match( botSrc.slice(closeFn, closeFn + 200), /animateClose\(plannerModal\)/, "closePlannerModal must keep routing through animateClose (the inline-mode page teardown lives there)" ); }); // The other half of install-modal-teardown: startInstallingOverlay's hide- // everything loop must clear what showModalElement leaves behind, not just // .show — an inline display:flex survives the loop as an invisible, still // hit-testable full-screen layer (popup mode), and a stale aria-hidden= // "false" mis-announces the hidden modal. Pin the teardown body. test("the install overlay's modal teardown clears inline display and corrects aria state", () => { const loopAt = botSrc.indexOf( "document.querySelectorAll('.modal-overlay.show, .activity-modal.show, .scheduler-modal.show, .planner-modal.show, .ai-help-modal.show')" ); assert.notStrictEqual(loopAt, -1, "the install overlay's modal teardown loop must still exist"); const block = botSrc.slice(loopAt, loopAt + 800); assert.match(block, /forEach\(m => \{/, "the teardown must use a block body (it does more than strip .show)"); assert.match(block, /m\.classList\.remove\('show'\)/, "the teardown must still strip .show"); assert.match( block, /m\.style\.removeProperty\('display'\)/, "the teardown must clear the inline display:flex showModalElement leaves behind — otherwise popup mode keeps an invisible hit-testable full-screen layer" ); assert.match( block, /if \(m\.hasAttribute\('aria-hidden'\)\) m\.setAttribute\('aria-hidden', 'true'\)/, "the teardown must correct a stale aria-hidden on elements that carry the attribute — openers set it false again on reopen" ); });