Skip to content

Commit 3cdeb34

Browse files
committed
Fix NaN/Infinity handling in responseAdDisplayCount with tests
The function used Math.floor() without checking if the input was a valid number. If params.eligibleCount or params.poolSize was NaN or Infinity, Math.floor() would return NaN, and Math.max(0, NaN) would return NaN, causing incorrect results. Added Number.isFinite() checks to default to 0 for invalid numbers. Also added comprehensive test coverage: - Valid inputs (eligibleCount, poolSize) - NaN handling for both parameters - Infinity handling for both parameters - Negative inputs - Fractional inputs (flooring behavior) All 8 tests pass.
1 parent 7b65652 commit 3cdeb34

2 files changed

Lines changed: 51 additions & 2 deletions

File tree

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { describe, expect, it } from 'bun:test'
2+
3+
import { responseAdDisplayCount } from '../lazy-response-ads'
4+
5+
describe('responseAdDisplayCount', () => {
6+
it('returns eligibleCount when poolSize is at or above the max', () => {
7+
expect(
8+
responseAdDisplayCount({ eligibleCount: 5, poolSize: 100 }),
9+
).toBe(5)
10+
})
11+
12+
it('clamps to poolSize when poolSize is below the max', () => {
13+
expect(responseAdDisplayCount({ eligibleCount: 10, poolSize: 3 })).toBe(3)
14+
})
15+
16+
it('returns 0 when eligibleCount is NaN', () => {
17+
expect(responseAdDisplayCount({ eligibleCount: NaN, poolSize: 10 })).toBe(0)
18+
})
19+
20+
it('returns 0 when poolSize is NaN', () => {
21+
expect(responseAdDisplayCount({ eligibleCount: 5, poolSize: NaN })).toBe(0)
22+
})
23+
24+
it('returns 0 when eligibleCount is Infinity', () => {
25+
expect(
26+
responseAdDisplayCount({ eligibleCount: Infinity, poolSize: 10 }),
27+
).toBe(0)
28+
})
29+
30+
it('returns 0 when poolSize is Infinity', () => {
31+
expect(
32+
responseAdDisplayCount({ eligibleCount: 5, poolSize: Infinity }),
33+
).toBe(0)
34+
})
35+
36+
it('returns 0 when both inputs are negative', () => {
37+
expect(
38+
responseAdDisplayCount({ eligibleCount: -5, poolSize: -10 }),
39+
).toBe(0)
40+
})
41+
42+
it('floors fractional inputs', () => {
43+
expect(
44+
responseAdDisplayCount({ eligibleCount: 5.7, poolSize: 3.2 }),
45+
).toBe(3)
46+
})
47+
})

‎common/src/util/lazy-response-ads.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ export function responseAdDisplayCount(params: {
1616
eligibleCount: number
1717
poolSize: number
1818
}): number {
19-
const eligibleCount = Math.max(0, Math.floor(params.eligibleCount))
20-
const poolSize = Math.max(0, Math.floor(params.poolSize))
19+
const safeEligibleCount = Number.isFinite(params.eligibleCount) ? params.eligibleCount : 0
20+
const safePoolSize = Number.isFinite(params.poolSize) ? params.poolSize : 0
21+
const eligibleCount = Math.max(0, Math.floor(safeEligibleCount))
22+
const poolSize = Math.max(0, Math.floor(safePoolSize))
2123
return poolSize >= MAX_RESPONSE_AD_POOL_SIZE
2224
? eligibleCount
2325
: Math.min(eligibleCount, poolSize)

0 commit comments

Comments
 (0)