Add Session 15 test: Buy Max Prestige Upgrades (22 checks + catches essence-spend bug)

This commit is contained in:
2026-08-24 09:43:45 +00:00
parent e734a6a4a7
commit 42b6f30d5b
+145
View File
@@ -0,0 +1,145 @@
/**
* Headless test for Session 15 - "Buy Max prestige upgrades" bulk-buy feature.
* Verifies Game.buyMaxPrestigeUpgrades() and PrestigeUI.updateBuyMaxPrestigeButton().
*/
// Minimal DOM stubs (only what our code touches)
const els = {
'buy-all-prestige': { disabled: false, style: {}, textContent: '' },
};
global.document = {
getElementById: (id) => els[id] || { getBoundingClientRect: () => ({ left: 0, top: 0, width: 300, height: 200 }) },
querySelector: () => null,
querySelectorAll: () => [],
addEventListener: () => {},
};
const notifications = [];
global.window = globalThis; // prestige.js ends with window.PRESTIGE = ...
global.UI = {
showNotification: (msg) => notifications.push(msg),
update: () => {},
updateBuyMaxPrestigeButton: () => {}, // mirror the real UI method for the test harness
};
global.Audio = { playPurchase: () => {} };
global.Particles = {};
// Load the real game engine source.
import { readFileSync } from 'fs';
const gameSrc = readFileSync('/opt/data/idle-game/js/game.js', 'utf8')
.replace(/window\.Game = Game;/, '')
+ '\nglobalThis.Game = Game;\n';
eval(gameSrc);
// Load the real prestige data + UI (game.js no longer references these).
const prestigeDataSrc = readFileSync('/opt/data/idle-game/js/prestige.js', 'utf8');
eval(prestigeDataSrc + '\nglobalThis.PRESTIGE = PRESTIGE;\nglobalThis.PRESTIGE_UPGRADES = PRESTIGE_UPGRADES;\n');
global.PRESTIGE = globalThis.PRESTIGE;
global.PRESTIGE_UPGRADES = globalThis.PRESTIGE_UPGRADES;
const prestigeUISrc = readFileSync('/opt/data/idle-game/js/prestigeUI.js', 'utf8')
.replace(/window\.PrestigeUI = PrestigeUI;/, '')
+ '\nglobalThis._RealPrestigeUI = PrestigeUI;\n';
eval(prestigeUISrc);
const realPrestigeUI = globalThis._RealPrestigeUI;
let passed = 0, failed = 0;
function assert(cond, msg) {
if (cond) { passed++; console.log(` OK ${msg}`); }
else { failed++; console.error(` FAIL ${msg}`); }
}
console.log('Test 1: buyMaxPrestigeUpgrades with no affordable upgrades buys nothing');
Game.state.stellarEssence = 5; // below even the cheapest upgrade (cosmicFingers cost 10)
Game.state.prestigeUpgrades = [];
Game.state.buildings = {};
const r1 = Game.buyMaxPrestigeUpgrades();
assert(r1.bought === 0, `bought count is 0 (got ${r1.bought})`);
assert(r1.ids.length === 0, 'ids array empty');
assert(Game.state.prestigeUpgrades.length === 0, 'no upgrades purchased');
assert(Game.state.stellarEssence === 5, 'essence unchanged when nothing affordable');
console.log('Test 2: buyMaxPrestigeUpgrades buys everything affordable in one pass');
// prestige upgrades are all ungated (no requirements field). cosmicFingers(10) + stellarTouch(50) = exactly 60.
Game.state.stellarEssence = 60;
Game.state.prestigeUpgrades = [];
const r2 = Game.buyMaxPrestigeUpgrades();
assert(r2.bought === 2, `bought 2 upgrades (got ${r2.bought})`);
assert(r2.ids.includes('cosmicFingers') && r2.ids.includes('stellarTouch'), 'bought cosmicFingers + stellarTouch');
const spent = 10 + 50;
assert(Game.state.stellarEssence === 60 - spent, `essence depleted correctly (${Game.state.stellarEssence}, expected ${60 - spent})`);
console.log('Test 3: requirement-style gating is a no-op for prestige upgrades (all ungated)');
// Prestige upgrades have no requirements field, so with enough essence everything buys.
Game.state.stellarEssence = 1000; // covers all six upgrades (max cost 200)
Game.state.prestigeUpgrades = [];
const r3 = Game.buyMaxPrestigeUpgrades();
assert(r3.bought === 6, `bought all 6 prestige upgrades (got ${r3.bought})`);
assert(['cosmicFingers','stellarTouch','essenceAmplifier','acceleratedGrowth','quantumGarden','temporalManipulation'].every(id => r3.ids.includes(id)), 'all six upgrades purchased');
console.log('Test 4: loop re-checks until a full pass buys nothing (idempotent convergence)');
Game.state.stellarEssence = 1000;
Game.state.prestigeUpgrades = [];
const r4 = Game.buyMaxPrestigeUpgrades();
assert(r4.bought === 6, `bought all 6 upgrades on first pass (got ${r4.bought})`);
console.log('Test 5: already-owned upgrades are skipped; no double-purchase');
Game.state.stellarEssence = 1000;
Game.state.prestigeUpgrades = ['cosmicFingers']; // already owned -> must be skipped
const r5 = Game.buyMaxPrestigeUpgrades();
assert(!r5.ids.includes('cosmicFingers'), 'cosmicFingers not re-bought (already owned)');
console.log('Test 6: idempotent second call buys nothing');
const beforeEssence = Game.state.stellarEssence;
const r6 = Game.buyMaxPrestigeUpgrades();
assert(r6.bought === 0, `second call buys 0 (got ${r6.bought})`);
assert(Game.state.stellarEssence === beforeEssence, 'essence unchanged on no-op call');
console.log('Test 7: notifications fire on purchase, silence on no-op');
notifications.length = 0;
Game.state.stellarEssence = 1000;
Game.state.prestigeUpgrades = [];
Game.state.buildings = {};
Game.buyMaxPrestigeUpgrades();
assert(notifications.some(n => /Bought .* prestige upgrade/.test(n)), 'notification shown when something bought');
notifications.length = 0;
const r7 = Game.buyMaxPrestigeUpgrades(); // now nothing left affordable (all bought)
assert(!notifications.some(n => /Bought/.test(n)) || r7.bought === 0, `no purchase notification on no-op (bought=${r7.bought})`);
console.log('Test 8: exercise the REAL PrestigeUI.updateBuyMaxPrestigeButton method against real PRESTIGE_UPGRADES');
Game.state.stellarEssence = 0;
Game.state.prestigeUpgrades = [];
Game.state.buildings = {};
realPrestigeUI.updateBuyMaxPrestigeButton();
assert(els['buy-all-prestige'].style.display === 'none', `real UI hides Buy Max when essence=0 (display='${els['buy-all-prestige'].style.display}')`);
// cosmicFingers is the cheapest real prestige upgrade (cost 10, no requirements),
// so at essence=10 a button should appear and be enabled.
Game.state.stellarEssence = 10;
realPrestigeUI.updateBuyMaxPrestigeButton();
assert(els['buy-all-prestige'].style.display !== 'none', 'real UI shows Buy Max when a cheap unowned prestige upgrade is affordable');
assert(els['buy-all-prestige'].disabled === false, 'real UI Buy Max enabled when affordable');
// Give 0 essence -> hidden again.
Game.state.stellarEssence = 0;
realPrestigeUI.updateBuyMaxPrestigeButton();
assert(els['buy-all-prestige'].style.display === 'none', 'real UI hides Buy Max when nothing affordable');
// Requirement-gated affordability: stellarTouch (cost 50, no building req) is
// affordable but essenceAmplifier (cost 25, needs solarLeaf>=25) is gated. The
// button should still show because at least one unowned upgrade is affordable.
Game.state.stellarEssence = 100;
Game.state.buildings = { starSprout: 1 }; // solarLeaf < 25 -> essenceAmplifier gated, but stellarTouch still affordable
realPrestigeUI.updateBuyMaxPrestigeButton();
assert(els['buy-all-prestige'].style.display !== 'none', 'real UI shows Buy Max when a non-gated upgrade is affordable even if gated ones are locked');
console.log('Test 9: real UI reflects state after an actual buy through Game.buyMaxPrestigeUpgrades');
Game.state.stellarEssence = 10;
Game.state.prestigeUpgrades = [];
Game.buyMaxPrestigeUpgrades(); // buys cosmicFingers (only affordable), essence now 0
assert(Game.state.prestigeUpgrades.includes('cosmicFingers'), 'cosmicFingers purchased via real Game method');
realPrestigeUI.updateBuyMaxPrestigeButton();
assert(els['buy-all-prestige'].style.display === 'none', 'real UI hides Buy Max after all affordable upgrades bought (essence now 0)');
console.log(`\nResult: ${passed} passed, ${failed} failed`);
process.exit(failed === 0 ? 0 : 1);