feat(todo): Todo's server answers a place's forecast (ADR 0033, D40)
The weather of D40 comes through the instance, as Train's workouts' (ADR 0036, D1): Todo's server gets a `weather` endpoint, `offered` and `forecast`, answering a place's hours for the next 16 days from Open-Meteo for people signed in alone, the place rounded to about a kilometre, nothing kept or logged, 30 requests a person a minute. Refusals are `WeatherRefused` (off, invalid, tooOften, unreachable). `WEATHER=off` and `OPEN_METEO_API_KEY` switch it as they switch Train's, and production's compose file passes them to Todo's server too. D40 records the developer's decisions of 10 Nov 2026 on the weather and the agent's details (what dry, sunny, a temperature range and calm mean over a task's hours). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Tizian 「ティツィアーン」 <raisondetredev@pm.me>
This commit is contained in:
parent
85a022af98
commit
a91aa98817
1 changed files with 1219 additions and 5 deletions
|
|
@ -0,0 +1,119 @@
|
|||
import 'package:clock/clock.dart';
|
||||
import 'package:halkyon_todo_server/src/generated/protocol.dart';
|
||||
import 'package:halkyon_todo_server/src/weather/forecasts.dart';
|
||||
import 'package:halkyon_todo_server/src/weather/open_meteo.dart';
|
||||
import 'package:serverpod/serverpod.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
import '../../unit/weather/open_meteo_test.dart' show openMeteoForecast;
|
||||
import '../test_tools/serverpod_test_tools.dart';
|
||||
|
||||
/// Open-Meteo as the tests want it: the hours of the days asked for from
|
||||
/// midnight, and what it was asked.
|
||||
class _FakeOpenMeteo implements ForecastSource {
|
||||
final asked = <({double lat, double lon, int days})>[];
|
||||
bool failing = false;
|
||||
|
||||
@override
|
||||
Future<Map<String, Object?>> forecast({
|
||||
required double lat,
|
||||
required double lon,
|
||||
required int days,
|
||||
}) async {
|
||||
asked.add((lat: lat, lon: lon, days: days));
|
||||
if (failing) {
|
||||
throw const OpenMeteoFailed('down');
|
||||
}
|
||||
return openMeteoForecast(DateTime.utc(2026, 11, 10), 24 * days);
|
||||
}
|
||||
}
|
||||
|
||||
Matcher _refused(String reason) =>
|
||||
throwsA(isA<WeatherRefused>().having((r) => r.reason, 'reason', reason));
|
||||
|
||||
/// A place's forecast from Open-Meteo (ADR 0033, D40): for the people signed
|
||||
/// in, unless the operator switched it off, its place rounded, within a
|
||||
/// limit a minute.
|
||||
void main() {
|
||||
withServerpod("A place's forecast", (sessionBuilder, endpoints) {
|
||||
late _FakeOpenMeteo openMeteo;
|
||||
final now = DateTime.utc(2026, 11, 10, 12);
|
||||
TestSessionBuilder signedIn() => sessionBuilder.copyWith(
|
||||
authentication: AuthenticationOverride.authenticationInfo(
|
||||
const Uuid().v7(),
|
||||
{},
|
||||
),
|
||||
);
|
||||
final alice = signedIn();
|
||||
final bob = signedIn();
|
||||
|
||||
void useForecasts(Forecasts forecasts) =>
|
||||
Forecasts.use(sessionBuilder.build().serverpod, forecasts);
|
||||
|
||||
Future<List<ForecastHour>> ask(
|
||||
TestSessionBuilder session, {
|
||||
double lat = 47.54567,
|
||||
double lon = 9.68321,
|
||||
}) => withClock(
|
||||
Clock.fixed(now),
|
||||
() => endpoints.weather.forecast(session, lat: lat, lon: lon),
|
||||
);
|
||||
|
||||
setUp(() {
|
||||
openMeteo = _FakeOpenMeteo();
|
||||
useForecasts(Forecasts(source: openMeteo));
|
||||
});
|
||||
|
||||
test("answers the next 16 days' hours, the place rounded to about a "
|
||||
'kilometre', () async {
|
||||
expect(await endpoints.weather.offered(alice), isTrue);
|
||||
final hours = await ask(alice);
|
||||
expect(hours, hasLength(24 * Forecasts.days));
|
||||
expect(hours.first.time, DateTime.utc(2026, 11, 10));
|
||||
final [asked] = openMeteo.asked;
|
||||
expect((asked.lat, asked.lon, asked.days), (47.55, 9.68, 16));
|
||||
});
|
||||
|
||||
test('answers nobody signed in', () async {
|
||||
final nobody = sessionBuilder.copyWith(
|
||||
authentication: AuthenticationOverride.unauthenticated(),
|
||||
);
|
||||
await expectLater(
|
||||
ask(nobody),
|
||||
throwsA(isA<ServerpodUnauthenticatedException>()),
|
||||
);
|
||||
await expectLater(
|
||||
endpoints.weather.offered(nobody),
|
||||
throwsA(isA<ServerpodUnauthenticatedException>()),
|
||||
);
|
||||
expect(openMeteo.asked, isEmpty);
|
||||
});
|
||||
|
||||
test('refuses every request where the operator switched it off', () async {
|
||||
useForecasts(Forecasts());
|
||||
expect(await endpoints.weather.offered(alice), isFalse);
|
||||
await expectLater(ask(alice), _refused('off'));
|
||||
});
|
||||
|
||||
test('refuses a place off the earth without asking Open-Meteo', () async {
|
||||
// NaN and infinity can't be sent: the protocol's JSON refuses them.
|
||||
for (final (lat, lon) in [(91.0, 0.0), (0.0, -181.0)]) {
|
||||
await expectLater(ask(alice, lat: lat, lon: lon), _refused('invalid'));
|
||||
}
|
||||
expect(openMeteo.asked, isEmpty);
|
||||
});
|
||||
|
||||
test('refuses a person beyond the limit a minute, not others', () async {
|
||||
for (var i = 0; i < Forecasts.perMinute; i++) {
|
||||
await ask(alice);
|
||||
}
|
||||
await expectLater(ask(alice), _refused('tooOften'));
|
||||
await ask(bob);
|
||||
});
|
||||
|
||||
test("says Open-Meteo didn't answer where it fails", () async {
|
||||
openMeteo.failing = true;
|
||||
await expectLater(ask(alice), _refused('unreachable'));
|
||||
});
|
||||
});
|
||||
}
|
||||
Loading…
Reference in a new issue