Files
linkspin/qa/topup-positions.test.cjs
martbost e545343fe0 Michael Camire fixes + hub shells in the tank (Marty 2026-11-14, LinkSpin Support)
1. Top-up across positions: an ad burns from ONE position, and topUpCampaign only looked at the ad's
   own position (or the main one when the list row had no memberId), so credits bought on a linked
   position could never top up the ad. Ported from InstantAdPay 2026-09-27 (Jim Watts): the ad's real
   member id is read from the campaign row; when it cannot cover, the refusal names where the free
   credits are and offers switchOptions; with the member's yes (switchTo, only a position on the
   account) moveFunding settles what the ad already ran up on the OLD position, moves the ad, then
   tops up. Ads screen asks in-page (own dialog) and retries. Create already had the largest-position guard.
2. Toasts: errors stay until closed (close button, role=alert), other notes 3.5 to 9 s by length.
3. Banner Spin per-banner clicks: his 3 clicks were all unmatched (no view on record for that browser),
   so they landed on the fallback row and every banner said 0. Click matching now also tries the same
   connection (daily-salted IP-only hash) when the browser key finds nothing (mail/exchange app view,
   click opened in the phone browser), view notes last 60 min instead of 15, and the stats split
   'Fallback shown' from 'Clicks not tied to a banner' so the rows add up to the total. History is not
   backfilled: views were never stored per visitor, so old clicks cannot be tied reliably.
4. Holding tank: hub-made waiting accounts with no sponsor are in the tank (waiting, rescue, readyTick
   read listAll with { shells: true }; 'hub-shell' no longer holds anyone back). Locked / sponsored /
   unknown, grace, 2-adoption cap unchanged; readyTick still never switches a shell on;
   accounts.listAll default unchanged (mailings skip shells).

Assets: cr.css, cr-shell.js, ads-screen.js ?v=20261114tu, banner-spin.js ?v=20261114bc.
QA: walk member 0 bugs (36 ok), netconv 57/57, networksso ALL PASSED, tank-hold all passed (new hub
shell part), profile policy 16/16, new topup-positions 8/8, new bannerspin-attrib 7/7, bannerspin e2e
75/75, hubsync 15 checks, tank-cap 15/15, sponsorfollow 30/30.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 11:59:38 -06:00

86 lines
5.9 KiB
JavaScript

// 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);
})();