docs/solutions/best-practices/test-guard-assertions-and-module-state-reset.md
PR #5369 hardened the auth/session/bootstrap surface after an adversarial sweep. Greptile's post-merge review flagged two test-quality regressions: the tests looked like they covered production guards, but one test exercised the wrong branch entirely, and a test helper leaked mutated state to later tests in the same process.
When a production check has multiple rejection paths, the regression test must force execution through the path it claims to cover. A common trap is JSON.stringify, which silently replaces Infinity (and -Infinity) with null and NaN with null:
// WRONG: payload.exp is null, not Infinity
const body = Buffer.from(JSON.stringify({ iat: now, exp: Infinity, n: 'x' })).toString('base64url');
In api/_session.js:129, the validation order is:
typeof payload.exp !== 'number' -> reject!Number.isFinite(payload.exp) -> rejectDate.now() >= payload.exp -> rejectWith exp: null, the test stops at step 1 and never reaches Number.isFinite. To exercise the non-finite guard, build a JSON literal that JSON.parse converts to Infinity:
// RIGHT: JSON.parse turns the unquoted literal 1e309 into Infinity
const infiniteBody = Buffer.from(`{"iat":${now},"exp":1e309,"n":"infinite02"}`).toString('base64url');
Confirm the guard is actually covered by temporarily removing it: the test must fail.
A helper that lets tests mutate module-level state (__setXForTests) must be paired with a reset helper (__resetXForTests) that restores the production default. In src/services/wm-session.ts, __setWmSessionFetchTimeoutForTests(ms) changed fetchNewSessionTimeoutMs, but __resetWmSessionForTests reset every other module field without restoring the timeout. Later tests in the same Node.js process inherited the shrunken timeout and could flake.
The fix adds the missing reset:
export function __resetWmSessionForTests(): void {
cached = null;
inflight = null;
recoveryInFlight = null;
sessionGeneration = 0;
interceptorInstalled = false;
sessionDeadUntil = 0;
sentryEnqueue = enqueueSentryCall;
fetchNewSessionTimeoutMs = 10_000; // <- was missing
}
Add a regression test that sets the timeout to a small value, calls reset, and verifies production behavior is restored.
// Builds a body JSON.parse will read as { ..., exp: Infinity }
const infiniteBody = Buffer.from(`{"iat":${now},"exp":1e309,"n":"x"}`).toString('base64url');
const sig = /* sign infiniteBody with the test HMAC key */;
assert.equal(await validateSessionToken(`wms_${infiniteBody}.${sig}`), false);
const mod = await import('../src/services/wm-session.ts?reset-repro=1');
mod.__setWmSessionFetchTimeoutForTests(50);
mod.__resetWmSessionForTests();
// With the default 10 s timeout restored, a fetch that resolves after 100 ms succeeds.
const outcome = await Promise.race([
mod.ensureWmSession().then(() => 'settled'),
after(500, 'still-pending'),
]);
assert.equal(outcome, 'settled');
api/_session.js:129 — non-finite expiration guardsrc/services/wm-session.ts:184 — __resetWmSessionForTests