// LinkSpin member erase (erase.js + scrub.js, 2026-11-15, ticket LS-1021) in JSON mode on a throwaway DATA_DIR, synthetic members only: // scrub rules, dry run changes nothing, a module's live copy is scrubbed and saved (not overwritten later), plain documents // and JSON Lines through the fs layer, their spin links (rotator.eraseOwner), the tombstone, blockers + force, the hub's // member.erased event through hubsync.receive, and scrub.js being the same file in the sibling InstantAdPay checkout. // node qa/erase.test.cjs 'use strict'; const fs = require('fs'); const os = require('os'); const path = require('path'); const crypto = require('crypto'); const assert = require('assert'); const ROOT = path.join(__dirname, '..'); 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 && e.stack ? e.stack.split('\n').slice(0, 3).join(' | ') : e); } } delete process.env.DATABASE_URL; delete process.env.DOCSTORE_URL; const { scrubDeep, scrubText, mentions } = require(path.join(ROOT, 'scrub.js')); const E = 'gone.person9@example.test', OTHER = 'stay@example.test'; const needles = { emails: [E], exact: ['goneuser', 'abc1234'] }; (async () => { await t('scrub: keys, list items, records and map values go; other people and look-alikes stay', () => { const doc = { byEmail: { [E]: { u: 'goneuser' }, [OTHER]: { u: 'stay', sponsorEmail: 'x@example.test' } }, byCode: { abc1234: E, zzz9999: OTHER }, sends: [{ subject: 'Hi', recipients: [OTHER, E, 'xgone.person9@example.test'], delivered: [E] }], log: [{ ts: 1, email: E, action: 'adopt' }, { ts: 2, email: OTHER }, { ts: 3, note: 'tankpay1:' + E + ':17' }], clicks: { a1: { owner: E, n: 3 }, a2: { owner: OTHER, n: 1 } }, own: { 's:1': { [E]: 5, [OTHER]: 6 } }, nums: [1, 2, 3], names: ['goneuserx', 'goneuser'], text: 'goneuser is a word here, kept', prov: { [OTHER]: { sponsorEmail: E, at: 1 } }, team: [{ email: OTHER, sponsorEmail: E, ts: 4 }] }; const removed = scrubDeep(doc, needles); assert.ok(removed >= 9, 'removed ' + removed); assert.deepStrictEqual(Object.keys(doc.byEmail), [OTHER]); assert.deepStrictEqual(doc.byCode, { zzz9999: OTHER }); assert.deepStrictEqual(doc.sends[0].recipients, [OTHER, 'xgone.person9@example.test'], 'a longer address that contains it is someone else'); assert.deepStrictEqual(doc.sends[0].delivered, []); assert.deepStrictEqual(doc.log.map(x => x.ts), [2]); assert.deepStrictEqual(Object.keys(doc.clicks), ['a2']); assert.deepStrictEqual(doc.own, { 's:1': { [OTHER]: 6 } }); assert.deepStrictEqual(doc.names, ['goneuserx'], 'a username only matches a whole string'); assert.strictEqual(doc.text, 'goneuser is a word here, kept'); assert.deepStrictEqual(doc.nums, [1, 2, 3]); assert.deepStrictEqual(doc.prov, { [OTHER]: { sponsorEmail: null, at: 1 } }, "another member's record only loses the pointer"); assert.deepStrictEqual(doc.team, [{ email: OTHER, sponsorEmail: null, ts: 4 }]); const lines = [JSON.stringify({ e: OTHER }), JSON.stringify({ e: E }), JSON.stringify({ u: 'goneuser' })].join('\n'); const r = scrubText(lines, needles, true); assert.strictEqual(r.removed, 2); assert.strictEqual(r.body, JSON.stringify({ e: OTHER })); assert.strictEqual(mentions('{"a":"' + OTHER + '"}', needles), false); }); // ---- erase.js on a throwaway data dir ---- const DIR = fs.mkdtempSync(path.join(os.tmpdir(), 'erase-iap-')); const W = (f, o) => fs.writeFileSync(path.join(DIR, f), typeof o === 'string' ? o : JSON.stringify(o)); const Rd = f => JSON.parse(fs.readFileSync(path.join(DIR, f), 'utf8')); W('updates-log.json', { sends: [{ subject: 'S', recipients: [OTHER, E], delivered: [E] }] }); W('legacy.json', { [E]: { b: 'faucetwave', s: 'e' }, [OTHER]: { b: 'tier1ads', s: 'a' } }); W('events.jsonl', JSON.stringify({ e: E }) + '\n' + JSON.stringify({ e: OTHER })); W('unrelated.json', { a: OTHER }); // a module that keeps its document in memory and writes it back later (the reason erase works through the live copy) let live = { rows: { [E]: { email: E, username: 'goneuser' }, [OTHER]: { email: OTHER } }, log: [{ email: E }] }; let saves = 0; W('lsinbound.json', live); const owner = { state: () => live, save: () => { saves++; W('lsinbound.json', live); } }; let reloads = 0; const accts = { [E]: { email: E, username: 'goneuser', code: 'abc1234', address: null, memberId: 0 }, [OTHER]: { email: OTHER, username: 'stay', code: 'zzz9999', sponsorRef: '' }, 'walleted@example.test': { email: 'walleted@example.test', username: 'wal', code: 'w1', address: '0x' + '1'.repeat(40), memberId: 0 } }; const accounts = { byEmail: async e => accts[e] || null, positions: async () => [], identities: async () => Object.values(accts), listByReferrer: async refs => Object.values(accts).filter(a => refs.includes(a.sponsorRef)), removeAccount: async e => { if (!accts[e]) return { error: 'none' }; delete accts[e]; return { ok: true }; } }; accounts.listAll = async () => Object.values(accts); let rot = [{ id: 7, owner: E }, { id: 8, owner: OTHER }]; const rotator = { eraseOwner: async (e, dry) => { const mine = rot.filter(r => r.owner === e); if (!dry) rot = rot.filter(r => r.owner !== e); return { rotations: mine.length, hits: 0, conv: 0 }; } }; const erase = require(path.join(ROOT, 'erase.js')); let forgot = []; erase.init({ dataDir: DIR, accounts, ads: { listCampaigns: async () => [] }, rotator, docstore: null, owners: { 'lsinbound.json': owner }, reload: { 'legacy.json': () => reloads++ }, forget: [e => forgot.push(e)] }); const before = fs.readFileSync(path.join(DIR, 'updates-log.json'), 'utf8'); await t('dry run (the default) counts and changes nothing', async () => { const r = await erase.erase(E); assert.strictEqual(r.dryRun, true); assert.strictEqual(r.found, true); assert.deepStrictEqual(r.blockers, []); assert.deepStrictEqual(Object.keys(r.docs).sort(), ['events.jsonl', 'legacy.json', 'lsinbound.json', 'updates-log.json']); assert.strictEqual(fs.readFileSync(path.join(DIR, 'updates-log.json'), 'utf8'), before); assert.ok(live.rows[E], 'live copy untouched'); assert.ok(accts[E]); assert.strictEqual(erase.isErased(E), false); assert.strictEqual(saves, 0); assert.strictEqual(r.spinLinks.rotations, 1); assert.strictEqual(rot.length, 2); }); await t('a linked wallet blocks it, force overrides', async () => { const r = await erase.erase('walleted@example.test', { dryRun: false }); assert.ok(/main wallet/.test(r.error)); assert.ok(accts['walleted@example.test']); const f = await erase.erase('walleted@example.test', { dryRun: false, force: true }); assert.strictEqual(f.forced, true); assert.ok(!accts['walleted@example.test']); }); await t('erase: the account, the live copy (saved), plain docs, JSON Lines; reload hook; caches; tombstone; others intact', async () => { const r = await erase.erase(E, { dryRun: false }); assert.strictEqual(r.ok, true); assert.ok(!accts[E]); assert.ok(accts[OTHER]); assert.deepStrictEqual(rot, [{ id: 8, owner: OTHER }], 'their spin link went, not the other one'); assert.deepStrictEqual(Object.keys(live.rows), [OTHER]); assert.deepStrictEqual(live.log, []); assert.strictEqual(saves, 1); assert.deepStrictEqual(Rd('lsinbound.json').rows[E], undefined); assert.deepStrictEqual(Rd('updates-log.json').sends[0], { subject: 'S', recipients: [OTHER], delivered: [] }); assert.deepStrictEqual(Object.keys(Rd('legacy.json')), [OTHER]); assert.strictEqual(reloads, 1); assert.strictEqual(fs.readFileSync(path.join(DIR, 'events.jsonl'), 'utf8'), JSON.stringify({ e: OTHER })); assert.deepStrictEqual(r.docResidue, []); assert.deepStrictEqual(forgot.slice(-1), [E]); assert.strictEqual(erase.isErased(E), true); assert.strictEqual(erase.isErased(OTHER), false); const log = fs.readFileSync(path.join(DIR, 'erase.json'), 'utf8'); assert.ok(!log.includes(E), 'the erase log keeps no email'); assert.ok(log.includes(erase.hashOf(E).slice(0, 16))); const again = await erase.erase(E, { dryRun: false }); assert.strictEqual(again.found, false); assert.deepStrictEqual(again.docs, {}, 'a second run finds nothing'); }); await t('hub member.erased: found by the email hash through hubsync.receive, answered 200', async () => { accts['hubgone@example.test'] = { email: 'hubgone@example.test', username: 'hg', code: 'hg12345', address: null, memberId: 0 }; process.env.HUB_ENABLED = '1'; process.env.HUB_URL = 'http://127.0.0.1:9'; process.env.HUB_SECRET = 's'.repeat(40); const hubsync = require(path.join(ROOT, 'hubsync.js')); hubsync.init({ dataDir: DIR, accounts, registry: {}, carry: { hold() {}, unhold() {} }, follow: null, chain: null, erase, erased: e => erase.isErased(e) }); const raw = JSON.stringify({ events: [{ seq: 9001, at: Date.now(), type: 'member.erased', personId: 'h_' + '0'.repeat(16), data: { emailHash: erase.hashOf('HubGone@example.test') } }] }); const ts = String(Date.now()); const sig = crypto.createHmac('sha256', 's'.repeat(40)).update(ts + '\n' + raw).digest('hex'); const r = await hubsync.receive({ 'x-hub-ts': ts, 'x-hub-sig': sig }, raw); assert.strictEqual(r.status, 200); assert.ok(!accts['hubgone@example.test'], 'erased'); assert.strictEqual(erase.isErased('hubgone@example.test'), true); const r2 = await hubsync.receive({ 'x-hub-ts': ts, 'x-hub-sig': sig }, raw); assert.strictEqual(r2.status, 200, 'delivered twice is fine'); }); await t('scrub.js is the same file in the sibling checkout (InstantAdPay <-> LinkSpin)', () => { const sib = [path.join(ROOT, '..', '..', 'linkspin', 'site'), path.join(ROOT, '..', '..', 'onchain-ad-membership', 'site'), path.join(ROOT, '..', 'ls-erase'), path.join(ROOT, '..', 'iap-erase')] .map(d => path.join(d, 'scrub.js')).find(f => fs.existsSync(f) && path.resolve(f) !== path.resolve(path.join(ROOT, 'scrub.js'))); if (!sib) { console.log(' (sibling scrub.js not found, skipped)'); return; } const h = f => crypto.createHash('sha1').update(fs.readFileSync(f, 'utf8').replace(/\r\n/g, '\n')).digest('hex'); assert.strictEqual(h(path.join(ROOT, 'scrub.js')), h(sib), 'scrub.js differs from ' + sib); }); console.log(fails ? fails + ' of ' + n + ' FAILED' : 'linkspin erase: all ' + n + ' passed'); process.exit(fails ? 1 : 0); })();