aboutsummaryrefslogtreecommitdiffstats
path: root/test/test_weather.lua
diff options
context:
space:
mode:
authorDanilo M. <danix@danix.xyz>2026-09-17 09:00:40 +0200
committerDanilo M. <danix@danix.xyz>2026-09-17 09:00:40 +0200
commite6c5c5e6443345e019549d8b41ffa0c19d050c9f (patch)
tree3ccb4be91e079954086ddb8835d21ecc86716663 /test/test_weather.lua
parente9dd31b04324d9a95acf6561a4b6ebcee9d3b8c0 (diff)
downloadconky-theme-udt-e6c5c5e6443345e019549d8b41ffa0c19d050c9f.tar.gz
conky-theme-udt-e6c5c5e6443345e019549d8b41ffa0c19d050c9f.zip
feat: parse the cached OWM response
Lua patterns instead of a JSON library: the current-weather response is flat and known, so six scalars do not justify a dependency. Every failure path (truncated write, empty file, garbage, an API error body, nil input) returns nil rather than raising, since a Lua error here is a blank dashboard. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Diffstat (limited to 'test/test_weather.lua')
-rw-r--r--test/test_weather.lua43
1 files changed, 43 insertions, 0 deletions
diff --git a/test/test_weather.lua b/test/test_weather.lua
index 1d35748..4ea953f 100644
--- a/test/test_weather.lua
+++ b/test/test_weather.lua
@@ -153,4 +153,47 @@ assert(weather.is_day(2000, 1000, 2000) == true, 'sunset counts as day')
assert(weather.is_day(500, 1000, 2000) == false, 'before dawn is night')
assert(weather.is_day(2500, 1000, 2000) == false, 'after dusk is night')
+-- === Parsing the cache ====================================================
+-- The parser reads scalars by key with Lua patterns rather than pulling in a
+-- JSON library: the response is flat and known, and six numbers do not justify
+-- a dependency. Caching the whole response still costs nothing, since a field
+-- added later is already on disk.
+local function read(path)
+ local f = assert(io.open(path, 'r'))
+ local s = f:read('*a')
+ f:close()
+ return s
+end
+
+local w = weather.parse(read('test/fixtures/weather.json'))
+assert(w, 'the fixture must parse')
+assert(w.id == 501, 'condition id, got ' .. tostring(w.id))
+assert(w.description == 'moderate rain', 'description, got ' .. tostring(w.description))
+assert(w.temp == 18.34, 'temp, got ' .. tostring(w.temp))
+assert(w.feels_like == 18.11, 'feels like, got ' .. tostring(w.feels_like))
+assert(w.humidity == 72, 'humidity, got ' .. tostring(w.humidity))
+assert(w.wind_speed == 4.12, 'wind m/s, got ' .. tostring(w.wind_speed))
+assert(w.wind_deg == 230, 'wind direction, got ' .. tostring(w.wind_deg))
+assert(w.sunrise == 1789600000, 'sunrise, got ' .. tostring(w.sunrise))
+assert(w.sunset == 1789646000, 'sunset, got ' .. tostring(w.sunset))
+assert(w.city == 'Example City', 'city, got ' .. tostring(w.city))
+assert(w.dt == 1789625000, 'observation time, got ' .. tostring(w.dt))
+
+-- temp comes before feels_like in the response and both live under "main",
+-- so a lazy pattern would read one for the other. They differ in the fixture
+-- precisely so this is checkable.
+assert(w.temp ~= w.feels_like, 'temp and feels_like must not collapse')
+
+-- Failure modes all return nil rather than raising: a Lua error here is a
+-- blank dashboard, not a message.
+assert(weather.parse(read('test/fixtures/weather_truncated.json')) == nil,
+ 'a truncated response must give nil')
+assert(weather.parse('') == nil, 'empty input gives nil')
+assert(weather.parse('not json at all') == nil, 'garbage gives nil')
+assert(weather.parse(nil) == nil, 'nil input gives nil')
+
+-- An error body carries cod 401 and no weather. It must not parse as data.
+assert(weather.parse('{"cod":401,"message":"Invalid API key."}') == nil,
+ 'an API error body must give nil')
+
print('test_weather: all assertions passed')