Skip to content

refactor(utils): swap getRandomFloat(Rounded) params to (min, max) for consistency - #1998

Merged
jerome-benoit merged 2 commits into
mainfrom
refactor/get-random-float-min-max-param-order
Jul 12, 2026
Merged

jerome-benoit merged 2 commits into
mainfrom
refactor/get-random-float-min-max-param-order

Conversation

@jerome-benoit

@jerome-benoit jerome-benoit commented Jul 12, 2026 •

Copy link
Copy Markdown
Contributor

PR Checklist

  • Please add a description of your changes.
  • Please ensure title follows conventional commits.
  • We need your changes to come with unit tests in order to keep this project in quality and easy to maintain.
  • If your changes contain any new feature please be sure to document it. (N/A — internal refactor + edge-case fix; JSDoc harmonized for the touched helpers.)
  • Please add a link to the open issue or task that this pull request will resolve.

Does this PR introduce a breaking change?

  • Yes
  • No

Closes #1967

This PR contains two commits:

  1. refactor(utils): swap getRandomFloat(Rounded) params to (min, max) — the issue's ask.
  2. fix(utils): reject non-finite interval width in getRandomFloat — a related correctness fix surfaced during review.

1. Refactor — parameter-order swap

Swap the parameter order of getRandomFloat and getRandomFloatRounded from (max, min[, scale]) to (min, max[, scale]) so they match the sibling bounds helpers randomInt (from node:crypto) and isValidRandomIntBounds, which take (min, max).

Behavior-preserving: the function bodies reference max/min by name, so only the parameter declaration order/defaults and each positional call site move. Every call site (production and tests) is migrated in the same commit so no site silently swaps min/max — the exact hazard the issue warns about.

Note on the issue text: it lists getRandomInteger as a sibling helper, but no such function exists in the codebase (grep-confirmed). The real (min, max)-ordered helpers are randomInt and isValidRandomIntBounds.

Also bundles the zero-behavior-change JSDoc polish the issue mentions:

  • getRandomFloat @param/@returns lines now end with a period, matching sibling helpers.
  • getRandomFloatRounded and getRandomFloatFluctuatedRounded gain JSDoc (@param, @returns, @throws) to harmonize the documented random-float family.

2. Fix — non-finite interval width

Documenting the getRandomFloat contract exposed a pre-existing gap: the guard validated each endpoint for finiteness but not the interval width. So getRandomFloat(-Number.MAX_VALUE, Number.MAX_VALUE) passed the guards and returned Infinity (or NaN when the random ratio is exactly 0, via 0 * Infinity), silently violating the documented [min, max] contract.

The fix replaces the per-endpoint finiteness check with a width check on max - min, which subsumes the endpoint check (a non-finite endpoint yields a non-finite difference) and additionally rejects the overflow. The max < min inverted-interval guard is kept (a finite inverted range has finite width and must still throw). Behavior is unchanged for every previously valid input — the default (0, MAX_VALUE) and all production call sites have finite width.

getRandomFloatRounded and getRandomFloatFluctuatedRounded inherit the guard by delegation; both now document it via @throws (the latter reaches it when a non-zero fluctuationPercent derives a non-finite interval width, e.g. staticValue near Number.MAX_VALUE).

Semver reassessment (label correction proposed)

package.json exports publishes only ./dist/start.js (the app entrypoint) — not the src/utils barrel. getRandomFloat/getRandomFloatRounded are re-exported from src/utils/index.ts, but that barrel is app-internal; no external consumer can import them. So this is an internal change — not a semver-major public break; no CHANGELOG entry. Suggest re-labeling refactor rather than semver-major. The reporter's silent-swap migration concern is honored in full.

Scope

Family swap of getRandomFloat + getRandomFloatRounded; getRandomFloatFluctuatedRounded's signature is untouched (no min/max pair), only its internal getRandomFloatRounded(min, max, scale) call is reordered — its local max/min (from Math.max/Math.min) retain correct semantics. The width fix lives in getRandomFloat (single source); the other two inherit it via delegation.

Blast radius

File Change
src/utils/Utils.ts getRandomFloat signature (max,min)→(min,max) + JSDoc + width guard; getRandomFloatRounded signature (max,min,scale)→(min,max,scale) + internal call + JSDoc; getRandomFloatFluctuatedRounded internal call + JSDoc (2 @throws; signature unchanged)
src/charging-station/ocpp/OCPPServiceUtils.ts 11 getRandomFloatRounded call sites — energy (1), per-phase power (3), power (2), amperage (5), each (maximum,minimum)→(minimum,maximum)
tests/utils/Utils.test.ts 5 sites swapped preserving intent + regression assertions (overflow throw, degenerate min===max, FluctuatedRounded overflow throw)

getRandomFloatFluctuatedRounded(...) call sites in OCPPServiceUtils.ts are not touched (signature unchanged).

