feat(todo): the server finds places through the instance's Photon (ADR 0033, D32)

Todo's locations search through the person's own instance, as Train's
planner does, not a public service: `SearchEndpoint` on `PlaceSearch`, for
those signed in, 60 searches a minute, nothing kept or logged. Its `Place`
carries the address's parts and the object in OpenStreetMap, and a shop's
car park or charger is left out, which came first for `Lindau EDEKA`.

`compose.todo-search.yaml` puts Todo's server on compose.search.yaml's
Photon. A file of its own: without it Todo starts as it did, and offers no
search.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Tizian 「ティツィアーン」 <raisondetredev@pm.me>
This commit is contained in:
Tizian 「ティツィアーン」 2026-11-05 14:58:38 +01:00
commit c9d3eb4dd1
Signed by: raisondetredev
GPG key ID: 07F6FCF405488B64

View file

@ -0,0 +1,246 @@
import 'dart:convert';
import 'dart:io';
import 'package:halkyon_todo_server/src/generated/protocol.dart';
import 'package:halkyon_todo_server/src/search/photon.dart';
import 'package:test/test.dart';
/// Photon's answer as its API documents it (`docs/api-v1.md` of v1.3.0,
/// read 29 Oct 2026): GeoJSON features, points `[lon, lat]`, their
/// properties GeocodeJSON's (`name`, `street`, `housenumber`, `city`,
/// `district`, `country`, `type`, …).
Map<String, Object?> photonAnswer(List<Map<String, Object?>> places) => {
'type': 'FeatureCollection',
'features': [
for (final place in places)
{
'type': 'Feature',
'geometry': {
'type': 'Point',
'coordinates': [place['lon'], place['lat']],
},
'properties': {
for (final MapEntry(:key, :value) in place.entries)
if (key != 'lat' && key != 'lon') key: value,
},
},
],
};
void main() {
test('a place is its name, where it is and its kind', () {
final places = placesOf(
photonAnswer([
{
'lat': 52.5145,
'lon': 13.3501,
'name': 'Tiergarten',
'osm_key': 'leisure',
'osm_value': 'park',
'type': 'other',
'district': 'Tiergarten',
'city': 'Berlin',
'country': 'Germany',
},
{
'lat': 52.52,
'lon': 13.405,
'name': 'Berlin',
'type': 'city',
'city': 'Berlin',
'country': 'Germany',
},
]),
);
// Serverpod's models have no equality of their own.
expect(
[for (final place in places) place.toJson()],
[
Place(
name: 'Tiergarten',
detail: 'Berlin, Germany',
kind: 'other',
osmValue: 'park',
district: 'Tiergarten',
city: 'Berlin',
lat: 52.5145,
lon: 13.3501,
).toJson(),
Place(
name: 'Berlin',
detail: 'Germany',
kind: 'city',
city: 'Berlin',
lat: 52.52,
lon: 13.405,
).toJson(),
],
);
});
test('an address without a name is its street and number', () {
final [place] = placesOf(
photonAnswer([
{
'lat': 48.137,
'lon': 11.575,
'type': 'house',
'street': 'Marienplatz',
'housenumber': '8',
'city': 'München',
'country': 'Deutschland',
},
]),
);
expect(place.name, 'Marienplatz 8');
expect(place.detail, 'München, Deutschland');
});
test('features without a point or a name, and answers without features, '
'are no places', () {
expect(
placesOf({
'features': [
{
'geometry': {
'type': 'LineString',
'coordinates': [
[1, 2],
[3, 4],
],
},
'properties': {'name': 'A line'},
},
{
'geometry': {
'type': 'Point',
'coordinates': [1, 2],
},
'properties': {'type': 'house'},
},
'not a feature',
],
}),
isEmpty,
);
expect(placesOf(const {}), isEmpty);
});
test('a place keeps its address and its object in OpenStreetMap, which '
"tell two of a town's supermarkets apart", () {
final [place] = placesOf(
photonAnswer([
{
'lat': 47.5606,
'lon': 9.7011,
'name': 'EDEKA',
'type': 'house',
'osm_type': 'W',
'osm_id': 79817244,
'osm_key': 'shop',
'osm_value': 'supermarket',
'street': 'Ludwig-Kick-Straße',
'housenumber': '1',
'district': 'Aeschach',
'city': 'Lindau (Bodensee)',
'country': 'Deutschland',
},
]),
);
expect(place.osmValue, 'supermarket');
expect(place.osmType, 'W');
expect(place.osmId, 79817244);
expect(place.street, 'Ludwig-Kick-Straße');
expect(place.housenumber, '1');
expect(place.district, 'Aeschach');
expect(place.city, 'Lindau (Bodensee)');
expect(
place.detail,
'Ludwig-Kick-Straße 1, Lindau (Bodensee), Deutschland',
);
});
test('a place without what OpenStreetMap knows of it has none of it', () {
final [place] = placesOf(
photonAnswer([
{'lat': 1, 'lon': 2, 'name': 'Somewhere', 'type': 'other'},
]),
);
expect(place.osmValue, isNull);
expect(place.osmType, isNull);
expect(place.osmId, isNull);
expect(place.street, isNull);
expect(place.district, isNull);
});
test('a car park or a charger named after a shop is no place to go: a '
'search for EDEKA finds the shop', () {
final places = placesOf(
photonAnswer([
for (final value in [
'parking',
'parking_entrance',
'parking_space',
'charging_station',
])
{
'lat': 47.56,
'lon': 9.70,
'name': 'EDEKA',
'type': 'other',
'osm_value': value,
},
{
'lat': 47.5606,
'lon': 9.7011,
'name': 'EDEKA',
'type': 'house',
'osm_value': 'supermarket',
},
]),
);
expect([for (final place in places) place.osmValue], ['supermarket']);
});
test('HttpPhoton asks /api with the query, the point, the language and '
'the limit, and fails on what is no answer', () async {
final server = await HttpServer.bind(InternetAddress.loopbackIPv4, 0);
final asked = <Uri>[];
var status = HttpStatus.ok;
server.listen((request) async {
asked.add(request.uri);
request.response
..statusCode = status
..write(jsonEncode(photonAnswer(const [])));
await request.response.close();
});
addTearDown(server.close);
final photon = HttpPhoton(Uri.parse('http://127.0.0.1:${server.port}/'));
await photon.search(
'Tempelhofer Feld',
near: PlacePoint(lat: 52.47, lon: 13.4),
language: 'de',
limit: 8,
);
expect(asked.single.path, '/api');
expect(asked.single.queryParameters, {
'q': 'Tempelhofer Feld',
'limit': '8',
'lat': '52.47',
'lon': '13.4',
'lang': 'de',
});
status = HttpStatus.badRequest;
await expectLater(
photon.search('x', limit: 8),
throwsA(isA<PhotonFailed>()),
);
});
}