47638ac1fe
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
115 lines
11 KiB
JavaScript
115 lines
11 KiB
JavaScript
// Engine Room (engine.js, the same file ships in LinkSpin): build() rules, facts() on stubbed sites, the member goal.
|
|
// Run: node qa/refuel-engine.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 E = require(path.join(ROOT, 'engine.js'));
|
|
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(' | ')); } }
|
|
const NOW = Date.UTC(2026, 10, 15, 18); const DAY = 864e5;
|
|
const rf = o => Object.assign({ on: true, started: true, active: true, streak: 2, best: 2, refuelDayLabel: 'Wed, Dec 2', graceLabel: 'Mon, Dec 7', daysToRefuel: 17, windowOpen: false,
|
|
cycle: { month: 2, both: true, iap: { refueled: true, credits: 2000, pct: 20, bonus: 400 }, linkspin: { refueled: true, credits: 2500, pct: 20, bonus: 500 } },
|
|
next: { month: 2, secondEngine: 20 }, pitStop: { pct: 10, iap: true, linkspin: true }, freeMiss: { available: true }, minUsd: { iap: 20, linkspin: 25 } }, o || {});
|
|
const camp = o => Object.assign({ id: 1, name: 'A', type: 'banner', status: 'active', budget: 1000, delivered: 300, clicks: 30, created: NOW - 10 * DAY }, o);
|
|
const iap = o => Object.assign({ site: 'iap', ok: true, credits: { available: 1000, inCampaigns: 700 }, campaigns: [camp({})], clicksWeek: 40, loginFree: 0 }, o || {});
|
|
const green = { state: 'green', note: 'ok' };
|
|
const ls = o => Object.assign({ site: 'linkspin', ok: true, credits: { available: 2000, inCampaigns: 0 }, campaigns: [], clicksWeek: 10, loginFree: 0, goalsCount: 1, goalsMonth: 6, conv30: 9,
|
|
parts: { bannerSpin: green, spinLink: green, page: green, testing: green } }, o || {});
|
|
const B = o => E.build(Object.assign({ now: NOW, refuel: rf(), iap: iap(), ls: ls(), goal: 20 }, o || {}));
|
|
|
|
(async () => {
|
|
await t('switch off: nothing', async () => { assert.deepStrictEqual(E.build({ refuel: { on: false } }), { on: false }); });
|
|
await t('locked until the first counted refuel: greyed, one move "Refuel to start your engine"', async () => {
|
|
const d = B({ refuel: rf({ started: false, active: false, cycle: null, streak: 0 }) });
|
|
assert.ok(d.locked); assert.strictEqual(d.next.key, 'start'); assert.strictEqual(d.next.label, 'Refuel to start your engine');
|
|
});
|
|
await t('fuel dial: credits free + in campaigns on both sites vs this cycle\'s refuel (with bonus), days left at the burn', async () => {
|
|
const d = B();
|
|
assert.strictEqual(d.fuel.left, 3700); assert.strictEqual(d.fuel.refuelCredits, 5400); assert.strictEqual(d.fuel.pct, 69);
|
|
assert.strictEqual(d.fuel.burnPerDay, 30); assert.strictEqual(d.fuel.daysLeft, 123);
|
|
});
|
|
await t('clicks dial: this week on both sites, and per 1,000 credits on recent campaigns', async () => {
|
|
const d = B({ iap: iap({ clicksPrevWeek: 30 }), ls: ls({ clicksPrevWeek: 20, campaigns: [camp({ id: 9, delivered: 700, clicks: 5 })] }) });
|
|
assert.strictEqual(d.clicks.week, 50); assert.strictEqual(d.clicks.per1000, 35); assert.strictEqual(d.clicks.prevWeek, 50); assert.strictEqual(d.clicks.pct, 50, 'same as last week = half way');
|
|
});
|
|
await t('goals dial: conversions this month vs the member goal; LinkSpin down = unknown, never zero', async () => {
|
|
assert.deepStrictEqual([B().goals.month, B().goals.target, B().goals.pct], [6, 20, 30]);
|
|
const d = B({ ls: null }); assert.strictEqual(d.goals.month, null); assert.strictEqual(d.parts[5].note, 'LinkSpin did not answer'); assert.deepStrictEqual(d.sites, { iap: true, linkspin: false });
|
|
});
|
|
await t('warning lights: 80%, out, refuel day coming, login slot open', async () => {
|
|
const d = B({ refuel: rf({ daysToRefuel: 4 }), iap: iap({ loginFree: 2, campaigns: [camp({ delivered: 850 }), camp({ id: 2, status: 'out', delivered: 1000 })] }) });
|
|
assert.deepStrictEqual(d.warnings.map(w => [w.key, w.on]), [['low', true], ['out', true], ['refuel', true], ['login', true]]);
|
|
const q = B(); assert.ok(q.warnings.every(w => !w.on));
|
|
});
|
|
await t('six part lights: green / amber / grey with setup links', async () => {
|
|
const d = B({ ls: ls({ goalsCount: 1, conv30: 0, parts: { bannerSpin: { state: 'grey', note: 'not set up' }, spinLink: green, page: { state: 'amber', note: 'draft only' }, testing: green } }) });
|
|
assert.deepStrictEqual(d.parts.map(p => p.label), ['Fuel', 'Banner spin', 'Spin link', 'Page', 'Testing', 'Conversion tracking']);
|
|
assert.deepStrictEqual(d.parts.map(p => p.state), ['green', 'grey', 'green', 'amber', 'green', 'amber']);
|
|
assert.strictEqual(d.parts[5].note, 'no conversions tracked yet'); assert.ok(/linkspin\.app\/links/.test(d.parts[1].link));
|
|
assert.strictEqual(B({ iap: iap({ campaigns: [] }), ls: ls({ campaigns: [] }) }).parts[0].state, 'grey');
|
|
});
|
|
await t('next move, in order of what matters most', async () => {
|
|
assert.strictEqual(B({ refuel: rf({ windowOpen: true, next: { month: 3, single: 20, both: 30 } }) }).next.key, 'refuel');
|
|
assert.strictEqual(B({ iap: iap({ campaigns: [camp({ status: 'out', delivered: 1000 })] }) }).next.key, 'relaunch');
|
|
assert.strictEqual(B({ iap: iap({ credits: { available: 20, inCampaigns: 0 }, campaigns: [camp({ delivered: 900, created: NOW - 2 * DAY })] }), ls: ls({ credits: { available: 0, inCampaigns: 0 } }) }).next.key, 'pitstop');
|
|
assert.strictEqual(B({ iap: iap({ campaigns: [camp({ delivered: 850, created: NOW - 29 * DAY })] }) }).next.key, 'topup');
|
|
assert.strictEqual(B({ ls: ls({ goalsCount: 0 }) }).next.key, 'tracking');
|
|
assert.strictEqual(B({ ls: ls({ parts: { bannerSpin: { state: 'grey', note: '' }, spinLink: green, page: green, testing: green } }) }).next.key, 'bannerspin');
|
|
assert.strictEqual(B({ ls: ls({ parts: { bannerSpin: green, spinLink: green, page: green, testing: { state: 'amber', note: '' } } }) }).next.key, 'testing');
|
|
const cut = B({ iap: iap({ campaigns: [camp({ id: 1, name: 'Good', delivered: 400, clicks: 40 }), camp({ id: 2, name: 'Weak', delivered: 400, clicks: 4 })] }) });
|
|
assert.strictEqual(cut.next.key, 'cut'); assert.ok(/"Weak"/.test(cut.next.why));
|
|
assert.strictEqual(B().next.key, 'cruise');
|
|
for (const d of [B(), cut]) assert.ok(!/—/.test(JSON.stringify(d)));
|
|
});
|
|
await t('links follow the site the member is on (2026-10-25): own buy/campaigns/conversion screen, LinkSpin-only tools cross over', async () => {
|
|
const onIap = B({ ls: ls({ goalsCount: 0 }) }), onLs = B({ site: 'linkspin', ls: ls({ goalsCount: 0 }) });
|
|
assert.strictEqual(onIap.site, 'iap'); assert.strictEqual(onLs.site, 'linkspin');
|
|
assert.strictEqual(onIap.next.link, 'https://instantadpay.com/my#conversions'); assert.strictEqual(onIap.next.guide, '/conversion-tracking');
|
|
assert.strictEqual(onLs.next.link, 'https://linkspin.app/conversions');
|
|
assert.strictEqual(onIap.parts[5].key, 'conversions'); assert.strictEqual(onIap.parts[1].link, 'https://linkspin.app/links#b'); assert.strictEqual(onIap.parts[1].key, 'bannerspin');
|
|
const lock = o => B(Object.assign({ refuel: rf({ started: false, active: false, cycle: null, streak: 0 }) }, o));
|
|
assert.strictEqual(lock().next.link, 'https://instantadpay.com/my#buy'); assert.strictEqual(lock({ site: 'linkspin' }).next.link, 'https://linkspin.app/credits#buy');
|
|
assert.ok(/\$25 or more here/.test(lock({ site: 'linkspin' }).next.why)); assert.ok(/\$20 or more here/.test(lock().next.why));
|
|
assert.strictEqual(B({ site: 'linkspin', iap: iap({ campaigns: [] }), ls: ls({ campaigns: [] }) }).parts[0].link, 'https://linkspin.app/ads');
|
|
});
|
|
await t('facts(): LinkSpin parts and the goals dial count from the Central-time 1st', async () => {
|
|
const own = 'o@x.test'; const ts = Date.UTC(2026, 10, 1, 4, 30); // 11:30 PM CT Oct 31 = Nov 1 in UTC
|
|
const conv = [{ owner: own, ts, self: false }, { owner: own, ts: Date.UTC(2026, 10, 3), self: false }, { owner: own, ts: Date.UTC(2026, 10, 4), self: true }];
|
|
const d = { site: 'linkspin', now: NOW,
|
|
adminMember: { view: async () => ({ credits: { available: 500, inCampaigns: 250 } }) },
|
|
ads: { listCampaigns: async () => [{ id: 1, name: 'x', type: 'banner', status: 'active', budget: 1000, spent: 500, clicks: 9, created: NOW - DAY }, { id: 2, house: true, status: 'active', spent: 1 }], loginSlots: async () => ({ free: 1 }) },
|
|
coach: { clicksSince: async (ids, since) => (assert.deepStrictEqual(ids, [1]), since === NOW - 7 * DAY ? 4 : since === NOW - 14 * DAY ? 10 : -1) },
|
|
convtrack: { goalsOf: () => [{ px: 'p' }], countSince: (o, since) => conv.filter(c => c.owner === o && c.ts >= since && !c.self).length },
|
|
bannerspin: { list: async () => [{ paused: false, imps30: 120, banners: [{ active: true, imps30: 80 }, { active: true, imps30: 40 }] }] },
|
|
rotator: { list: async () => [{ paused: false, hits7: 0, destinations: [{ active: true, hits7: 0 }] }] },
|
|
pages: { list: async () => [{ status: 'draft', views: 0 }] } };
|
|
const f = await E.facts(d, own);
|
|
assert.deepStrictEqual(f.credits, { available: 500, inCampaigns: 250 }); assert.strictEqual(f.campaigns.length, 1); assert.strictEqual(f.clicksWeek, 4); assert.strictEqual(f.clicksPrevWeek, 6); assert.strictEqual(f.loginFree, 1);
|
|
assert.strictEqual(f.goalsMonth, 1, 'Oct 31 11:30 PM CT is October'); assert.strictEqual(f.conv30, 2);
|
|
assert.deepStrictEqual([f.parts.bannerSpin.state, f.parts.spinLink.state, f.parts.page.state, f.parts.testing.state], ['green', 'amber', 'amber', 'green']);
|
|
});
|
|
await t('streak note: both sites refueled says so, one site asks for the OTHER by name, window open quotes the next rates', async () => {
|
|
assert.strictEqual(B().streak.note, 'Both engines fueled this cycle: +20% on each.');
|
|
assert.ok(!/Refuel the other|Refuel (LinkSpin|InstantAdPay) this cycle/.test(B().streak.note));
|
|
const oneIap = rf({ cycle: { month: 2, both: false, iap: { refueled: true, credits: 2000, pct: 15, bonus: 300 }, linkspin: { refueled: false } } });
|
|
assert.strictEqual(B({ refuel: oneIap }).streak.note, 'Refuel LinkSpin this cycle for +20% on both.');
|
|
const oneLs = rf({ cycle: { month: 2, both: false, iap: { refueled: false }, linkspin: { refueled: true, credits: 2500, pct: 15, bonus: 375 } } });
|
|
assert.strictEqual(B({ refuel: oneLs }).streak.note, 'Refuel InstantAdPay this cycle for +20% on both.');
|
|
assert.strictEqual(B({ refuel: rf({ windowOpen: true, next: { month: 3, single: 20, both: 30 } }) }).streak.note, 'Window open: next refuel +20% (+30% on each with both).');
|
|
assert.strictEqual(B({ refuel: rf({ started: false, active: false, cycle: null, streak: 0 }) }).streak.note, '');
|
|
});
|
|
await t('the member goal lives with the streak (InstantAdPay); switch off refuses', async () => {
|
|
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'iap-eng-')); const cfg = { refuelStreak: '1' };
|
|
const marks = require(path.join(ROOT, 'marks.js'));
|
|
const R = require(path.join(ROOT, 'refuel.js')); R.init({ dataDir: dir, db: require(path.join(ROOT, 'db.js')), marks, siteConfig: () => cfg, accounts: {}, ads: {}, message: async () => {} });
|
|
assert.deepStrictEqual(await R.setGoal('g@x.test', 25), { ok: true, goal: 25 });
|
|
assert.strictEqual((await R.view('g@x.test')).goal, 25);
|
|
cfg.refuelStreak = '0'; assert.ok((await R.setGoal('g@x.test', 5)).error);
|
|
});
|
|
console.log('\n' + (n - fails) + '/' + n + ' passed');
|
|
process.exit(fails ? 1 : 0);
|
|
})();
|