From fd6f88cab7c9dc3b7f84921833da1ff9866800c9 Mon Sep 17 00:00:00 2001 From: martbost Date: Tue, 27 Oct 2026 07:56:56 -0500 Subject: [PATCH] Fix NAS stop trap on LinkSpin: snapshot true delivery before every stop, reconcile active ads only, restart stopped ads from the true count The InstantAdPay 2026-09-17 fix was never ported. nas.deactivate zeroed `remaining` with no snapshot, so served = assigned - remaining read the undelivered rest as delivered, and reconcileNas read EVERY status: a paused ad was charged for its zeroed remainder (up to its budget) and stopped ads showed their whole allocation as network views. Now: nas.deactivate returns the served count read before the stop; ads.stopNas persists it on pause, end and exhaustion; reconcileNas only looks at active campaigns; resume/top-up of a stopped ad uses the new nas.restart (re-bases assigned on the true count) instead of a plain top-up. QA: qa/nas-stop.test.cjs (9), loginqueue 13, refill 12, run.sh member passed. Co-Authored-By: Claude Opus 5.5 --- ads.js | Bin 123469 -> 125156 bytes nas.js | 15 +++++++++++- qa/nas-stop.test.cjs | 55 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 qa/nas-stop.test.cjs diff --git a/ads.js b/ads.js index 09b85d4519577165f33d81cc377eadbdf2e4b5d2..eb9cfbc50ea8526873b3c00045e76e14d74213ce 100644 GIT binary patch delta 1861 zcmbVN&u<$=6xK*ok<1|yvQrSy;0w|0bz7n12Yh?Bk`p)TS1SH zzy>tluCLz!qj(A@T!R$HLP44+BLWx*C0v`Jf{7@{fI^ZYRT%S0zX`lpeIisMG#n~1 zU$V{U}K7>-1kD2&2LVyK0?ej@!S7IaS{ z;}OJ3a#sV#baYYx!tp9xkE9P_CUGy3^TdwNhbhNGc7htn*SX#bb^;g9DgaRkP8AUp zJ4%>L$yO!*v&vr1zi+Z<8}oN98>-;yRWPGeBq1noPE)%L*JL(466%_p|K4O*CQJF| zVQve?;B(^-z(Ik|#+5PA)VJ8JPCS4iRUHjezte_>eT7*( zA^A}(0>^a`Od&TQJcMg7i_GGeYUiNP8~tg$;5 z_n<32OqXv#Ts5$<cL&E@l=Gd3k1kJx=2f&qSWRoe*8x(=U;xy zu1tTcvg-3R2yH8Gy?cP80-K}gQcd$I<(bBTvIgjh;(0p2Et+uH2sHHwR4Nb;S+*Ex zxCE7)ps+NYBGj!gJ8sbM`kT`$aIQapDgWs{doAx=XD{ZLK4Yu-qgUCb{LXduRsQ1x z_Co#;*c;R5Hrf4W@<&bfW`2&%ee1t)5=Kg!=3ZNWy2`N zT&qyK{gE1@negNTZzZPx?_`vpe$k3iYWjaG#);F9s4^;VKV!|<%rd>kfl-(^<0dcI zD7yWF1EV$j^g<6t5h1;j{DRN|jiSVIJ)i>|Q#@0q_j@oJPM=`MD7yWk2cvtx464JB ym1 { (c ? ok : bad).push(n + (c || extra === undefined ? '' : ' -> ' + extra)); }; +const DATA = fs.mkdtempSync(path.join(os.tmpdir(), 'ls-nas-stop-')); +const nas = require('../nas.js'); +const rows = {}; const calls = { restart: 0, deactivate: 0 }; +nas.enabled = () => true; +nas.impressionsPerCredit = type => type === 'banner' ? 5 : 10; +nas.readServed = async id => { const r = rows[id]; return r ? { assigned: r.assigned, remaining: r.remaining, served: Math.max(0, Math.min(r.assigned, r.assigned - r.remaining)), clicks: 0 } : null; }; +nas.deactivate = async id => { const r = rows[id]; const served = Math.max(0, r.assigned - r.remaining); r.remaining = 0; calls.deactivate++; return { ok: true, served }; }; +nas.topUp = async (id, add) => { rows[id].assigned += add; rows[id].remaining += add; return { ok: true }; }; +nas.restart = async (id, base, add) => { rows[id].assigned = base + add; rows[id].remaining = add; calls.restart++; return { ok: true }; }; +nas.updateCreative = async () => ({ ok: true }); +const ads = require('../ads.js'); +const ADV = 'stop@qa.test', FILE = path.join(DATA, 'campaigns.json'); +const banner = (id, x) => Object.assign({ id, owner: ADV, memberId: 0, type: 'banner', name: 'QA ' + id, targetUrl: 'https://example.com/', imageUrl: 'https://example.com/b.png', width: 728, height: 90, + budget: 1000, spent: 0, accrued: 0, imps: 0, batchImps: 0, clicks: 0, status: 'active', nasAdId: id, nasServed: 0, created: Date.now() }, x || {}); +const seed = camps => { fs.writeFileSync(FILE, JSON.stringify({ v: 1, nextId: 9100, campaigns: camps, burnsPending: [] })); ads.init({ dataDir: DATA, chain: { creditBalance: async () => 0 } }); }; +const C = id => JSON.parse(fs.readFileSync(FILE, 'utf8')).campaigns.find(c => c.id === id); +const used = c => (c.spent || 0) + (c.accrued || 0); +(async () => { + seed([banner(9001), banner(9002, { budget: 100 })]); + await ads.addEarned(ADV, 10000); + rows[9001] = { assigned: 5000, remaining: 5000 }; rows[9002] = { assigned: 500, remaining: 500 }; + rows[9001].remaining -= 1000; await ads.reconcileNas(); + t('1,000 real NAS views charge 200 credits', used(C(9001)) === 200, String(used(C(9001)))); + await ads.setStatus(ADV, 9001, 'paused'); + t('pause records the TRUE 1,000 served, not the 5,000 allocation', C(9001).nasServed === 1000, String(C(9001).nasServed)); + await ads.reconcileNas(); + t('a paused ad is not charged for its zeroed remainder', used(C(9001)) === 200, String(used(C(9001)))); + const r0 = calls.restart; await ads.setStatus(ADV, 9001, 'active'); await ads.reconcileNas(); + t('resume restarts the stopped ad from the true count', calls.restart === r0 + 1 && rows[9001].assigned === 5000 && rows[9001].remaining === 4000, JSON.stringify(rows[9001])); + t('no phantom delivery or charge after the resume', C(9001).nasServed === 1000 && used(C(9001)) === 200, C(9001).nasServed + ' / ' + used(C(9001))); + rows[9001].remaining -= 500; await ads.reconcileNas(); + t('real views after the resume still charge (500 = 100 credits)', used(C(9001)) === 300, String(used(C(9001)))); + // exhaustion: budget 100 = 500 views; NAS serves 300, a budget top-up of on-site spend exhausts it on the next reconcile + const c2 = C(9002); c2.spent = 60; const s = JSON.parse(fs.readFileSync(FILE, 'utf8')); s.campaigns[s.campaigns.findIndex(x => x.id === 9002)] = c2; fs.writeFileSync(FILE, JSON.stringify(s)); ads.init({ dataDir: DATA, chain: { creditBalance: async () => 0 } }); + rows[9002].remaining -= 300; const d0 = calls.deactivate; await ads.reconcileNas(); + const x = C(9002); + t('exhausted: charge capped at the budget and the NAS ad stopped', used(x) === 100 && x.status === 'out' && calls.deactivate === d0 + 1, JSON.stringify({ used: used(x), st: x.status })); + t('exhausted: served kept at the TRUE 300', x.nasServed === 300, String(x.nasServed)); + await ads.reconcileNas(); + t('an exhausted (out) ad is never reconciled again', C(9002).nasServed === 300 && used(C(9002)) === 100, String(C(9002).nasServed)); + console.log('PASS ' + ok.length); + for (const b of bad) console.log('FAIL ' + b); + try { fs.rmSync(DATA, { recursive: true, force: true }); } catch (e) {} + process.exit(bad.length ? 1 : 0); +})().catch(e => { console.error(e); process.exit(1); });