import 'package:anyway/data/datasources/trip_local_datasource.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:shared_preferences/shared_preferences.dart'; void main() { TestWidgetsFlutterBinding.ensureInitialized(); late TripLocalDataSource dataSource; Map buildTripJson(String uuid) { return { 'uuid': uuid, 'total_time_minute': 90, 'landmarks': [ { 'uuid': 'lm-$uuid', 'name': 'Landmark $uuid', 'location': [0.0, 0.0], 'type': {'type': 'sightseeing'}, }, ], }; } setUp(() { SharedPreferences.setMockInitialValues({}); dataSource = TripLocalDataSourceImpl(); }); test('loadTrips returns empty list when storage is empty', () async { final trips = await dataSource.loadTrips(); expect(trips, isEmpty); }); test('upsertTrip stores and retrieves a trip', () async { await dataSource.upsertTrip(buildTripJson('trip-1')); final trips = await dataSource.loadTrips(); expect(trips, hasLength(1)); expect(trips.first['uuid'], 'trip-1'); final fetched = await dataSource.getTrip('trip-1'); expect(fetched, isNotNull); expect(fetched!['uuid'], 'trip-1'); }); test('deleteTrip removes the persisted entry', () async { await dataSource.upsertTrip(buildTripJson('trip-1')); await dataSource.upsertTrip(buildTripJson('trip-2')); await dataSource.deleteTrip('trip-1'); final remaining = await dataSource.loadTrips(); expect(remaining, hasLength(1)); expect(remaining.single['uuid'], 'trip-2'); }); test('upsertTrip overwrites an existing trip and keeps latest first', () async { await dataSource.upsertTrip(buildTripJson('trip-1')); await dataSource.upsertTrip(buildTripJson('trip-2')); final updatedTrip1 = buildTripJson('trip-1')..['total_time_minute'] = 120; await dataSource.upsertTrip(updatedTrip1); final trips = await dataSource.loadTrips(); expect(trips, hasLength(2)); expect(trips.first['uuid'], 'trip-1'); expect(trips.first['total_time_minute'], 120); expect(trips[1]['uuid'], 'trip-2'); }); }