Files
Kelx/tests/bot-store-mod-install.test.js
T
2026-11-30 16:32:37 +00:00

189 lines
12 KiB
JavaScript

"use strict";
// Regression tests for audit finding 11: the Store's "Plugins/Mods" tab could
// never install a mod.
//
// The tab was labelled "Plugins/Mods" (bot.ejs) but its search hard-coded the
// Modrinth facet `[["project_type:plugin"]]`, the modal offered every loader
// (mod loaders included, via MR_VALID_LOADERS), and the install button
// hard-coded provider "modrinth-plugin" with destPath `plugins/<file>` — so
// even a mod build reached through the UI would have landed in plugins/,
// where a Minecraft server never loads it. The mod machinery existed only in
// the dashboard-assistant flow (destRoot 'mods' vs 'plugins', provider
// `modrinth-${projectType}`).
//
// The fix gives the tab a Plugins|Mods type toggle that drives one shared
// project-type state: the search facet, the loaders offered in the modal,
// the provider id, and the install destination (plugins/ or mods/) all
// follow the type of the result list a card was searched under. The card
// captures its type at render time, so flipping the toggle while a modal is
// open cannot retarget an already-open install.
//
// bot.js is a browser IIFE (DOM + fetch bound at module scope), so these
// tests pin the fix structurally on well-anchored source windows, matching
// the convention of the other bot-*.test.js files.
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_EJS = path.join(ROOT, "views", "bot.ejs");
const botSrc = fs.readFileSync(BOT_JS, "utf8");
const botEjs = fs.readFileSync(BOT_EJS, "utf8");
function sourceWindow(src, startAnchor, endAnchor, label) {
const start = src.indexOf(startAnchor);
assert.notStrictEqual(start, -1, `${label || "window"}: start anchor not found: ${startAnchor}`);
const end = src.indexOf(endAnchor, start + startAnchor.length);
assert.notStrictEqual(end, -1, `${label || "window"}: end anchor not found: ${endAnchor}`);
return src.slice(start, end);
}
// ---------------------------------------------------------------------------
// Shared project-type state
// ---------------------------------------------------------------------------
test("mrPluginsState tracks a projectType that defaults to plugin", () => {
const stateWindow = sourceWindow(botSrc, "let mrPluginsState = {", "};", "mrPluginsState");
assert.match(stateWindow, /projectType:\s*"plugin"/,
"the Plugins/Mods tab must default to browsing plugins (backwards compatible)");
});
test("the modal has its own current project type, reset on close", () => {
assert.match(botSrc, /let mrCurrentProjectType = "plugin";/,
"mrCurrentProjectType must be declared next to mrCurrentProjectId");
const closeWindow = sourceWindow(botSrc, "function closeModrinthPluginModal()", "if (mrPluginModalClose)", "closeModrinthPluginModal");
assert.match(closeWindow, /mrCurrentProjectType = "plugin";/,
"closing the modal must reset the project type so a stale type cannot leak into the next open");
});
// ---------------------------------------------------------------------------
// Search facet follows the selected type
// ---------------------------------------------------------------------------
test("the store-tab search builds its facet from the tracked project type", () => {
const loadWindow = sourceWindow(botSrc, "async function loadModrinthPlugins(reset)", "function renderModrinthCard(", "loadModrinthPlugins");
assert.match(loadWindow, /const projectType = mrPluginsState\.projectType === "mod" \? "mod" : "plugin";/,
"loadModrinthPlugins must derive the facet type from mrPluginsState.projectType");
assert.match(loadWindow, /project_type:\$\{projectType\}/,
"the Modrinth facet must be built from the tracked project type");
});
test("no hard-coded plugin-only facet or provider remains in bot.js", () => {
assert.ok(!botSrc.includes("project_type:plugin"),
"the literal 'project_type:plugin' facet is gone; the type must come from mrPluginsState.projectType");
assert.ok(!botSrc.includes("modrinth-plugin"),
"the literal 'modrinth-plugin' provider id is gone; the UI must use modrinth-${type} like the assistant flow does");
});
// ---------------------------------------------------------------------------
// Plugins | Mods toggle in the tab header
// ---------------------------------------------------------------------------
test("the tab header renders a Plugins|Mods toggle and wires both sides", () => {
const uiWindow = sourceWindow(botSrc, "function renderPluginsUI()", "async function loadModrinthPlugins(", "renderPluginsUI");
assert.match(uiWindow, /id="mrTypePlugin"/, "a Plugins toggle button must be rendered");
assert.match(uiWindow, /id="mrTypeMod"/, "a Mods toggle button must be rendered");
assert.match(uiWindow, /setStoreProjectType\("plugin"\)/, "the Plugins button must switch the type to plugin");
assert.match(uiWindow, /setStoreProjectType\("mod"\)/, "the Mods button must switch the type to mod");
assert.match(uiWindow, /mrPluginsState\.projectType = projectType;/,
"switching must update mrPluginsState.projectType so the next search uses the new facet");
assert.match(uiWindow, /loadModrinthPlugins\(true\)/,
"switching must reload the grid from offset 0");
});
test("switching the type supersedes an in-flight search instead of racing it", () => {
const loadWindow = sourceWindow(botSrc, "async function loadModrinthPlugins(reset)", "function renderModrinthCard(", "loadModrinthPlugins");
// A fresh search while another load is in flight must bump the generation
// counter, so the stale response is discarded rather than rendered under a
// toggle that no longer matches it.
assert.match(loadWindow, /if \(mrPluginsState\.loading\) \{/,
"the in-flight guard must be a block that can supersede, not a bare early return");
assert.match(loadWindow, /mrPluginsLoadSeq\+\+/,
"a superseding fresh search must bump the load generation");
assert.match(loadWindow, /loadSeq !== mrPluginsLoadSeq/,
"responses must be checked against the current generation before rendering");
assert.match(loadWindow, /mrPluginsState\.pendingReset = false;\s*\n\s*loadModrinthPlugins\(true\);/,
"the superseding search must re-run once the stale load unwinds");
});
test("the search debounce is cancelled by re-renders, Refresh, and type flips", () => {
assert.match(botSrc, /let mrPluginsSearchTimer = null;/,
"the debounce timer must live at module level so it survives across tab re-renders");
const uiWindow = sourceWindow(botSrc, "function renderPluginsUI()", "async function loadModrinthPlugins(", "renderPluginsUI");
// Three cancellation sites inside renderPluginsUI: on re-render (before the
// state reset), in the Refresh handler, and in setStoreProjectType — plus
// the assignment in the input handler. A stale 400ms timer that fired after
// a flip/refresh/rerender would re-inject a query the user already revoked.
const cancelCount = (uiWindow.match(/clearTimeout\(mrPluginsSearchTimer\)/g) || []).length;
assert.ok(cancelCount >= 3,
`Refresh and setStoreProjectType (and the re-render itself) must cancel the debounce; found ${cancelCount} clearTimeout sites`);
assert.match(uiWindow, /mrPluginsSearchTimer = setTimeout\(/,
"the input handler must arm the module-level debounce");
});
test("re-rendering the tab must not force-reset loading and leak a stale response", () => {
const uiWindow = sourceWindow(botSrc, "function renderPluginsUI()", "async function loadModrinthPlugins(", "renderPluginsUI");
assert.ok(!uiWindow.includes("mrPluginsState.loading = false"),
"renderPluginsUI must not force loading=false: an in-flight load from a previous tab render would pass every generation check and append stale cards into the fresh grid; route the initial load through loadModrinthPlugins's supersede path instead");
});
// ---------------------------------------------------------------------------
// Cards capture the type of the list they belong to
// ---------------------------------------------------------------------------
test("cards are rendered with — and open the modal under — their list's project type", () => {
const loadWindow = sourceWindow(botSrc, "async function loadModrinthPlugins(reset)", "function renderModrinthCard(", "loadModrinthPlugins");
assert.match(loadWindow, /renderModrinthCard\(hit, fragment, projectType\)/,
"the grid must pass the searched type down to every card");
const cardWindow = sourceWindow(botSrc, "function renderModrinthCard(", "async function openModrinthPluginModal(", "renderModrinthCard");
assert.match(cardWindow, /const cardProjectType = projectType === "mod" \? "mod" : "plugin";/,
"each card must capture its list's type at render time");
assert.match(cardWindow, /openModrinthPluginModal\(hit\.project_id, hit\.title, cardProjectType\)/,
"both the Install button and the card click must open the modal under the captured type");
});
// ---------------------------------------------------------------------------
// Modal threading: loaders, provider id, destination, labels
// ---------------------------------------------------------------------------
test("openModrinthPluginModal accepts a project type and mirrors it into the modal", () => {
const modalWindow = sourceWindow(botSrc, "async function openModrinthPluginModal(projectId, name, projectType)", "function closeModrinthPluginModal()", "openModrinthPluginModal");
assert.match(modalWindow, /mrCurrentProjectType = projectType === "mod" \? "mod" : "plugin";/,
"the modal must normalise and store the requested type");
assert.match(modalWindow, /Install Mod/, "the modal chrome must be able to say Install Mod");
assert.match(modalWindow, /Install Plugin/, "the modal chrome must be able to say Install Plugin");
});
test("the platform dropdown only offers loaders compatible with the destination", () => {
const platformsWindow = sourceWindow(botSrc, "function populateModrinthPlatforms()", "function populateModrinthVersions()", "populateModrinthPlatforms");
assert.match(platformsWindow, /mrCurrentProjectType === "mod" \? MR_MOD_LOADERS : MR_PLUGIN_LOADERS/,
"loaders must be filtered to the open project's type: a paper build installed into mods/ or a fabric build into plugins/ would never load");
assert.ok(!platformsWindow.includes("MR_VALID_LOADERS"),
"the modal must no longer offer the full MR_VALID_LOADERS set, which mixes plugin and mod loaders");
});
test("the modal install targets mods/ for mods and plugins/ for plugins", () => {
const installWindow = sourceWindow(botSrc, 'mrPluginInstallBtn.addEventListener("click"', "function normalizeStoreComparable(", "install button handler");
assert.match(installWindow, /const destRoot = mrCurrentProjectType === "mod" \? "mods" : "plugins";/,
"the destination root must follow the modal's project type (mirrors the assistant flow's destRoot)");
assert.match(installWindow, /applyVersion\(`modrinth-\$\{mrCurrentProjectType\}`/,
"the provider id must be modrinth-mod or modrinth-plugin, matching executeStoreOperation's convention");
assert.match(installWindow, /destPath: `\$\{destRoot\}\/\$\{pluginFileName\}`/,
"the file must be installed under the computed destination root");
});
// ---------------------------------------------------------------------------
// Label/behaviour contract
// ---------------------------------------------------------------------------
test("the Store tab keeps its Plugins/Mods label — now backed by real mod installs", () => {
assert.match(botEjs, /Plugins\/Mods/,
'the tab labelled "Plugins/Mods" must stay labelled that way; the fix makes the label true');
});