import { test } from 'node:test' import assert from 'node:assert/strict' import { MAX_RATE_DOMAIN, computeRateDomain } from './rateDomain.js' test('computeRateDomain: covers the draws and the frequentist bounds', () => { const domain = computeRateDomain([[0, 1, 2, 3, 4]], [6]) assert.ok(domain.niceMax >= 6, `niceMax ${domain.niceMax} should cover the AC upper bound`) assert.equal(domain.clipped, false) }) test('computeRateDomain: ticks start at zero and end at niceMax', () => { const domain = computeRateDomain([[0, 1, 2]], [4]) assert.equal(domain.ticks[0], 0) assert.equal(domain.ticks[domain.ticks.length - 1], domain.niceMax) }) test('computeRateDomain: one extreme draw does not blow out the axis', () => { // A single 400-per-1,000 draw in a 53-student cell would otherwise squash // every other group into the leftmost pixel. const bulk = new Array(500).fill(2) bulk[0] = 400 const domain = computeRateDomain([bulk], [3]) assert.ok(domain.niceMax < 10, `niceMax was ${domain.niceMax}`) }) test('computeRateDomain: caps the domain and reports the clip', () => { // The cap keeps a tiny-denominator group from flattening every other curve, // but the caller has to be able to say so on screen rather than silently // cropping data off the right edge. const wide = new Array(500).fill(0).map((_, i) => (i < 490 ? 60 : 0)) const domain = computeRateDomain([wide], [80]) assert.equal(domain.niceMax, MAX_RATE_DOMAIN) assert.equal(domain.clipped, true) }) test('computeRateDomain: never returns a zero-width domain', () => { const domain = computeRateDomain([[0, 0, 0]], [0]) assert.ok(domain.niceMax > 0) assert.equal(domain.clipped, false) }) test('computeRateDomain: handles no data at all', () => { const domain = computeRateDomain([], []) assert.ok(domain.niceMax > 0) assert.ok(domain.ticks.length > 1) }) test('computeRateDomain: ignores empty rate arrays', () => { const domain = computeRateDomain([[], [1, 2, 3]], []) assert.ok(domain.niceMax > 0 && domain.niceMax < MAX_RATE_DOMAIN) })