563407e45b
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
134 lines
10 KiB
JavaScript
134 lines
10 KiB
JavaScript
// Sponsor autopilot (sponsorpilot.js, Marty 2026-11-25). No server: the real module with in-memory accounts, messages and mail
|
|
// on a throwaway dir, mocked clock. node qa/sponsor-pilot.test.mjs
|
|
// Covers: switch off = nothing sent or written; only free directs of the named sponsor; first touch after 2 days, later ones
|
|
// every 6, four at most, a different message each time; pace (one per tick, gap, day cap, 8 AM to 8 PM Central); a hand-written
|
|
// sponsor message postpones the next touch; never-mail people skipped; one send across two app copies (claim); a reply stops
|
|
// the automatics and pings once; the copy has no em dashes and no income promise.
|
|
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 !== undefined && !c ? ' ' + String(typeof x === 'string' ? x : JSON.stringify(x)).slice(0, 400) : '')); if (c) passes++; else fails++; };
|
|
const DAY = 86400000, H = 3600000, MIN = 60000;
|
|
const P = require(path.join(SITE, 'sponsorpilot.js'));
|
|
|
|
// noon Central on a fixed day (CST = UTC-6)
|
|
let now = Date.UTC(2026, 10, 25, 18, 0, 0);
|
|
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'pilot-'));
|
|
const acc = new Map(); const add = a => { acc.set(a.email, Object.assign({ created: now - 30 * DAY, lastSeen: 0, memberId: 0, address: '', sponsorRef: '', username: a.email.split('@')[0] }, a)); };
|
|
add({ email: 'marty@x.test', username: 'martbost', memberId: 1, address: '0xabc' });
|
|
add({ email: 'other@x.test', username: 'othersp', memberId: 9, address: '0xdef' });
|
|
add({ email: 'a@x.test', sponsorRef: 'martbost', created: now - 12 * DAY }); // most overdue
|
|
add({ email: 'b@x.test', sponsorRef: 'martbost', created: now - 5 * DAY });
|
|
add({ email: 'c@x.test', sponsorRef: 'martbost', created: now - 1 * DAY }); // too new (first touch after 2 days)
|
|
add({ email: 'w@x.test', sponsorRef: 'martbost', address: '0x111' }); // wallet linked: not at risk
|
|
add({ email: 'm@x.test', sponsorRef: 'martbost', memberId: 44 }); // member: not at risk
|
|
add({ email: 'u@x.test', sponsorRef: 'martbost', created: now - 9 * DAY }); // unsubscribed
|
|
add({ email: 'f@x.test', sponsorRef: 'martbost', created: now - 9 * DAY }); // flagged
|
|
add({ email: 'q@x.test', sponsorRef: 'martbost', created: now - 9 * DAY, mutes: ['marty@x.test'] });
|
|
add({ email: 'z@x.test', sponsorRef: 'othersp', created: now - 12 * DAY }); // someone else's direct
|
|
const chats = []; const mails = []; const pings = []; const claimed = new Set(); let site = { pilotOn: '0', pilotSponsors: 'martbost' };
|
|
const accounts = {
|
|
byEmail: async e => acc.get(String(e).toLowerCase()) || null,
|
|
byUsername: async u => [...acc.values()].find(a => a.username === u) || null,
|
|
downline: async e => { const sp = acc.get(e); return [{ members: [...acc.values()].filter(a => a.sponsorRef === sp.username) }]; },
|
|
getMutes: async e => (acc.get(e) || {}).mutes || []
|
|
};
|
|
const messages = {
|
|
sendChat: async (m, from, to, body) => { chats.push({ from, to, body, sent: now }); return { id: chats.length, sent: now }; },
|
|
lastFrom: async (from, to) => chats.filter(c => c.from === from && c.to === to).reduce((mx, c) => Math.max(mx, c.sent), 0)
|
|
};
|
|
const mailer = { hasKey: () => true, send: async (to, subject, body) => { mails.push({ to, subject, body }); return true; } };
|
|
const noon = () => { now = now - (now % DAY) + 18 * H + DAY; }; // next day, noon Central: keeps every block inside the sending hours
|
|
const boot = () => P.init({ dataDir: dir, db: { enabled: () => false }, accounts, messages, mailer, siteConfig: () => site, clock: () => now,
|
|
brand: { name: 'InstantAdPay', url: 'https://instantadpay.com/my' }, online: () => false,
|
|
hooks: { excluded: em => em === 'f@x.test', excludedPerson: async em => (em === 'u@x.test' ? 'unsubscribed' : null),
|
|
claim: async k => { if (claimed.has(k)) return false; claimed.add(k); return true; }, notify: async t => { pings.push(t); } } });
|
|
await boot();
|
|
|
|
// ---- copy ----
|
|
{
|
|
const all = P.TOUCHES.map((_, i) => P.render(i, { username: 'sam' })).join('\n');
|
|
ok(P.TOUCHES.length === 4 && new Set(P.TOUCHES).size === 4, 'four different messages');
|
|
ok(!/—|--/.test(all), 'no em dashes in the copy');
|
|
ok(!/guarantee|you will earn|you will make|passive income|\$\d/i.test(all), 'no income promise or dollar figure in the copy');
|
|
ok(/Hi sam,/.test(all) && !/\{name\}|\{site\}|\{url\}/.test(all), 'placeholders filled');
|
|
ok(/Hi there,/.test(P.render(0, {})), 'no username: "Hi there"');
|
|
}
|
|
// ---- switch off ----
|
|
{
|
|
const r = await P.tick();
|
|
ok(r.off && chats.length === 0 && mails.length === 0 && !fs.existsSync(path.join(dir, 'sponsor-pilot.json')), 'switch off: nothing sent, nothing written', r);
|
|
const v = await P.view();
|
|
ok(v.sponsors.length === 1 && v.sponsors[0].stats.free === 6 && v.sponsors[0].stats.due === 5, 'preview while off: 6 free directs, 5 past the 2 days (never-mail people are only dropped at send time)', v.sponsors[0]);
|
|
ok(!JSON.stringify(v).includes('@x.test'), 'preview prints no email address');
|
|
}
|
|
// ---- on: order, pace, exclusions ----
|
|
site.pilotOn = '1';
|
|
{
|
|
let r = await P.tick();
|
|
ok(r.sent === 1 && chats.length === 1 && chats[0].to === 'a@x.test' && chats[0].from === 'marty@x.test', 'first send goes to the most overdue direct, as the sponsor', chats[0]);
|
|
ok(mails.length === 1 && mails[0].to === 'a@x.test' && /New message from @martbost/.test(mails[0].subject) && /Reply in your dashboard/.test(mails[0].body), 'the email goes with it', mails[0]);
|
|
r = await P.tick();
|
|
ok(r.sent === 0 && chats.length === 1, 'pace: nothing more inside the gap');
|
|
ok(P._gapMs(P.cfg()) === 12 * MIN, '60 a day over 12 hours = one every 12 minutes', P._gapMs(P.cfg()));
|
|
now += 13 * MIN; r = await P.tick();
|
|
ok(r.sent === 1 && chats[1].to === 'b@x.test', 'next tick after the gap: the next most overdue (never-mail people skipped on the way)', [r, chats[1] && chats[1].to]);
|
|
now += 13 * MIN; r = await P.tick();
|
|
ok(r.sent === 0 && chats.length === 2, 'c joined yesterday, the wallet and member directs, the unsubscribed, flagged and muted ones and the other sponsor\'s direct get nothing', chats.map(c => c.to));
|
|
}
|
|
// ---- two app copies: one send ----
|
|
{
|
|
add({ email: 'd@x.test', sponsorRef: 'martbost', created: now - 4 * DAY });
|
|
now += 31 * MIN; claimed.add('pilot:d@x.test:0'); // the other copy already took this touch
|
|
const r = await P.tick();
|
|
ok(r.sent === 0 && !chats.some(c => c.to === 'd@x.test'), 'a touch another copy claimed is not sent twice', r);
|
|
claimed.delete('pilot:d@x.test:0'); acc.get('d@x.test').address = '0x9'; // and now they linked a wallet: out of scope
|
|
}
|
|
// ---- cadence: later touches every 6 days, a new message each time, four at most ----
|
|
{
|
|
now += 5 * DAY - 2 * H; const a0 = chats.filter(c => c.to === 'a@x.test').length; for (let i = 0; i < 4; i++) { await P.tick(); now += 13 * MIN; }
|
|
ok(chats.filter(c => c.to === 'a@x.test').length === a0, 'day 5 after the first touch: nothing more for a yet');
|
|
const sentTo = async who => { noon(); const n0 = chats.filter(c => c.to === who).length; for (let i = 0; i < 6; i++) { await P.tick(); now += 13 * MIN; } return chats.filter(c => c.to === who).length - n0; };
|
|
now += 1 * DAY; ok(await sentTo('a@x.test') === 1, 'day 6: second touch to a');
|
|
const bodies = () => chats.filter(c => c.to === 'a@x.test').map(c => c.body);
|
|
ok(bodies()[1] !== bodies()[0] && /link a wallet/.test(bodies()[1]), 'the second message is the wallet one');
|
|
now += 6 * DAY; await sentTo('a@x.test'); now += 6 * DAY; await sentTo('a@x.test'); now += 6 * DAY; await sentTo('a@x.test'); now += 6 * DAY; await sentTo('a@x.test');
|
|
ok(bodies().length === 4 && new Set(bodies()).size === 4, 'four touches, four different messages, then it stops', bodies().length);
|
|
const v = await P.view(); ok(v.sponsors[0].stats.finished >= 1, 'the preview counts them as finished');
|
|
}
|
|
// ---- a hand-written sponsor message postpones the automatic one ----
|
|
{
|
|
add({ email: 'h@x.test', sponsorRef: 'martbost', created: now - 20 * DAY });
|
|
noon(); chats.push({ from: 'marty@x.test', to: 'h@x.test', body: 'hand written', sent: now - 1 * DAY });
|
|
now += 31 * MIN; for (let i = 0; i < 4; i++) { await P.tick(); now += 13 * MIN; }
|
|
ok(chats.filter(c => c.to === 'h@x.test').length === 1, 'sponsor wrote by hand yesterday: no automatic message on top');
|
|
}
|
|
// ---- a reply stops the automatics and pings once ----
|
|
{
|
|
noon(); add({ email: 'r@x.test', sponsorRef: 'martbost', created: now - 4 * DAY });
|
|
now += 31 * MIN; for (let i = 0; i < 4; i++) { await P.tick(); now += 13 * MIN; }
|
|
ok(chats.filter(c => c.to === 'r@x.test').length === 1, 'r got the first touch');
|
|
ok(await P.onChat('r@x.test', 'marty@x.test', 'How do I link a wallet?') === true && pings.length === 1 && /@r answered/.test(pings[0]) && !/x\.test/.test(pings[0]), 'reply: recorded, owner pinged once, no email in the ping', pings);
|
|
ok(await P.onChat('r@x.test', 'marty@x.test', 'hello?') === false && pings.length === 1, 'a second reply does not ping again');
|
|
ok(await P.onChat('z@x.test', 'other@x.test', 'hi') === false && await P.onChat('never-touched@x.test', 'marty@x.test', 'hi') === false, 'other sponsors and untouched people are ignored');
|
|
now += 7 * DAY; noon(); for (let i = 0; i < 4; i++) { await P.tick(); now += 13 * MIN; }
|
|
ok(chats.filter(c => c.to === 'r@x.test').length === 1, 'no automatic message after they answered');
|
|
}
|
|
// ---- hours and the day cap ----
|
|
{
|
|
add({ email: 'n@x.test', sponsorRef: 'martbost', created: now - 4 * DAY });
|
|
const saved = now; now = Date.UTC(2027, 0, 20, 9, 0, 0); // 3 AM Central
|
|
ok((await P.tick()).sent === 0 && !chats.some(c => c.to === 'n@x.test'), 'outside 8 AM to 8 PM Central: nothing');
|
|
now = Date.UTC(2027, 0, 20, 18, 0, 0); site.pilotPerDay = 1;
|
|
for (const e of ['n2@x.test', 'n3@x.test']) add({ email: e, sponsorRef: 'martbost', created: now - 4 * DAY });
|
|
let sent = 0; for (let i = 0; i < 5; i++) { sent += (await P.tick()).sent || 0; now += 3 * H / 5; }
|
|
ok(sent === 1, 'day cap 1: one message that Central day', sent);
|
|
now = saved; delete site.pilotPerDay;
|
|
}
|
|
console.log('\n' + passes + ' passed, ' + fails + ' failed');
|
|
process.exit(fails ? 1 : 0);
|