feat: add weather unit preferences
Some checks failed
CI / Lint, test, typecheck and build (push) Has been cancelled

This commit is contained in:
zv
2026-07-04 20:16:11 +02:00
parent ab6b7b414f
commit 91acdb39b8
27 changed files with 623 additions and 76 deletions

View File

@@ -54,10 +54,16 @@ function warning(overrides: Partial<WeatherWarning> = {}): WeatherWarning {
}
describe("weather briefs", () => {
it("uses selected temperature and wind units in the morning brief", () => {
it("uses selected temperature, wind and precipitation units in the morning brief", () => {
const brief = buildWeatherBrief({
forecast: forecast([
hour({ time: "2026-06-14T08:00", temperature: 10, windSpeed: 4, precipitationProbability: 80 }),
hour({
time: "2026-06-14T08:00",
temperature: 10,
windSpeed: 4,
precipitationProbability: 80,
precipitation: 25.4,
}),
hour({ time: "2026-06-14T09:00", temperature: 12, windSpeed: 5, precipitationProbability: 30 }),
]),
warnings: [],
@@ -67,6 +73,7 @@ describe("weather briefs", () => {
language: "en",
temperatureUnit: "f",
windSpeedUnit: "mph",
precipitationUnit: "in",
now: new Date("2026-06-14T06:00:00.000Z"),
});
@@ -74,25 +81,34 @@ describe("weather briefs", () => {
expect(brief?.pushBody).toContain("50°F-54°F");
expect(brief?.pushBody).toContain("wind 11 mph");
expect(brief?.pushBody).not.toContain("km/h");
expect(brief?.body.join(" ")).toContain("1.00 in");
});
it("warns about tomorrow storms from model weather codes without IMGW warning", () => {
const brief = buildTomorrowWeatherBrief({
forecast: forecast([
hour({ time: "2026-06-15T08:00", temperature: 14, weatherCode: 3, precipitationProbability: 20 }),
hour({ time: "2026-06-15T15:00", temperature: 21, weatherCode: 95, precipitationProbability: 30 }),
hour({
time: "2026-06-15T15:00",
temperature: 21,
weatherCode: 95,
precipitationProbability: 30,
precipitation: 25.4,
}),
]),
warnings: [],
province: "mazowieckie",
countyTeryt: "1465",
locationName: "Warsaw",
language: "en",
precipitationUnit: "in",
now: new Date("2026-06-14T16:00:00.000Z"),
});
expect(brief?.severity).toBe("warning");
expect(brief?.headline).toBe("Thunderstorms are possible tomorrow");
expect(brief?.pushBody).toContain("thunderstorms");
expect(brief?.pushBody).toContain("1.00 in");
expect(brief?.pushBody).not.toContain("IMGW");
});

View File

@@ -1,9 +1,18 @@
import { describe, expect, it } from "vitest";
import {
DEFAULT_DISTANCE_UNIT,
DEFAULT_PRECIPITATION_UNIT,
DEFAULT_PRESSURE_UNIT,
DEFAULT_WIND_SPEED_UNIT,
formatDistance,
formatPrecipitation,
formatPressure,
formatTemperature,
formatWindSpeed,
isDistanceUnit,
isPrecipitationUnit,
isPressureUnit,
isTemperatureUnit,
isWindSpeedUnit,
} from "@/lib/weather-utils";
@@ -18,12 +27,40 @@ describe("weather utilities", () => {
expect(formatTemperature(10, "en", "f")).toBe("50°F");
expect(formatWindSpeed(4, "en", "mph")).toBe("9 mph");
expect(formatWindSpeed(4, "en", "ms")).toBe("4.0 m/s");
expect(formatWindSpeed(4, "en", "kt")).toBe("8 kt");
expect(formatWindSpeed(4, "en", "bft")).toBe("3 Bft");
});
it("formats selected precipitation, pressure and distance units", () => {
expect(DEFAULT_PRECIPITATION_UNIT).toBe("mm");
expect(DEFAULT_PRESSURE_UNIT).toBe("hpa");
expect(DEFAULT_DISTANCE_UNIT).toBe("km");
expect(formatPrecipitation(25.4, "en", "mm")).toBe("25.4 mm");
expect(formatPrecipitation(10, "en", "cm")).toBe("1 cm");
expect(formatPrecipitation(25.4, "en", "in")).toBe("1.00 in");
expect(formatPressure(1013.25, "en", "hpa")).toBe("1,013.3 hPa");
expect(formatPressure(1013.25, "en", "kpa")).toBe("101.3 kPa");
expect(formatPressure(1013.25, "en", "inhg")).toBe("29.92 inHg");
expect(formatPressure(1013.25, "en", "mmhg")).toBe("760 mm Hg");
expect(formatDistance(10, "en", "km")).toBe("10 km");
expect(formatDistance(10, "en", "mi")).toBe("6.2 mi");
});
it("validates supported presentation units", () => {
expect(isTemperatureUnit("c")).toBe(true);
expect(isTemperatureUnit("kelvin")).toBe(false);
expect(isWindSpeedUnit("kmh")).toBe(true);
expect(isWindSpeedUnit("kt")).toBe(true);
expect(isWindSpeedUnit("bft")).toBe(true);
expect(isWindSpeedUnit("knots")).toBe(false);
expect(isPrecipitationUnit("in")).toBe(true);
expect(isPrecipitationUnit("l")).toBe(false);
expect(isPressureUnit("kpa")).toBe(true);
expect(isPressureUnit("mbar")).toBe(false);
expect(isDistanceUnit("mi")).toBe(true);
expect(isDistanceUnit("m")).toBe(false);
});
});