Skip to content

Commit 5c45d43

Browse files
committed
test(qr-auth): replace flaky max-deviation bias check with chi-square
The short-code distribution test asserted that no base62 character deviated more than 15% from its expected count. That statistic is the maximum over 62 correlated near-normal cells, so its tail is fat: at n=36000 the per-cell relative SD is ~4.1%, which puts the 15% bound at |z| ~ 3.65, and taken as a max over 62 cells it fires on a perfectly uniform generator about 1.6% of the time. Measured directly: 48 spurious failures in 3000 simulated runs. It had been rerun-to-green repeatedly and most recently red-herringed a PR review. Chi-square is the correct test for "is this multinomial uniform", and unlike 0.15 its threshold is derivable. df=61, Wilson-Hilferty puts the p=1e-6 critical value at ~129, so the bound is 130. Power is unchanged. Removing rejection sampling from generateShortCode reintroduces modulo bias (256 % 62 = 8, so eight characters draw five chances per 256 instead of four) and was verified against the real code in an isolated worktree: chi-square 243.06 against the 130 limit. The threshold sits in a wide empty gap, 3000 clean runs peaked at 104 while 200 biased runs bottomed out at 174.5. Also iterate the alphabet explicitly rather than the observed keys, so a character that never appears counts as zero instead of being skipped. Verified: 30/30 consecutive runs of the real test pass. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent 57b6be1 commit 5c45d43

1 file changed

Lines changed: 31 additions & 9 deletions

File tree

test/qr-auth.test.ts

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,9 @@ describe('QR Token Manager (unit)', () => {
250250
});
251251
});
252252

253+
/** Must match the alphabet in `generateShortCode` (tunnel-manager.ts). */
254+
const BASE62_ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
255+
253256
describe('Short code distribution (bias check)', () => {
254257
it('should produce roughly uniform character distribution', () => {
255258
// Generate 6000 codes (36000 chars) and check distribution
@@ -265,17 +268,36 @@ describe('Short code distribution (bias check)', () => {
265268
}
266269
}
267270

268-
// Expected count per char: 36000 / 62 ≈ 580.6
269-
const expected = 36000 / 62;
270-
let maxDeviation = 0;
271-
for (const [, count] of charCounts) {
272-
const deviation = Math.abs(count - expected) / expected;
273-
maxDeviation = Math.max(maxDeviation, deviation);
271+
// Chi-square goodness-of-fit against a uniform base62 alphabet.
272+
//
273+
// This deliberately does NOT assert on the max per-character deviation.
274+
// That statistic is the maximum of 62 correlated near-normal cells, so its
275+
// tail is fat: with n=36000 the per-cell relative SD is ~4.1%, which puts a
276+
// 15% bound at |z| ~ 3.65 and, taken as a max over 62 cells, fails on a
277+
// perfectly uniform generator about 1.6% of the time. Measured over 3000
278+
// simulated runs: 48 spurious failures. That is the flake.
279+
//
280+
// Chi-square is the right tool for "is this multinomial uniform", and its
281+
// threshold is derivable rather than eyeballed. df = 62 - 1 = 61, so under
282+
// the null E[X²] = 61 and SD = sqrt(2*61) ~ 11.05; the Wilson-Hilferty
283+
// approximation puts the p = 1e-6 critical value at ~129. Rounding to 130
284+
// gives a false-positive rate around one run in a million.
285+
//
286+
// Power is unaffected. Dropping rejection sampling reintroduces modulo bias
287+
// (256 % 62 = 8, so the first 8 characters draw 5 chances per 256 instead
288+
// of 4, ~25% overrepresented), which scores X² ~ 237. Simulated: 3000 clean
289+
// runs peaked at 104, while 200 biased runs bottomed out at 174.5, so the
290+
// threshold sits in a wide empty gap between the two.
291+
const alphabetSize = 62;
292+
const expected = 36000 / alphabetSize;
293+
let chiSquare = 0;
294+
for (let i = 0; i < alphabetSize; i++) {
295+
const count = charCounts.get(BASE62_ALPHABET[i]) ?? 0;
296+
chiSquare += (count - expected) ** 2 / expected;
274297
}
275298

276-
// With rejection sampling, deviation should be < 15% (generous)
277-
// Without rejection sampling (modulo bias), first 6 chars would be ~25% overrepresented
278-
expect(maxDeviation).toBeLessThan(0.15);
299+
expect(charCounts.size).toBe(alphabetSize);
300+
expect(chiSquare).toBeLessThan(130);
279301

280302
tm.stopTokenRotation();
281303
});

0 commit comments

Comments
 (0)