The One-Night Overcharge — JavaScript Bug Hunt
Inspired by the fencepost bugs that plague every booking site.
- Language: JavaScript
- Layer: Frontend
- Difficulty: Easy
- Concepts: Dates, Off-by-one
- Modelled on: Airbnb-style booking
- Visible tests: two nights are two nights; same-day stay is zero nights; month boundary is exact
- Reward: 50 XP for a complete fix
Incident
- Severity: SEV-2 (priority: High)
- Ticket: BUG-1861
- Reported by: pricing
- Time to fix: 10 minutes, the target for an easy incident
Failing checks on the shipped build
- two nights are two nights — expected 2 but got 3
- same-day stay is zero nights — expected 0 but got 1
- month boundary is exact — expected 3 but got 4
How to work this incident
- Read the report: write down the expected behaviour and the actual one in a sentence each.
- Reproduce it: run the visible tests and find the failing check that matches the report.
- Read the logs and the failing checks for the first value that is wrong — the earliest wrong value is nearer the cause than the loudest error.
- Follow the value the screen shows back through the state and the pure helpers that compute it; the view is usually only the messenger.
- Fix the cause rather than the symptom, run the visible tests again, and submit against the hidden ones — then write the root cause down.
Briefing
Inspired by the fencepost bugs that plague every booking site. Guests booking two nights are being quoted three, and a same-day "day use" booking charges a full night.
The nightly math lives in nights.js — dates arrive as YYYY-MM-DD strings.
Bug report
BUG-1861 · Priority: High · Reported by: pricing
Check-in 2024-06-10, check-out 2024-06-12 is TWO nights. We quote three. Check-in == check-out is ZERO nights. We quote one.
Guests are (rightly) filing refunds.
Logs
[quote] 2024-06-10 -> 2024-06-12 nights=3 (expected 2)The code as shipped
src/booking/nights.js (editable)
// Nights between an ISO check-in and check-out date (UTC, no times).
var DAY_MS = 24 * 60 * 60 * 1000;
function toUtc(iso) {
var p = iso.split("-");
return Date.UTC(parseInt(p[0], 10), parseInt(p[1], 10) - 1, parseInt(p[2], 10));
}
exports.nightsBetween = function (checkin, checkout) {
var diff = toUtc(checkout) - toUtc(checkin);
return Math.floor(diff / DAY_MS) + 1;
};
Open the hunt to start the incident clock, edit the files, run the visible tests and submit against the hidden ones, then write the root cause down. More JavaScript bug hunts.