Files
instantadpay/qa/tank-cap.test.mjs
T

59 lines
5.3 KiB
JavaScript

// InstantAdPay holding-tank adoption cap + BTC4Hits wording (2026-11-11). No server: the real tank.js with in-memory
// accounts, a fake chain and a capturing mailer/messages, on a throwaway dir. node qa/tank-cap.test.mjs
// Marty: an adopter may hold up to 10 people from the tank at one time (not yet switched on payouts), as a setting;
// a BTC4Hits import is told the truth (their BTC4Hits account came over), never "You joined InstantAdPay without a sponsor".
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { createRequire } from 'node:module';
const SITE = path.resolve(path.dirname(new URL(import.meta.url).pathname.replace(/^\/([A-Za-z]:)/, '$1')), '..');
const require = createRequire(path.join(SITE, 'package.json'));
delete process.env.DATABASE_URL;
let fails = 0, passes = 0; const ok = (c, l, x) => { console.log((c ? 'PASS ' : 'FAIL ') + l + (x && !c ? ' ' + String(x).slice(0, 300) : '')); if (c) passes++; else fails++; };
const DAY = 86400000;
function bench(cap) {
delete require.cache[require.resolve(path.join(SITE, 'tank.js'))];
const tank = require(path.join(SITE, 'tank.js'));
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'iap-tankcap-'));
const by = new Map(); let n = 100; const pub = a => a ? Object.assign({}, a) : null; const find = f => { for (const a of by.values()) if (f(a)) return pub(a); return null; };
const accounts = { add(email, o) { by.set(email, Object.assign({ email, username: null, code: 'c' + (n++), sponsorRef: '', memberId: 0, address: null, created: Date.now() - 3 * DAY, lastSeen: Date.now() - 3600000, joinedVia: 'page', joinedRef: null }, o || {})); },
async byEmail(e) { return pub(by.get(String(e || '').toLowerCase())); }, async byUsername(u) { return find(a => a.username === String(u || '').toLowerCase()); }, async byCode(c) { return find(a => a.code === String(c || '').toLowerCase()); },
async byMemberId(id) { return find(a => Number(a.memberId) === Number(id) && id > 0); }, async setSponsorRef(e, ref) { const a = by.get(e); a.sponsorRef = String(ref || ''); return { ok: true }; },
async listAll() { return [...by.values()].map(pub); }, async sponsorOf() { return null; }, _by: by };
const events = []; const mails = []; const chats = [];
tank.init({ dataDir: dir, accounts, chain: { recentEvents: () => events.slice(), getConfig: () => ({ explorer: 'https://polygonscan.com' }) },
messages: { sendChat: async (id, from, to, text) => { chats.push({ to, text }); }, lastFrom: async () => 0 },
mailer: { hasKey: () => true, send: async (to, subject, text) => { mails.push({ to, subject, text }); } }, site: 'https://instantadpay.test',
capOpen: cap === undefined ? undefined : () => cap, hooks: {} });
accounts.add('adopter@x.co', { username: 'helper', memberId: 77, address: '0x' + '1'.repeat(40), joinedVia: null });
events.push({ type: 'Purchase', buyerId: 77, priceCents: 2000, block: 1 });
return { tank, accounts, mails, chats };
}
{
const { tank, accounts, mails, chats } = bench();
for (let i = 0; i < 12; i++) accounts.add('m' + i + '@b4h.test', i < 6 ? { joinedVia: 'btc4hits', joinedRef: 'btc4hits' } : {});
ok(tank.capOpen() === 10, 'default cap is 10');
let n = 0; for (let i = 0; i < 10; i++) { const r = await tank.adopt('adopter@x.co', 'm' + i + '@b4h.test'); if (r.ok) n++; else console.log(' adopt ' + i + ': ' + r.error); }
ok(n === 10, 'adopts 10 in a row');
const r11 = await tank.adopt('adopter@x.co', 'm10@b4h.test');
ok(r11.error && /helping 10 people/.test(r11.error) && /10 at one time is the most/.test(r11.error) && !/—/.test(r11.error), 'the 11th is refused with a clear message', JSON.stringify(r11));
const v = await tank.view('adopter@x.co'); ok(v.cap === 10 && v.eligible === false, 'tank view shows cap 10, not eligible');
accounts._by.get('m3@b4h.test').memberId = 901;
ok((await tank.adopt('adopter@x.co', 'm10@b4h.test')).ok, 'a held member switching on payouts frees a slot right away');
ok((await tank.adopt('adopter@x.co', 'm11@b4h.test')).error, 'and the cap holds again');
const imp = mails.filter(m => m.to === 'm0@b4h.test'); const reg = mails.filter(m => m.to === 'm7@b4h.test');
ok(imp.length === 1 && reg.length === 1, 'exactly one email to each adoptee');
ok(/BTC4Hits has closed, and your BTC4Hits account came over to InstantAdPay/.test(imp[0].text) && !/You joined InstantAdPay/.test(imp[0].text), 'BTC4Hits import: email says the account came over', imp[0].text);
ok(/Your BTC4Hits account moved to InstantAdPay without a sponsor/.test((chats.find(c => c.to === 'm0@b4h.test') || {}).text || ''), 'BTC4Hits import: default first chat reads right');
ok(/^You joined InstantAdPay without a sponsor/.test(reg[0].text), 'ordinary tank member keeps the old wording');
ok(!mails.some(m => /—/.test(m.text + m.subject)), 'no em dash in any adoption email');
}
{
const { tank, accounts } = bench(3);
for (let i = 0; i < 4; i++) accounts.add('s' + i + '@b4h.test');
for (let i = 0; i < 3; i++) await tank.adopt('adopter@x.co', 's' + i + '@b4h.test');
const r = await tank.adopt('adopter@x.co', 's3@b4h.test'); ok(r.error && /3 at one time/.test(r.error), 'tankAdoptCap setting is respected (3)', JSON.stringify(r));
ok(bench('junk').tank.capOpen() === 10, 'a bad setting falls back to 10');
}
console.log('\n' + passes + ' passed, ' + fails + ' failed'); process.exit(fails ? 1 : 0);