import 'package:flutter_test/flutter_test.dart'; import 'package:ikar/core/storage/message_persistence.dart'; import 'package:sqflite_common_ffi/sqflite_ffi.dart'; void main() { sqfliteFfiInit(); late Database db; setUp(() async { db = await databaseFactoryFfi.openDatabase(inMemoryDatabasePath); await db.execute( 'CREATE TABLE messages (id TEXT NOT NULL, account_id INTEGER NOT NULL, ' 'chat_id INTEGER NOT NULL, text TEXT NOT NULL, PRIMARY KEY (account_id, chat_id, id))', ); await db.insert('messages', { 'id': 'temp', 'account_id': 42, 'chat_id': 7, 'text': 'ciphertext', }); }); tearDown(() => db.close()); test('replaces only the matching account and chat', () async { await db.insert('messages', { 'id': 'temp', 'account_id': 84, 'chat_id': 7, 'text': 'other', }); await MessagePersistence.replace(db, { 'id': 'ack', 'account_id': 42, 'chat_id': 7, 'text': 'ciphertext', }, removeId: 'temp'); final rows = await db.query('messages', orderBy: 'account_id'); expect(rows.map((r) => r['id']), ['ack', 'temp']); }); test('failed deletion rolls back the replacement insert', () async { await db.execute( "CREATE TRIGGER reject_delete BEFORE DELETE ON messages BEGIN SELECT RAISE(ABORT, 'disk error'); END", ); await expectLater( MessagePersistence.replace(db, { 'id': 'ack', 'account_id': 42, 'chat_id': 7, 'text': 'ciphertext', }, removeId: 'temp'), throwsA(isA()), ); expect((await db.query('messages')).single['id'], 'temp'); }); }