import { describe, expect, test } from 'bun:test'; import { efficiency, parsePoolArguments, percentile, sampleProcessTree, verifyHashParity, } from '../benchmark-research-pool'; describe('bounded pool measurement integrity', () => { test('nearest-rank tails preserve zero and reject incomplete/nonfinite samples', () => { expect(percentile([0, 4, 2, 1, 3], 0.5)).toBe(2); expect( percentile( Array.from({ length: 24 }, (_, i) => i), 0.95 ) ).toBe(22); for (const values of [[], [NaN], [Infinity], [-1]]) { expect(() => percentile(values, 0.95)).toThrow(); } }); test('paired efficiency uses full serial duration divided by slots times parallel duration', () => { expect(efficiency(100, 60, 2)).toBeCloseTo(0.83333333); expect(efficiency(100, 100, 1)).toBe(1); expect(efficiency(100, 120, 4)).toBeCloseTo(0.20833333); expect(() => efficiency(100, 0, 2)).toThrow(); }); test('parity is per candidate index, never sorted by result or completion order', () => { const a = 'a'.repeat(64), b = 'b'.repeat(64); verifyHashParity(undefined, [a, b]); verifyHashParity([a, b], [a, b]); for (const hashes of [[b, a], [a], [], [a, 'invalid']]) { expect(() => verifyHashParity([a, b], hashes)).toThrow(); } }); test('measurement execution is explicit and malformed options cannot silently run', () => { expect(parsePoolArguments(['--measure', '--smoke', '--output', '/tmp/report.json'])).toEqual({ smoke: true, output: '/tmp/report.json', }); for (const args of [ [], ['--smoke'], ['unknown', '--measure'], ['--measure', '--output'], ['--measure', '--output', '--smoke'], ['--measure', '--measure'], ]) { expect(() => parsePoolArguments(args)).toThrow(); } }); test('RSS includes the actual driver and records process identities', () => { const sample = sampleProcessTree(); expect(sample.processes.some(processInfo => processInfo.pid === process.pid)).toBe(true); expect(sample.rssBytes).toBeGreaterThan(0); expect(sample.rssBytes).toBe( sample.processes.reduce((total, processInfo) => total + processInfo.rssBytes, 0) ); }); });