Add sync queue orchestration and local mutation envelopes
This commit is contained in:
@@ -0,0 +1,115 @@
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:relationship_saver/features/local/local_repository.dart';
|
||||
import 'package:relationship_saver/features/sync/sync_coordinator.dart';
|
||||
import 'package:relationship_saver/features/sync/sync_queue_repository.dart';
|
||||
import 'package:relationship_saver/features/sync/sync_state.dart';
|
||||
import 'package:relationship_saver/integrations/backend/backend_gateway_fake.dart';
|
||||
import 'package:relationship_saver/integrations/backend/backend_gateway_provider.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('pushPending flushes queued local mutations', () async {
|
||||
final ProviderContainer container = ProviderContainer(
|
||||
overrides: [backendGatewayProvider.overrideWithValue(_TestGateway())],
|
||||
);
|
||||
addTearDown(container.dispose);
|
||||
|
||||
await container.read(localRepositoryProvider.future);
|
||||
await container
|
||||
.read(localRepositoryProvider.notifier)
|
||||
.addPerson(
|
||||
name: 'Queued Person',
|
||||
relationship: 'Friend',
|
||||
notes: 'sync me',
|
||||
tags: <String>['test'],
|
||||
);
|
||||
|
||||
final SyncState before = await container.read(
|
||||
syncQueueRepositoryProvider.future,
|
||||
);
|
||||
expect(before.pendingChanges.length, 1);
|
||||
|
||||
final SyncRunResult result = await container
|
||||
.read(syncCoordinatorProvider)
|
||||
.pushPending();
|
||||
|
||||
expect(result.success, isTrue);
|
||||
expect(result.accepted, 1);
|
||||
|
||||
final SyncState after = await container.read(
|
||||
syncQueueRepositoryProvider.future,
|
||||
);
|
||||
expect(after.pendingChanges, isEmpty);
|
||||
expect(after.cursor, isNotNull);
|
||||
});
|
||||
|
||||
test('pullRemote applies backend changes into local repository', () async {
|
||||
final SyncPullResult pullResult = SyncPullResult(
|
||||
cursor: 'cursor-remote-1',
|
||||
changes: <ChangeEnvelope>[
|
||||
ChangeEnvelope(
|
||||
schemaVersion: 1,
|
||||
entityType: 'person',
|
||||
entityId: 'p-remote-1',
|
||||
op: ChangeOperation.upsert,
|
||||
modifiedAt: DateTime(2026, 2, 15, 8).toUtc(),
|
||||
clientMutationId: 'remote-1',
|
||||
payload: const <String, dynamic>{
|
||||
'name': 'Remote Person',
|
||||
'relationship': 'Sibling',
|
||||
'affinityScore': 88,
|
||||
'nextMoment': '2026-02-20T12:00:00Z',
|
||||
'tags': <String>['remote'],
|
||||
'notes': 'arrived from backend',
|
||||
},
|
||||
),
|
||||
],
|
||||
);
|
||||
|
||||
final ProviderContainer container = ProviderContainer(
|
||||
overrides: [
|
||||
backendGatewayProvider.overrideWithValue(
|
||||
_TestGateway(pullResult: pullResult),
|
||||
),
|
||||
],
|
||||
);
|
||||
addTearDown(container.dispose);
|
||||
|
||||
await container.read(localRepositoryProvider.future);
|
||||
|
||||
final SyncRunResult result = await container
|
||||
.read(syncCoordinatorProvider)
|
||||
.pullRemote();
|
||||
|
||||
expect(result.success, isTrue);
|
||||
expect(result.pulled, 1);
|
||||
|
||||
final localState = await container.read(localRepositoryProvider.future);
|
||||
final bool hasRemote = localState.people.any(
|
||||
(person) => person.id == 'p-remote-1' && person.name == 'Remote Person',
|
||||
);
|
||||
expect(hasRemote, isTrue);
|
||||
|
||||
final SyncState queueState = await container.read(
|
||||
syncQueueRepositoryProvider.future,
|
||||
);
|
||||
expect(queueState.cursor, 'cursor-remote-1');
|
||||
});
|
||||
}
|
||||
|
||||
class _TestGateway extends BackendGatewayFake {
|
||||
_TestGateway({this.pullResult});
|
||||
|
||||
final SyncPullResult? pullResult;
|
||||
|
||||
@override
|
||||
Future<SyncPullResult> pullChanges({String? cursor, int limit = 200}) async {
|
||||
return pullResult ?? super.pullChanges(cursor: cursor, limit: limit);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
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'},
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user