feat(todo): the sync engine and Todo's first models (ADR 0033, D5, D6)

Todo's server syncs with serverpod_offline_sync as Learn's and Train's
do, and holds two synced tables, every payload sealed on the devices:
content_key (the person's data key, sealed to the account) and project
(the Inbox or one of the person's own, changed when edited). A person's
space goes with them, with everything in it.

The migration only adds: the two tables, each indexed on spaceId and
cascading with its space (ON DELETE CASCADE to offline_sync_spaces), the
sync engine's own tables (crdt_*, offline_sync_*) with their foreign
keys, the gen_random_uuid_v7() function their ids default to, and the
modules' version rows. Nothing is dropped or changed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Tizian 「ティツィアーン」 <raisondetredev@pm.me>
This commit is contained in:
Tizian 「ティツィアーン」 2026-11-01 01:00:53 +01:00
commit 3876e30681
Signed by: raisondetredev
GPG key ID: 07F6FCF405488B64

View file

@ -0,0 +1,118 @@
import 'dart:typed_data';
import 'package:halkyon_todo_server/src/generated/protocol.dart';
import 'package:halkyon_todo_server/src/todo_base.dart';
import 'package:halkyon_todo_server/src/todo_data.dart';
import 'package:serverpod/serverpod.dart';
import 'package:serverpod_offline_sync_server/serverpod_offline_sync_server.dart';
import 'package:test/test.dart';
import '../test_tools/serverpod_test_tools.dart';
void main() {
withServerpod(
"A person's projects",
(sessionBuilder, endpoints) {
late Session session;
late UuidValue alice;
late UuidValue bob;
setUp(() {
// The test pod is built without the generated Serverpod class, which
// sets up sync for the running server.
Serverpod.instance.initializeOfflineSync(syncTables: syncTables);
session = sessionBuilder.build();
alice = const Uuid().v7obj();
bob = const Uuid().v7obj();
});
tearDown(() async {
for (final space in [alice, bob]) {
await session.db.unsafeExecute(
'DELETE FROM "offline_sync_spaces" WHERE "uuidSpaceId" = @space',
parameters: QueryParameters.named({'space': space.uuid}),
);
}
});
/// How many rows of [table] are in [person]'s personal space, counted
/// past the sync engine, as the operator would.
Future<int> rowsOf(UuidValue person, String table) async {
final result = await session.db.unsafeQuery(
'SELECT count(*) FROM "$table" t '
'JOIN "offline_sync_spaces" s ON s."id" = t."spaceId" '
'WHERE s."uuidSpaceId" = @space',
parameters: QueryParameters.named({'space': person.uuid}),
);
return result.first.first! as int;
}
test("Todo deletes a person's data with them (ADR 0014's hook)", () {
expect(todoBase().deletePersonData, contains(deleteTodoData));
});
test(
'deleting a person deletes their space with every key and project in '
"it, and nobody else's",
() async {
for (final person in [alice, bob]) {
await _projectOf(session, person);
}
for (final table in ['content_key', 'project']) {
expect(await rowsOf(alice, table), 1, reason: table);
}
await session.db.transaction(
(transaction) => deleteTodoData(session, alice, transaction),
);
// SELECT count(*) FROM "offline_sync_spaces" WHERE "uuidSpaceId" = $1
expect(
await OfflineSyncSpace.db.count(
session,
where: (t) => t.uuidSpaceId.equals(alice),
),
0,
);
for (final table in ['content_key', 'project']) {
expect(await rowsOf(alice, table), 0, reason: table);
expect(await rowsOf(bob, table), 1, reason: table);
}
},
);
},
// The sync engine's database, as the running server has it. Writing
// through it takes transactions of its own, outside the one a test would
// be rolled back in, so the tests delete what they made instead.
databaseInterceptor: (session, inner) {
final database = offlineSyncDatabaseInterceptor(session, inner);
if (database is OfflineSyncDatabase) {
_syncDatabases[session] = database;
}
return database;
},
rollbackDatabase: RollbackDatabase.disabled,
);
}
/// The sync engine's database of each session, as the test pod's own
/// database wraps it.
final _syncDatabases = Expando<OfflineSyncDatabase>();
/// A key and a project in [person]'s personal space, written as their device
/// would through the sync engine (payloads stand in for sealed ones: the
/// server never reads them).
Future<void> _projectOf(Session session, UuidValue person) =>
_syncDatabases[session]!.transactionForUser(person, (transaction) async {
final sealed = ByteData.sublistView(Uint8List.fromList([1, 2, 3]));
await ContentKey.db.insertRow(
session,
ContentKey(dataKeyId: 'todo1.0.key', sealedKey: sealed),
transaction: transaction,
);
await Project.db.insertRow(
session,
Project(keyId: 'todo1.0.key', payload: sealed, nonce: sealed),
transaction: transaction,
);
});