fix: correctness bugs - prediction math, division by zero, unvalidated days, locality filter #3

Merged
jperera merged 1 commit from fix/correctness-bugs into main 2026-04-02 20:03:52 +02:00
Owner

Summary

  • Prediction math: predictPrice(regression, 7) was predicting 7 days from the start of the data window, not from today. Fixed to use prices.length + 7 so predictions are relative to the latest data point. Applied to both analyzeTrends() and analyzeStationTrend().
  • Division by zero: Dashboard template crashed when avgPrice is 0 (no data), producing Infinity/NaN. Now shows "Sin datos" gracefully.
  • Unvalidated days param: /api/prices/history had no bounds checking (unlike /api/trends), allowing ?days=999999999 to scan the entire table. Now clamped to [1, 365].
  • Locality filter fix: filterByPostalCodePrefix had a hardcoded lorca locality check that caused TypeError in tests (and was a hidden business rule bypassing postal code filtering). Removed.
  • Performance: Replace linear scan priceHistory.find() in analyzeTrends with a pre-built Map lookup.

Test plan

  • bun test — all 30 tests pass (3 previously broken tests now fixed)
  • Verify trend predictions are reasonable (7 days from now, not from data start)
## Summary - **Prediction math**: `predictPrice(regression, 7)` was predicting 7 days from the *start* of the data window, not from today. Fixed to use `prices.length + 7` so predictions are relative to the latest data point. Applied to both `analyzeTrends()` and `analyzeStationTrend()`. - **Division by zero**: Dashboard template crashed when `avgPrice` is 0 (no data), producing `Infinity`/`NaN`. Now shows "Sin datos" gracefully. - **Unvalidated `days` param**: `/api/prices/history` had no bounds checking (unlike `/api/trends`), allowing `?days=999999999` to scan the entire table. Now clamped to `[1, 365]`. - **Locality filter fix**: `filterByPostalCodePrefix` had a hardcoded `lorca` locality check that caused `TypeError` in tests (and was a hidden business rule bypassing postal code filtering). Removed. - **Performance**: Replace linear scan `priceHistory.find()` in `analyzeTrends` with a pre-built Map lookup. ## Test plan - [x] `bun test` — all 30 tests pass (3 previously broken tests now fixed) - [ ] Verify trend predictions are reasonable (7 days from now, not from data start)
- Fix prediction math: predictPrice now uses prices.length + 7 instead
  of 7, so predictions are 7 days from today, not from data window start
- Fix division by zero in dashboard when avgPrice is 0 (no data)
- Validate days param in /api/prices/history with bounds (1-365)
- Remove hardcoded 'lorca' locality check from filterByPostalCodePrefix
  that caused test failures and bypassed postal code filtering
- Optimize station name lookup in analyzeTrends with a Map instead of
  linear scan on every iteration
jperera force-pushed fix/correctness-bugs from e4ffed224d to 3fd73c0458 2026-04-02 20:03:37 +02:00 Compare
jperera deleted branch fix/correctness-bugs 2026-04-02 20:03:52 +02:00
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
jperera/gasolineras!3
No description provided.