Test intent preservation (not blind reversal)

Line Before After Preserved intent
264 getRandomFloat(0, 1) getRandomFloat(1, 0) max < min → still throws
270 getRandomFloat(+Inf, -Inf) getRandomFloat(-Inf, +Inf) non-finite interval → still throws
274 getRandomFloat(0, -MAX) getRandomFloat(-MAX, 0) valid [-MAX, 0] range
809 getRandomFloatRounded(10, 0, 2) getRandomFloatRounded(0, 10, 2) [0, 10] range
818 getRandomFloatRounded(10, 0) getRandomFloatRounded(0, 10) [0, 10] range
new — getRandomFloat(-MAX, MAX) overflow width → throws (fix guard)
new — getRandomFloat(5, 5) degenerate interval → returns 5
new — getRandomFloatFluctuatedRounded(MAX, 10) overflow via delegation → throws
new — getRandomFloatFluctuatedRounded(MAX, 0) zero fluctuation returns early → does NOT throw (locks the non-zero precondition)

Verification

  • pnpm format clean; pnpm typecheck 0; pnpm lint 0; pnpm build exit 0.
  • tests/utils/Utils.test.ts and OCPPServiceUtils-MeterValues/-Pure pass. No existing test outcome changed by either commit.
  • Grep completeness: zero call sites left in the old (max, min) order.
  • Mutation sanity (swap): reverting one internal call to the old order makes Utils.test.ts fail with RangeError.
  • Mutation sanity (fix): reverting the width guard to the per-endpoint check makes the overflow assertions fail — confirming they guard the width check.
  • Review: 3 independent reviewers + cross-validation; all confirmed the width guard fully subsumes the endpoint check with zero regression, the [min,max] inclusive claim is accurate, and all 11 OCPP sites are correct.

OCPP / physical-model note

The migrated getRandomFloatRounded sites feed simulated MeterValues (Energy.Active.Import.Register, Power.Active.Import, Current.Import). Per OCPP 1.6 / 2.0.1 these sampled values are readings with no spec-imposed min/max ordering; the min/max are simulator-side bounds. The swap preserves floor vs ceiling, so the per-measurand distribution is identical and OCPP compliance is unaffected.

@hyperspace-pr-bot

Copy link
Copy Markdown
Contributor

Summary

The following content is AI-generated and provides a summary of the pull request:


PR Checklist

  • Please add a description of your changes.
  • Please ensure title follows conventional commits.
  • We need your changes to come with unit tests in order to keep this project in quality and easy to maintain.
  • If your changes contain any new feature please be sure to document it.
  • Please add a link to the open issue or task that this pull request will resolve.

Does this PR introduce a breaking change?

  • Yes
  • No

Refactor: Swap getRandomFloat(Rounded) parameter order to (min, max) for consistency

This is a behavior-preserving internal refactor that swaps the parameter order of getRandomFloat and getRandomFloatRounded from (max, min) to (min, max), aligning them with all sibling helpers (getRandomInteger, randomInt, isValidRandomIntBounds, getRandomFloatFluctuatedRounded) which already use (min, max) convention.

Changes

File What changed
src/utils/Utils.ts getRandomFloat signature: (max, min) → (min, max) + JSDoc @param order updated
src/utils/Utils.ts getRandomFloatRounded signature: (max, min, scale) → (min, max, scale) + internal call updated
src/utils/Utils.ts getRandomFloatFluctuatedRounded internal call to getRandomFloatRounded reordered (signature unchanged)
src/charging-station/ocpp/OCPPServiceUtils.ts 11 call sites migrated — energy (1), per-phase power (3), power (2), amperage (5)
tests/utils/Utils.test.ts 5 call sites updated with intent preserved (no assertion outcomes changed)

Notes

  • All call sites (production and tests) are migrated in the same commit to prevent any silent min/max swaps.
  • getRandomFloatFluctuatedRounded's public signature is untouched; only its internal getRandomFloatRounded call is reordered.
  • Although the original issue was tagged semver-major, this is an internal API not exposed via package.json exports, making this a non-breaking internal refactor.

Closes #1967


  • 🔄 Regenerate and Update Summary
  • ✏️ Insert as PR Description (deletes this comment)
  • 🗑️ Delete comment
PR Bot Information

Version: 1.27.6

@hyperspace-pr-bot hyperspace-pr-bot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PR is a clean, well-scoped refactor. The implementation looks correct. Let me verify one subtle test case: the second throws test at line 270 calls getRandomFloat(Number.NEGATIVE_INFINITY, Number.POSITIVE_INFINITY). Under the new signature (min, max), min = -Infinity and max = +Infinity. The first guard max < min → +Inf < -Inf is false, so it doesn't throw there. The second guard !Number.isFinite(max) || !Number.isFinite(min) → true, so it throws RangeError. The test intent is correctly preserved.

