Files
linkspin/qa/refuel-kit.test.cjs
T

70 lines
5.4 KiB
JavaScript

// Refuel Kit on LinkSpin: kit.js (shared with InstantAdPay) + kitspin.js "Load into my Banner Spin" against the REAL
// bannerspin.js, throwaway data, LinkSpin placeholder art. Entitlement is stubbed here (InstantAdPay's refuel.js decides it;
// qa/refuel.test.cjs covers that path). Run: node qa/refuel-kit.test.cjs
'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); }
let qrcode = null; for (const p of ['qrcode', path.join(ROOT, '..', '..', 'onchain-ad-membership', 'site', 'node_modules', 'qrcode')]) { try { qrcode = require(p); break; } catch (e) {} }
let ffmpegOk = true; try { execSync('ffmpeg -version', { stdio: 'ignore' }); } catch (e) { ffmpegOk = false; }
if (!qrcode || !ffmpegOk) { console.log('skip: needs ffmpeg and the qrcode package (here or in the InstantAdPay repo)'); process.exit(0); }
function world(opts) {
opts = opts || {};
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'ls-kit-')); const up = path.join(dir, 'uploads'); fs.mkdirSync(up);
const W = { dir, up, months: Object.assign({}, opts.months || { '2026-11': 1 }), ownBuy: opts.ownBuy !== false };
W.BS = fresh('bannerspin.js'); W.BS.init({ dataDir: dir, uploadsDir: up, schedule: false, facts: async () => ({ ownBuy: W.ownBuy, buyers: 0 }), rotator: { findMine: () => null } });
W.K = fresh('kit.js');
W.K.init({ dataDir: dir, program: 'linkspin', publicDir: path.join(ROOT, 'public'), siteUrl: 'https://linkspin.app', qrcode,
entitled: async () => ({ on: true, months: W.months, today: '2026-11-10' }),
linkFor: async () => ({ username: 'sam', link: 'https://linkspin.app/join/sam' }) });
W.K.usePlaceholder('2026-11'); W.K.saveKit({ month: '2026-11', theme: 'Harvest', released: true });
W.load = b => require(path.join(ROOT, 'kitspin.js')).load(Object.assign({ kit: W.K, bannerspin: W.BS, uploadsDir: up, email: 's@x.test', month: '2026-11' }, b || {}));
return W;
}
(async () => {
await t('LinkSpin art only: the placeholder kit is the LinkSpin banners', async () => {
const W = world(); const k = W.K.listKits()[0];
assert.strictEqual(k.program, 'linkspin'); assert.ok(k.complete);
const base = fs.readFileSync(path.join(W.dir, 'kits', '2026-11', '728x90.png')); assert.ok(base.equals(fs.readFileSync(path.join(ROOT, 'public', 'banners', 'linkspin-728x90.png'))));
});
await t('locked kit and locked Banner Spin tier both refuse', async () => {
const W = world({ months: {} }); assert.ok(/next counted refuel/.test((await W.load()).error));
const W2 = world({ ownBuy: false }); const r = await W2.load(); assert.strictEqual(r.status, 403); assert.ok(/\$25/.test(r.error));
});
await t('Starter tier: uses an existing Banner Spin of the right size, creates new ones while the tier allows, says why when it cannot; 1200x630 is not a Banner Spin size', async () => {
const W = world();
const own = await W.BS.create('s@x.test', { name: 'My leaderboard', size: '728x90' }); assert.ok(own.ok);
const r = await W.load();
const by = Object.fromEntries(r.results.map(x => [x.size, x]));
assert.deepStrictEqual(Object.keys(by).sort(), ['160x600', '300x250', '468x60', '728x90']);
assert.strictEqual(by['728x90'].spinId, own.spin.id); assert.strictEqual(by['728x90'].created, false);
const made = r.results.filter(x => x.created).length; const refused = r.results.filter(x => x.error);
assert.strictEqual(made, 1, 'Starter holds 2 Banner Spins: one existed, one more created'); assert.strictEqual(refused.length, 2); assert.ok(/Starter tier holds 2/.test(refused[0].error));
const spins = await W.BS.list('s@x.test'); const b = spins.find(s => s.id === own.spin.id).banners[0];
assert.strictEqual(b.label, 'Refuel Kit 2026-11'); assert.strictEqual(b.target, 'https://linkspin.app/join/sam'); assert.ok(/^\/uploads\/[a-f0-9]{24}\.png$/.test(b.image));
assert.ok(fs.existsSync(path.join(W.up, b.image.slice(9)))); assert.strictEqual(b.w, 728); assert.ok(b.bytes <= W.BS.MAX_BYTES);
});
await t('loading the same kit twice adds nothing twice', async () => {
const W = world(); await W.BS.create('s@x.test', { name: 'L', size: '728x90' }); await W.BS.create('s@x.test', { name: 'M', size: '300x250' });
await W.load({ sizes: ['728x90', '300x250'] }); const r2 = await W.load({ sizes: ['728x90', '300x250'] });
assert.ok(r2.results.every(x => x.already));
const spins = await W.BS.list('s@x.test'); assert.deepStrictEqual(spins.map(s => s.banners.length), [1, 1]);
});
await t('the animated version (month 3+) loads as a GIF within the 150 KB Banner Spin limit, or is refused plainly', async () => {
const W = world({ months: { '2026-11': 3 } }); await W.BS.create('s@x.test', { name: 'R', size: '300x250' });
const r = await W.load({ sizes: ['300x250'], variant: 'gif' }); const x = r.results[0];
if (x.error) assert.ok(/150 KB/.test(x.error), x.error);
else { const b = (await W.BS.list('s@x.test'))[0].banners[0]; assert.ok(/\.gif$/.test(b.image)); }
});
console.log('\n' + (n - fails) + '/' + n + ' passed');
process.exit(fails ? 1 : 0);
})();