diff --git a/test/cloud_ride_identity_migration_test.dart b/test/cloud_ride_identity_migration_test.dart new file mode 100644 index 0000000..269ad22 --- /dev/null +++ b/test/cloud_ride_identity_migration_test.dart @@ -0,0 +1,37 @@ +import 'package:drift/native.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:motorcycle_dash/database/database.dart'; + +void main() { + TestWidgetsFlutterBinding.ensureInitialized(); + + test('new rides receive a persisted cloudRideId independent of local id', () async { + final db = AppDatabase.forTesting(NativeDatabase.memory()); + addTearDown(db.close); + + final localId = await db.startRide(); + final cloudId = await db.getCloudRideId(localId); + + expect(cloudId, isNotNull); + expect(cloudId, startsWith('ride_')); + expect(cloudId, isNot(localId.toString())); + + final secondLocalId = await db.startRide(); + final secondCloudId = await db.getCloudRideId(secondLocalId); + expect(secondCloudId, isNot(cloudId)); + + // Re-reading the same local row returns the exact persisted identity. + expect(await db.getCloudRideId(localId), cloudId); + }); + + test('ensureCloudRideId is idempotent', () async { + final db = AppDatabase.forTesting(NativeDatabase.memory()); + addTearDown(db.close); + + final localId = await db.startRide(); + final first = await db.ensureCloudRideId(localId); + final second = await db.ensureCloudRideId(localId); + + expect(second, first); + }); +}