// Shards of Valor bonus heroes (2026-11-05, Marty approved): unit checks of the gamepass.js hero store, no server. // node qa/sov-heroes-store.cjs // Sold ONE TIME, paid exactly like a pass: buy opens a waiting order; a FRESH package of at least the price, by one of the member's own // positions, fulfils it. Covers: the heroSales switch (off refuses members, the admin test may buy), $20 each / $50 bundle (Valda, Morag, // Roland by default), order -> fulfil -> ownership (permanent, no expiry), the bundle grants all three, no double charge (owned hero // refused, a waiting order for an owned hero cancelled, each tx once, one package one order), packages bought before the order or too // small, a pass order and a hero order side by side, settings (price 0 = free for everyone, bundle list), the hand-off claim (heroes, // heroBundle, heroSale, heroBundleCap only when set), comp / revoke, stats, and that hero sales never touch the pass records. Exit 1 on failure. 'use strict'; const fs = require('fs'); const os = require('os'); const path = require('path'); const gp = require('../gamepass'); let fails = 0; const ok = (c, label, extra) => { console.log((c ? 'PASS ' : 'FAIL ') + label + (extra ? ' ' + extra : '')); if (!c) fails++; }; const SC = { gamePassEnabled: '1', sovTiers: '0', heroSales: '0', capsStartDate: '2026-10-01' }; const maps = {}; gp.init({ dataDir: fs.mkdtempSync(path.join(os.tmpdir(), 'iap-heroes-')), siteConfig: () => Object.assign({}, SC), persistedMap: k => (maps[k] = maps[k] || new Map()), isProd: false, accounts: { byMemberId: async () => null, byEmail: async () => null }, marks: { claim: async () => true }, polUsd: async () => 0.1, telegram: async () => {}, notify: async () => {} }); const passes = maps.gamepass, orders = maps.gporders; const T0 = Date.parse('2026-11-05T19:00:00Z'); let txN = 0; const pkg = (email, cents, at, tx) => gp.onEvent({ type: 'Purchase', priceCents: cents, tx: tx || ('0xhero' + (++txN)), ts: at, devEmail: email, devMinedAt: at, devNow: at }); (async () => { // 1. defaults and the switch const c = gp.cfg().heroes; ok(c.sale === false && c.price.kestrel === 2000 && c.price.valda === 2000 && c.price.morag === 2000 && c.price.roland === 2000 && c.bundle.price === 5000 && c.bundle.heroes.join() === 'valda,morag,roland' && c.cap === null && c.free.length === 0, 'defaults: heroSales off, $20 each, $50 bundle of Valda + Morag + Roland, no bundle cap, nothing free', JSON.stringify(c)); let r = gp.buyHero('m@x.com', T0, 'kestrel'); ok(r.error === 'Bonus heroes are not on sale yet.', 'heroSales off: a member is refused', JSON.stringify(r)); ok(![...orders.values()].length, 'a refused buy opens no order'); let v = gp.view('m@x.com', T0).heroes; ok(v.show === false && v.items.every(i => !i.open), 'heroSales off: the member card hides the heroes', JSON.stringify(v.items.map(i => i.open))); v = gp.view('admin@x.com', T0, { preview: true }).heroes; ok(v.show && v.preview && v.items.every(i => i.open) && v.bundle.open && v.items.map(i => i.package).join() === '20,20,20,20' && v.bundle.package === 50, 'the admin preview: every hero open, $20 packages, the bundle on the $50 package', JSON.stringify(v)); r = gp.buyHero('admin@x.com', T0, 'roland', { preview: true }); ok(r.needPay && r.price === 2000 && r.package === 20 && /Roland the Paladin joins you in Shards of Valor/.test(r.message) && !/—/.test(r.message), 'the admin test opens a $20 order', JSON.stringify(r)); r = gp.cancelHeroOrder('admin@x.com', 'roland'); ok(r.ok && r.order, 'the admin cancels the test order'); ok(!(await pkg('admin@x.com', 2000, T0 + 1000)), 'a cancelled order is never fulfilled later'); // 2. on sale: one hero, order then a fresh package SC.heroSales = '1'; r = gp.buyHero('m@x.com', T0, 'kestrel'); ok(r.needPay && r.payUrl.includes('#buy') && r.usd === 20, 'on sale: buy opens a waiting order, no charge', JSON.stringify(r)); const r2 = gp.buyHero('m@x.com', T0 + 10, 'kestrel'); ok(r2.reused && r2.order === r.order, 'a second click reuses the same order'); ok(!(await pkg('m@x.com', 2000, T0 - 60000)), 'a package bought before the order does not fulfil it'); ok(!(await pkg('m@x.com', 1000, T0 + 1000)), 'a $10 package is too small'); let ev = await pkg('m@x.com', 2000, T0 + 2000, '0xkes1'); ok(ev && ev.hero === 'kestrel' && ev.heroes.join() === 'kestrel', 'a fresh $20 package fulfils it', JSON.stringify(ev)); ok(gp.heroesOwned('m@x.com').owned.join() === 'kestrel', 'Kestrel is owned'); ok(!(await pkg('m@x.com', 2000, T0 + 3000, '0xkes1')), 'the same tx never counts twice'); r = gp.buyHero('m@x.com', T0 + 4000, 'kestrel'); ok(/already have Kestrel/.test(r.error || ''), 'no double charge: an owned hero is refused', JSON.stringify(r)); const rec = passes.get('m@x.com'); ok(rec.heroes.kestrel.tx === '0xkes1' && rec.heroes.kestrel.via === 'package' && rec.heroes.kestrel.usd === 20 && !rec.heroes.kestrel.until && !(rec.bought || []).length && !rec.tiers, 'stored for good (no expiry), never as a pass sale', JSON.stringify(rec)); ok(gp.flag('m@x.com', T0).active === false, 'a hero is not a Game Pass'); ok(gp.heroesOwned('m@x.com').owned.includes('kestrel') && gp.heroesOwned('m@x.com', T0 + 3650 * 86400000).owned.includes('kestrel'), 'owned years later too'); // 3. the bundle: all three, and its refusals r = gp.buyHero('m@x.com', T0, 'bundle'); ok(r.needPay && r.price === 5000 && r.package === 50 && /Valda the Barbarian, Morag the Druid, Roland the Paladin/.test(r.message), 'Kestrel owner may buy the bundle (Kestrel is not in it)', JSON.stringify(r)); ok(!(await pkg('m@x.com', 2000, T0 + 5000)), 'a $20 package does not pay the $50 bundle'); ev = await pkg('m@x.com', 5000, T0 + 6000); ok(ev && ev.hero === 'bundle' && ev.heroes.join() === 'valda,morag,roland', 'a $50 package fulfils the bundle: all three', JSON.stringify(ev)); ok(gp.heroesOwned('m@x.com').owned.join() === 'kestrel,valda,morag,roland' && gp.heroesOwned('m@x.com').bundle, 'four owned, bundle flag set'); r = gp.buyHero('m@x.com', T0, 'bundle'); ok(/already have/.test(r.error || ''), 'the bundle twice: refused'); r = gp.buyHero('n@x.com', T0, 'morag'); await pkg('n@x.com', 2000, T0 + 100); r = gp.buyHero('n@x.com', T0 + 200, 'bundle'); ok(/already have Morag the Druid, so the bundle is not for you/.test(r.error || ''), 'a member who owns one bundle hero is sent to buy the rest one at a time', JSON.stringify(r)); // 4. a waiting order made pointless by a bundle is cancelled (no second charge); one package fulfils one order gp.buyHero('o@x.com', T0, 'valda'); gp.buyHero('o@x.com', T0 + 1, 'bundle'); ev = await pkg('o@x.com', 5000, T0 + 1000); ok(ev && ev.hero === 'bundle', 'a $50 package picks the highest-priced order it covers (the bundle)', JSON.stringify(ev)); ok(![...orders.values()].some(o => o.email === 'o@x.com' && o.status === 'waiting'), 'the Valda order is cancelled: Valda came with the bundle'); ok(!(await pkg('o@x.com', 2000, T0 + 2000)), 'a later package charges nothing more for her'); // a pass order and a hero order side by side: each package fulfils one gp.buy('p@x.com', T0, 'weekly'); gp.buyHero('p@x.com', T0, 'roland'); const e1 = await pkg('p@x.com', 2000, T0 + 1000), e2 = await pkg('p@x.com', 500, T0 + 2000); ok(e1 && e1.hero === 'roland' && e2 && e2.tier === 'weekly' && gp.flag('p@x.com', T0 + 3000).active, 'a $20 package pays the hero, a $5 package the weekly pass', JSON.stringify({ e1, e2 })); // 5. settings: price 0 = free for every account; bundle list SC.heroPriceKestrelCents = '0'; ok(gp.heroesOwned('nobody@x.com').owned.join() === 'kestrel', 'Kestrel priced 0: owned by every account'); r = gp.buyHero('q@x.com', T0, 'kestrel'); ok(/free for everyone/.test(r.error || ''), 'a free hero is never sold'); let claim = gp.heroClaim('nobody@x.com'); ok(claim.heroes.join() === 'kestrel' && claim.heroSale.free.join() === 'kestrel' && claim.heroBundle === false && !('heroBundleCap' in claim), 'the hand-off says Kestrel is free (the game opens her for guests too)', JSON.stringify(claim)); delete SC.heroPriceKestrelCents; SC.heroBundleHeroes = 'kestrel,valda,morag'; ok(gp.cfg().heroes.bundle.heroes.join() === 'kestrel,valda,morag', 'the bundle list is a setting'); delete SC.heroBundleHeroes; // 6. the hand-off claim and the bundle cap (unset by default; only for bundle owners once set) claim = gp.heroClaim('m@x.com'); ok(claim.heroes.join() === 'kestrel,valda,morag,roland' && claim.heroBundle === true && !('heroBundleCap' in claim) && claim.heroSale.bundle.join() === 'valda,morag,roland' && claim.heroSale.each === 20 && claim.heroSale.bundlePrice === 50, 'claim: owned heroes, bundle, sale; no cap', JSON.stringify(claim)); SC.heroBundleCap = '40'; ok(gp.heroClaim('m@x.com').heroBundleCap === 40 && !('heroBundleCap' in gp.heroClaim('n@x.com')), 'heroBundleCap set: only bundle owners carry it'); delete SC.heroBundleCap; // 7. comp, revoke, stats r = gp.compHero('z@x.com', 'valda', 'admin hero test'); ok(r.ok && gp.heroesOwned('z@x.com').owned.join() === 'valda', 'comp grants a hero'); r = gp.revokeHero('z@x.com', 'valda', 'admin hero test'); ok(r.ok && !gp.heroesOwned('z@x.com').owned.length, 'revoke takes it back (support only)'); const st = gp.heroStats(); ok(st.sold.kestrel === 1 && st.sold.valda === 2 && st.sold.morag === 3 && st.sold.roland === 3 && st.bundles === 2 && st.usd === 160 && st.comped === 0, 'stats: per hero, bundles, dollars (3 x $20 + 2 x $50 = $160)', JSON.stringify(st)); const ps = gp.adminStats(T0 + 3000).passes; ok(ps.soldTotal === 1 && ps.usdTotal === 5, 'pass stats count only the weekly pass (no hero in the pass pool)', JSON.stringify(ps)); console.log(fails ? '\n' + fails + ' FAILED' : '\nALL PASSED'); process.exit(fails ? 1 : 0); })().catch(e => { console.error(e); process.exit(1); });