84d566e784
erase.js runs inside the app so the documents modules keep in memory and the MySQL rows stay consistent. Refused unless
force: a main wallet, an on-chain member number, positions, a team here, ads still running or queued. SQL: the account,
sessions, signals, credits (unspent ad credits go away), credit log, views, visits, drips, nudges, digests, adoptions,
marks, promo redemptions, prospects, pipeline notes, their ads and every row hanging off them; messages they sent and
notices to them; a chat another member sent them stays for that member with a tombstone recipient; help-desk tickets
stay as history with the requester tombstoned and their own messages blanked; other members' mute lists. Documents:
every doc that mentions them is scrubbed by scrub.js (new, same file in LinkSpin) through the owning module's live copy
(lsinbound, lssync, sponsorfollow, refill, tank-ready, hubsync) or through docstore (legacy reloaded). A residue sweep
over every text column reports anything left. A 30-day email-hash tombstone (erase.json) stops lsinbound and the hub
path from bringing them back; the log keeps hashes and counts only. hubsync handles member.erased {emailHash}.
qa/erase.test.cjs 6 pass (JSON mode); qa/erase.mysql.test.cjs passes against a disposable mysql:8 on core.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
71 lines
7.3 KiB
JavaScript
71 lines
7.3 KiB
JavaScript
// Member erase (erase.js) against a REAL, disposable MySQL: the SQL half (deletes, the chat message another member keeps,
|
|
// the help-desk ticket kept as scrubbed history, other members' mute lists, the residue sweep). Skipped without
|
|
// TEST_DATABASE_URL. Synthetic members only; NEVER point it at a live database (it creates and fills tables).
|
|
// TEST_DATABASE_URL=mysql://root:pw@127.0.0.1:3306/scratch node qa/erase.mysql.test.cjs
|
|
'use strict';
|
|
const fs = require('fs'); const os = require('os'); const path = require('path'); const assert = require('assert');
|
|
const ROOT = path.join(__dirname, '..');
|
|
const URL_ = process.env.TEST_DATABASE_URL;
|
|
if (!URL_) { console.log('erase mysql: TEST_DATABASE_URL not set, skipped'); process.exit(0); }
|
|
process.env.DATABASE_URL = URL_; delete process.env.DOCSTORE_URL;
|
|
const db = require(path.join(ROOT, 'db.js')); const accounts = require(path.join(ROOT, 'accounts.js')); const erase = require(path.join(ROOT, 'erase.js'));
|
|
const E = 'gone9@example.test', O = 'other9@example.test';
|
|
(async () => {
|
|
const DIR = fs.mkdtempSync(path.join(os.tmpdir(), 'erase-sql-'));
|
|
{ // db.js alters promo_codes and visit_seen before it creates them (fine on the live database, not on an empty one)
|
|
const c0 = await require('mysql2/promise').createConnection(URL_);
|
|
await c0.query(`CREATE TABLE IF NOT EXISTS promo_codes (code VARCHAR(24) NOT NULL PRIMARY KEY, credits INT NOT NULL, partner VARCHAR(80) NULL, note VARCHAR(200) NULL,
|
|
max_uses INT NOT NULL DEFAULT 0, expires BIGINT NOT NULL DEFAULT 0, active TINYINT NOT NULL DEFAULT 1, created BIGINT NOT NULL)`);
|
|
await c0.query('CREATE TABLE IF NOT EXISTS visit_seen (campaign_id INT NOT NULL, email VARCHAR(190) NOT NULL, day CHAR(10) NOT NULL, ts BIGINT NOT NULL, UNIQUE KEY uq_visit (campaign_id, email), INDEX (email, day))');
|
|
await c0.end();
|
|
}
|
|
await db.init({ dataDir: DIR }); accounts.init({ dataDir: DIR });
|
|
const q = db.q;
|
|
await q('CREATE TABLE IF NOT EXISTS account_signals (email VARCHAR(190) PRIMARY KEY, signup_ip VARCHAR(45) NULL)');
|
|
await q('CREATE TABLE IF NOT EXISTS ad_clicks (id VARCHAR(16) PRIMARY KEY, campaign_id INT NOT NULL, owner_email VARCHAR(190) NOT NULL, ts BIGINT NOT NULL)');
|
|
await q('CREATE TABLE IF NOT EXISTS event_marks (k VARCHAR(160) PRIMARY KEY, ts BIGINT NOT NULL)');
|
|
const now = Date.now();
|
|
await q("INSERT INTO accounts (email, sponsor_ref, code, created, username) VALUES (?, 'akuwhite', 'gonec01', ?, 'goneuser9'), (?, '', 'othc01', ?, 'other9')", [E, now, O, now]);
|
|
await q('UPDATE accounts SET chat_mutes=? WHERE email=?', [JSON.stringify([E, 'x@example.test']), O]);
|
|
await q("INSERT INTO sessions (token, email, member_id, expires) VALUES ('t1', ?, 0, ?)", [E, now + 1e6]);
|
|
const c = await q("INSERT INTO campaigns (owner_email, member_id, type, name, target_url, budget, status, created) VALUES (?, 0, 'visits', 'x', 'https://x.test', 60, 'out', ?)", [E, now]);
|
|
const cid = c.insertId;
|
|
await q("INSERT INTO camp_hours (campaign_id, day, hour, n) VALUES (?, '2026-11-01', 1, 3)", [cid]);
|
|
await q("INSERT INTO visit_seen (campaign_id, email, day, ts) VALUES (?, ?, '2026-11-01', 1)", [cid, O]); // another member's visit to THEIR ad
|
|
await q("INSERT INTO visit_seen (campaign_id, email, day, ts) VALUES (999, ?, '2026-11-01', 1)", [E]);
|
|
await q("INSERT INTO ad_clicks (id, campaign_id, owner_email, ts) VALUES ('c1', ?, ?, 1)", [cid, E]);
|
|
await q('INSERT INTO earned_credits (email, balance, updated) VALUES (?, 551, 1), (?, 10, 1)', [E, O]);
|
|
await q("INSERT INTO credit_log (email, ts, day, delta, kind, note) VALUES (?, 1, '2026-11-01', 5, 'x', 'n')", [E]);
|
|
await q("INSERT INTO account_signals (email, signup_ip) VALUES (?, '1.2.3.4')", [E]);
|
|
await q("INSERT INTO adoptions (adoptee, adopter, ts, expires) VALUES (?, ?, 1, 2)", [E, O]);
|
|
await q("INSERT INTO event_marks (k, ts) VALUES (?, 1), ('keep:other', 1)", ['tankpay1:' + E + ':17']);
|
|
await q("INSERT INTO sponsor_messages (from_member, from_email, to_email, subject, body, sent, kind) VALUES (0, ?, ?, '', 'their words', 1, 'chat'), (0, ?, ?, '', 'my words', 2, 'chat'), (1, 'admin@example.test', ?, 'Your ad', 'n', 3, 'notice')", [E, O, O, E, E]);
|
|
const tk = await q("INSERT INTO tickets (ref, platform, email, name, username, subject, category, created, updated) VALUES ('LS-9001', 'linkspin', ?, 'Gone Person', 'goneuser9', 'delete me', 'account', 1, 1)", [E]);
|
|
await q("INSERT INTO ticket_messages (ticket_id, author, body, created) VALUES (?, 'member', 'please delete gone9@example.test', 1), (?, 'admin', 'Done.', 2)", [tk.insertId, tk.insertId]);
|
|
let fails = 0;
|
|
const t = async (name, fn) => { try { await fn(); console.log('ok', name); } catch (e) { fails++; console.log('FAIL', name, '::', e.stack.split('\n').slice(0, 3).join(' | ')); } };
|
|
erase.init({ dataDir: DIR, accounts, docstore: null, owners: {}, reload: {}, forget: [] });
|
|
await t('dry run counts every table and changes nothing', async () => {
|
|
const r = await erase.erase(E);
|
|
assert.strictEqual(r.sql.accounts, 1); assert.strictEqual(r.sql.campaigns, 1); assert.strictEqual(r.sql.camp_hours, 1); assert.strictEqual(r.sql.visit_seen, 2);
|
|
assert.strictEqual(r.sql.sponsor_messages, 2, 'sent by them + the notice to them'); assert.strictEqual(r.sql['sponsor_messages (kept, scrubbed)'], 1);
|
|
assert.strictEqual(r.sql['tickets (kept, scrubbed)'], 1); assert.strictEqual(r.sql['ticket_messages (kept, scrubbed)'], 1);
|
|
assert.strictEqual(r.sql['accounts.chat_mutes (others, scrubbed)'], 1);
|
|
assert.ok(r.residue.length > 0); assert.strictEqual((await q('SELECT COUNT(*) n FROM accounts WHERE email=?', [E]))[0].n, 1);
|
|
});
|
|
await t('erase: rows gone, the other member keeps their message (recipient tombstoned), the ticket stays scrubbed, residue empty', async () => {
|
|
const r = await erase.erase(E, { dryRun: false });
|
|
assert.strictEqual(r.ok, true); assert.deepStrictEqual(r.residue, [], JSON.stringify(r.residue));
|
|
const one = async (sql, a) => Number((await q(sql, a))[0].n);
|
|
assert.strictEqual(await one('SELECT COUNT(*) n FROM accounts WHERE email=?', [E]), 0); assert.strictEqual(await one('SELECT COUNT(*) n FROM accounts WHERE email=?', [O]), 1);
|
|
assert.strictEqual(await one('SELECT COUNT(*) n FROM campaigns WHERE id=?', [cid]), 0); assert.strictEqual(await one('SELECT COUNT(*) n FROM visit_seen WHERE campaign_id=?', [cid]), 0);
|
|
assert.strictEqual(await one('SELECT COUNT(*) n FROM earned_credits WHERE email=?', [O]), 1); assert.strictEqual(await one("SELECT COUNT(*) n FROM event_marks WHERE k='keep:other'"), 1);
|
|
const msgs = await q('SELECT from_email, to_email, body FROM sponsor_messages ORDER BY sent');
|
|
assert.deepStrictEqual(msgs.map(m => [m.from_email, m.body]), [[O, 'my words']]); assert.ok(/^erased-[0-9a-f]{12}@erased\.invalid$/.test(msgs[0].to_email));
|
|
const [tkt] = await q("SELECT email, name, username FROM tickets WHERE ref='LS-9001'"); assert.ok(/@erased\.invalid$/.test(tkt.email)); assert.strictEqual(tkt.name, null);
|
|
const tm = await q('SELECT author, body FROM ticket_messages ORDER BY id'); assert.ok(/removed/.test(tm[0].body)); assert.strictEqual(tm[1].body, 'Done.');
|
|
assert.deepStrictEqual(JSON.parse((await q('SELECT chat_mutes FROM accounts WHERE email=?', [O]))[0].chat_mutes), ['x@example.test']);
|
|
});
|
|
console.log(fails ? fails + ' FAILED' : 'erase mysql: all passed'); process.exit(fails ? 1 : 0);
|
|
})().catch(e => { console.error(e); process.exit(1); });
|