fix(moderation): windowValue must not fall back to the 30d total on a null column
All checks were successful
PR Checks / bot-install (pull_request) Successful in 15s
PR Checks / client-build (pull_request) Successful in 24s
PR Checks / server-tests (pull_request) Successful in 39s

windowValue mapped only the 24h/7d keys and used `?? row.d30` as the fallback:

    const col = { '24h': row.d1, '7d': row.d7 }[key] ?? row.d30

so a null d1/d7 (which the function is documented to tolerate) returned the
30-day count instead of 0, inflating the 24h/7d moderation tiles. It happens to
be masked today because `SUM(created_at >= ?)` nulls d1/d7/d30 only in unison,
but the contract is wrong and the existing test used an all-null row that hid it.

Map all three window keys explicitly so each reads its own column and a null
coerces to 0 via `Number(col) || 0`. Add a regression test with a null narrow
column and a non-null d30.

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2026-07-22 13:14:02 -05:00
parent bcdba4ce0a
commit c075ab981c
2 changed files with 10 additions and 1 deletions

View File

@@ -87,3 +87,12 @@ test('windowValue: null row (no rows in window) yields 0', () => {
test('windowValue: null sum column yields 0', () => {
assert.strictEqual(moderation.windowValue({ d1: null, d7: null, d30: null }, '7d'), 0)
})
test('windowValue: a null narrow-window column never falls back to the 30d total', () => {
// Regression: an earlier rewrite used `{...}[key] ?? row.d30`, so a null d1/d7
// returned the 30-day count instead of 0 — inflating the 24h/7d tiles.
const row = { d1: null, d7: null, d30: 5 }
assert.strictEqual(moderation.windowValue(row, '24h'), 0)
assert.strictEqual(moderation.windowValue(row, '7d'), 0)
assert.strictEqual(moderation.windowValue(row, '30d'), 5)
})