Files
ttrpg-initiative-tracker/shared/tests/static.eslint.test.js
T

57 lines
1.8 KiB
JavaScript
Raw Normal View History

// STATIC GUARD: prod source must pass eslint with zero errors/warnings.
// Scans App.js + storage adapters + shared modules. Catches unused imports,
// dead code, undefined vars before they hit the browser console.
// Run via: npx eslint <files> --format json -> parse -> fail on any problem.
const { execSync } = require('child_process');
const path = require('path');
const ROOT = path.resolve(__dirname, '..', '..');
const TARGETS = [
'src/App.js',
'src/storage/contract.js',
'src/storage/firebase.js',
'src/storage/index.js',
'src/storage/server.js',
'shared/index.js',
'shared/logEvent.js',
'shared/turn.js',
];
function runEslint(files) {
const abs = files.map(f => `"${path.join(ROOT, f)}"`).join(' ');
// execSync throws on non-zero exit (eslint returns 1 when problems found).
// Capture stdout regardless.
let stdout;
try {
stdout = execSync(`npx eslint ${abs} --format json`, {
encoding: 'utf8',
cwd: ROOT,
stdio: ['pipe', 'pipe', 'pipe'],
});
} catch (err) {
stdout = err.stdout || '';
}
return JSON.parse(stdout || '[]');
}
describe('static guard: eslint clean on prod source', () => {
test('zero eslint errors or warnings', () => {
const results = runEslint(TARGETS);
const problems = [];
let totalErrors = 0;
let totalWarnings = 0;
for (const file of results) {
totalErrors += file.errorCount;
totalWarnings += file.warningCount;
for (const msg of file.messages) {
problems.push(` ${path.relative(ROOT, file.filePath)}:${msg.line}:${msg.column} ${msg.ruleId}${msg.message}`);
}
}
if (totalErrors + totalWarnings > 0) {
const summary = `eslint found ${totalErrors} error(s), ${totalWarnings} warning(s):\n${problems.join('\n')}`;
throw new Error(summary);
}
}, 30000); // eslint spawn can be slow
});