Refill notices on LinkSpin (Fuel Your Offers stage 1, Marty approved 2026-10-24): refill.js copied from InstantAdPay. Low (80% of budget delivered, refillLowPct), out, and idle (7 days after an unanswered out notice, 100+ credits free, nothing running): one email + one on-site notice per owner per pass, naming each ad and what it delivered, then the relaunch line (100+ credits free) or the /credits link, then the packages from the chain catalog and a streak-line hook for stage 2. Email skips drip unsubscribes and carries the unsubscribe link; the notice (kind notice) always goes. Every send is watched 45 days for purchase / relaunch / resumed / topup. marks.js: one-time claims shared by both app copies during a deploy (event_marks with a DB, exclusive file create on the shared volume without one); every send is claimed first. Settings refillNotices '1' (default on) + refillLowPct 80; admin GET /api/admin/refill, POST /run and /reset, an Overview card (admin-refill.js). qa/refill.test.cjs (12 checks incl. two app copies on one volume) + run.sh fuel mode.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
martbost
2026-10-24 05:11:12 -05:00
parent 7b3844b757
commit ab4f1693df
7 changed files with 523 additions and 1 deletions
+123
View File
@@ -0,0 +1,123 @@
// Refill notices (refill.js) on throwaway data: stubbed ads/accounts/chain/mail, a temp DATA_DIR, file marks.
// 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, '..');
const DIR = fs.mkdtempSync(path.join(os.tmpdir(), 'ls-refill-'));
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() {
const W = {
campaigns: [
{ id: 1, owner: 'a@x.test', type: 'banner', name: 'Alpha banner', budget: 1000, spent: 850, imps: 4000, clicks: 30, status: 'active', created: now - 20 * 864e5 },
{ id: 2, owner: 'a@x.test', type: 'text', name: 'Alpha text', budget: 500, spent: 500, imps: 5000, clicks: 12, status: 'out', created: now - 20 * 864e5 },
{ id: 3, owner: 'b@x.test', type: 'banner', name: 'Bravo', budget: 2500, spent: 2500, imps: 12000, clicks: 80, status: 'out', created: now - 20 * 864e5 },
{ id: 4, owner: 'house@linkspin', house: true, type: 'banner', name: 'House', budget: 100, spent: 100, status: 'out', created: now },
{ id: 5, owner: 'c@x.test', type: 'banner', name: 'Charlie', budget: 1000, spent: 100, status: 'active', created: now },
],
credits: { 'a@x.test': 40, 'b@x.test': 1200, 'c@x.test': 0 },
unsub: new Set(['b@x.test']),
inbox: [], mail: [], events: [],
};
W.deps = {
dataDir: DIR, siteUrl: 'https://linkspin.app', houseOwner: 'house@linkspin',
siteConfig: () => W.cfg || {},
ads: { adminList: async () => W.campaigns.map(c => Object.assign({}, c)) },
accounts: { byEmail: async e => ({ email: e, memberId: { 'a@x.test': 11, 'b@x.test': 12, 'c@x.test': 13 }[e] || 0 }), positions: async () => [] },
chain: { recentEvents: () => W.events, catalog: async () => [{ priceCents: 1000, creditAmount: 1000 }, { priceCents: 2500, creditAmount: 2500 }] },
mailer: { hasKey: () => true, send: async (to, subject, text) => { W.mail.push({ to, subject, text }); } },
drip: { isUnsubscribed: async e => W.unsub.has(e), unsubUrl: e => 'https://linkspin.app/unsubscribe?e=' + e },
creditsFor: async e => W.credits[e] || 0,
message: async (to, subject, html) => { W.inbox.push({ to, subject, html }); },
};
return W;
}
(async () => {
const marks = fresh('marks.js'); marks.init({ dataDir: DIR });
let W = world();
const refill = fresh('refill.js');
refill.init(Object.assign({ marks }, W.deps));
await t('one message per owner per pass; out beats low; house and healthy ads skipped', async () => {
const r = await refill.tick();
assert.strictEqual(r.sent, 2);
assert.deepStrictEqual(W.inbox.map(x => x.to).sort(), ['a@x.test', 'b@x.test']);
const a = W.inbox.find(x => x.to === 'a@x.test');
assert.ok(/finished delivering/.test(a.subject), a.subject);
assert.ok(/Alpha text/.test(a.html) && !/Alpha banner/.test(a.html), 'only the out campaign in the out notice');
});
await t('email respects unsubscribes; the on-site notice still goes', async () => {
assert.ok(W.inbox.some(x => x.to === 'b@x.test'));
assert.ok(!W.mail.some(x => x.to === 'b@x.test'));
const m = W.mail.find(x => x.to === 'a@x.test');
assert.ok(m && /Stop these emails/.test(m.text));
});
await t('relaunch line with 100+ credits, buy link below', async () => {
const b = W.inbox.find(x => x.to === 'b@x.test'); assert.ok(/without buying anything/.test(b.html) && /linkspin\.app\/ads/.test(b.html));
const a = W.mail.find(x => x.to === 'a@x.test'); assert.ok(/needs a package first/.test(a.text) && /linkspin\.app\/credits/.test(a.text));
});
await t('campaign lines say what each delivered; packages from the catalog', async () => {
const a = W.mail.find(x => x.to === 'a@x.test');
assert.ok(/"Alpha text" \(text\): 500 of 500 credits delivered, 5,000 impressions, 12 clicks/.test(a.text), a.text);
assert.ok(/\$25 {2}-> {2}2,500 credits/.test(a.text));
});
await t('no em dashes, no income promises', async () => {
for (const m of W.mail.concat(W.inbox)) { const s = (m.text || m.html) + m.subject; assert.ok(!/—/.test(s), 'em dash'); assert.ok(!/(earn|make) (money|\$)|guarantee/i.test(s), 'income claim'); }
});
await t('next pass: the low notice for the other ad, then nothing', async () => {
const r = await refill.tick(); assert.strictEqual(r.sent, 1);
assert.ok(/nearly out/.test(W.inbox[W.inbox.length - 1].subject));
const r2 = await refill.tick(); assert.strictEqual(r2.sent, 0);
});
await t('settings switch off: nothing sent', async () => {
W.cfg = { refillNotices: '0' }; W.campaigns.push({ id: 6, owner: 'c@x.test', type: 'banner', name: 'Charlie 2', budget: 100, spent: 100, status: 'out', created: now });
const r = await refill.tick(); assert.strictEqual(r.sent, 0); W.cfg = {};
});
await t('conversion: a purchase after the send', async () => {
W.events.push({ type: 'Purchase', buyerId: 12, priceCents: 2500, tx: '0xbuy', ts: Date.now() + 1000 });
await refill.tick();
const s = refill.stats(); const b = s.recent.find(x => x.owner === 'b@x.test');
assert.ok(b.converted && b.converted.kind === 'purchase', JSON.stringify(b));
assert.strictEqual(s.totals.purchasesUsd, 25);
});
await t('idle follow-up after 7 days, once, only with 100+ credits and nothing running', async () => {
// a's out record: make it 8 days old, give a credits, stop a's running ad
const f = path.join(DIR, 'refill.json'); const S = JSON.parse(fs.readFileSync(f, 'utf8'));
for (const r of Object.values(S.records)) if (r.owner === 'a@x.test' && r.state === 'out') r.ts = Date.now() - 8 * 864e5;
fs.writeFileSync(f, JSON.stringify(S));
W.credits['a@x.test'] = 700; W.campaigns.find(c => c.id === 1).status = 'paused';
const before = W.inbox.length; await refill.tick();
const idle = W.inbox.slice(before).filter(x => x.to === 'a@x.test');
assert.strictEqual(idle.length, 1); assert.ok(/700 credits are sitting idle/.test(idle[0].subject));
const b2 = W.inbox.length; await refill.tick(); assert.strictEqual(W.inbox.length, b2);
});
await t('admin stats', async () => {
const s = refill.stats(); assert.ok(s.enabled); assert.ok(s.byState.idle.sent === 1); assert.ok(s.totals.sent >= 4);
});
await t('streak hook line lands in the bonus block', async () => {
const r2 = fresh('refill.js'); r2.init(Object.assign({}, W.deps, { streakLine: async () => ({ text: 'Refuel streak: month 2.', html: '<b>Refuel streak</b>: month 2.' }) }));
const m = await r2.compose('a@x.test', [W.campaigns[0]], 'low', 50);
assert.ok(/Refuel streak: month 2\./.test(m.text) && /Refuel streak<\/b>/.test(m.html));
});
await t('two app copies on one volume: one message, not two', async () => {
const D2 = fs.mkdtempSync(path.join(os.tmpdir(), 'ls-refill2-'));
const W2 = world(); W2.deps.dataDir = D2;
const mk = () => { const m = fresh('marks.js'); m.init({ dataDir: D2 }); const r = fresh('refill.js'); r.init(Object.assign({}, W2.deps, { marks: m })); return r; };
const A = mk(), B = mk();
await Promise.all([A.tick(), B.tick()]);
const per = {}; for (const x of W2.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(W2.mail.length, 1);
});
console.log('\n' + (n - fails) + '/' + n + ' passed');
try { fs.rmSync(DIR, { recursive: true, force: true }); } catch (e) {}
process.exit(fails ? 1 : 0);
})();