104 lines
3.2 KiB
Dart
104 lines
3.2 KiB
Dart
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:relationship_saver/features/sync/sync_queue_repository.dart';
|
|
import 'package:relationship_saver/integrations/backend/models/backend_models.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
void main() {
|
|
setUp(() {
|
|
SharedPreferences.setMockInitialValues(<String, Object>{});
|
|
});
|
|
|
|
test('enqueue persists and can be reloaded', () async {
|
|
final ProviderContainer container = ProviderContainer();
|
|
|
|
await container
|
|
.read(syncQueueRepositoryProvider.notifier)
|
|
.enqueue(_change(id: 'p-1', mutationId: 'cm-1'));
|
|
final SyncStateData firstState = await _readState(container);
|
|
expect(firstState.pending.length, 1);
|
|
|
|
container.dispose();
|
|
|
|
final ProviderContainer reloaded = ProviderContainer();
|
|
addTearDown(reloaded.dispose);
|
|
final SyncStateData reloadedState = await _readState(reloaded);
|
|
expect(reloadedState.pending.length, 1);
|
|
expect(reloadedState.pending.first.clientMutationId, 'cm-1');
|
|
});
|
|
|
|
test(
|
|
'applyPushResult removes completed mutation ids and tracks rejections',
|
|
() async {
|
|
final ProviderContainer container = ProviderContainer();
|
|
addTearDown(container.dispose);
|
|
|
|
final SyncQueueRepository notifier = container.read(
|
|
syncQueueRepositoryProvider.notifier,
|
|
);
|
|
|
|
await notifier.enqueueAll(<ChangeEnvelope>[
|
|
_change(id: 'p-1', mutationId: 'cm-a'),
|
|
_change(id: 'p-2', mutationId: 'cm-b'),
|
|
]);
|
|
|
|
await notifier.applyPushResult(
|
|
SyncPushResult(
|
|
cursor: 'cursor-2',
|
|
accepted: const <MutationAck>[MutationAck(clientMutationId: 'cm-a')],
|
|
rejected: const <MutationRejection>[
|
|
MutationRejection(
|
|
clientMutationId: 'cm-b',
|
|
code: 'VALIDATION_FAILED',
|
|
message: 'invalid payload',
|
|
),
|
|
],
|
|
),
|
|
at: DateTime(2026, 2, 15, 12),
|
|
);
|
|
|
|
final SyncStateData state = await _readState(container);
|
|
expect(state.pending, isEmpty);
|
|
expect(state.cursor, 'cursor-2');
|
|
expect(state.rejected.length, 1);
|
|
expect(state.lastError, contains('Push rejected'));
|
|
},
|
|
);
|
|
}
|
|
|
|
class SyncStateData {
|
|
const SyncStateData({
|
|
required this.cursor,
|
|
required this.pending,
|
|
required this.rejected,
|
|
required this.lastError,
|
|
});
|
|
|
|
final String? cursor;
|
|
final List<ChangeEnvelope> pending;
|
|
final List<MutationRejection> rejected;
|
|
final String? lastError;
|
|
}
|
|
|
|
Future<SyncStateData> _readState(ProviderContainer container) async {
|
|
final state = await container.read(syncQueueRepositoryProvider.future);
|
|
return SyncStateData(
|
|
cursor: state.cursor,
|
|
pending: state.pendingChanges,
|
|
rejected: state.lastRejected,
|
|
lastError: state.lastError,
|
|
);
|
|
}
|
|
|
|
ChangeEnvelope _change({required String id, required String mutationId}) {
|
|
return ChangeEnvelope(
|
|
schemaVersion: 1,
|
|
entityType: 'person',
|
|
entityId: id,
|
|
op: ChangeOperation.upsert,
|
|
modifiedAt: DateTime(2026, 2, 15, 12).toUtc(),
|
|
clientMutationId: mutationId,
|
|
payload: const <String, dynamic>{'name': 'Alex', 'relationship': 'Friend'},
|
|
);
|
|
}
|