Repository navigation
refactor(utils): swap getRandomFloat(Rounded) params to (min, max) for consistency - #1998
Conversation
SummaryThe following content is AI-generated and provides a summary of the pull request: PR Checklist
Does this PR introduce a breaking change?
Refactor: Swap
|
| 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 internalgetRandomFloatRoundedcall is reordered.- Although the original issue was tagged
semver-major, this is an internal API not exposed viapackage.jsonexports, 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
- Correlation ID:
6a575a90-7d88-11f1-9958-44c4783ca568 - Output Template: Repository PR Template
- Event Trigger:
pull_request.opened - GithubContextProvider: [FEATURE] (semver-major) getRandomFloat: swap parameter order to (min, max) for consistency #1967
- Summary Prompt: Default Prompt
- LLM:
anthropic--claude-4.6-sonnet - File Content Strategy: Full file content
There was a problem hiding this comment.
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
- Correlation ID:
6a575a90-7d88-11f1-9958-44c4783ca568 - Agent Instructions:
- Event Trigger:
pull_request.opened - LLM:
anthropic--claude-4.6-sonnet - File Content Strategy: Full file content
…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
bd2b43d to
6427ff7
Compare
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
6427ff7 to
5b7623e
Compare
PR Checklist
Does this PR introduce a breaking change?
Closes #1967
This PR contains two commits:
refactor(utils): swap getRandomFloat(Rounded) params to (min, max)— the issue's ask.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
getRandomFloatandgetRandomFloatRoundedfrom(max, min[, scale])to(min, max[, scale])so they match the sibling bounds helpersrandomInt(fromnode:crypto) andisValidRandomIntBounds, which take(min, max).Behavior-preserving: the function bodies reference
max/minby 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.Also bundles the zero-behavior-change JSDoc polish the issue mentions:
getRandomFloat@param/@returnslines now end with a period, matching sibling helpers.getRandomFloatRoundedandgetRandomFloatFluctuatedRoundedgain JSDoc (@param,@returns,@throws) to harmonize the documented random-float family.2. Fix — non-finite interval width
Documenting the
getRandomFloatcontract exposed a pre-existing gap: the guard validated each endpoint for finiteness but not the interval width. SogetRandomFloat(-Number.MAX_VALUE, Number.MAX_VALUE)passed the guards and returnedInfinity(orNaNwhen the random ratio is exactly0, via0 * 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. Themax < mininverted-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.getRandomFloatRoundedandgetRandomFloatFluctuatedRoundedinherit the guard by delegation; both now document it via@throws(the latter reaches it when a non-zerofluctuationPercentderives a non-finite interval width, e.g.staticValuenearNumber.MAX_VALUE).Semver reassessment (label correction proposed)
package.jsonexportspublishes only./dist/start.js(the app entrypoint) — not thesrc/utilsbarrel.getRandomFloat/getRandomFloatRoundedare re-exported fromsrc/utils/index.ts, but that barrel is app-internal; no external consumer canimportthem. So this is an internal change — not a semver-major public break; no CHANGELOG entry. Suggest re-labelingrefactorrather thansemver-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 internalgetRandomFloatRounded(min, max, scale)call is reordered — its localmax/min(fromMath.max/Math.min) retain correct semantics. The width fix lives ingetRandomFloat(single source); the other two inherit it via delegation.Blast radius
src/utils/Utils.tsgetRandomFloatsignature(max,min)→(min,max)+ JSDoc + width guard;getRandomFloatRoundedsignature(max,min,scale)→(min,max,scale)+ internal call + JSDoc;getRandomFloatFluctuatedRoundedinternal call + JSDoc (2@throws; signature unchanged)src/charging-station/ocpp/OCPPServiceUtils.tsgetRandomFloatRoundedcall sites — energy (1), per-phase power (3), power (2), amperage (5), each(maximum,minimum)→(minimum,maximum)tests/utils/Utils.test.tsmin===max, FluctuatedRounded overflow throw)getRandomFloatFluctuatedRounded(...)call sites inOCPPServiceUtils.tsare not touched (signature unchanged).Test intent preservation (not blind reversal)
getRandomFloat(0, 1)getRandomFloat(1, 0)max < min→ still throwsgetRandomFloat(+Inf, -Inf)getRandomFloat(-Inf, +Inf)getRandomFloat(0, -MAX)getRandomFloat(-MAX, 0)[-MAX, 0]rangegetRandomFloatRounded(10, 0, 2)getRandomFloatRounded(0, 10, 2)[0, 10]rangegetRandomFloatRounded(10, 0)getRandomFloatRounded(0, 10)[0, 10]rangegetRandomFloat(-MAX, MAX)getRandomFloat(5, 5)5getRandomFloatFluctuatedRounded(MAX, 10)getRandomFloatFluctuatedRounded(MAX, 0)Verification
pnpm formatclean;pnpm typecheck0;pnpm lint0;pnpm buildexit 0.tests/utils/Utils.test.tsandOCPPServiceUtils-MeterValues/-Purepass. No existing test outcome changed by either commit.(max, min)order.Utils.test.tsfail withRangeError.[min,max]inclusive claim is accurate, and all 11 OCPP sites are correct.OCPP / physical-model note
The migrated
getRandomFloatRoundedsites feed simulatedMeterValues(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.