diff --git a/changelog.d/762-tile-budget-module.md b/changelog.d/762-tile-budget-module.md new file mode 100644 index 000000000..6d000d357 --- /dev/null +++ b/changelog.d/762-tile-budget-module.md @@ -0,0 +1,4 @@ +- Move the tile budget helpers `utcDayKey`, `normalizeBudget` and + `isOverBudget` out of the TomTom module into `src/data/tileBudget.js`, so + other keyed tile proxies can share them. `src/data/tomtomTiles.js` + re-exports them; behaviour does not change (monwiss, #762). diff --git a/src/data/tileBudget.js b/src/data/tileBudget.js index dcc44a28c..83cf9b693 100644 --- a/src/data/tileBudget.js +++ b/src/data/tileBudget.js @@ -22,29 +22,29 @@ export function utcDayKey(epochMs = Date.now()) { } /** - * Normalize a persisted budget state against today's UTC day key. - * Rolls the counter to zero on day change; replaces missing/corrupt state. - * Returns the SAME object when it is already valid for `dayKey` (cheap to - * call on every request). + * Normalize a persisted budget state against the current period key. + * Rolls the counter to zero when the period changes; replaces missing/corrupt + * state. Returns the SAME object when it is already valid for `periodKey` + * (cheap to call on every request). * * @param {{date:string, count:number}|null|undefined} state - Persisted state. - * @param {string} dayKey - Today's UTC day key (from `utcDayKey`). - * @returns {{date:string, count:number}} Valid state for `dayKey`. + * @param {string} periodKey - Current period key, e.g. from `utcDayKey`. + * @returns {{date:string, count:number}} Valid state for `periodKey`. */ -export function normalizeBudget(state, dayKey) { +export function normalizeBudget(state, periodKey) { const valid = Boolean(state) && - state.date === dayKey && + state.date === periodKey && Number.isFinite(state.count) && state.count >= 0; - return valid ? state : { date: dayKey, count: 0 }; + return valid ? state : { date: periodKey, count: 0 }; } /** - * Whether the daily soft cap has been reached. + * Whether the soft cap for the current period has been reached. * * @param {{count:number}} state - Normalized budget state. - * @param {number} limit - Daily tile budget; non-positive/invalid never blocks. + * @param {number} limit - Budget per period; non-positive/invalid never blocks. * @returns {boolean} True when `count >= limit`. */ export function isOverBudget(state, limit) { diff --git a/src/data/tileBudget.test.mjs b/src/data/tileBudget.test.mjs index fa48440bb..c1baa4235 100644 --- a/src/data/tileBudget.test.mjs +++ b/src/data/tileBudget.test.mjs @@ -8,19 +8,3 @@ test('tile budget helpers live in one module and TomTom re-exports them', () => assert.equal(tomtom.normalizeBudget, normalizeBudget); assert.equal(tomtom.isOverBudget, isOverBudget); }); - -test('normalizeBudget keeps a valid state and resets another period', () => { - const state = { date: '2026-09-24', count: 3 }; - assert.equal(normalizeBudget(state, '2026-09-24'), state); - assert.deepEqual(normalizeBudget(state, '2026-09-25'), { - date: '2026-09-25', - count: 0, - }); - assert.deepEqual( - normalizeBudget({ date: '2026-09-24', count: -1 }, '2026-09-24'), - { - date: '2026-09-24', - count: 0, - }, - ); -}); diff --git a/src/data/tomtomTiles.js b/src/data/tomtomTiles.js index 9f3b1f0b9..14101c831 100644 --- a/src/data/tomtomTiles.js +++ b/src/data/tomtomTiles.js @@ -1,9 +1,10 @@ /** - * @file Pure tile math + budget accounting for the TomTom traffic-flow proxy. + * @file Pure tile math for the TomTom traffic-flow proxy; budget helpers are + * re-exported from `./tileBudget.js`. * * Shared by the `/api/tomtom` vite plugin (server-side: coordinate validation, * daily budget governor) and `src/layers/traffic/flowSource.js` (client-side: which tiles - * cover the current traffic fetch bounds). Zero dependencies, Cesium-free, so + * cover the current traffic fetch bounds). No external dependencies, Cesium-free, so * both sides can unit-test against it with node:test. * * Slippy scheme: standard Web Mercator XYZ, y grows southward