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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 | 2x 2x 2x 2x 2x 89x 89x 89x 10x 10x 79x 2x 36x 36x 34x 34x 34x 34x 34x 34x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 18x 4x 4x 4x 2x 18x 33x 4x 4x 4x 4x 4x 33x 5x 5x 5x 5x 33x 2x 2x 2x 2x 33x 2x 2x 2x 2x 33x 2x 2x 2x 2x 2x 2x 2x 2x 33x 2x 2x 2x 2x 1x 1x 1x 1x 32x 2x 2x 2x 1x 1x 1x 31x 2x 2x 2x 2x 31x 31x 31x 30x 1x 31x 31x 3x 3x 2x 19x 19x 17x 17x 17x 5x 12x 11x 1x 10x 1x 2x 18x 18x 16x 16x 16x 1x 15x 15x 1x 14x 14x 14x 1x 14x 14x 2x 10x 10x 8x 8x 3x 5x 4x 4x 1x 1x 2x 6x 6x 4x 3x 3x 1x 1x 6x 6x 30x 30x 30x 30x 6x 12x 12x 8x 4x 12x 10x 10x 6x 6x 6x 10x 7x 3x 10x 10x 10x 1x 1x 10x 10x 6x 12x 12x 1x 1x 12x 12x 1x 1x 10x 10x 90x 63x 27x 90x 90x 10x 10x 10x 10x 5x 5x 2x 2x 10x 2x 2x | /**
* Test helper routes — available only in development.
*
* POST /api/test/setup → Create test scenario, return testRunId + created IDs
* GET /api/test/verify/:col/:id → Read Firestore doc for assertion
* POST /api/test/write/:col → Write a document to an allowed collection
* POST /api/test/teardown → Delete all data for a testRunId
* POST /api/test/reset → Wipe all test data, restore fixtures
*/
const router = require('express').Router();
const { db } = require('../utils/firebase');
const { generateId } = require('../utils/helpers');
const log = require('../utils/log');
const TEST_PREFIX = 'test_';
function requireTestApiKey(req, res) {
Iif (!process.env.TEST_API_KEY) {
res.status(500).json({ error: 'TEST_API_KEY not configured on server' });
return true;
}
const key = req.headers['x-test-api-key'];
if (!key || key !== process.env.TEST_API_KEY) {
res.status(403).json({ error: 'Invalid test API key' });
return true;
}
return false;
}
// POST /api/test/setup
router.post('/test/setup', async (req, res) => {
try {
if (requireTestApiKey(req, res)) return;
const testRunId = `${TEST_PREFIX}${generateId()}`;
const now = Date.now();
const created = {
testRunId,
users: [],
rooms: [],
gifts: [],
banners: [],
funFacts: [],
reports: [],
appeals: [],
alerts: [],
conversations: [],
economyConfig: {},
};
const spec = req.body || {};
// Create test users
let userIndex = 0;
for (const userSpec of spec.users || []) {
userIndex++;
const uid = `${testRunId}_user_${generateId()}`;
// Allocate real uniqueId via atomic counter transaction
const counterRef = db.doc('counters/uniqueId');
const uniqueId = await db.runTransaction(async (t) => {
const counterDoc = await t.get(counterRef);
const current = counterDoc.exists ? counterDoc.data().value : 100000000;
const next = current + 1;
t.set(counterRef, { value: next }, { merge: true });
return next;
});
const userData = {
uid,
firebaseUid: uid,
uniqueId,
displayName: userSpec.name || `Test User ${userIndex}`,
userType: userSpec.role || 'MEMBER',
shyCoins: userSpec.shyCoins ?? 0,
shyBeans: userSpec.shyBeans ?? 0,
gcsScore: 100,
warningCount: 0,
hasActiveWarning: false,
luckScore: 0,
pityCounter: 0,
isSuspended: false,
createdAt: now,
lastSeenAt: now,
_testRun: testRunId,
};
await db.doc(`users/${uniqueId}`).set(userData);
// Create device binding if deviceInfo is provided
if (userSpec.deviceInfo) {
const { deviceId, manufacturer, model, lastIp, isp } = userSpec.deviceInfo;
await db.doc(`deviceBindings/${deviceId}`).set({
deviceId,
uniqueId, // number — must match user doc type for Firestore queries
manufacturer: manufacturer || 'Unknown',
model: model || 'Unknown',
lastIp: lastIp || null,
isp: isp || null,
boundAt: Date.now(),
_testRun: testRunId,
});
// Also set lastIp on user doc for ban tests
if (lastIp) {
await db.doc(`users/${uniqueId}`).update({ lastIp });
}
}
created.users.push({ ...userData });
}
// Create test rooms
for (const roomSpec of spec.rooms || []) {
const roomId = `${testRunId}_room_${generateId()}`;
const ownerId = roomSpec.ownerId || (created.users[0]?.uid ?? testRunId);
const roomData = {
id: roomId,
name: `[TEST] ${roomSpec.name || 'Room'}`,
ownerId,
status: roomSpec.status || 'ACTIVE',
createdAt: now,
_testRun: testRunId,
};
await db.doc(`rooms/${roomId}`).set(roomData);
created.rooms.push(roomData);
}
// Create test gifts
for (const giftSpec of spec.gifts || []) {
const giftId = `${testRunId}_gift_${generateId()}`;
const giftData = {
id: giftId,
name: `[TEST] ${giftSpec.name || 'Gift'}`,
coinValue: giftSpec.coinValue ?? 10,
showInStore: giftSpec.showInStore ?? true,
showOnWheel: giftSpec.showOnWheel ?? true,
weight: 1,
order: 0,
animationUrl: '',
soundUrl: '',
iconUrl: '',
_testRun: testRunId,
};
await db.doc(`gifts/${giftId}`).set(giftData);
created.gifts.push(giftData);
}
// Create test banners
for (const bannerSpec of spec.banners || []) {
const bannerId = `${testRunId}_banner_${generateId()}`;
const bannerData = {
id: bannerId,
title: bannerSpec.title || 'Test Banner',
imageUrl: bannerSpec.imageUrl || '',
actionType: bannerSpec.actionType || 'NONE',
actionValue: bannerSpec.actionValue || '',
isActive: bannerSpec.isActive ?? true,
sortOrder: bannerSpec.sortOrder ?? 0,
createdAt: now,
_testRun: testRunId,
};
await db.doc(`banners/${bannerId}`).set(bannerData);
created.banners.push(bannerData);
}
// Create test fun facts
for (const factSpec of spec.funFacts || []) {
const factId = `${testRunId}_fact_${generateId()}`;
const factData = {
id: factId,
text: factSpec.text || 'Test fact',
category: factSpec.category || 'trivia',
emoji: factSpec.emoji || '📝',
sourceLanguage: factSpec.sourceLanguage || 'English',
isActive: factSpec.isActive ?? true,
createdAt: now,
_testRun: testRunId,
};
await db.doc(`funFacts/${factId}`).set(factData);
created.funFacts.push(factData);
}
// Create test conversations with messages subcollection
// (seeded BEFORE reports so reports can reference conversationIndex)
for (const convSpec of spec.conversations || []) {
const convId = `${testRunId}_conv_${generateId()}`;
const participants = convSpec.participants || [];
const convData = {
id: convId,
participants,
createdAt: now,
_testRun: testRunId,
};
await db.doc(`conversations/${convId}`).set(convData);
for (const msg of convSpec.messages || []) {
const msgId = `${testRunId}_msg_${generateId()}`;
await db.doc(`conversations/${convId}/messages/${msgId}`).set({
text: msg.text || '',
senderId: msg.senderId || '',
createdAt: now,
});
}
created.conversations.push(convData);
}
// Create test reports (index-based user references)
for (const reportSpec of spec.reports || []) {
const reportId = `${testRunId}_report_${generateId()}`;
const reportedUser = created.users[reportSpec.reportedUserIndex || 0];
const reporterUser = created.users[reportSpec.reporterUserIndex || 1];
if (!reportedUser || !reporterUser) throw new Error('Report seed requires at least 2 users');
// Link to a seeded conversation if conversationIndex is provided
const linkedConv =
reportSpec.conversationIndex !== undefined && reportSpec.conversationIndex !== null
? created.conversations[reportSpec.conversationIndex]
: null;
const reportData = {
id: reportId,
reportedUserId: reportedUser.uid,
reportedUserUniqueId: reportedUser.uniqueId,
reportedUserName: reportedUser.displayName,
reporterId: reporterUser.uid,
reporterName: reporterUser.displayName,
reason: reportSpec.reason || 'Spam',
status: reportSpec.status || 'pending',
...(linkedConv ? { conversationId: linkedConv.id } : {}),
createdAt: now,
_testRun: testRunId,
};
await db.doc(`reports/${reportId}`).set(reportData);
created.reports.push(reportData);
}
// Create test suspension appeals
// NOTE: Does NOT set user as suspended — appeal tests manage suspension state themselves
// to avoid cross-file fragility (other tests depend on user not being suspended)
for (const appealSpec of spec.appeals || []) {
const appealId = `${testRunId}_appeal_${generateId()}`;
const appealUser = created.users[appealSpec.userIndex || 0];
if (!appealUser) throw new Error('Appeal seed requires users to be seeded first');
const appealData = {
id: appealId,
userId: appealUser.uniqueId,
appealText: appealSpec.appealText || 'I did not do this',
status: appealSpec.status || 'pending',
createdAt: now,
_testRun: testRunId,
};
await db.doc(`suspensionAppeals/${appealId}`).set(appealData);
created.appeals.push(appealData);
}
// Create test alerts
for (const alertSpec of spec.alerts || []) {
const alertId = `${testRunId}_alert_${generateId()}`;
const alertData = {
id: alertId,
type: alertSpec.type || 'error_rate',
severity: alertSpec.severity || 'medium',
message: alertSpec.message || 'Test alert',
status: alertSpec.status || 'new',
createdAt: now,
_testRun: testRunId,
};
await db.doc(`alerts/${alertId}`).set(alertData);
created.alerts.push(alertData);
}
// Read current economy config for backup/restore
// If the doc doesn't exist, use production defaults so restore always has valid data
const ECONOMY_DEFAULTS = {
beanConversionRate: 0.6,
beanRedeemBonusThreshold: 2000,
beanRedeemBonusMultiplier: 1.1,
pullCosts: { 1: 10, 10: 100, 100: 1000 },
broadcastSendThreshold: 0,
broadcastWinThreshold: 5000,
dropRateExponent: 1.5,
pitySoftStart: 80,
pityHardLimit: 120,
pitySoftMaxShift: 0.15,
pityHighValueThreshold: 5000,
dailyBase: 50,
milestoneRewards: { 7: 100, 14: 200, 30: 500, 60: 1000, 90: 2000 },
};
try {
const ecoDoc = await db.doc('config/economy').get();
created.economyConfig = ecoDoc.exists ? ecoDoc.data() : ECONOMY_DEFAULTS;
} catch {
// Fallback to defaults — economy config fetch is non-critical for test setup
created.economyConfig = ECONOMY_DEFAULTS;
}
log.info('test-helpers', 'Test setup complete', {
testRunId,
users: created.users.length,
rooms: created.rooms.length,
});
res.json(created);
} catch (err) {
log.error('test-helpers', 'Setup failed', { error: err.message });
res.status(500).json({ error: err.message });
}
});
// GET /api/test/verify/:collection/:id
router.get('/test/verify/:collection/:id', async (req, res) => {
try {
if (requireTestApiKey(req, res)) return;
const { collection, id } = req.params;
const ALLOWED_COLLECTIONS = [
'users',
'rooms',
'gifts',
'conversations',
'banners',
'funFacts',
'reports',
'suspensionAppeals',
'alerts',
];
if (!ALLOWED_COLLECTIONS.includes(collection)) {
return res.status(400).json({ error: 'Collection not allowed' });
}
const doc = await db.doc(`${collection}/${id}`).get();
if (!doc.exists) {
return res.status(404).json({ error: 'Document not found' });
}
res.json({ id: doc.id, ...doc.data() });
} catch (err) {
res.status(500).json({ error: err.message });
}
});
// POST /api/test/write/:collection — write a document to an allowed collection
router.post('/test/write/:collection', async (req, res) => {
try {
if (requireTestApiKey(req, res)) return;
const { collection } = req.params;
const ALLOWED_COLLECTIONS = [
'users',
'rooms',
'gifts',
'conversations',
'banners',
'funFacts',
'reports',
'suspensionAppeals',
'alerts',
];
if (!ALLOWED_COLLECTIONS.includes(collection)) {
return res.status(400).json({ error: 'Collection not allowed' });
}
const data = req.body;
if (!data || typeof data !== 'object') {
return res.status(400).json({ error: 'Request body must be a JSON object' });
}
const docId = data.id || generateId();
const writeData = { ...data, id: docId };
// Propagate _testRun so teardown can clean up documents created via this endpoint
if (data._testRun) {
writeData._testRun = data._testRun;
}
await db.doc(`${collection}/${docId}`).set(writeData, { merge: true });
res.json({ success: true, id: docId });
} catch (err) {
res.status(500).json({ error: err.message });
}
});
// POST /api/test/teardown
router.post('/test/teardown', async (req, res) => {
try {
if (requireTestApiKey(req, res)) return;
const { testRunId } = req.body;
if (!testRunId?.startsWith(TEST_PREFIX)) {
return res.status(400).json({ error: 'Invalid testRunId' });
}
const deleted = await deleteTestData(testRunId);
log.info('test-helpers', 'Teardown complete', { testRunId, deleted });
res.json({ success: true, deleted });
} catch (err) {
log.error('test-helpers', 'Teardown failed', { error: err.message });
res.status(500).json({ error: err.message });
}
});
// POST /api/test/reset — wipe ALL test data
router.post('/test/reset', async (req, res) => {
try {
if (requireTestApiKey(req, res)) return;
const deleted = await deleteTestData(null);
log.info('test-helpers', 'Full test reset complete', { deleted });
res.json({ success: true, deleted });
} catch (err) {
log.error('test-helpers', 'Reset failed', { error: err.message });
res.status(500).json({ error: err.message });
}
});
/** Delete all docs in known subcollections, then the parent doc */
async function deleteDocWithSubcollections(docRef) {
const subcollections = ['warnings', 'transactions', 'backpack', 'stalkers', 'giftWall'];
for (const sub of subcollections) {
const snap = await docRef.collection(sub).get();
const batch = db.batch();
snap.docs.forEach((d) => batch.delete(d.ref));
if (snap.size > 0) await batch.commit();
}
await docRef.delete();
}
/**
* Delete test data. If testRunId is null, deletes ALL test data.
*/
async function deleteTestData(testRunId) {
let deleted = 0;
// 1. Find all test users and delete docs + subcollections
let userQuery;
if (testRunId) {
userQuery = db.collection('users').where('_testRun', '==', testRunId);
} else {
userQuery = db.collection('users').where('_testRun', '>=', TEST_PREFIX);
}
const userSnap = await userQuery.get();
const userUniqueIds = [];
for (const doc of userSnap.docs) {
userUniqueIds.push(doc.data().uniqueId || doc.id);
await deleteDocWithSubcollections(doc.ref);
deleted++;
}
// 2. Delete device bindings tagged with this testRun
let bindingQuery;
if (testRunId) {
bindingQuery = db.collection('deviceBindings').where('_testRun', '==', testRunId);
} else {
bindingQuery = db.collection('deviceBindings').where('_testRun', '>=', TEST_PREFIX);
}
const bindingSnap = await bindingQuery.get();
const batch1 = db.batch();
for (const doc of bindingSnap.docs) {
batch1.delete(doc.ref);
deleted++;
}
if (bindingSnap.size > 0) await batch1.commit();
// 3. Delete device and network bans linked to test users
// Query both number and string variants (Firestore equality is type-strict)
for (const uid of userUniqueIds) {
for (const uidVariant of [uid, String(uid)]) {
const deviceBanSnap = await db
.collection('deviceBans')
.where('linkedUniqueId', '==', uidVariant)
.get();
for (const doc of deviceBanSnap.docs) {
await doc.ref.delete();
deleted++;
}
const networkBanSnap = await db
.collection('networkBans')
.where('linkedUniqueId', '==', uidVariant)
.get();
for (const doc of networkBanSnap.docs) {
await doc.ref.delete();
deleted++;
}
}
}
// 4. Delete other top-level test docs (gifts, rooms, banners, funFacts, conversations, etc.)
// Note: system PMs created by admin actions won't have _testRun set — accepted trade-off
const otherCollections = [
'gifts',
'rooms',
'banners',
'funFacts',
'conversations',
'reports',
'suspensionAppeals',
'alerts',
'reportLocks',
];
for (const col of otherCollections) {
let query;
if (testRunId) {
query = db.collection(col).where('_testRun', '==', testRunId);
} else {
query = db.collection(col).where('_testRun', '>=', TEST_PREFIX);
}
const snap = await query.get();
for (const doc of snap.docs) {
if (col === 'conversations') {
// Conversations have messages subcollection — delete those first
const msgSnap = await doc.ref.collection('messages').get();
const msgBatch = db.batch();
msgSnap.docs.forEach((m) => msgBatch.delete(m.ref));
if (msgSnap.size > 0) await msgBatch.commit();
}
await doc.ref.delete();
deleted++;
}
}
// 5. Clean up starting screens config document
// Starting screens live as fields in a single doc, not as individual collection docs,
// so _testRun-based queries can't find them.
try {
const ssDoc = await db.doc('config/startingScreens').get();
Iif (ssDoc.exists) {
const ssData = ssDoc.data() || {};
const testScreenIds = Object.keys(ssData).filter(
(key) => key.startsWith('pw-') || key.startsWith('screen-') || key.startsWith('test-'),
);
if (testScreenIds.length > 0) {
const { FieldValue } = require('firebase-admin/firestore');
const updates = {};
for (const id of testScreenIds) {
updates[id] = FieldValue.delete();
}
await db.doc('config/startingScreens').update(updates);
deleted += testScreenIds.length;
}
}
} catch {
// Best-effort cleanup — config deletion failure is non-critical
}
// 6. Restore uniqueId counter to the highest remaining real user (best-effort)
if (userUniqueIds.length > 0) {
try {
const maxSnap = await db.collection('users').orderBy('uniqueId', 'desc').limit(1).get();
const maxId = maxSnap.empty ? 100000000 : maxSnap.docs[0].data().uniqueId;
await db.doc('counters/uniqueId').set({ value: maxId }, { merge: true });
} catch {
// Best-effort — counter restoration failure does not block test cleanup
}
}
return deleted;
}
module.exports = router;
module.exports.deleteTestData = deleteTestData;
|