fix brittle tests

This commit is contained in:
Emil Lerch 2026-03-05 10:41:14 -08:00
parent 58c88f2320
commit 6e84aa815e
Signed by: lobo
GPG key ID: A7B62D657EF764F8
3 changed files with 25 additions and 38 deletions

View file

@ -242,27 +242,14 @@ test "handleWeather: default endpoint uses IP address" {
try handler.handleWeather(&harness.opts, ht.req, ht.res, client_ip);
try ht.expectStatus(200);
try ht.expectBody(
\\<!DOCTYPE html>
\\<html>
\\<head>
\\<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
\\<link rel="stylesheet" href="https://adobe-fonts.github.io/source-code-pro/source-code-pro.css">
\\<style>
\\body{background:#000;color:#bbb}
\\pre{font-family:"Source Code Pro","DejaVu Sans Mono",Menlo,"Lucida Sans Typewriter","Lucida Console",monaco,"Bitstream Vera Sans Mono",monospace;font-size:75%}
\\</style>
\\</head>
\\<body><pre>
\\Weather report: Union City, California, United States
\\
\\<span style="color:#ffff00"> \ / </span> Clear
\\<span style="color:#ffff00"> .-. </span> <span style="color:#d7ff00">+68(+68)</span> °F
\\<span style="color:#ffff00"> ― ( ) ― </span> ↓ <span style="color:#6c6c6c">3</span> mph
\\<span style="color:#ffff00"> `-' </span> 6 mi
\\<span style="color:#ffff00"> / \ </span> 0.0 in
\\</pre></body></html>
);
// Don't assert exact location name since the GeoLite2 database updates
// upstream and city mappings change over time. Verify structural properties:
// response is HTML with a weather report containing expected weather data.
const pr = try ht.parseResponse();
try std.testing.expect(std.mem.indexOf(u8, pr.body, "Weather report:") != null);
try std.testing.expect(std.mem.indexOf(u8, pr.body, "California, United States") != null);
try std.testing.expect(std.mem.indexOf(u8, pr.body, "°F") != null);
try std.testing.expect(std.mem.indexOf(u8, pr.body, "<!DOCTYPE html>") != null);
}
test "handleWeather: x-forwarded-for with multiple IPs" {

View file

@ -455,20 +455,12 @@ test "handler: format v2" {
try handleWeather(&harness.opts, ht.req, ht.res, client_ip);
try ht.expectStatus(200);
// Should we have 2 empty lines?
try ht.expectBody(
\\Weather report: Union City, California, United States
\\
\\ Current conditions
\\ Clear
\\ 🌡️ 20.0°C (68.0°F)
\\ 💧 50%
\\ 🌬️ 5.0 km/h N
\\ 🔽 1013.0 hPa
\\ 💦 0.0 mm
\\
\\
);
// Don't assert exact location name since the GeoLite2 database updates
// upstream and city mappings change over time. Verify structural properties.
const pr = try ht.parseResponse();
try std.testing.expect(std.mem.indexOf(u8, pr.body, "Weather report:") != null);
try std.testing.expect(std.mem.indexOf(u8, pr.body, "California, United States") != null);
try std.testing.expect(std.mem.indexOf(u8, pr.body, "Current conditions") != null);
}
test "handler: format custom (%c)" {
@ -552,5 +544,9 @@ test "handler: format line 3" {
try handleWeather(&harness.opts, ht.req, ht.res, client_ip);
try ht.expectStatus(200);
try ht.expectBody("Union City, California, United States: ☀️ +20°C\n");
// Don't assert exact location name since the GeoLite2 database updates
// upstream and city mappings change over time. Verify structural properties.
const pr = try ht.parseResponse();
try std.testing.expect(std.mem.indexOf(u8, pr.body, "California, United States:") != null);
try std.testing.expect(std.mem.indexOf(u8, pr.body, "°C") != null);
}

View file

@ -221,12 +221,16 @@ test "lookup works" {
return error.SkipZigTest;
defer geoip.deinit();
// Test that the function doesn't crash with various IPs
// Test that lookup returns a valid location for a well-known residential IP.
// We don't assert exact values since the GeoLite2 database is fetched from
// the latest upstream release and city/coordinate mappings change over time.
const maybe_result = geoip.lookup("73.158.64.1");
try std.testing.expect(maybe_result != null);
const result = maybe_result.?;
defer result.deinit();
try std.testing.expectEqual(@as(f64, 37.5958), result.coords.latitude);
try std.testing.expect(result.coords.latitude > 37.0 and result.coords.latitude < 38.0);
try std.testing.expect(result.coords.longitude < -121.0 and result.coords.longitude > -123.0);
try std.testing.expect(result.name.len > 0);
}