// BixPay (bixpay.js, 2026-10-29): the partner-link validator, the sponsor-follows resolver (own / sponsor at level 3 / none -> // company), the gate, the email + nudge lines and the click store. Fake accounts, throwaway data dir. Run: node qa/bixpay.test.cjs 'use strict'; const fs = require('fs'); const os = require('os'); const path = require('path'); const assert = require('assert'); const ROOT = path.join(__dirname, '..'); let fails = 0, n = 0; async function t(name, fn) { n++; try { await fn(); console.log('ok', name); } catch (e) { fails++; console.log('FAIL', name, '::', e.message); } } const bixpay = require(path.join(ROOT, 'bixpay.js')); const walletnudge = require(path.join(ROOT, 'walletnudge.js')); const PARTNER = 'https://bix.ibportal.io/auth/register?e=XqwU9T1aJ5N0UJ0yr9x0TGUiSQTsVCkTM01MIovVNnc&a=1'; const CUSTOMER = 'https://bix.ibportal.io/auth/register?e=boUGQ_dC9B472D9LGfzxPKvgfBw1iyqx-xffnx1kqPM&a=1&appId=3'; const L3 = 'https://bix.ibportal.io/auth/register?e=' + 'L3token_'.repeat(5) + '&a=1'; // a five-deep line: leaf -> s1 -> s2 -> s3 (has a link) -> s4 -> s5; plus a loop pair and an orphan const A = {}; const mk = (email, sponsor, link, username) => { A[email] = { email, sponsorRef: sponsor || '', bixPartnerLink: link || null, username: username || null, code: email.split('@')[0] }; }; mk('leaf@x.test', 's1', null, 'leaf'); mk('s1@x.test', 's2', null, 's1'); mk('s2@x.test', 's3', null, 's2'); mk('s3@x.test', 's4', L3, 's3'); mk('s4@x.test', 's5', null, 's4'); mk('s5@x.test', '', null, 's5'); mk('own@x.test', 's1', PARTNER, 'own'); mk('orphan@x.test', '', null, 'orphan'); mk('loopa@x.test', 'loopb', null, 'loopa'); mk('loopb@x.test', 'loopa', null, 'loopb'); // twelve deep with the only link at level 11: must fall to the company (10-level walk) let prev = ''; for (let i = 12; i >= 1; i--) { mk('d' + i + '@x.test', prev, i === 12 ? L3 : null, 'd' + i); prev = 'd' + i; } mk('deep@x.test', 'd1', null, 'deep'); const accounts = { byEmail: async e => A[e] || null, sponsorOf: async e => { const a = A[e]; if (!a || !a.sponsorRef) return null; return Object.values(A).find(x => x.username === a.sponsorRef || x.code === a.sponsorRef) || null; } }; let site = { bixpayLive: '1', bixCompanyLink: '' }; const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'bixpay-test-')); bixpay.init({ dataDir: dir, accounts, siteConfig: () => site, refAccount: async tok => Object.values(A).find(x => x.username === tok || x.code === tok) || null }); (async () => { await t('validator: the partner link passes and normalises', () => { const v = bixpay.validate(' ' + PARTNER + ' '); assert.ok(v.ok); assert.strictEqual(v.link, PARTNER); }); await t('validator: extra params are dropped, e= and a=1 kept', () => { const v = bixpay.validate(PARTNER + '&utm=x'); assert.ok(v.ok); assert.strictEqual(v.link, PARTNER); }); await t('validator: the appId=3 customer link is refused with the message', () => { const v = bixpay.validate(CUSTOMER); assert.ok(!v.ok); assert.strictEqual(v.error, bixpay.CUSTOMER_LINK_MSG); }); await t('validator: wrong host, wrong path, no e=, no a=1, too long, empty, http', () => { assert.ok(bixpay.validate('https://bix.fi/r?code=ABCDEFGH').error); assert.ok(bixpay.validate('https://bix.ibportal.io/auth/login?e=' + 'x'.repeat(40) + '&a=1').error); assert.ok(bixpay.validate('https://bix.ibportal.io/auth/register?a=1').error); assert.ok(bixpay.validate('https://bix.ibportal.io/auth/register?e=' + 'x'.repeat(40)).error); assert.ok(bixpay.validate('https://bix.ibportal.io/auth/register?e=' + 'x'.repeat(40) + '&a=2').error); assert.ok(bixpay.validate('https://bix.ibportal.io/auth/register?e=' + 'x'.repeat(300) + '&a=1').error); assert.ok(bixpay.validate('').error); assert.ok(bixpay.validate(null).error); assert.ok(bixpay.validate('http://bix.ibportal.io/auth/register?e=' + 'x'.repeat(40) + '&a=1').error); assert.ok(bixpay.validate('javascript:alert(1)').error); }); await t('resolver: own link wins', async () => { const r = await bixpay.resolve(A['own@x.test']); assert.strictEqual(r.whose, 'you'); assert.strictEqual(r.link, PARTNER); }); await t('resolver: no own link -> first sponsor up the chain with one (level 3)', async () => { const r = await bixpay.resolve(A['leaf@x.test']); assert.strictEqual(r.whose, 'sponsor'); assert.strictEqual(r.name, '@s3'); assert.strictEqual(r.email, 's3@x.test'); assert.ok(r.link.includes('L3token_')); }); await t('resolver: nobody up the chain -> company (default = Marty’s partner link)', async () => { const r = await bixpay.resolve(A['orphan@x.test']); assert.strictEqual(r.whose, 'network'); assert.strictEqual(r.link, bixpay.DEFAULT_COMPANY_LINK); assert.strictEqual(r.name, null); }); await t('resolver: null member -> company', async () => { const r = await bixpay.resolve(null); assert.strictEqual(r.whose, 'network'); }); await t('resolver: the walk stops at 10 levels (link at level 12 is not reached)', async () => { const r = await bixpay.resolve(A['deep@x.test']); assert.strictEqual(r.whose, 'network'); }); await t('resolver: a sponsor loop ends in the company link, no hang', async () => { const r = await bixpay.resolve(A['loopa@x.test']); assert.strictEqual(r.whose, 'network'); }); await t('resolver: site config bixCompanyLink overrides the default; a bad one falls back', async () => { site.bixCompanyLink = L3; assert.strictEqual((await bixpay.resolve(null)).link, L3.replace(/&a=1$/, '&a=1')); site.bixCompanyLink = 'https://evil.example/x'; assert.strictEqual((await bixpay.resolve(null)).link, bixpay.DEFAULT_COMPANY_LINK); site.bixCompanyLink = ''; }); await t('resolveRef: a public page ?ref resolves like that member', async () => { const r = await bixpay.resolveRef('leaf'); assert.strictEqual(r.whose, 'sponsor'); const o = await bixpay.resolveRef('own'); assert.strictEqual(o.whose, 'you'); const u = await bixpay.resolveRef('nobody_zz'); assert.strictEqual(u.whose, 'network'); }); await t('usingLine: the three wordings', async () => { assert.strictEqual(bixpay.usingLine(await bixpay.resolve(A['own@x.test'])), 'Using: your link.'); assert.strictEqual(bixpay.usingLine(await bixpay.resolve(A['leaf@x.test'])), "Using: @s3's link. Add yours in Profile."); assert.strictEqual(bixpay.usingLine(await bixpay.resolve(null)), 'Using: the network link. Add yours in Profile.'); }); await t('gate: live/announcing follow site config; email and nudge lines are empty while off', () => { assert.ok(bixpay.live()); assert.ok(bixpay.announcing()); site.bixpayLiveSince = new Date(Date.now() - 8 * 86400000).toISOString(); assert.ok(!bixpay.announcing()); site.bixpayLiveSince = ''; assert.ok(bixpay.emailLine('own').includes('/go/bixpay?s=email&m=own')); assert.ok(bixpay.nudgeLine('own').includes('s=nudge')); assert.ok(!/—/.test(bixpay.emailLine('own'))); site.bixpayLive = '0'; assert.ok(!bixpay.live()); assert.strictEqual(bixpay.emailLine('own'), ''); assert.strictEqual(bixpay.nudgeLine('own'), ''); site.bixpayLive = '1'; }); await t('walletnudge copy carries the BixPay sentence only when given one', () => { const c0 = walletnudge.copy([{ key: 'g', name: 'Gem Kingdom', unpaid: 1.5 }], 0, 'https://instantadpay.com/my#wallet', ''); assert.ok(!/BixPay/.test(c0.text) && !/BixPay/.test(c0.html)); const c1 = walletnudge.copy([{ key: 'g', name: 'Gem Kingdom', unpaid: 1.5 }], 0, 'https://instantadpay.com/my#wallet', bixpay.nudgeLine('own')); assert.ok(/BixPay Visa card/.test(c1.text) && /go\/bixpay\?s=nudge/.test(c1.html)); assert.ok(c1.text.indexOf('Link your wallet:') < c1.text.indexOf('BixPay'), 'the wallet ask stays first'); }); await t('clicks: per surface, per member, per day; unknown surface -> other; bot-free store', () => { bixpay.click('wallet', 'own@x.test'); bixpay.click('promo', 'own@x.test'); bixpay.click('promo', 'own@x.test'); bixpay.click('guide', null); bixpay.click('weird', 'leaf@x.test'); const m = bixpay.memberClicks('own@x.test'); assert.strictEqual(m.total, 3); assert.strictEqual(m.promo, 2); assert.strictEqual(m.wallet, 1); const v = bixpay.adminView(30); assert.strictEqual(v.total, 5); assert.strictEqual(v.bySurface.promo, 2); assert.strictEqual(v.bySurface.other, 1); assert.strictEqual(v.members[0].email, 'own@x.test'); assert.strictEqual(v.daily.length, 1); bixpay._reset(); assert.strictEqual(bixpay.memberClicks('own@x.test').total, 3, 'persisted to disk'); }); await t('copy rules: no em dashes in any BixPay copy shipped to members', () => { const files = ['bixpay.js', 'public/assets/promo.js', 'public/assets/my.js', 'public/bixpay.html', 'public/assets/bixpay-guide.js', 'chatbot.js']; for (const f of files) { const s = fs.readFileSync(path.join(ROOT, f), 'utf8'); const lines = s.split('\n').filter(l => /bix/i.test(l) && /—/.test(l)); assert.strictEqual(lines.length, 0, f + ' has an em dash on a BixPay line'); } const pj = fs.readFileSync(path.join(ROOT, 'public/assets/promo.js'), 'utf8'); assert.ok(/no load fee/.test(pj) && !/in-app swap/i.test(pj.split('BIX_POSTS')[1].split('BIX_CDN')[0]), 'posts never promise the in-app swap'); }); fs.rmSync(dir, { recursive: true, force: true }); console.log('\nbixpay: ' + (n - fails) + '/' + n + ' passed'); process.exit(fails ? 1 : 0); })();