183 lines
10 KiB
JavaScript
183 lines
10 KiB
JavaScript
"use strict";
|
|
|
|
// Regression tests for audit finding 10: openProviderDetails had no
|
|
// staleness guard.
|
|
//
|
|
// Unlike the Modrinth install modal (which re-checks mrCurrentProjectId
|
|
// after its await), the provider version view rendered whatever response
|
|
// landed last. A slow live-fallback provider (e.g. Paper resolving ~200
|
|
// version metadatas client-side) could overwrite a newer view the user
|
|
// opened afterwards — Versions tab, another provider, the Plugins/Mods tab —
|
|
// and an install clicked in that window targeted a list the user never
|
|
// opened. The same hole existed in renderStoreProvidersView and
|
|
// openStoreModal, and on the write side in renderPluginsUI /
|
|
// renderPluginsPlaceholder / closeStoreModal (which replace storeContent
|
|
// without invalidating in-flight fetches).
|
|
//
|
|
// The fix gives the whole Store panel one generation counter: every view
|
|
// that takes over storeContent claims it via claimStorePanel() before
|
|
// painting, and every async writer re-checks its claim after each await and
|
|
// in its catch before painting an error. These tests pin that structure on
|
|
// well-anchored source windows, matching the convention of the other
|
|
// bot-*.test.js files; the race semantics themselves are exercised
|
|
// behaviourally by bot-store-race-behavior.test.js.
|
|
|
|
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 botSrc = fs.readFileSync(BOT_JS, "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);
|
|
}
|
|
|
|
function requireOnce(haystack, needle, message) {
|
|
const first = haystack.indexOf(needle);
|
|
assert.notStrictEqual(first, -1, message || `expected to find: ${needle}`);
|
|
assert.strictEqual(haystack.indexOf(needle, first + 1), -1,
|
|
`${message || needle} — expected exactly one occurrence`);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// The generation counter and its claim helper
|
|
// ---------------------------------------------------------------------------
|
|
|
|
test("the store panel has a single view generation counter and claim helper", () => {
|
|
requireOnce(botSrc, "let storeViewSeq = 0;",
|
|
"the store panel generation counter must be declared exactly once");
|
|
requireOnce(botSrc, "function claimStorePanel() {",
|
|
"the claim helper must be declared exactly once");
|
|
const declWindow = sourceWindow(botSrc, "let storeCurrentProvider = null;", "let papermcVersionsCache", "store state");
|
|
assert.match(declWindow, /function claimStorePanel\(\) \{\s*\n\s*return \+\+storeViewSeq;/,
|
|
"claimStorePanel must bump the generation and hand it back");
|
|
});
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// openProviderDetails — the finding 10 view
|
|
// ---------------------------------------------------------------------------
|
|
|
|
test("openProviderDetails claims the panel before painting and re-checks after its fetch", () => {
|
|
const fn = sourceWindow(botSrc, "async function openProviderDetails(", "function renderPluginsPlaceholder() {", "openProviderDetails");
|
|
const claimPos = fn.indexOf("const viewSeq = claimStorePanel();");
|
|
const loadingPaint = fn.indexOf("storeContent.innerHTML = '<div class=\"store-loading\">Loading versions...</div>';");
|
|
assert.notStrictEqual(claimPos, -1, "openProviderDetails must claim the panel");
|
|
assert.ok(claimPos < loadingPaint,
|
|
"the claim must happen before the first storeContent write");
|
|
|
|
const awaitPos = fn.indexOf("await fetchStoreVersions(");
|
|
const checkPos = fn.indexOf("if (viewSeq !== storeViewSeq) return;");
|
|
const paintPos = fn.indexOf("const versions = data.versions");
|
|
assert.ok(awaitPos !== -1 && checkPos > awaitPos && checkPos < paintPos,
|
|
"the staleness check must sit between the fetch and the render: a superseded response must be discarded, not painted");
|
|
|
|
const catchWindow = fn.slice(fn.indexOf("} catch (err) {"));
|
|
assert.match(catchWindow, /if \(viewSeq !== storeViewSeq\) return;/,
|
|
"a stale failure must not paint its error over the newer view either");
|
|
});
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// The other async storeContent writers
|
|
// ---------------------------------------------------------------------------
|
|
|
|
test("renderStoreProvidersView claims the panel and re-checks after fetching providers", () => {
|
|
const fn = sourceWindow(botSrc, "async function renderStoreProvidersView(", "async function applyVersion(", "renderStoreProvidersView");
|
|
const claimPos = fn.indexOf("const viewSeq = claimStorePanel();");
|
|
const firstPaint = fn.indexOf("storeContent.innerHTML = '<div class=\"store-loading\">Loading providers...</div>';");
|
|
assert.ok(claimPos !== -1 && claimPos < firstPaint,
|
|
"the claim must precede the first storeContent write");
|
|
const checkPos = fn.indexOf("if (viewSeq !== storeViewSeq) return;");
|
|
const awaitPos = fn.indexOf("await fetchStoreProviders()");
|
|
const gridPaint = fn.indexOf("storeContent.innerHTML = '';");
|
|
assert.ok(checkPos > awaitPos && checkPos < gridPaint,
|
|
"the staleness check must sit between the providers fetch and the grid paint");
|
|
assert.match(fn.slice(fn.indexOf("} catch (err) {")), /if \(viewSeq !== storeViewSeq\) return;/,
|
|
"the catch must not paint a stale error");
|
|
});
|
|
|
|
test("openStoreModal claims the panel and re-checks after each of its awaits", () => {
|
|
const fn = sourceWindow(botSrc, "async function openStoreModal() {", "function closeStoreModal() {", "openStoreModal");
|
|
const claimPos = fn.indexOf("const viewSeq = claimStorePanel();");
|
|
assert.ok(claimPos !== -1 && claimPos < fn.indexOf("await loadServerInfo()"),
|
|
"the claim must happen before the first await so a concurrent open wins");
|
|
|
|
const afterLoad = fn.slice(fn.indexOf("await loadServerInfo()"), fn.indexOf("updateStoreVisibility()"));
|
|
assert.match(afterLoad, /if \(viewSeq !== storeViewSeq\) return;/,
|
|
"the open must abort after loadServerInfo if a newer view claimed the panel");
|
|
// Each post-await check is pinned to its exact window so removing any one
|
|
// of them fails here, not just behaviorally: the fetch check sits between
|
|
// the fetch and the error paint, the parse check between r.json() and the
|
|
// providers read.
|
|
const fetchWin = fn.slice(fn.indexOf("await fetch(`/api/servers/"), fn.indexOf("if (!r.ok) {"));
|
|
assert.match(fetchWin, /if \(viewSeq !== storeViewSeq\) return;/,
|
|
"a staleness check must sit between the providers fetch and the error paint");
|
|
const jsonWin = fn.slice(fn.indexOf("await r.json()"), fn.indexOf("const providers ="));
|
|
assert.match(jsonWin, /if \(viewSeq !== storeViewSeq\) return;/,
|
|
"a staleness check must sit between parsing the providers and reading them");
|
|
assert.match(fn.slice(fn.indexOf("} catch (e) {")), /if \(viewSeq !== storeViewSeq\) return;/,
|
|
"the catch must not paint a stale error");
|
|
});
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// The synchronous claimants — replacing storeContent invalidates in-flight fetches
|
|
// ---------------------------------------------------------------------------
|
|
|
|
test("renderPluginsUI and renderPluginsPlaceholder claim the panel before replacing storeContent", () => {
|
|
for (const [anchor, label] of [
|
|
["function renderPluginsUI() {", "renderPluginsUI"],
|
|
["function renderPluginsPlaceholder() {", "renderPluginsPlaceholder"],
|
|
]) {
|
|
const fn = sourceWindow(botSrc, anchor, "async function loadModrinthPlugins(", label);
|
|
const claimPos = fn.indexOf("claimStorePanel();");
|
|
const paintPos = fn.indexOf("storeContent.innerHTML");
|
|
assert.ok(claimPos !== -1, `${label} must claim the panel`);
|
|
assert.ok(claimPos < paintPos,
|
|
`${label} must claim the panel before writing storeContent so an in-flight provider fetch is invalidated`);
|
|
}
|
|
});
|
|
|
|
test("closeStoreModal claims the panel so late fetches cannot paint into the closed modal", () => {
|
|
const fn = sourceWindow(botSrc, "function closeStoreModal() {", "function compareMcVersionsDesc(", "closeStoreModal");
|
|
const claimPos = fn.indexOf("claimStorePanel();");
|
|
assert.ok(claimPos !== -1 && claimPos < fn.indexOf("animateClose("),
|
|
"the panel must be claimed as the close starts, not after the animation");
|
|
});
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Coverage: no storeContent write may bypass the claim/check discipline
|
|
// ---------------------------------------------------------------------------
|
|
|
|
test("every storeContent writer sits inside a function that claims the panel", () => {
|
|
// The complete inventory of storeContent writers in bot.js: the three
|
|
// async views (guarded per-await above), the two plugins renderers and
|
|
// openStoreModal's loading/error/empty paints. If a new writer appears
|
|
// outside these windows, this assertion fails and the new code needs a
|
|
// claim + checks of its own.
|
|
const writerAnchors = [
|
|
"storeContent.innerHTML = `<div class=\"store-loading\">Loading providers...</div>`;", // openStoreModal
|
|
"storeContent.innerHTML = '<div class=\"store-loading\">Loading providers...</div>';", // renderStoreProvidersView
|
|
"storeContent.innerHTML = '<div class=\"store-loading\">Loading versions...</div>';", // openProviderDetails
|
|
"storeContent.innerHTML = '<div class=\"store-placeholder\">", // renderPluginsPlaceholder
|
|
"storeContent.innerHTML = `\n <div class=\"mr-plugins-root\">", // renderPluginsUI
|
|
];
|
|
const claims = ["openStoreModal", "renderStoreProvidersView", "openProviderDetails", "renderPluginsPlaceholder", "renderPluginsUI"];
|
|
for (const writer of writerAnchors) {
|
|
const pos = botSrc.indexOf(writer);
|
|
assert.notStrictEqual(pos, -1, `writer anchor not found (layout changed?): ${writer.slice(0, 60)}`);
|
|
const covering = claims.map((name) => {
|
|
const fnStart = botSrc.indexOf(`function ${name}(`);
|
|
const fnEnd = botSrc.indexOf("\n }", fnStart);
|
|
return fnStart !== -1 && pos > fnStart && pos < fnEnd;
|
|
});
|
|
assert.ok(covering.some(Boolean), `storeContent writer at ${pos} is not inside any claiming function: ${writer.slice(0, 60)}`);
|
|
}
|
|
});
|