Files
instantadpay/qa/refuel-kit.test.cjs
martbost 50337bfbbc Refuel Kit (Fuel Your Offers stage 3, Marty approved 2026-10-24): monthly members-only banner sets for InstantAdPay, unlocked by the member's next counted refuel. kit.js (the same file ships in LinkSpin): kits as data in DATA_DIR/kits (month, theme, released, one base image each for 728x90 300x250 468x60 160x600 1200x630, this site's own program only); a counted refuel unlocks the kit of its Central month (refuel.js now keeps rec.kits month -> streak month, exposed in view.kits); kits stay usable after their month, a missed refuel only stops new ones, a kit released early waits for its month. Server-side personalization per member with ffmpeg (badge renderer approach): @username on a dark strip + a QR code (qrcode package) to their own /join link at fixed spots per size; cached in DATA_DIR/kits/out and served publicly at /kitimg/<hmac>.png|jpg|gif (only files rendered for entitled members; names cannot be guessed and change with the art). Streak month 3+ adds an animated GIF (three-frame loop), month 6+ records a BannerKitAI custom-banners credit (entitlement + note, no integration). Member: Promo tools Refuel Kit card (fuel.js?v=20261024kit): Make mine, Animated, Download, Copy image link, "Use in an IAP banner campaign" (opens Campaigns and prefills banner type, size, name, image URL and the member's link). Admin: Settings > Refuel Kit (admin-kits.js): month + theme, upload the five sizes, "Use placeholder art (public/banners)", preview with any username (still or animated), release (needs all five sizes). Behind the refuelStreak switch (myKits { on:false } while off). Chatbot CANNED + FACTS mention the kit. No paid image APIs; placeholder art = existing public/banners. qa/refuel-kit.test.cjs 8 checks (admin validation, switch, unlock by month, Pit Stop does not unlock, kits persist, GIF at 3, credit at 6, exact render sizes for all five, GIF loop, cache, unguessable serving, preview).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-24 05:44:27 -05:00

114 lines
8.8 KiB
JavaScript

// Refuel Kit (kit.js) with the real refuel.js entitlement, throwaway data, placeholder art from public/banners, real ffmpeg.
// Run: node qa/refuel-kit.test.cjs (skips the render checks when ffmpeg or a font is missing)
'use strict';
const fs = require('fs');
const os = require('os');
const path = require('path');
const assert = require('assert');
const { execSync } = require('child_process');
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.stack.split('\n').slice(0, 2).join(' | ')); } }
function fresh(mod) { const p = require.resolve(path.join(ROOT, mod)); delete require.cache[p]; return require(p); }
const ct = (day, hour) => { const [y, m, d] = day.split('-').map(Number); return Date.UTC(y, m - 1, d, (hour == null ? 12 : hour) + 6); };
let ffmpegOk = true; try { execSync('ffmpeg -version', { stdio: 'ignore' }); } catch (e) { ffmpegOk = false; }
function dims(buf) {
if (buf[0] === 0x89) return [buf.readUInt32BE(16), buf.readUInt32BE(20)];
if (buf.slice(0, 3).toString() === 'GIF') return [buf.readUInt16LE(6), buf.readUInt16LE(8)];
for (let i = 2; i < buf.length - 9;) { if (buf[i] !== 0xff) { i++; continue; } const m = buf[i + 1]; const len = buf.readUInt16BE(i + 2); if (m >= 0xc0 && m <= 0xc3) return [buf.readUInt16BE(i + 7), buf.readUInt16BE(i + 5)]; i += 2 + len; }
return null;
}
function world(cfg) {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'iap-kit-'));
const W = { dir, cfg: Object.assign({ refuelStreak: '1' }, cfg || {}) };
const marks = fresh('marks.js'); marks.init({});
W.R = fresh('refuel.js');
W.R.init({ dataDir: dir, db: require(path.join(ROOT, 'db.js')), marks, siteConfig: () => W.cfg, accounts: { byMemberId: async () => null, byEmail: async e => ({ email: e }) },
ads: { addEarned: async () => {} }, message: async () => {}, featuredPerDay: () => 40 });
W.K = fresh('kit.js');
W.K.init({ dataDir: dir, program: 'iap', publicDir: path.join(ROOT, 'public'), siteUrl: 'https://instantadpay.com',
entitled: async email => { const v = await W.R.view(email, W.now); return v && v.on ? { on: true, months: v.kits || {}, nextRefuel: v.refuelDayLabel, today: v.today } : { on: false }; },
linkFor: async email => email === 'nouser@x.test' ? null : { username: 'mike', link: 'https://instantadpay.com/join/mike' } });
W.buy = (tx, day, cents) => W.R.record({ site: 'iap', email: 'm@x.test', tx, cents: cents || 2000, credits: cents || 2000, ts: ct(day) });
return W;
}
(async () => {
await t('admin: month format, sizes, file type; release needs all five sizes; placeholder art fills all five', async () => {
const W = world();
assert.ok(W.K.saveKit({ month: '2026-13' }).error);
assert.ok(W.K.saveKit({ month: '2026-11', theme: 'Harvest' }).ok);
assert.ok(W.K.uploadBase('2026-11', '999x1', Buffer.alloc(64), 'image/png').error);
assert.ok(W.K.uploadBase('2026-11', '728x90', Buffer.from('not an image at all, honestly, no...........'), 'image/png').error);
assert.ok(/all five sizes/.test(W.K.saveKit({ month: '2026-11', released: true }).error));
const r = W.K.usePlaceholder('2026-11'); assert.deepStrictEqual(r.sizes, W.K.ORDER);
assert.ok(W.K.saveKit({ month: '2026-11', released: true }).ok);
const k = W.K.listKits()[0]; assert.ok(k.released && k.complete && k.theme === 'Harvest');
});
await t('switch off: no kits for anyone', async () => {
const W = world({ refuelStreak: '0' }); W.K.usePlaceholder('2026-11'); W.K.saveKit({ month: '2026-11', released: true });
assert.deepStrictEqual(await W.K.myKits('m@x.test'), { on: false });
assert.ok((await W.K.renderFor('m@x.test', '2026-11', '728x90')).error);
});
await t('locked until the next counted refuel; unlocked by a refuel in that Central month; drafts never shown', async () => {
const W = world(); W.K.usePlaceholder('2026-11'); W.K.saveKit({ month: '2026-11', released: true }); W.K.usePlaceholder('2026-12');
W.now = ct('2026-11-03');
let mk = await W.K.myKits('m@x.test'); assert.strictEqual(mk.kits.length, 1); assert.strictEqual(mk.kits[0].unlocked, false);
assert.ok(/next counted refuel/.test((await W.K.renderFor('m@x.test', '2026-11', '728x90')).error));
await W.buy('0x1', '2026-11-03');
mk = await W.K.myKits('m@x.test'); assert.ok(mk.kits[0].unlocked); assert.strictEqual(mk.kits[0].gif, false); assert.strictEqual(mk.link, 'https://instantadpay.com/join/mike');
});
await t('a Pit Stop does not unlock a kit; the next counted refuel does; kits stay usable after the month; a missed month stops only new kits', async () => {
const W = world();
for (const m of ['2026-11', '2026-12', '2027-02']) { W.K.usePlaceholder(m); W.K.saveKit({ month: m, released: true }); }
await W.buy('0x1', '2026-11-20'); // unlocks November
await W.buy('0x2', '2026-12-05'); // a Pit Stop: not a refuel, December stays locked
W.now = ct('2026-12-06'); let mk = await W.K.myKits('m@x.test');
assert.deepStrictEqual(mk.kits.map(k => [k.month, k.unlocked]), [['2026-12', false], ['2026-11', true]], // February is released early but waits for its month
JSON.stringify(mk.kits.map(k => [k.month, k.unlocked])));
await W.buy('0x3', '2026-12-17'); // counted refuel in December: unlocks December
W.now = ct('2027-02-10'); mk = await W.K.myKits('m@x.test'); // January missed (free miss), February not refueled yet
assert.deepStrictEqual(mk.kits.map(k => [k.month, k.unlocked]), [['2027-02', false], ['2026-12', true], ['2026-11', true]], JSON.stringify(mk.kits.map(k => [k.month, k.unlocked])));
});
await t('animated version from a 3-month streak; BannerKitAI custom-banner credit from 6 months (entitlement + note only)', async () => {
const W = world(); let day = '2026-06-02';
const months = [];
for (let k = 1; k <= 6; k++) { const m = day.slice(0, 7); months.push(m); W.K.usePlaceholder(m); W.K.saveKit({ month: m, released: true }); await W.buy('t' + k, day); day = new Date(ct(day) + 30 * 864e5).toISOString().slice(0, 10); }
W.now = ct(day); const mk = await W.K.myKits('m@x.test');
const by = Object.fromEntries(mk.kits.map(k => [k.month, k]));
assert.strictEqual(by[months[1]].gif, false); assert.strictEqual(by[months[2]].gif, true); assert.strictEqual(by[months[5]].custom, true); assert.strictEqual(by[months[4]].custom, false);
assert.strictEqual(mk.customCredits.length, 1); assert.ok(/BannerKitAI/.test(mk.customNote));
assert.ok(/3-month streak/.test((await W.K.renderFor('m@x.test', months[1], '300x250', 'gif')).error));
});
if (!ffmpegOk || !require(path.join(ROOT, 'kit.js')).font()) { console.log('skip render checks: ffmpeg or font missing'); }
else {
await t('personalized renders: every size at its exact dimensions, @name + QR drawn (differs from the base), cached, served by unguessable name', async () => {
const W = world(); W.K.usePlaceholder('2026-11'); W.K.saveKit({ month: '2026-11', released: true }); await W.buy('0x1', '2026-11-03'); W.now = ct('2026-11-04');
for (const size of W.K.ORDER) {
const r = await W.K.renderFor('m@x.test', '2026-11', size);
assert.ok(r.ok, size + ' ' + r.error); const buf = fs.readFileSync(r.file);
assert.deepStrictEqual(dims(buf), W.K.SIZES[size], size + ' dims');
assert.ok(/^\/kitimg\/[a-f0-9]{28}\.(png|jpg)$/.test(r.url)); assert.ok(r.abs.startsWith('https://instantadpay.com/kitimg/'));
assert.ok(W.K.servePath(r.url.slice(8)));
const again = await W.K.renderFor('m@x.test', '2026-11', size); assert.strictEqual(again.url, r.url, 'cached');
}
assert.strictEqual(W.K.servePath('0000000000000000000000000000.png'), null);
assert.strictEqual(W.K.servePath('../kits.json'), null);
});
await t('animated GIF at month 3: three-frame loop at the exact size', async () => {
const W = world(); let day = '2026-09-02';
for (let k = 1; k <= 3; k++) { W.K.usePlaceholder(day.slice(0, 7)); W.K.saveKit({ month: day.slice(0, 7), released: true }); await W.buy('g' + k, day); if (k < 3) day = new Date(ct(day) + 30 * 864e5).toISOString().slice(0, 10); }
W.now = ct(day);
const r = await W.K.renderFor('m@x.test', day.slice(0, 7), '300x250', 'gif');
assert.ok(r.ok, r.error); const buf = fs.readFileSync(r.file); assert.strictEqual(buf.slice(0, 6).toString(), 'GIF89a'); assert.deepStrictEqual(dims(buf), [300, 250]);
assert.ok(buf.includes(Buffer.from('NETSCAPE2.0')), 'loops');
});
await t('admin preview renders without any member', async () => {
const W = world(); W.K.usePlaceholder('2026-11');
const r = await W.K.preview('2026-11', '160x600', 'yourname'); assert.ok(r.ok, r.error); assert.deepStrictEqual(dims(fs.readFileSync(r.file)), [160, 600]);
});
}
console.log('\n' + (n - fails) + '/' + n + ' passed');
process.exit(fails ? 1 : 0);
})();