Files
Europa/Nuvolari/app/test/features/places/map_target_test.dart
T
Alby96andClaude Opus 5 a686f7625e Mark the place the map is pointed at, and make following work
Choosing a town moved the camera and then left the reader to work out which of
the settlements now on screen was the one they asked for. A town has no precise
position, so the map now marks its centre: a white disc under a coloured dot,
drawn as a style layer so the native renderer keeps it pinned through every pan
and zoom. The radar frames are inserted below it, because a band of rain must
not paint over the one thing that says which town this is.

A searched municipality becomes a PointTarget rather than being thrown away as
"the whole region". It is marked, the recentre button returns to it, and the
chip names it - but it is never persisted, so looking something up no longer
costs the user the place their app opens on. That was a real defect: searching
went through select(FreeTarget), which wrote null over the stored preference.

Following the device was broken outright. MapLibre reports a camera animation
the app started exactly as it reports the user grabbing the map, so engaging
follow and then animating to the position cancelled the follow it had just
started; the resulting FreeTarget then re-framed the whole region. Follow now
moves the camera first and switches tracking on second, and FreeTarget no longer
moves the camera as a reaction to the state changing - framing the region is an
action, so panning away while following keeps the view the user panned to.

Verified on the emulator with a fix in Turin: a searched town is marked and
saveable, the app reopens on it with the marker in place, following centres on
the blue dot, and panning stops the chase without yanking the map away.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-10 23:17:21 +02:00

177 lines
6.1 KiB
Dart

import 'dart:io';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:nuvolari/core/region/geo.dart';
import 'package:nuvolari/core/region/region_config.dart';
import 'package:nuvolari/core/region/region_repository.dart';
import 'package:nuvolari/features/places/map_target.dart';
import 'package:nuvolari/features/places/places_repository.dart';
import 'package:nuvolari/features/places/saved_place.dart';
import 'package:nuvolari/features/places/saved_places.dart';
const torino = GeoPoint(7.686, 45.070);
const cuneo = GeoPoint(7.549, 44.393);
SavedPlace place(String id, String name, GeoPoint point) =>
SavedPlace(id: id, name: name, point: point, createdAt: DateTime.utc(2026));
({ProviderContainer container, InMemoryPreferredTargetStore store}) harness(
RegionConfig region, {
List<SavedPlace>? places,
String? preferred,
}) {
final store = InMemoryPreferredTargetStore(preferred);
final container = ProviderContainer(
overrides: [
regionConfigProvider.overrideWith((ref) async => region),
placesStoreProvider.overrideWithValue(InMemoryPlacesStore(places)),
preferredTargetStoreProvider.overrideWithValue(store),
],
);
addTearDown(container.dispose);
return (container: container, store: store);
}
void main() {
late RegionConfig region;
setUpAll(() {
region = RegionConfig.parse(
File('assets/regions/piemonte.json').readAsStringSync(),
);
});
group('MapTargetController', () {
test('selecting a place remembers it as what to reopen on', () async {
final casa = place('a', 'Casa', torino);
final h = harness(region, places: [casa]);
await h.container.read(savedPlacesProvider.future);
await h.container
.read(mapTargetProvider.notifier)
.select(PlaceTarget(casa));
expect(h.container.read(mapTargetProvider), PlaceTarget(casa));
expect(await h.store.load(), 'place:a');
});
test('choosing the whole region clears the stored preference', () async {
final h = harness(region, preferred: 'follow');
await h.container.read(savedPlacesProvider.future);
await h.container
.read(mapTargetProvider.notifier)
.select(const FreeTarget());
expect(await h.store.load(), isNull);
});
// Searching is looking, not deciding. Before PointTarget existed a search
// went through select(FreeTarget), which wrote null over the preference and
// silently cost the user the place their app opened on.
test('previewing a searched point leaves the preference alone', () async {
final casa = place('a', 'Casa', torino);
final h = harness(region, places: [casa], preferred: 'place:a');
await h.container.read(savedPlacesProvider.future);
h.container
.read(mapTargetProvider.notifier)
.previewPoint(point: cuneo, label: 'Cuneo');
expect(
h.container.read(mapTargetProvider),
const PointTarget(point: cuneo, label: 'Cuneo'),
);
expect(
await h.store.load(),
'place:a',
reason: 'a search must not change what the app reopens on',
);
});
test('a previewed point is never persisted, even if asked', () async {
final h = harness(region);
await h.container.read(savedPlacesProvider.future);
expect(
() => h.container
.read(mapTargetProvider.notifier)
.select(const PointTarget(point: cuneo, label: 'Cuneo')),
throwsA(isA<AssertionError>()),
);
});
// What the map marks is the whole point of the target for anything that is
// not the user themselves: a town has no precise position, so its centre is
// the honest answer.
test('says what the map should mark', () {
final casa = place('a', 'Casa', torino);
expect(PlaceTarget(casa).markedPoint, torino);
expect(
const PointTarget(point: cuneo, label: 'Cuneo').markedPoint,
cuneo,
);
expect(const FreeTarget().markedPoint, isNull);
expect(
const FollowUser().markedPoint,
isNull,
reason: 'the blue dot marks the user, not a pin',
);
});
test('points differ by coordinates and by label', () {
const at = PointTarget(point: cuneo, label: 'Cuneo');
expect(at, const PointTarget(point: cuneo, label: 'Cuneo'));
expect(at, isNot(const PointTarget(point: torino, label: 'Cuneo')));
expect(at, isNot(const PointTarget(point: cuneo, label: 'Cuneo Centro')));
});
test('restores the preferred place once places have loaded', () async {
final casa = place('a', 'Casa', torino);
final h = harness(region, places: [casa], preferred: 'place:a');
await h.container.read(savedPlacesProvider.future);
// Reading it is what builds it, and the restore starts from there: it
// reads the store, so it settles a microtask later, not immediately.
expect(h.container.read(mapTargetProvider), const FreeTarget());
await Future<void>.delayed(Duration.zero);
expect(h.container.read(mapTargetProvider), PlaceTarget(casa));
});
test('forgets a preferred place that no longer exists', () async {
final h = harness(region, places: [], preferred: 'place:gone');
await h.container.read(savedPlacesProvider.future);
h.container.read(mapTargetProvider);
await Future<void>.delayed(Duration.zero);
expect(h.container.read(mapTargetProvider), const FreeTarget());
expect(await h.store.load(), isNull);
});
test(
'panning while following stops the chase but keeps the choice',
() async {
final h = harness(region);
await h.container.read(savedPlacesProvider.future);
await h.container
.read(mapTargetProvider.notifier)
.select(const FollowUser());
h.container.read(mapTargetProvider.notifier).releaseFollow();
expect(h.container.read(mapTargetProvider), const FreeTarget());
expect(
await h.store.load(),
'follow',
reason:
'looking elsewhere now is not changing your mind for next time',
);
},
);
});
}