// Monthly Refuel, LinkSpin side (refuel.js) against InstantAdPay's REAL refuel.js (sibling repo) wired in-process in // place of the signed HTTP calls. Throwaway data dirs; no network. Run: node qa/refuel.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, '..'); const IAP_ROOT = path.join(ROOT, '..', '..', 'onchain-ad-membership', 'site'); if (!fs.existsSync(path.join(IAP_ROOT, 'refuel.js'))) { console.log('skip: InstantAdPay repo not beside this one'); process.exit(0); } 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.stack.split('\n').slice(0, 2).join(' | ')); } } function fresh(file) { const p = require.resolve(file); delete require.cache[p]; return require(p); } const ct = (day, hour) => { const [y, m, d] = day.split('-').map(Number); return Date.UTC(y, m - 1, d, (hour == null ? 12 : hour) + 6); }; process.env.LS_SYNC_SECRET = 'x'.repeat(40); function world(opts) { opts = opts || {}; const W = { lsEarned: [], lsLog: [], lsInbox: [], lsMail: [], iapEarned: [], down: false, posts: 0, dropAck: false }; // InstantAdPay side W.iapDir = fs.mkdtempSync(path.join(os.tmpdir(), 'iap-rf-')); const iapMarks = fresh(path.join(IAP_ROOT, 'marks.js')); iapMarks.init({}); W.IAP = fresh(path.join(IAP_ROOT, 'refuel.js')); W.iapCfg = { refuelStreak: '1' }; W.IAP.init({ dataDir: W.iapDir, db: require(path.join(IAP_ROOT, 'db.js')), marks: iapMarks, siteConfig: () => W.iapCfg, accounts: { byMemberId: async () => null, byEmail: async e => ({ email: e, username: 'sam' }) }, ads: { addEarned: async (email, credits, o) => { W.iapEarned.push({ email, credits, ref: o.log.ref }); } }, message: async () => {}, featuredPerDay: () => 40 }); // the signed partner endpoints, in-process (same routing as InstantAdPay server.js) const post = async (pth, b) => { W.posts++; if (W.down) throw new Error('InstantAdPay 502'); const op = pth.replace('/api/partner/refuel/', ''); if (op === 'purchase') { const r = await W.IAP.record({ site: 'linkspin', email: b.email, tx: b.tx, cents: b.cents, credits: b.credits, ts: b.ts }); return Object.assign({ ok: true }, r, { items: r.on ? await W.IAP.outbox('linkspin', b.email) : [] }); } if (op === 'outbox') return { ok: true, on: W.IAP.cfg().on, items: W.IAP.cfg().on ? await W.IAP.outbox('linkspin') : [] }; if (op === 'ack') { if (W.dropAck) throw new Error('ack lost'); return { ok: true, acked: await W.IAP.ack('linkspin', b.refs) }; } if (op === 'status') return Object.assign({ ok: true }, await W.IAP.view(b.email)); throw new Error('404'); }; // LinkSpin side: one volume, any number of app copies W.lsDir = fs.mkdtempSync(path.join(os.tmpdir(), 'ls-rf-')); W.lsCfg = { refuelStreak: opts.lsOff ? '0' : '1' }; W.copy = () => { const marks = fresh(path.join(ROOT, 'marks.js')); marks.init({ dataDir: W.lsDir }); const R = fresh(path.join(ROOT, 'refuel.js')); R.init({ dataDir: W.lsDir, siteConfig: () => W.lsCfg, marks, siteUrl: 'https://linkspin.app', post, accounts: { byMemberId: async id => id === 4 ? { email: 's@x.test', memberId: 4 } : null, positionByMember: async () => null, byEmail: async e => ({ email: e }) }, ads: { addEarned: async (email, credits, o) => { W.lsEarned.push({ email, credits, purchased: !!(o && o.purchased) }); }, logCredit: async (email, delta, reason) => { W.lsLog.push({ email, delta, reason }); } }, message: async (to, subject, html) => { W.lsInbox.push({ to, subject, html }); }, mailer: { hasKey: () => true, send: async (to, subject, text) => { W.lsMail.push({ to, subject, text }); } }, drip: { isUnsubscribed: async () => false, unsubUrl: () => 'https://linkspin.app/unsubscribe' } }); return R; }; W.ev = (tx, cents, day) => ({ type: 'Purchase', buyerId: 4, tx, priceCents: cents, creditAmount: cents, ts: ct(day) }); return W; } (async () => { await t('LinkSpin switch off: nothing reported, nothing pulled, card hidden', async () => { const W = world({ lsOff: true }); const R = W.copy(); assert.strictEqual(await R.onPurchase(W.ev('0x1', 2500, '2026-11-02')), null); assert.deepStrictEqual(await R.tick(), { on: false }); assert.deepStrictEqual(await R.status('s@x.test'), { on: false }); assert.strictEqual(W.posts, 0); }); await t('InstantAdPay switch off: the report comes back on:false, nothing granted', async () => { const W = world(); W.iapCfg.refuelStreak = '0'; const R = W.copy(); const r = await R.onPurchase(W.ev('0x1', 2500, '2026-11-02')); assert.strictEqual(r.on, false); assert.strictEqual(W.lsEarned.length, 0); assert.strictEqual(fs.readdirSync(path.join(W.lsDir, 'refuel-pending')).length, 0); }); await t('a $25 LinkSpin package: month 1 +10% applied on LinkSpin, purchased-grade, logged, notice; $10 ignored', async () => { const W = world(); const R = W.copy(); assert.strictEqual(await R.onPurchase(W.ev('0x0', 1000, '2026-11-02')), null); const r = await R.onPurchase(W.ev('0x1', 2500, '2026-11-02')); assert.strictEqual(r.kind, 'refuel'); assert.deepStrictEqual(W.lsEarned, [{ email: 's@x.test', credits: 250, purchased: true }]); assert.ok(/^Refuel streak month 1: \+10% on your \$25 package$/.test(W.lsLog[0].reason)); assert.ok(/Refuel counted/.test(W.lsInbox[0].subject)); assert.strictEqual((await W.IAP.outbox('linkspin')).length, 0, 'acked'); }); await t('cross-site both engines: InstantAdPay first, LinkSpin second -> LinkSpin +15%, InstantAdPay topped up by the difference', async () => { const W = world(); const R = W.copy(); await W.IAP.record({ site: 'iap', email: 's@x.test', tx: '0xI', cents: 2000, credits: 2000, ts: ct('2026-11-02') }); await R.onPurchase(W.ev('0xL', 2500, '2026-11-08')); assert.strictEqual(W.lsEarned[0].credits, 375); assert.deepStrictEqual(W.iapEarned.map(x => [x.ref, x.credits]), [['rf:iap:0xI', 200], ['rftop:iap:0xL', 100]]); }); await t('cross-site both engines the other way: LinkSpin first, InstantAdPay second -> LinkSpin top-up arrives through the outbox pull', async () => { const W = world(); const R = W.copy(); await R.onPurchase(W.ev('0xL', 2500, '2026-11-02')); await W.IAP.record({ site: 'iap', email: 's@x.test', tx: '0xI', cents: 2000, credits: 2000, ts: ct('2026-11-04') }); assert.strictEqual(W.iapEarned[0].credits, 300); await R.tick(); assert.deepStrictEqual(W.lsEarned.map(x => x.credits), [250, 125]); // 10% then topped up to 15% of 2,500 assert.ok(/Twin Engine top-up/.test(W.lsLog[1].reason)); await R.tick(); assert.strictEqual(W.lsEarned.length, 2, 'nothing twice'); }); await t('two LinkSpin app copies see the same purchase: one report, one grant', async () => { const W = world(); const A = W.copy(), B = W.copy(); await Promise.all([A.onPurchase(W.ev('0x1', 5000, '2026-11-02')), B.onPurchase(W.ev('0x1', 5000, '2026-11-02'))]); assert.strictEqual(W.lsEarned.length, 1); assert.strictEqual(W.lsEarned[0].credits, 500); await Promise.all([A.tick(), B.tick()]); assert.strictEqual(W.lsEarned.length, 1); }); await t('InstantAdPay down: the report waits in a pending file and goes through on the next tick', async () => { const W = world(); const R = W.copy(); W.down = true; const r = await R.onPurchase(W.ev('0x1', 2500, '2026-11-02')); assert.ok(r.queued); assert.strictEqual(fs.readdirSync(path.join(W.lsDir, 'refuel-pending')).length, 1); assert.strictEqual(R.adminView().pending[0].tries, 1); W.down = false; await R.tick(); assert.strictEqual(W.lsEarned.length, 1); assert.strictEqual(fs.readdirSync(path.join(W.lsDir, 'refuel-pending')).length, 0); }); await t('a lost ack: the row comes back on the next pull and is acked without being applied twice', async () => { const W = world(); const R = W.copy(); W.dropAck = true; await R.onPurchase(W.ev('0x1', 2500, '2026-11-02')); assert.strictEqual(W.lsEarned.length, 1); assert.strictEqual((await W.IAP.outbox('linkspin')).length, 1); W.dropAck = false; await R.tick(); assert.strictEqual(W.lsEarned.length, 1); assert.strictEqual((await W.IAP.outbox('linkspin')).length, 0); }); await t('reminders for a LinkSpin-home member go out from LinkSpin (notice + email with unsubscribe), once', async () => { const W = world(); const R = W.copy(); await R.onPurchase(W.ev('0x1', 2500, '2026-11-02')); await W.IAP.tick(ct('2026-11-27', 10)); await R.tick(); await R.tick(); const rem = W.lsInbox.filter(m => /refuel day is/i.test(m.subject)); assert.strictEqual(rem.length, 1); assert.ok(W.lsMail.some(m => /refuel day is/i.test(m.subject) && /unsubscribe/.test(m.text))); }); await t('status + streak line for the Credits card and the refill notice', async () => { const W = world(); const R = W.copy(); await R.onPurchase(W.ev('0x1', 2500, '2026-11-02')); const v = await R.status('s@x.test'); assert.ok(v.on && v.active && v.streak === 1 && v.cycle.linkspin.refueled); const sl = await R.streakLine('s@x.test'); assert.ok(/^Refuel streak: (month 1|1 month)/.test(sl.text), sl.text); }); console.log('\n' + (n - fails) + '/' + n + ' passed'); process.exit(fails ? 1 : 0); })();