Files
instantadpay/qa/tank-readiness.test.mjs
martbost 52f0a5496b Holding-tank readiness on the tank cards (Marty 2026-11-18, warm-up stage 1, no email)
Each waiting lead on My line > Holding tank and Admin > Members tank card now shows how they got into the tank, a stage chip (New / Warming / Engaged / Activated / Ready, real actions only), the network heat score 0-100 with its band, and their last real action. Computed on the network hub (POST /v1/tank/readiness); tankready.js sends this site's facts (own last sign-in, wallet, entry reason from the adoption records and join data; sponsor usernames, never emails) and caches the answer. Tank list sorts hottest first by default (grace and ready-to-activate grouping unchanged), new Heat sort and "Warm or hot" filter on the tank only. Hub down = the card exactly as before. No mail, no Telegram, no sponsor moves; adoption rules, caps and the v2 batch untouched. qa/tank-readiness.test.mjs (against a local hub) added to member + tankhold. tankready.js?v=20261118rd, my.js / admin.js / site.css ?v=20261118rd. iap-qa member ALL PASSED.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-18 19:01:46 -06:00

83 lines
6.4 KiB
JavaScript

// Holding-tank readiness (2026-11-18): the site module against a REAL local network hub (its own temp data dir, throwaway people),
// never production. Checks the entry reasons this site derives from its own records, the signed call, the cache, and that the
// hub being down leaves the tank list exactly as it was. No mail module is even loaded.
// node qa/tank-readiness.test.mjs (HUB_DIR = the network-hub checkout, default ../../network-hub)
import { createRequire } from 'module';
import fs from 'fs'; import os from 'os'; import path from 'path'; import assert from 'assert';
const require = createRequire(import.meta.url);
const HUB_DIR = process.env.HUB_DIR || path.resolve(path.dirname(new URL(import.meta.url).pathname.replace(/^\/([A-Za-z]:)/, '$1')), '../../../network-hub');
const SECRET = 'q'.repeat(40);
process.env.DATA_DIR = fs.mkdtempSync(path.join(os.tmpdir(), 'hub-rdy-'));
process.env.HUB_SITES = JSON.stringify({ iap: { secret: SECRET, seeEmails: true } });
process.env.HUB_ADMIN_SECRET = 'z'.repeat(40);
delete process.env.DATABASE_URL;
const hubStore = require(path.join(HUB_DIR, 'lib/store'));
const hubPeople = require(path.join(HUB_DIR, 'lib/people'));
const hubAct = require(path.join(HUB_DIR, 'lib/activity'));
const { start } = require(path.join(HUB_DIR, 'server'));
const TR = require('../tankready.js');
let pass = 0; const ok = (c, m) => { assert.ok(c, m); pass++; console.log(' ok', m); };
const DAY = 86400000; const now = Date.now();
// the hub knows two of the leads: one imported from an old site, one who signed in on LinkSpin yesterday
hubPeople.importRows([{ email: 'old@t.test', username: 'oldtimer', joinedAt: now - 500 * DAY }], 'vbb');
const live = hubPeople.join({ site: 'iap', email: 'live@t.test', username: 'livewire' }).person;
hubAct.record(live.id, 'linkspin', 'signin', now - DAY, now);
hubPeople.join({ site: 'iap', email: 'spons@t.test', username: 'sponsorx' });
const server = await start(0); const port = server.address().port;
process.env.HUB_URL = 'http://127.0.0.1:' + port; process.env.HUB_SECRET = SECRET; process.env.HUB_SITE = 'iap'; process.env.HUB_ENABLED = '1';
// a fake site: accounts, tank waiting list, adoption history, follow records
const accts = {
'old@t.test': { email: 'old@t.test', username: 'oldtimer', created: now - 3 * DAY, joinedVia: 'hub', joinedRef: 'hub:vbb' },
'live@t.test': { email: 'live@t.test', username: 'livewire', created: now - 10 * DAY, joinedVia: 'page', joinedRef: 'viefaucet.com', lastSeen: now - 2 * 3600e3 },
'resc@t.test': { email: 'resc@t.test', username: 'rescued1', created: now - 30 * DAY },
'dead@t.test': { email: 'dead@t.test', username: 'deadlink', created: now - 30 * DAY },
'b4h@t.test': { email: 'b4h@t.test', created: now - 7 * DAY, joinedVia: 'btc4hits', joinedRef: 'btc4hits' },
'back@t.test': { email: 'back@t.test', username: 'backagain', created: now - 40 * DAY },
'spons@t.test': { email: 'spons@t.test', username: 'sponsorx' }
};
const waiting = ['old@t.test', 'live@t.test', 'resc@t.test', 'dead@t.test', 'b4h@t.test', 'back@t.test'].map(e => ({ email: e, name: '@' + (accts[e].username || 'x'), joined: accts[e].created, lastSeen: accts[e].lastSeen || 0, wallet: e === 'live@t.test' }));
const hist = new Map([
['resc@t.test', [{ adopter: 'spons@t.test', ts: now - 4 * DAY, status: 'rescued', closed: null }]],
['dead@t.test', [{ adopter: 'dead:gone99', ts: now - 5 * DAY, status: 'rescued', closed: null }]],
['back@t.test', [{ adopter: 'spons@t.test', ts: now - 20 * DAY, status: 'rescued' }, { adopter: 'spons@t.test', ts: now - 15 * DAY, status: 'expired', closed: now - 8 * DAY }]]
]);
let calls = 0;
TR.init({ site: 'iap', accounts: { byEmail: async e => accts[e] || null }, tank: { waiting: async () => { calls++; return waiting; }, historyMap: async () => hist }, followGet: () => null });
console.log('tank readiness: local hub on :' + port);
const c = await TR.refresh();
ok(c && c.by.size === 6, 'one call returned readiness for all six leads');
const get = e => c.by.get(e);
ok(get('old@t.test').entry.reason === 'imported' && get('old@t.test').entry.detail.oldSite === 'vbb', 'hub-made account: the hub says imported from ViralBannerBlitz with the old join date');
ok(get('live@t.test').entry.reason === 'joined' && get('live@t.test').entry.detail.ref === 'viefaucet.com', 'joined with no sponsor, with the source');
ok(get('live@t.test').stage === 'activated' && get('live@t.test').heat.score >= 40, 'wallet linked = Activated, signed in recently = warm or hot (' + get('live@t.test').heat.score + ')');
ok(get('live@t.test').last.kind === 'signin' && get('live@t.test').last.site === 'iap', 'last action: the site sign-in 2 hours ago beats the LinkSpin one yesterday');
ok(get('resc@t.test').entry.reason === 'rescued' && get('resc@t.test').entry.detail.from === 'sponsorx', 'rescued, naming the sponsor by username');
ok(get('dead@t.test').entry.reason === 'sponsor_gone' && !get('dead@t.test').entry.detail.from, 'dead sponsor link: sponsor gone, no name');
ok(get('b4h@t.test').entry.reason === 'imported' && get('b4h@t.test').entry.detail.oldSite === 'BTC4Hits', 'BTC4Hits import (not on the hub: the site report alone)');
ok(get('back@t.test').entry.reason === 'returned', 'back after an adoption window ended (newest record wins)');
ok(get('b4h@t.test').stage === 'new' && get('b4h@t.test').heat.band === 'cold' && get('b4h@t.test').last === null, 'never seen: New, Cold, no activity');
const txt = JSON.stringify([...c.by.values()]);
ok(!/@t\.test/.test(txt), 'no email in anything the hub sent back');
ok(!JSON.stringify(hubStore.all().map(p => p.tankEntry || null)).includes('@'), 'no email stored in the entry facts on the hub');
ok(hubPeople.byEmail('live@t.test').tankEntry && hubPeople.byEmail('live@t.test').tankEntry.reason === 'joined', 'the entry fact is recorded on the hub');
const att = await TR.attach(waiting);
ok(att.every(w => w.rd) && calls === 1, 'attach uses the fresh cache, no second call');
ok(waiting.every(w => !w.rd), 'the original rows are not changed');
// the hub goes away: the card shows the rows as before (after the cache is gone)
server.close(); TR._reset();
const down = await TR.attach(waiting, 1500);
ok(down.length === 6 && down.every(w => !w.rd), 'hub down: the same rows, no extras, no error');
ok(TR.status().ok === false, 'status says the hub is not answering');
process.env.HUB_ENABLED = '0'; TR._reset();
ok((await TR.attach(waiting)) === waiting, 'switched off: untouched');
console.log('tank readiness: ALL PASSED (' + pass + ')');
process.exit(0);