Files
cgm-remote-monitor/tests/hooks.js
T
Ben WestandCopilot 6a681509f3 feat: add database-level production safety checks (GAP-SYNC-047)
Adds multi-layer protection against running destructive tests on production:

1. Pre-flight check (hooks.js): Verifies NODE_ENV=test before any DB connection
2. Database name check: Requires 'test' substring in database name
3. Entry count threshold: Refuses if database has >100 entries (configurable)

Environment Variables:
- TEST_SAFETY_MAX_ENTRIES: Max entries before refusing (default: 100)
- TEST_SAFETY_REQUIRE_TEST_DB: Require 'test' in DB name (default: true)
- TEST_SAFETY_SKIP: Emergency bypass for all checks (default: false)

Files:
- tests/lib/production-safety.js: Core safety check module
- tests/00_production-safety.test.js: Runs first to gate test suite
- tests/production-safety.test.js: Unit tests for safety module
- tests/hooks.js: Updated to use new module

This addresses concerns about users with 'test' in production DB names
by adding the entry count threshold as a secondary safety measure.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-03-18 19:49:49 -07:00

86 lines
2.7 KiB
JavaScript

'use strict;'
var testHelpers = require('./lib/test-helpers');
var productionSafety = require('./lib/production-safety');
// GAP-SYNC-046: Pre-flight safety check (no DB required)
productionSafety.preflightCheck();
var slowTestThreshold = parseInt(process.env.SLOW_TEST_THRESHOLD, 10) || 2000;
var enableTimingWarnings = process.env.ENABLE_TIMING_WARNINGS === 'true';
var enableRequireCacheClear = process.env.CLEAR_REQUIRE_CACHE === 'true';
var restoreSetTimeout = null;
var testTimings = [];
function clearRequireCache () {
Object.keys(require.cache).forEach(function(key) {
delete require.cache[key];
});
}
exports.mochaHooks = {
beforeAll(done) {
if (enableTimingWarnings) {
console.log('[TIMING INSTRUMENTATION] Enabled - will warn on setTimeout anti-patterns');
restoreSetTimeout = testHelpers.enableSetTimeoutWarnings({
warnOnLongDelays: true,
longDelayThreshold: 100
});
}
if (enableRequireCacheClear) {
console.log('[CACHE CLEAR] Enabled - will clear require cache after each test (slower but more isolated)');
}
done();
},
beforeEach(done) {
this.testStartTime = Date.now();
done();
},
afterEach(done) {
if (this.testStartTime) {
var elapsed = Date.now() - this.testStartTime;
var testTitle = this.currentTest ? this.currentTest.fullTitle() : 'unknown';
testTimings.push({
title: testTitle,
duration: elapsed,
slow: elapsed > slowTestThreshold
});
if (elapsed > slowTestThreshold) {
console.warn('[SLOW TEST] "' + testTitle + '" took ' + elapsed + 'ms (threshold: ' + slowTestThreshold + 'ms)');
}
}
if (enableRequireCacheClear) {
clearRequireCache();
}
done();
},
afterAll(done) {
if (restoreSetTimeout) {
var setTimeoutCallCount = restoreSetTimeout();
console.log('[TIMING INSTRUMENTATION] Disabled - detected ' + setTimeoutCallCount + ' setTimeout calls');
}
if (testTimings.length > 0) {
var slowTests = testTimings.filter(function(t) { return t.slow; });
if (slowTests.length > 0) {
console.log('\n[SLOW TEST SUMMARY] ' + slowTests.length + ' slow test(s) detected:');
slowTests.forEach(function(t, i) {
console.log(' ' + (i + 1) + '. ' + t.title + ' (' + t.duration + 'ms)');
});
}
var totalTime = testTimings.reduce(function(sum, t) { return sum + t.duration; }, 0);
var avgTime = Math.round(totalTime / testTimings.length);
console.log('\n[TIMING STATS] Total: ' + testTimings.length + ' tests, Avg: ' + avgTime + 'ms, Slow: ' + slowTests.length);
}
done();
}
};