// Top-up across positions (Michael Camire 2026-11-14, LinkSpin Support): he bought credits on his linked position, // the dashboard showed them, but adding 1000 credits to his text ad failed. An ad burns from ONE member id, and the // top-up only ever looked at that one (or, with no memberId on the list row, the main position). Same fix as // InstantAdPay 2026-09-27 (Jim Watts): say where the free credits are, offer the position that can pay, and on the // member's yes move the ad's funding there (old charges settle on the old position first) and top up. // Unit test on ads.js directly: throwaway DATA_DIR, JSON store, a fake chain. No server, no mail, no network. // node qa/topup-positions.test.cjs 'use strict'; const assert = require('assert'); const fs = require('fs'); const os = require('os'); const path = require('path'); for (const k of ['DATABASE_URL', 'NAS_DB_HOST', 'NAS_DB_USER', 'NAS_DB_PASSWORD', 'NAS_DB_NAME', 'OUTBOUND', 'ENGINE_KEY']) delete process.env[k]; const SITE = path.join(__dirname, '..'); const DATA = fs.mkdtempSync(path.join(os.tmpdir(), 'topup-pos-')); const ads = require(path.join(SITE, 'ads.js')); const ONCHAIN = { 101: 300, 202: 6000, 303: 40 }; // main position, the linked one he bought on, a small third const chain = { creditBalance: async id => ONCHAIN[id] || 0 }; const OWNER = 'mc@topup.test', OTHER = 'someone@topup.test'; let passed = 0, failed = 0; const results = []; async function t(name, fn) { try { await fn(); passed++; results.push('ok ' + name); } catch (e) { failed++; results.push('FAIL ' + name + '\n ' + String(e && e.stack || e).split('\n').slice(0, 4).join('\n ')); } } const text = extra => Object.assign({ type: 'text', name: 'My text ad', targetUrl: 'https://example.com/offer', title: 'A good offer', body: 'Read about it here.', budget: 200 }, extra || {}); const rowOf = id => JSON.parse(fs.readFileSync(path.join(DATA, 'campaigns.json'), 'utf8')).campaigns.find(c => c.id === id); (async () => { ads.init({ dataDir: DATA, chain }); let C; await t('a text ad created on the main position pays from it', async () => { const r = await ads.createCampaign(OWNER, 101, text(), [101, 202, 303]); assert.ok(!r.error, r.error); C = r.campaign || r; assert.ok(C.id, 'campaign id'); assert.strictEqual(rowOf(C.id).memberId, 101); }); await t('1000 credits from a position that has 100 free: refused with where the credits are, nothing moved', async () => { const r = await ads.topUpCampaign(OWNER, 101, C.id, 1000, { positions: [101, 202, 303] }); assert.ok(r.error, 'refused'); assert.match(r.error, /position #101, which has 100 credits free/); assert.match(r.error, /#202 has 6,000 free/); assert.match(r.error, /from #202 instead/); assert.deepStrictEqual(r.switchOptions, [{ memberId: 202, free: 6000 }]); // #303 (40) cannot cover 1000 assert.strictEqual(rowOf(C.id).memberId, 101); assert.strictEqual(rowOf(C.id).budget, 200); }); await t('switchTo a position not on the account is ignored (no move, same refusal)', async () => { const r = await ads.topUpCampaign(OWNER, 101, C.id, 1000, { positions: [101, 303], switchTo: 202 }); assert.ok(r.error); assert.ok(!r.switchOptions.length); assert.strictEqual(rowOf(C.id).memberId, 101); assert.strictEqual(rowOf(C.id).budget, 200); }); await t('another member cannot top up or move the ad', async () => { const r = await ads.topUpCampaign(OTHER, 202, C.id, 1000, { positions: [202], switchTo: 202 }); assert.ok(r.error); assert.strictEqual(rowOf(C.id).memberId, 101); }); await t('with the member\'s yes (switchTo 202): the ad moves to #202, old charges settle on #101, 1000 added', async () => { // 30 credits of views the ad already ran up on #101, not yet burned: they must burn from #101, never #202 const F = path.join(DATA, 'campaigns.json'); const db = JSON.parse(fs.readFileSync(F, 'utf8')); db.campaigns.find(c => c.id === C.id).accrued = 30; fs.writeFileSync(F, JSON.stringify(db)); ads.init({ dataDir: DATA, chain }); const r = await ads.topUpCampaign(OWNER, 101, C.id, 1000, { positions: [101, 202, 303], switchTo: 202 }); assert.ok(!r.error, r.error); assert.strictEqual(r.added, 1000); assert.strictEqual(r.paidFrom, 202); assert.strictEqual(r.budget, 1200); const row = rowOf(C.id); assert.strictEqual(row.memberId, 202); assert.strictEqual(row.budget, 1200); assert.strictEqual(row.status, 'active'); assert.strictEqual(row.accrued, 0); assert.strictEqual(row.spent, 30); const burns = JSON.parse(fs.readFileSync(F, 'utf8')).burnsPending.filter(b => b.ref === 'campaign-' + C.id); assert.deepStrictEqual(burns.map(b => [b.memberId, b.amount]), [[101, 30]]); }); await t('balances follow the move: #101 keeps only its own 30 to burn, #202 holds the rest of the ad', async () => { const b = await ads.balances([101, 202, 303], OWNER); const per = Object.fromEntries(b.per.map(p => [p.memberId, p.avail])); assert.strictEqual(per[101], 300 - 30); assert.strictEqual(per[202], 6000 - (1200 - 30)); assert.strictEqual(per[303], 40); }); await t('a top-up the ad\'s own position covers needs no switch and stays put', async () => { const r = await ads.topUpCampaign(OWNER, 101, C.id, 500, { positions: [101, 202, 303] }); assert.ok(!r.error, r.error); assert.strictEqual(r.paidFrom, 202); assert.strictEqual(rowOf(C.id).memberId, 202); }); await t('no single position can cover: says the most one go can add', async () => { const r = await ads.topUpCampaign(OWNER, 101, C.id, 5000, { positions: [101, 202, 303] }); assert.ok(r.error); assert.match(r.error, /No single position has 5,000 free; the most you can add in one go is 4,330/); assert.deepStrictEqual(r.switchOptions, []); }); console.log(results.join('\n')); console.log('\ntopup-positions: ' + passed + ' passed, ' + failed + ' failed'); try { fs.rmSync(DATA, { recursive: true, force: true }); } catch (e) {} process.exit(failed ? 1 : 0); })();