MotorSignal-App/test/fake_route_provider.dart

112 lines
3.1 KiB
Dart

import 'package:motorcycle_dash/services/map/map_provider_interface.dart';
import 'package:motorcycle_dash/services/routes/route_models.dart';
import 'package:motorcycle_dash/services/routes/route_provider.dart';
import 'dart:async';
class FakeRouteProvider implements RouteProvider {
final bool shouldFail;
final bool shouldTimeout;
final List<PlaceSearchResult>? customSearchResults;
final ResolvedPlace? customResolvedPlace;
final Future<void> Function(int count)? onCalculate;
int _calcCount = 0;
List<RouteStop>? lastCapturedStops;
FakeRouteProvider({
this.shouldFail = false,
this.shouldTimeout = false,
this.customSearchResults,
this.customResolvedPlace,
this.onCalculate,
});
@override
String get name => 'Fake Provider';
@override
Future<RouteCalculationResult> calculateRoute(
List<RouteStop> stops, {
bool avoidHighways = false,
bool avoidTolls = false,
bool avoidFerries = false,
RoutingOptions? options,
}) async {
lastCapturedStops = stops;
if (onCalculate != null) {
await onCalculate!(_calcCount++);
}
if (shouldTimeout) {
await Future.delayed(const Duration(seconds: 2));
throw RouteProviderException('providerTimeout', 'Fake timeout');
}
if (shouldFail) {
throw RouteProviderException('routeNotFound', 'Fake route not found');
}
if (stops.length < 2) {
throw RouteProviderException('invalidRequest', 'Too few stops');
}
if (stops.length > 25) {
throw RouteProviderException('invalidRequest', 'Too many stops');
}
final ordered = List<RouteStop>.from(stops);
ordered.sort((a, b) => a.order.compareTo(b.order));
final dist = MotorSignalRoute.calculateTotalDistanceMeters(ordered);
final geometry = ordered.map((s) => s.latLng).toList();
return RouteCalculationResult(
straightLineDistanceMeters: dist,
estimatedDistanceMeters: dist * 1.2,
estimatedDurationSeconds: (dist / 20).toInt(),
geometry: geometry,
isStraightLine: false,
providerName: name,
warnings: ['This is a fake route.'],
);
}
@override
Future<List<PlaceSearchResult>> searchPlaces(
String query, {
AppLatLng? nearLocation,
}) async {
if (shouldFail) {
throw RouteProviderException('placeSearchUnavailable', 'Fake search error');
}
if (customSearchResults != null) return customSearchResults!;
if (query.length < 2) return [];
return [
PlaceSearchResult(
providerId: 'fake_1',
primaryText: 'Fake Result 1 for $query',
secondaryText: 'Fake City',
attribution: 'Fake Attribution',
),
];
}
@override
Future<ResolvedPlace> resolvePlace(String providerPlaceId) async {
if (shouldFail) {
throw RouteProviderException('placeNotFound', 'Fake not found');
}
if (customResolvedPlace != null) return customResolvedPlace!;
return ResolvedPlace(
providerId: providerPlaceId,
displayName: 'Fake Place',
latitude: 0.0,
longitude: 0.0,
attribution: 'Fake Attribution',
);
}
}