repositories / dotfiles
dotfiles
bugabingas dorkfiles
owned by admin
quickshell/nuguland/stress-test.mjs
Rawimport assert from 'node:assert/strict';
import { existsSync, globSync } from 'node:fs';
import { join, relative } from 'node:path';
import { spawnIsolated } from './test-fixtures/isolation.js';
// `just stress <test> [runs]`: run one JS test file many times at once and report
// every failure. Concurrency is the load that exposes timing-dependent tests.
const project = process.cwd();
function resolveTest(target) {
assert(!/^\/|\.\./.test(target), `unsafe test: ${target}`);
if (target.includes('/')) {
assert(existsSync(target), `missing test: ${target}`);
return target;
}
const names = /\.m?js$/.test(target) ? [target] : [`${target}.test.js`, `${target}.test.mjs`];
const found = globSync('**/*.{js,mjs}', { cwd: project, exclude: ['**/vault-native/**', '**/node_modules/**', '**/.git/**'] })
.filter(file => names.includes(file.split('/').at(-1))).map(file => join(project, file));
assert.equal(found.length, 1, `missing or ambiguous test: ${target} ${found.map(path => relative(project, path)).join(' ')}`);
return found[0];
}
function run(file) {
return new Promise(resolve => {
// Each run is isolated like the processes it starts; vault tests find the shared cargo target.
const child = spawnIsolated(process.execPath, [file], {
env: { CARGO_TARGET_DIR: process.env.CARGO_TARGET_DIR }, stdio: ['ignore', 'pipe', 'pipe'],
});
let output = '';
for (const stream of [child.stdout, child.stderr]) stream.on('data', data => { output += data; });
child.on('close', code => resolve({ code, output }));
});
}
const [target, runsText = '16'] = process.argv.slice(2);
try {
assert(target, 'usage: just stress <test> [runs]');
const runs = Number(runsText);
assert(Number.isInteger(runs) && runs > 0 && runs <= 64, 'runs must be 1..64');
const file = resolveTest(target);
const results = await Promise.all(Array.from({ length: runs }, () => run(file)));
const failures = results.filter(result => result.code !== 0);
for (const [index, failure] of failures.entries()) {
const lines = failure.output.split('\n').filter(line => /ERROR|Error|not ok|✖|failed/.test(line)).slice(0, 3);
console.log(`failure ${index + 1}: ${lines.join(' | ') || `exit ${failure.code}`}`);
}
console.log(`${relative(project, file)}: ${runs - failures.length}/${runs} concurrent runs passed`);
if (failures.length) process.exitCode = 1;
} catch (error) {
console.error(error.message);
process.exitCode = 2;
}