fix: Make the CrdtDatabase class ephemeral (#82)

This commit is contained in:
Marcelo Mendonça Soares 2026-06-24 01:18:23 -03:00 • committed by GitHub
commit 517abde4a1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 362 additions and 151 deletions

View file

@ -15,12 +15,7 @@ Future<void> run(List<String> args) async {
Protocol(),
Endpoints(),
authenticationHandler: demoAuthenticationHandler,
// TODO: Remove the duplicate [syncTables] argument from here and the
// [initializeCrdtSync] call below.
databaseInterceptor: (_, inner) => CrdtDatabase(
inner,
syncTables: demoSyncTables,
),
databaseInterceptor: crdtDatabaseInterceptor,
)..initializeCrdtSync(syncTables: demoSyncTables);
// Start the server.

View file

@ -0,0 +1,70 @@
import 'package:serverpod_offline_sync_server/serverpod_offline_sync_server.dart';
import 'package:serverpod_offline_sync_test_server/src/generated/protocol.dart'
as server;
import 'package:test/test.dart';
import '../test_tools/serverpod_test_tools.dart';
void main() {
final syncTables = [server.Person.t, server.Address.t];
withServerpod(
'Given a Serverpod with CRDT sync initialized,',
(sessionBuilder, _) {
final session = sessionBuilder.build();
session.serverpod.initializeCrdtSync(syncTables: syncTables);
test(
'when the database interceptor wraps a session database, '
'then it returns a CRDT-aware database.',
() {
final wrapped = crdtDatabaseInterceptor(session, session.db);
expect(wrapped, isA<CrdtDatabase>());
},
);
test(
'when the database interceptor wraps an already-wrapped database, '
'then it returns the same instance.',
() {
final wrapped = crdtDatabaseInterceptor(session, session.db);
final reWrapped = crdtDatabaseInterceptor(session, wrapped);
expect(identical(reWrapped, wrapped), isTrue);
},
);
test(
'when resolving the CRDT sync for the pod, '
'then the configured instance is returned.',
() {
expect(session.crdt, isNotNull);
},
);
},
);
withServerpod(
'Given a Serverpod without CRDT sync initialized,',
(sessionBuilder, _) {
final session = sessionBuilder.build();
test(
'when the database interceptor wraps a session database, '
'then it returns the original database unchanged.',
() {
final inner = session.db;
final result = crdtDatabaseInterceptor(session, inner);
expect(identical(result, inner), isTrue);
},
);
test(
'when resolving the CRDT sync for the pod, '
'then a StateError is thrown.',
() {
expect(() => session.crdt, throwsStateError);
},
);
},
);
}

View file

@ -165,7 +165,7 @@ void main() {
clientToServer.add(event);
});
final serverSubscription = CrdtSync.instance
final serverSubscription = rawServerSession.crdt
.sync(
serverSession,
userId: testCrdtUserId,