Skip to content

armgit / TREK

Public Imported from GitHub
0

The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.

fix(weather): ask the forecast for the day east of UTC still reaches

Maurice0b6a77eeb575
diff --git a/server/src/nest/weather/weather.impl.ts b/server/src/nest/weather/weather.impl.ts
index bf28da623..39432243a 100644
--- a/server/src/nest/weather/weather.impl.ts
+++ b/server/src/nest/weather/weather.impl.ts
@@ -654,13 +654,18 @@ function calendarDaysAhead(day: string, now: Date): number {
/**
* The forecast counts its range from the place's own date, which trails UTC's in
- * the Americas for part of the day, so its last day can still be refused there.
+ * the Americas for part of the day, so its last day can still be refused there,
+ * and runs ahead of it east of UTC, where it still answers the day after.
*/
function isOutsideForecastRange(status: number, reason: string | undefined): boolean {
return status === 400 && /out of allowed range/i.test(reason ?? '');
}
-/** Last year's day standing in for a trip day the forecast does not reach. */
+/**
+ * Last year's day standing in for a trip day the forecast does not reach. A day
+ * at the forecast's edge comes within its reach in hours, so it keeps the
+ * forecast's TTL.
+ */
async function detailedClimate(
lat: string,
lng: string,
@@ -668,6 +673,7 @@ async function detailedClimate(
now: Date,
descriptions: Record<number, string>,
ck: string,
+ ttlMs: number,
): Promise<WeatherResult> {
let refYear = targetDate.getFullYear() - 1;
// Archive API only has data up to yesterday — go back further if needed
@@ -677,7 +683,7 @@ async function detailedClimate(
const climate = await detailedFromArchive(lat, lng, refDateStr, 'climate', descriptions);
if (!climate) return { temp: 0, main: '', description: '', type: '', error: 'no_forecast' };
- setCache(ck, climate, TTL_CLIMATE_MS);
+ setCache(ck, climate, ttlMs);
return climate;
}
@@ -693,9 +699,9 @@ async function _getDetailedWeatherImpl(lat: string, lng: string, date: string, l
const dateStr = targetDate.toISOString().slice(0, 10);
const descriptions = lang === 'de' ? WMO_DESCRIPTION_DE : WMO_DESCRIPTION_EN;
- // Climate path: past the forecast's last day, last year's day stands in for the trip's.
- if (calendarDaysAhead(dateStr, now) > FORECAST_LAST_DAY) {
- return detailedClimate(lat, lng, targetDate, now, descriptions, ck);
+ // Climate path: past the last day the forecast answers anywhere, last year's day stands in for the trip's.
+ if (calendarDaysAhead(dateStr, now) > FORECAST_LAST_DAY + 1) {
+ return detailedClimate(lat, lng, targetDate, now, descriptions, ck, TTL_CLIMATE_MS);
}
// Past day: the forecast API answers nulls once a date is a couple of months
@@ -721,7 +727,7 @@ async function _getDetailedWeatherImpl(lat: string, lng: string, date: string, l
if (!response.ok || data.error) {
if (isOutsideForecastRange(response.status, data.reason)) {
- return detailedClimate(lat, lng, targetDate, now, descriptions, ck);
+ return detailedClimate(lat, lng, targetDate, now, descriptions, ck, TTL_FORECAST_MS);
}
throw new ApiError(response.status || 500, data.reason || 'Open-Meteo API error');
}
diff --git a/server/tests/unit/nest/weather/weather.impl.test.ts b/server/tests/unit/nest/weather/weather.impl.test.ts
index d7876491c..4e014fe86 100644
--- a/server/tests/unit/nest/weather/weather.impl.test.ts
+++ b/server/tests/unit/nest/weather/weather.impl.test.ts
@@ -1295,10 +1295,43 @@ describe('getDetailedWeather at the edge of the forecast range', () => {
expect(result.type).toBe('climate');
expect(result.temp_max).toBe(8);
const asked = vi.mocked(fetch).mock.calls.map(([u]) => String(u));
- expect(asked.some((u) => u.includes(FORECAST))).toBe(false);
expect(asked.some((u) => u.includes(`${ARCHIVE}`) && u.includes('start_date=2025-12-11'))).toBe(true);
});
+ it('WEATHER-HORIZON-001b: a day past what the forecast answers anywhere does not ask it', async () => {
+ vi.setSystemTime(new Date('2026-11-25T13:00:00Z'));
+ openMeteo('2026-12-10');
+
+ const result = await getDetailedWeather('47.13', '11.33', '2026-12-12', 'en');
+
+ expect(result.type).toBe('climate');
+ const asked = vi.mocked(fetch).mock.calls.map(([u]) => String(u));
+ expect(asked.some((u) => u.includes(FORECAST))).toBe(false);
+ });
+
+ it('WEATHER-HORIZON-001c: a place whose local date is already ahead of UTC gets the forecast for the day 16 UTC days out', async () => {
+ // 17:00 UTC is 02:00 the next day in Tokyo, so Open-Meteo's range there
+ // ends a day later than UTC's today + 15.
+ vi.setSystemTime(new Date('2026-11-25T17:00:00Z'));
+ openMeteo('2026-12-11');
+
+ const result = await getDetailedWeather('35.68', '139.69', '2026-12-11', 'en');
+
+ expect(result.type).toBe('forecast');
+ expect(result.temp_max).toBe(10);
+ });
+
+ it('WEATHER-HORIZON-001d: climate given on a refusal at the edge is asked for again within hours', async () => {
+ vi.setSystemTime(new Date('2026-11-25T13:00:00Z'));
+ openMeteo('2026-12-10');
+ expect((await getDetailedWeather('47.14', '11.34', '2026-12-11', 'en')).type).toBe('climate');
+
+ vi.setSystemTime(new Date('2026-11-26T01:00:00Z'));
+ openMeteo('2026-12-11');
+
+ expect((await getDetailedWeather('47.14', '11.34', '2026-12-11', 'en')).type).toBe('forecast');
+ });
+
it('WEATHER-HORIZON-002: the day 15 days out keeps its forecast, late in the UTC day too', async () => {
vi.setSystemTime(new Date('2026-11-25T23:30:00Z'));
openMeteo('2026-12-10');