Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 | 1x 1x 1x 1x 1x 1x 1x 1x 10x 50x 50x 6x 6x 6x 200x 200x 24x 25x 25x 24x 24x 24x 24x 8x 8x 16x 2x 24x 24x 24x 20x 20x 8x 16x 32x 32x 3x 3x 3x 3x 20x 20x 20x 20x 20x 3x 3x 11x 3x 2x 2x 4x 2x 1x 1x 21x 20x 20x 20x 20x 200x 200x 200x 20x 20x 20x 8x 20x 16x 1x | /**
* Cron: Clean up stale test data older than 1 hour.
* Only runs in development environment.
*
* Safety net for test data that wasn't cleaned up by normal teardown.
* Queries by _testRun prefix and filters by age client-side
* (avoids needing composite Firestore indexes).
*/
const { db, FieldValue } = require('../utils/firebase');
const { clearBanCache } = require('../utils/bans');
const { restoreUniqueIdCounter } = require('../utils/unique-id-counter');
const log = require('../utils/log');
const TEST_PREFIX = 'test_';
const MAX_AGE_MS = 60 * 60 * 1000; // 1 hour
// All collections that test-helpers.js tags with _testRun
const TAGGED_COLLECTIONS = [
'users',
'rooms',
'gifts',
'conversations',
'banners',
'reports',
'suspensionAppeals',
'alerts',
'deviceBindings',
'reportLocks',
];
// Subcollections to delete before their parent doc
const USER_SUBCOLLECTIONS = ['warnings', 'transactions', 'backpack', 'stalkers', 'giftWall'];
async function deleteSubcollections(docRef, subcollections) {
for (const sub of subcollections) {
const snap = await docRef.collection(sub).limit(500).get();
if (snap.empty) continue;
const batch = db.batch();
snap.docs.forEach((d) => batch.delete(d.ref));
await batch.commit();
}
}
/** Delete stale docs from a tagged collection. Returns count and any deleted user IDs. */
async function cleanupTaggedCollection(colName, cutoff) {
const snap = await db
.collection(colName)
.where('_testRun', '>=', TEST_PREFIX)
.where('_testRun', '<', TEST_PREFIX + '\uf8ff')
.limit(500)
.get();
if (snap.empty) return { deleted: 0, userIds: [] };
const staleDocs = snap.docs.filter((doc) => {
const createdAt = doc.data().createdAt;
return typeof createdAt === 'number' ? createdAt < cutoff : true;
});
let deleted = 0;
const userIds = [];
for (const doc of staleDocs) {
if (colName === 'users') {
userIds.push(doc.data().uniqueId || doc.id);
await deleteSubcollections(doc.ref, USER_SUBCOLLECTIONS);
} else if (colName === 'conversations') {
await deleteSubcollections(doc.ref, [
'messages',
'userSettings',
'mutes',
'settings',
'mod_log',
]);
}
await doc.ref.delete();
deleted++;
}
return { deleted, userIds };
}
/** Clean up device/network bans linked to deleted test users. */
async function cleanupLinkedBans(deletedUserUniqueIds) {
let deleted = 0;
for (const uid of deletedUserUniqueIds) {
for (const uidVariant of [uid, String(uid)]) {
for (const banCol of ['deviceBans', 'networkBans']) {
const banSnap = await db
.collection(banCol)
.where('linkedUniqueId', '==', uidVariant)
.limit(100)
.get();
if (banSnap.empty) continue;
const batch = db.batch();
banSnap.docs.forEach((d) => batch.delete(d.ref));
await batch.commit();
deleted += banSnap.size;
}
}
}
// Any deleted ban must stop gating immediately — the per-request ban gate
// caches verdicts for 5 minutes otherwise (SHY-0149).
if (deleted > 0) clearBanCache();
return deleted;
}
/** Clean up test starting screens from config document. */
async function cleanupTestStartingScreens() {
try {
const ssDoc = await db.doc('config/startingScreens').get();
if (!ssDoc.exists) return 0;
const ssData = ssDoc.data() || {};
const testScreenIds = Object.keys(ssData).filter(
(key) => key.startsWith('pw-') || key.startsWith('screen-') || key.startsWith('test-'),
);
if (testScreenIds.length === 0) return 0;
const updates = {};
for (const id of testScreenIds) {
updates[id] = FieldValue.delete();
}
await db.doc('config/startingScreens').update(updates);
return testScreenIds.length;
} catch {
// Best-effort cleanup — config deletion failure is non-critical
return 0;
}
}
async function testDataCleanup() {
if (process.env.NODE_ENV === 'production') return;
const cutoff = Date.now() - MAX_AGE_MS;
let totalDeleted = 0;
const deletedUserUniqueIds = [];
for (const colName of TAGGED_COLLECTIONS) {
const result = await cleanupTaggedCollection(colName, cutoff);
totalDeleted += result.deleted;
deletedUserUniqueIds.push(...result.userIds);
}
totalDeleted += await cleanupLinkedBans(deletedUserUniqueIds);
totalDeleted += await cleanupTestStartingScreens();
// Restore counter if test users were deleted. Type-immune + raise-only —
// see utils/unique-id-counter.js (a string-typed uniqueId doc tops the
// desc order and once poisoned this counter into string concatenation).
if (deletedUserUniqueIds.length > 0) {
await restoreUniqueIdCounter(db);
}
if (totalDeleted > 0) {
log.info('cron', 'testDataCleanup: removed stale test data', { deleted: totalDeleted });
}
}
module.exports = testDataCleanup;
|