All files / src/cron testDataCleanup.js

100% Statements 78/78
100% Branches 31/31
100% Functions 9/9
100% Lines 70/70

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                  1x 1x   1x 1x     1x                             1x     10x 50x 50x 5x 7x 5x           198x             198x   10x 12x 12x     10x 10x   10x 11x 9x 9x 2x 1x               11x 11x     10x         18x 18x 9x 18x 36x         36x 1x 1x 1x 1x       18x         18x 18x 17x   4x 4x 13x   4x   2x 2x 6x   2x 2x     1x         19x   18x 18x 18x   18x 198x 198x 198x     18x 18x     18x 8x 8x 8x 8x     18x 12x       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 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',
  'funFacts',
  '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;
      }
    }
  }
  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
  if (deletedUserUniqueIds.length > 0) {
    const counterRef = db.doc('counters/uniqueId');
    const maxSnap = await db.collection('users').orderBy('uniqueId', 'desc').limit(1).get();
    const maxId = maxSnap.empty ? 100000000 : maxSnap.docs[0].data().uniqueId;
    await counterRef.set({ value: maxId }, { merge: true });
  }
 
  if (totalDeleted > 0) {
    log.info('cron', 'testDataCleanup: removed stale test data', { deleted: totalDeleted });
  }
}
 
module.exports = testDataCleanup;