0688bdeb72
Islands and looks in Shards of Valor, never power and never more POL: every tier gets exactly the weekly pass caps and polgrant
still answers pass: true. Per-tier expiry side by side (record.tiers, legacy until mirrors weekly, lazy migration); flag() adds
tier (highest active) and tiers; hand-off token gamePass { active, until, tier }; /api/partner/player pass { active, until, tier,
tiers }. Buy { tier } with purchased credits or a waiting order per tier that a big-enough package fulfils (highest price first,
each tx once). No auto-renew, no proration. Admin: per-tier stats (Central week/month), comp per tier, end-a-tier (refund).
Tournament pool keeps weekly sales only unless passTournamentTierSales. Member card shows three tiers; /my#passes opens it.
Tests: qa/sov-passes-store.cjs, qa/sov-passes-local.mjs (bash qa/run.sh sov-passes). Cache tags my.js/admin.js 20261018tiers.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
151 lines
16 KiB
JavaScript
151 lines
16 KiB
JavaScript
// Pass tiers (2026-10-18, ultima-rpg docs/IAP-PASSES-SPEC.md section 8): unit checks of the gamepass.js store, no server.
|
|
// node qa/sov-passes-store.cjs
|
|
// Loads gamepass.js with in-memory maps and a fake site config. Covers: lazy migration of a weekly-only record, buy per tier,
|
|
// extend from max(now, until), side-by-side expiry, highest active tier wins, the sovTiers switch (off refuses, admin may buy),
|
|
// waiting orders per tier fulfilled by a big-enough package (each tx once, highest price first), the same caps for every tier
|
|
// (no extra POL), comp and revoke, admin stats per tier (each account once at its top tier, Central week/month). Exit 1 on failure.
|
|
'use strict';
|
|
const fs = require('fs'); const os = require('os'); const path = require('path');
|
|
const gp = require('../gamepass');
|
|
const DAY = 86400000;
|
|
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', capsStartDate: '2026-10-01' };
|
|
const maps = {};
|
|
const dataDir = fs.mkdtempSync(path.join(os.tmpdir(), 'iap-sovstore-'));
|
|
gp.init({ dataDir, 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-10-20T17:00:00Z'); // Tue Oct 20, noon Central
|
|
const paid = async () => ({ ok: true, campaignId: 1 });
|
|
const short = async () => ({ error: 'short', available: 0, need: 1 });
|
|
|
|
(async () => {
|
|
// 1. migration: a record from before tiers (until only) reads as the weekly tier; flag answers as before plus tier
|
|
passes.set('old@x.com', { until: T0 + 3 * DAY, bought: [{ ts: T0 - 4 * DAY, via: 'credits', usd: 5, days: 7 }] });
|
|
let f = gp.flag('old@x.com', T0);
|
|
ok(f.active === true && f.until === T0 + 3 * DAY && f.tier === 'weekly' && f.tiers.weekly === T0 + 3 * DAY && f.tiers.explorer === null, 'weekly-only record migrates: active, same until, tier weekly', JSON.stringify(f));
|
|
f = gp.flag('old@x.com', T0 + 5 * DAY);
|
|
ok(f.active === false && f.tier === null && f.until === T0 + 3 * DAY, 'expired weekly-only record: inactive, until kept as before tiers, tier null', JSON.stringify(f));
|
|
f = gp.flag('never@x.com', T0); ok(f.active === false && f.until === null && f.tier === null, 'never bought: { active false, until null, tier null }', JSON.stringify(f));
|
|
await gp.buy('old@x.com', paid, T0, 'weekly');
|
|
const oldRec = passes.get('old@x.com');
|
|
ok(oldRec.tiers && oldRec.tiers.weekly.until === T0 + 10 * DAY && oldRec.until === T0 + 10 * DAY, 'buying weekly on a migrated record extends from its until and writes tiers + legacy until', JSON.stringify({ until: oldRec.until, tiers: oldRec.tiers }));
|
|
|
|
// 2. the switch: off refuses Explorer/Champion for members, the weekly pass is unchanged; the admin may buy
|
|
let r = await gp.buy('a@x.com', paid, T0, 'explorer');
|
|
ok(r.error === 'The Explorer pass is not open yet.', 'sovTiers off: Explorer refused', JSON.stringify(r));
|
|
r = await gp.buy('a@x.com', paid, T0, 'champion');
|
|
ok(r.error === 'The Champion pass is not open yet.', 'sovTiers off: Champion refused', JSON.stringify(r));
|
|
ok(!passes.get('a@x.com'), 'a refused buy writes nothing', '');
|
|
let v = gp.view('a@x.com', T0);
|
|
ok(v.passes.length === 1 && v.passes[0].tier === 'weekly' && v.show === true && v.tierPreview === false, 'sovTiers off: a member sees only the weekly pass', JSON.stringify(v.passes.map(x => x.tier)));
|
|
v = gp.view('admin@x.com', T0, { admin: true });
|
|
ok(v.passes.map(x => x.tier).join() === 'weekly,explorer,champion' && v.tierPreview === true && v.passes[1].open === true && v.passes[1].price === 2000 && v.passes[2].price === 4000 && v.passes[1].days === 30, 'sovTiers off: the admin sees all three (preview), prices 2000 / 4000, 30 days', JSON.stringify(v.passes));
|
|
r = await gp.buy('admin@x.com', paid, T0, 'explorer', { admin: true });
|
|
ok(r.ok && r.tier === 'explorer' && r.until === T0 + 30 * DAY && r.days === 30, 'sovTiers off: the admin can buy Explorer (testing)', JSON.stringify(r));
|
|
r = await gp.buy('a@x.com', paid, T0); // tier missing = weekly
|
|
ok(r.ok && r.tier === 'weekly' && r.until === T0 + 7 * DAY && r.days === 7, 'tier missing = weekly: 500 credits, 7 days, as before', JSON.stringify(r));
|
|
let charged = null; await gp.buy('z@x.com', async (price, label) => { charged = { price, label }; return { ok: true }; }, T0);
|
|
ok(charged && charged.price === 500 && charged.label === 'Game Pass (7 days)', 'weekly charge unchanged: 500 credits, label "Game Pass (7 days)"', JSON.stringify(charged));
|
|
r = await gp.buy('a@x.com', paid, T0, 'platinum'); ok(r.error === 'There is no such pass.', 'unknown tier refused', JSON.stringify(r));
|
|
SC.gamePassEnabled = '0'; SC.sovTiers = '1';
|
|
r = await gp.buy('a@x.com', paid, T0, 'explorer'); ok(!!r.error, 'sovTiers on but the weekly Game Pass off: Explorer refused for members', JSON.stringify(r));
|
|
SC.gamePassEnabled = '1';
|
|
|
|
// 3. on: each tier extends only itself; buying again extends from max(now, until)
|
|
charged = null; r = await gp.buy('b@x.com', async (price, label) => { charged = { price, label }; return { ok: true }; }, T0, 'explorer');
|
|
ok(r.ok && r.until === T0 + 30 * DAY && charged.price === 2000 && charged.label === 'Explorer pass (30 days)', 'sovTiers on: Explorer charges 2000 credits, 30 days', JSON.stringify(charged));
|
|
let rec = passes.get('b@x.com');
|
|
ok(rec.tiers.explorer.until === T0 + 30 * DAY && !rec.tiers.weekly && !rec.tiers.champion && rec.until === 0 && rec.bought[0].tier === 'explorer' && rec.bought[0].usd === 20, 'only the Explorer tier moved; legacy until (weekly) stays 0; bought row has tier + $20', JSON.stringify(rec));
|
|
r = await gp.buy('b@x.com', paid, T0 + 10 * DAY, 'explorer');
|
|
ok(r.until === T0 + 60 * DAY, 'Explorer again while active: extends from its until (+30 days, nothing lost)', new Date(r.until).toISOString());
|
|
r = await gp.buy('c@x.com', paid, T0, 'champion'); ok(r.ok && r.until === T0 + 30 * DAY, 'Champion: 30 days', '');
|
|
r = await gp.buy('c@x.com', paid, T0 + 45 * DAY, 'champion');
|
|
ok(r.until === T0 + 75 * DAY, 'Champion again after it ended: extends from now', new Date(r.until).toISOString());
|
|
charged = null; await gp.buy('c2@x.com', async (price) => { charged = price; return { ok: true }; }, T0, 'champion'); ok(charged === 4000, 'Champion charges 4000 credits', String(charged));
|
|
|
|
// 4. side by side: Champion over Explorer, then Explorer shows through; weekly underneath
|
|
await gp.buy('d@x.com', paid, T0, 'weekly'); // weekly to T0+7
|
|
await gp.buy('d@x.com', paid, T0, 'explorer'); // explorer to T0+30
|
|
await gp.buy('d@x.com', paid, T0 - 20 * DAY, 'champion'); // champion bought earlier, to T0+10
|
|
f = gp.flag('d@x.com', T0 + DAY);
|
|
ok(f.tier === 'champion' && f.until === T0 + 10 * DAY && f.active && f.tiers.weekly === T0 + 7 * DAY && f.tiers.explorer === T0 + 30 * DAY && f.tiers.champion === T0 + 10 * DAY, 'all three on: tier champion, until = Champion end, tiers side by side', JSON.stringify(f));
|
|
f = gp.flag('d@x.com', T0 + 12 * DAY);
|
|
ok(f.tier === 'explorer' && f.until === T0 + 30 * DAY && f.tiers.champion === null && f.tiers.weekly === null, 'Champion ended: Explorer shows through', JSON.stringify(f));
|
|
f = gp.flag('d@x.com', T0 + 31 * DAY); ok(f.active === false && f.tier === null, 'all ended: no pass', JSON.stringify(f));
|
|
|
|
// 5. no extra POL: the caps are identical for weekly, explorer and champion holders (and higher than free)
|
|
const day = '2026-10-21'; const at = Date.parse(day + 'T17:00:00Z');
|
|
await gp.buy('w@x.com', paid, at, 'weekly'); await gp.buy('e@x.com', paid, at, 'explorer'); await gp.buy('ch@x.com', paid, at, 'champion');
|
|
const capsOf = e => { const c = gp.capsFor(e, at); return [c.pass, c.daily, c.total].join('/'); };
|
|
ok(capsOf('w@x.com') === capsOf('e@x.com') && capsOf('e@x.com') === capsOf('ch@x.com') && capsOf('w@x.com') === 'true/1.5/5' && capsOf('free@x.com') === 'false/0.3/2', 'capsFor: weekly = explorer = champion = pass caps 1.50 / 5', [capsOf('w@x.com'), capsOf('e@x.com'), capsOf('ch@x.com'), capsOf('free@x.com')].join(' '));
|
|
const g = e => gp.grant({ email: e, game: 'polhunter', kind: 'daily', pol: 3, ref: 'cap:' + e, now: at });
|
|
const gw = g('w@x.com'), ge = g('e@x.com'), gc = g('ch@x.com');
|
|
ok(gw.allowed === 1.5 && ge.allowed === 1.5 && gc.allowed === 1.5 && gw.pass && ge.pass && gc.pass, 'polgrant: a 3 POL daily grant allows 1.50 for every tier, pass: true (a boolean)', [gw.allowed, ge.allowed, gc.allowed, typeof gc.pass].join(' '));
|
|
|
|
// 6. waiting orders per tier; a package of at least the tier price fulfils it; each tx once; highest-priced first
|
|
r = await gp.buy('p@x.com', short, T0, 'explorer');
|
|
ok(r.needPay && r.tier === 'explorer' && r.payUrl === '/my?from=gamepass&tier=explorer#buy' && r.package === 20 && /\$20 package/.test(r.message), 'Explorer short: waiting order, payUrl with tier, "$20 package"', JSON.stringify({ payUrl: r.payUrl, msg: r.message }));
|
|
const ordE = r.order;
|
|
r = await gp.buy('p@x.com', short, T0, 'explorer'); ok(r.order === ordE, 'same tier again reuses its waiting order', '');
|
|
r = await gp.buy('p@x.com', short, T0, 'champion'); const ordC = r.order;
|
|
ok(r.needPay && r.order !== ordE && r.package === 50 && /\$50 package/.test(r.message), 'Champion short: its own waiting order; copy names the $50 package (there is no $40 one)', r.message);
|
|
r = await gp.buy('p@x.com', short, T0, 'weekly');
|
|
ok(r.payUrl === '/my?from=gamepass#buy' && /Buy a \$5 package/.test(r.message), 'weekly short: payUrl and copy exactly as before', r.payUrl);
|
|
let ev = await gp.onEvent({ type: 'Purchase', priceCents: 500, tx: '0xa1', ts: T0 + 60000, devEmail: 'p@x.com' });
|
|
ok(ev && ev.tier === 'weekly', 'a $5 package fulfils the weekly order, not Explorer or Champion', JSON.stringify(ev));
|
|
ev = await gp.onEvent({ type: 'Purchase', priceCents: 1000, tx: '0xa2', ts: T0 + 60000, devEmail: 'p@x.com' });
|
|
ok(ev === null, 'a $10 package fulfils nothing (Explorer needs $20)', JSON.stringify(ev));
|
|
ev = await gp.onEvent({ type: 'Purchase', priceCents: 2000, tx: '0xa3', ts: T0 + 60000, devEmail: 'p@x.com' });
|
|
ok(ev && ev.tier === 'explorer' && ev.order === ordE && ev.until === T0 + 60000 + 30 * DAY, 'a $20 package fulfils the Explorer order', JSON.stringify(ev));
|
|
ev = await gp.onEvent({ type: 'Purchase', priceCents: 2000, tx: '0xa3', ts: T0 + 60000, devEmail: 'p@x.com' });
|
|
ok(ev === null, 'the same tx again counts once', JSON.stringify(ev));
|
|
ev = await gp.onEvent({ type: 'Purchase', priceCents: 5000, tx: '0xa4', ts: T0 + 60000, devEmail: 'p@x.com' });
|
|
ok(ev && ev.tier === 'champion' && ev.order === ordC, 'a $50 package fulfils the Champion order', JSON.stringify(ev));
|
|
await gp.buy('q@x.com', short, T0, 'weekly'); await gp.buy('q@x.com', short, T0, 'explorer'); await gp.buy('q@x.com', short, T0, 'champion');
|
|
ev = await gp.onEvent({ type: 'Purchase', priceCents: 2000, tx: '0xb1', ts: T0, devEmail: 'q@x.com' });
|
|
ok(ev && ev.tier === 'explorer', 'three orders waiting, a $20 package: the highest-priced one it covers (Explorer) is fulfilled first', JSON.stringify(ev));
|
|
ev = await gp.onEvent({ type: 'Purchase', priceCents: 50000, tx: '0xb2', ts: T0, devEmail: 'q@x.com' });
|
|
ok(ev && ev.tier === 'champion', 'then a $500 package: Champion (one pass per purchase)', JSON.stringify(ev));
|
|
ev = await gp.onEvent({ type: 'Purchase', priceCents: 500, tx: '0xb3', ts: T0, devEmail: 'q@x.com' });
|
|
ok(ev && ev.tier === 'weekly', 'then a $5 package: the weekly order', JSON.stringify(ev));
|
|
// an old waiting order (no tier field) is a weekly order
|
|
orders.set('gp-legacy', { status: 'waiting', email: 'leg@x.com', ts: T0 });
|
|
ev = await gp.onEvent({ type: 'Purchase', priceCents: 500, tx: '0xc1', ts: T0, devEmail: 'leg@x.com' });
|
|
ok(ev && ev.tier === 'weekly' && ev.order === 'gp-legacy', 'a waiting order from before tiers is fulfilled as weekly', JSON.stringify(ev));
|
|
|
|
// 7. comp and revoke
|
|
let cp = gp.comp('k@x.com', 'qa', 'champion', T0); f = gp.flag('k@x.com', T0 + 1);
|
|
ok(cp.tier === 'champion' && f.tier === 'champion' && passes.get('k@x.com').bought[0].via === 'admin' && passes.get('k@x.com').bought[0].usd === 0, 'comp champion: an admin row worth $0', JSON.stringify(cp));
|
|
let rv = gp.revoke('k@x.com', 'champion', 'refund qa', T0 + DAY);
|
|
ok(rv.ok && gp.flag('k@x.com', T0 + DAY + 1).active === false && passes.get('k@x.com').revokes[0].tier === 'champion', 'revoke ends the tier now and records it', JSON.stringify(rv));
|
|
rv = gp.revoke('k@x.com', 'champion', 'again', T0 + 2 * DAY); ok(!!rv.error, 'revoking an inactive tier is refused', rv.error);
|
|
|
|
// 8. admin stats: each account once at its top tier, Central week and month
|
|
for (const k of [...passes.keys()]) passes.delete(k); for (const k of [...orders.keys()]) orders.delete(k);
|
|
const NOW = Date.parse('2026-11-04T18:00:00Z'); // Wed Nov 4, noon Central (CST); week from Mon Nov 2, month from Nov 1
|
|
await gp.buy('s1@x.com', paid, Date.parse('2026-11-02T06:30:00Z'), 'weekly'); // Mon Nov 2 00:30 Central: this week
|
|
await gp.buy('s1@x.com', paid, Date.parse('2026-11-02T05:30:00Z'), 'explorer'); // Sun Nov 1 23:30 Central: last week, this month
|
|
await gp.buy('s2@x.com', paid, Date.parse('2026-11-01T04:30:00Z'), 'champion'); // Oct 31 23:30 Central: last month
|
|
await gp.buy('s2@x.com', paid, Date.parse('2026-11-03T18:00:00Z'), 'champion'); // a renewal this week
|
|
await gp.buy('s3@x.com', paid, NOW - 2 * DAY, 'weekly'); // ends within 7 Central days
|
|
gp.comp('s4@x.com', 'qa', 'explorer', NOW - DAY);
|
|
await gp.buy('s5@x.com', short, NOW, 'champion');
|
|
const st = gp.adminStats(NOW); const S = st.tiers;
|
|
ok(S.weekly.active === 1 && S.explorer.active === 2 && S.champion.active === 1, 'active counts each account once at its top tier (s1 = explorer, not weekly)', JSON.stringify({ w: S.weekly.active, e: S.explorer.active, c: S.champion.active }));
|
|
ok(S.weekly.soldWeek === 2 && S.explorer.soldWeek === 0 && S.explorer.soldMonth === 1 && S.champion.soldWeek === 1 && S.champion.soldMonth === 1 && S.champion.soldTotal === 2 && S.champion.usdTotal === 80, 'Central week (from Mon 00:00) and month (from the 1st 00:00) boundaries', JSON.stringify({ w: [S.weekly.soldWeek, S.weekly.soldMonth], e: [S.explorer.soldWeek, S.explorer.soldMonth], c: [S.champion.soldWeek, S.champion.soldMonth, S.champion.soldTotal, S.champion.usdTotal] }));
|
|
ok(S.champion.renewals30d === 1 && S.weekly.renewals30d === 0, 'renewals30d: a second Champion purchase by a Champion holder', String(S.champion.renewals30d));
|
|
ok(S.weekly.expiring7d === 2 && S.explorer.comped === 1 && S.champion.waitingOrders === 1, 'expiring7d (weekly passes ending in 7 Central days), comped, waiting orders per tier', JSON.stringify({ exp: S.weekly.expiring7d, comped: S.explorer.comped, wait: S.champion.waitingOrders }));
|
|
ok(st.passes.active === 4 && st.passes.soldTotal === 5 && st.passes.comped === 1 && st.passes.waitingOrders === 1, 'the existing all-tier passes block still adds up', JSON.stringify(st.passes));
|
|
|
|
// 9. the tournament pool: weekly sales only unless passTournamentTierSales
|
|
const rows = await gp.runTournament(Date.parse('2026-11-03T18:00:00Z'), { force: true });
|
|
ok(rows.sales === 2 && rows.salesUsd === 10, 'passTournamentTierSales off: only the 2 weekly sales ($10) feed the pool', JSON.stringify({ sales: rows.sales, usd: rows.salesUsd }));
|
|
SC.passTournamentTierSales = '1';
|
|
const rows2 = await gp.runTournament(Date.parse('2026-11-03T18:00:00Z'), { force: true });
|
|
ok(rows2.sales === 3 && rows2.salesUsd === 50, 'passTournamentTierSales on: the Champion renewal ($40) is added', JSON.stringify({ sales: rows2.sales, usd: rows2.salesUsd }));
|
|
try { fs.rmSync(dataDir, { recursive: true, force: true }); } catch (e) {}
|
|
console.log(fails ? fails + ' FAILURE(S)' : 'ALL PASS');
|
|
process.exit(fails ? 1 : 0);
|
|
})().catch(e => { console.error('FAIL harness error', e && e.stack || e); process.exit(1); });
|