All changes look correct — the parameter reordering is consistent and complete across all three files. There are no substantive issues to flag.

The PR is a well-executed, behavior-preserving refactor. The signature swap is applied atomically across all production call sites and tests, the JSDoc is updated to match the new parameter order, and the test intent table in the PR description is accurate. No issues found.

PR Bot Information

Version: 1.27.6

…r consistency

Swap the parameter order of getRandomFloat and getRandomFloatRounded from
(max, min[, scale]) to (min, max[, scale]) to match the sibling bounds
helpers randomInt (node:crypto) and isValidRandomIntBounds, which take
(min, max).

The change is behavior-preserving: the function bodies reference max/min by
name, so only the parameter declaration order/defaults and every positional
call site move. All call sites (production and tests) are migrated in this
same commit so no site silently swaps min/max.

Scope: family swap of getRandomFloat + getRandomFloatRounded.
getRandomFloatFluctuatedRounded's signature is untouched (it has no min/max
pair); only its internal getRandomFloatRounded call is reordered.

Also bundles the zero-behavior-change JSDoc polish noted in the issue:
- getRandomFloat @param/@returns lines now end with a period, matching the
  sibling helpers (isValidRandomIntBounds, roundTo, clampToSafeTimerValue).
- getRandomFloatRounded and getRandomFloatFluctuatedRounded gain JSDoc
  (params, returns, @throws) to harmonize the documented random-float family.

Semver reassessment: package.json exports only ./dist/start.js (the app
entrypoint), not the src/utils barrel. getRandomFloat is app-internal and
cannot be imported by any external consumer, so this is a refactor, not a
semver-major public break; no CHANGELOG breaking-change entry is added. The
reporter's silent-swap migration concern is fully honored.

Blast radius migrated:
- src/utils/Utils.ts: getRandomFloat signature + JSDoc, getRandomFloatRounded
  signature + internal call, getRandomFloatFluctuatedRounded internal call.
- src/charging-station/ocpp/OCPPServiceUtils.ts: 11 getRandomFloatRounded
  call sites (energy, per-phase power, power, amperage).
- tests/utils/Utils.test.ts: 5 call sites, each swapped to preserve the
  original (min, max) semantic and pass/throw outcome (incl. the RangeError
  cases at getRandomFloat(1, 0) and the non-finite interval).

Mutation sanity: reverting one internal call to the old order makes
Utils.test.ts fail with RangeError, confirming the tests guard the order.

Closes #1967
@jerome-benoit
jerome-benoit force-pushed the refactor/get-random-float-min-max-param-order branch 3 times, most recently from bd2b43d to 6427ff7 Compare July 12, 2026 12:25
getRandomFloat's finiteness guard validated each endpoint but not the
interval width, so a finite-endpoint yet unrepresentable range such as
getRandomFloat(-Number.MAX_VALUE, Number.MAX_VALUE) passed the guards and
returned Infinity (or NaN when the random ratio is exactly 0, via
0 * Infinity), silently violating the documented "[min, max]" contract.

Replace the per-endpoint finiteness check with a width check on
`max - min`, which subsumes the endpoint check (a non-finite endpoint
yields a non-finite difference) and additionally rejects the overflow
case. The `max < min` inverted-interval guard is kept (a finite inverted
range has a finite width and must still be rejected). Behavior is
unchanged for every previously valid input: the default (0, MAX_VALUE)
and all production call sites have finite width.

getRandomFloatRounded and getRandomFloatFluctuatedRounded inherit the
guard through delegation. getRandomFloatRounded's JSDoc @throws is updated
to the width wording; getRandomFloatFluctuatedRounded gains a second
@throws documenting the delegated non-finite-width RangeError, reachable
only for a non-zero fluctuationPercent (a zero percent returns early
without delegating).

Regression tests: getRandomFloat(-Number.MAX_VALUE, Number.MAX_VALUE) and
getRandomFloatFluctuatedRounded(Number.MAX_VALUE, 10) now throw RangeError;
getRandomFloat(5, 5) returns the degenerate point; and
getRandomFloatFluctuatedRounded(Number.MAX_VALUE, 0) does not throw,
locking the non-zero precondition of the delegated guard. Mutation sanity:
reverting to the per-endpoint guard makes the overflow assertion fail.

Refs #1967
@jerome-benoit
jerome-benoit force-pushed the refactor/get-random-float-min-max-param-order branch from 6427ff7 to 5b7623e Compare July 12, 2026 12:33
@jerome-benoit
jerome-benoit merged commit bc003cc into main Jul 12, 2026
44 checks passed
@jerome-benoit
jerome-benoit deleted the refactor/get-random-float-min-max-param-order branch October 29, 2026 23:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE] (semver-major) getRandomFloat: swap parameter order to (min, max) for consistency

1 participant