// Refill notice copy + deploy-overlap claim (refill.js) on throwaway data. Run: node qa/refill.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); } } function fresh(mod) { const p = require.resolve(path.join(ROOT, mod)); delete require.cache[p]; return require(p); } const now = Date.now(); function world(dir, cfg) { const W = { inbox: [], mail: [], cfg: cfg || {} }; W.campaigns = [ { id: 1, owner: 'a@x.test', type: 'banner', name: 'Alpha', budget: 1000, spent: 1000, accrued: 0, imps: 5000, clicks: 40, status: 'out', created: now - 30 * 864e5 }, { id: 2, owner: 'b@x.test', type: 'text', name: 'Bravo', budget: 1000, spent: 850, accrued: 0, imps: 8500, clicks: 9, status: 'active', created: now - 30 * 864e5 }, ]; W.deps = { dataDir: dir, siteConfig: () => W.cfg, ads: { adminList: async () => W.campaigns.map(c => Object.assign({}, c)), rates: () => ({ videoTiers: [] }) }, accounts: { byEmail: async e => ({ email: e, memberId: 5 }), positions: async () => [] }, chain: { recentEvents: () => [] }, mailer: { hasKey: () => true, send: async (to, subject, text) => { W.mail.push({ to, subject, text }); } }, drip: { isUnsubscribed: async () => false }, creditsFor: async () => 300, message: async (to, subject, html) => { W.inbox.push({ to, subject, html }); }, }; return W; } (async () => { await t('bonus block names Five Dollar Friday AND Wired Wednesday ($20 floor, +25%)', async () => { const W = world(fs.mkdtempSync(path.join(os.tmpdir(), 'iap-refill-'))); const r = fresh('refill.js'); r.init(W.deps); const m = await r.compose('a@x.test', [W.campaigns[0]], 'out', 300); assert.ok(/Five Dollar Friday: any package from \$5 up/.test(m.text), 'friday text'); assert.ok(/Wired Wednesday: any package from \$20 up, bought on a Wednesday \(Central time\), earns an extra 25%/.test(m.text), 'wednesday text'); assert.ok(/Wired Wednesday:<\/b>/.test(m.html), 'wednesday html'); assert.ok(!/—/.test(m.text + m.html), 'no em dash'); }); await t('Wednesday follows its settings (off, other floor and pct)', async () => { const W = world(fs.mkdtempSync(path.join(os.tmpdir(), 'iap-refill-')), { wednesdayPromo: '0' }); const r = fresh('refill.js'); r.init(W.deps); let m = await r.compose('a@x.test', [W.campaigns[0]], 'out', 300); assert.ok(!/Wired Wednesday/.test(m.text)); W.cfg = { wednesdayBonusPct: '30', wednesdayMinCents: '5000', fridayPromo: '0' }; m = await r.compose('a@x.test', [W.campaigns[0]], 'out', 300); assert.ok(/from \$50 up.*extra 30%/.test(m.text) && !/Five Dollar Friday/.test(m.text)); }); await t('streak hook line (stage 2) lands in text and html; absent hook changes nothing', async () => { const W = world(fs.mkdtempSync(path.join(os.tmpdir(), 'iap-refill-'))); const r = fresh('refill.js'); r.init(Object.assign({}, W.deps, { streakLine: async e => e === 'a@x.test' ? { text: 'Refuel streak: 2 months.', html: 'Refuel streak: 2 months.' } : null })); const m = await r.compose('a@x.test', [W.campaigns[0]], 'out', 300); assert.ok(/Refuel streak: 2 months\./.test(m.text) && /Refuel streak:<\/b>/.test(m.html)); const m2 = await r.compose('b@x.test', [W.campaigns[1]], 'low', 300); assert.ok(!/Refuel streak/.test(m2.text)); }); await t('two app copies (shared claims): each owner gets one message', async () => { const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'iap-refill-')); const W = world(dir); const marks = fresh('marks.js'); marks.init({}); // no DB: one process-wide set stands in for event_marks const A = fresh('refill.js'); A.init(Object.assign({}, W.deps, { marks })); const B = fresh('refill.js'); B.init(Object.assign({}, W.deps, { marks })); await Promise.all([A.tick(), B.tick()]); const per = {}; for (const x of W.inbox) per[x.to] = (per[x.to] || 0) + 1; assert.deepStrictEqual(per, { 'a@x.test': 1, 'b@x.test': 1 }, JSON.stringify(per)); assert.strictEqual(W.mail.length, 2); await Promise.all([A.tick(), B.tick()]); assert.strictEqual(W.inbox.length, 2, 'nothing repeats on later passes'); }); console.log('\n' + (n - fails) + '/' + n + ' passed'); process.exit(fails ? 1 : 0); })();