Referral layer: guest play from share links (/r/<username>), guest clears paid on sign-in, sharer's link via their IAP join page, 10% referrer slice of each referral refuel's pool income (IAP sponsor), share buttons + stats in the lobby and on chapter clears, wallet-less progress follows the member once a wallet is linked; 11 new checks

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
martbost
2026-09-30 17:14:08 -05:00
parent 83da8c33f2
commit 06daa4ef0a
10 changed files with 203 additions and 25 deletions
+36
View File
@@ -94,6 +94,42 @@ const spawnLib = require('../lib/spawn');
eq(ledger.account(42).unpaid, 0, 'the balance moved onto the payout row');
ledger.mark(due[0].id, { status: 'failed', error: 'test' }); eq(ledger.account(42).unpaid, 0.005, 'a failed payout returns the POL to the balance');
// ---- referral layer (2026-09-30): share link, guest play, adoption on sign-in, the referrer's slice ----
const hit = async (p, opt = {}, ck = '') => { const r = await fetch(B + p, Object.assign({ redirect: 'manual' }, opt, { headers: Object.assign({ Cookie: ck, 'Content-Type': 'application/json' }, opt.headers || {}) })); const set = r.headers.getSetCookie ? r.headers.getSetCookie() : []; let body = null; try { body = await r.json(); } catch (e) {} return { status: r.status, body, set, loc: r.headers.get('location') }; };
const jarAdd = (ck, set) => { const parts = ck ? ck.split('; ') : []; for (const c of set) { const m = /^([^=]+)=([^;]*)/.exec(c); if (!m) continue; const i = parts.findIndex(x => x.startsWith(m[1] + '=')); if (i >= 0) parts.splice(i, 1); if (m[2]) parts.push(m[1] + '=' + m[2]); } return parts.join('; '); };
await admin('/api/admin/settings', { signups: 'open', thresholdPol: 5, refSharePct: 10 });
eq((await hit('/r/nobody')).loc, '/', 'a share link for an unknown player goes to the landing page');
const sh = await hit('/r/runner42'); eq([sh.status, sh.loc, sh.set.some(c => /^dr\.ref=runner42/.test(c))], [302, '/play', true], 'a player share link sets the referrer and opens the game');
let g = jarAdd('', sh.set);
const me401 = await hit('/api/me', {}, g); eq([me401.status, me401.body.guestOk, /join\/runner42\?from=driprunner/.test(me401.body.signin)], [401, true, true], 'a newcomer is offered guest play, sign-in goes through the sharer’s join link');
const gs = await hit('/api/guest', { method: 'POST', body: '{}' }, g); g = jarAdd(g, gs.set); eq(gs.status, 200, 'guest session starts');
const gme = await hit('/api/me', {}, g); eq([gme.status, gme.body.me.guest, gme.body.player.chapter], [200, true, 1], 'guest plays chapter 1');
const gr = (await hit('/api/run/start', { method: 'POST', body: JSON.stringify({ W, H }) }, g)).body;
const ggen = spawnLib.drips(gr.seed, W, H); const gcl = []; let gT = 0; while (gcl.length < 20) { const gg = ggen(); for (const d of gg.drips) if (gcl.length < 20) { gT = spawnLib.arrival(gg.t, d.x0, W, gr.speed) + 0.1; gcl.push({ k: 'd', i: d.i, t: Math.round(gT * 1000) / 1000 }); } }
backdate(gr.runId, gT + 5);
const gev = (await hit('/api/run/events', { method: 'POST', body: JSON.stringify({ runId: gr.runId, t: gT + 0.2, events: gcl, goal: true }) }, g)).body;
eq([gev.cleared.chapter, gev.cleared.pol, gev.cleared.reason], [1, 0, 'guest'], 'a guest clear pays nothing yet (reason guest)');
eq((await hit('/api/refuel/start', { method: 'POST', body: JSON.stringify({ runId: gr.runId }) }, g)).status, 403, 'a guest cannot refuel before signing in');
// the newcomer joins IAP under runner42 and signs in: the guest run is adopted and the clear pays
const tokN = sso.mint({ memberId: 77, email: 'newbie@example.com', wallet: '0x' + 'cd'.repeat(20), username: 'newbie', sponsorRef: 'runner42' });
const au = await hit('/auth?t=' + tokN, {}, g); g = jarAdd(g, au.set);
eq([au.status, au.loc], [302, '/play?collected=1'], 'sign-in adopts the guest and pays the pending clear');
const nme = (await hit('/api/me', {}, g)).body; eq([nme.me.guest, nme.player.chapter, nme.player.bank, nme.balance.unpaid], [false, 2, 20, 0.01], 'progress and the drip followed the newcomer');
// the newcomer refuels: runner42 (their IAP sponsor) gets the slice
const nr = (await hit('/api/run/start', { method: 'POST', body: JSON.stringify({ W, H }) }, g)).body;
const nrf = (await hit('/api/refuel/start', { method: 'POST', body: JSON.stringify({ runId: nr.runId }) }, g)).body;
const body2 = JSON.stringify({ nonce: nrf.nonce, memberId: 77, campaignId: 88, credits: 3 }); const ts2 = String(Date.now());
await hit('/api/refuel/callback', { method: 'POST', body: body2, headers: { 'X-DR-Ts': ts2, 'X-DR-Sig': crypto.createHmac('sha256', SECRET).update(ts2 + '\n' + body2).digest('hex') } });
const stR = (await admin('/api/admin/state')).body; const refuelPol = stR.refuels[0].pol; const slice = Math.round(refuelPol * 0.10 * 10000) / 10000;
const runnerAcct = stR.accounts.find(a => a.memberId === 42); eq(runnerAcct.refTotal, slice, 'the sponsor earned 10% of the referral’s refuel pool income (' + slice + ' POL)');
const rme = await call('/api/me'); eq([rme.body.referrals.count, rme.body.referrals.clicks, /\/r\/runner42$/.test(rme.body.share)], [1, 1, true], 'the sharer sees one referral, one link visit, and their share link');
// wallet-less member later links a wallet: the email-derived history moves to the on-chain id
const tokW = sso.mint({ memberId: 0, email: 'free@example.com', wallet: null, username: 'freebie' }); let w = '';
const aw = await hit('/auth?t=' + tokW); w = jarAdd(w, aw.set); const wme = (await hit('/api/me', {}, w)).body; eq(wme.me.memberId < 0, true, 'a wallet-less member plays on a temporary id');
process.env.DATA_DIR = DIR; const gameLib = require('../lib/game'); gameLib.updPlayer(wme.me.memberId, x => { x.bank = 33; x.chapter = 4; x.cleared = { 1: true, 2: true, 3: true }; });
const tokW2 = sso.mint({ memberId: 91, email: 'free@example.com', wallet: '0x' + 'ef'.repeat(20), username: 'freebie' });
await hit('/auth?t=' + tokW2); const moved = gameLib.player(91); eq([moved && moved.chapter, moved && moved.bank, !!gameLib.player(wme.me.memberId)], [4, 33, false], 'after the wallet is linked, progress moves to the on-chain id');
console.log(fails ? '\n' + fails + ' FAILED' : '\nall good');
child.kill(); fs.rmSync(DIR, { recursive: true, force: true }); process.exit(fails ? 1 : 0);
})().catch(e => { console.error(e); child.kill(); process.exit(1); });