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

62 lines
5.9 KiB
JavaScript

// 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 moved here), never "You joined LinkSpin 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(), 'ls-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: 0, joinedVia: 'carried', joinedRef: 'network' }, 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://linkspin.test',
capOpen: cap === undefined ? undefined : () => cap, hooks: {} });
accounts.add('adopter@x.co', { username: 'helper', memberId: 77, address: '0x' + '1'.repeat(40), lastSeen: Date.now() - 3600000, joinedVia: null, joinedRef: null });
events.push({ type: 'Purchase', buyerId: 77, priceCents: 2500, block: 1 });
return { tank, accounts, mails, chats };
}
{ // default cap 10, measured as people held who have not switched on payouts
const B = bench(); const { tank, accounts, mails, chats } = B;
for (let i = 0; i < 12; i++) accounts.add('m' + i + '@b4h.test', { joinedRef: i < 6 ? 'btc4hits' : 'network' });
ok(tank.capOpen() === 10, 'default cap is 10');
let okCount = 0; for (let i = 0; i < 10; i++) { const r = await tank.adopt('adopter@x.co', 'm' + i + '@b4h.test'); if (r.ok) okCount++; else console.log(' adopt ' + i + ': ' + r.error); }
ok(okCount === 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 && /10 at one time/.test(v.reason), 'tank view shows cap 10 and the reason');
accounts._by.get('m3@b4h.test').memberId = 901; // one of them switched on payouts: the slot frees at once
const r11b = await tank.adopt('adopter@x.co', 'm10@b4h.test'); ok(r11b.ok, 'a held member switching on payouts frees a slot right away', JSON.stringify(r11b));
ok((await tank.adopt('adopter@x.co', 'm11@b4h.test')).error, 'and the cap holds again at 10 not yet switched on');
// wording
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', JSON.stringify(mails.map(m => m.to)));
ok(/BTC4Hits has moved to LinkSpin, and your BTC4Hits account came with you/.test(imp[0].text) && /\/from\/btc4hits/.test(imp[0].text) && !/You joined LinkSpin/.test(imp[0].text), 'BTC4Hits import: email says the account moved, points to /from/btc4hits', imp[0].text);
ok(/Your BTC4Hits account moved to LinkSpin without a sponsor/.test((chats.find(c => c.to === 'm0@b4h.test') || {}).text || ''), 'BTC4Hits import: the default first chat reads right too');
ok(/^You joined LinkSpin without a sponsor/.test(reg[0].text) && /\/my#messages/.test(reg[0].text), 'ordinary tank member keeps the old wording');
ok(imp[0].subject === '@helper is now your sponsor on LinkSpin', 'subject unchanged', imp[0].subject);
ok(!mails.some(m => /—/.test(m.text + m.subject)), 'no em dash in any adoption email');
ok(!mails.some(m => m.to === 'adopter@x.co'), 'no email to the adopter on a hand adoption');
}
{ // the setting
const B = bench(3); const { tank, accounts } = B;
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));
const B2 = bench('junk'); ok(B2.tank.capOpen() === 10, 'a bad setting falls back to 10');
}
console.log('\n' + passes + ' passed, ' + fails + ' failed'); process.exit(fails ? 1 : 0);