chore: Move the drift_offline_first to legacy in favor of Serverpod implementation (#32)

This commit is contained in:
Marcelo Mendonça Soares 2026-04-05 23:20:21 -03:00 • committed by Marcelo Soares
commit cbc472523b
No known key found for this signature in database
GPG key ID: CB2F09CE66E99EAD

View file

@ -0,0 +1,82 @@
import 'package:clock/clock.dart';
import 'package:drift_offline_sync/drift_offline_sync.dart';
import 'package:test/test.dart';
final _dateTime = DateTime.parse('2001-09-09T01:46:40.000Z');
void main() {
group('Given an HLC with lower canonical time than wall time', () {
final hlc = Hlc(_dateTime, 17, 'abc');
final wallTime = _dateTime.advance();
test('when incrementing then dateTime becomes wall time and counter resets.', () {
withClock(Clock.fixed(wallTime), () {
final sendHlc = hlc.increment();
expect(sendHlc, isNot(hlc));
expect(sendHlc.dateTime, wallTime);
expect(sendHlc.counter, 0);
expect(sendHlc.nodeId, hlc.nodeId);
});
});
});
group('Given an HLC with equal canonical time and wall time', () {
final hlc = Hlc(_dateTime, 17, 'abc');
final wallTime = _dateTime;
test('when incrementing then counter increments and dateTime is unchanged.', () {
withClock(Clock.fixed(wallTime), () {
final sendHlc = hlc.increment();
expect(sendHlc, isNot(hlc));
expect(sendHlc.dateTime, hlc.dateTime);
expect(sendHlc.counter, 18);
expect(sendHlc.nodeId, hlc.nodeId);
});
});
});
group('Given an HLC with higher canonical time than wall time', () {
final hlc = Hlc(_dateTime, 17, 'abc');
final wallTime = _dateTime.retreat();
test('when incrementing then counter increments and dateTime is unchanged.', () {
withClock(Clock.fixed(wallTime), () {
final sendHlc = hlc.increment();
expect(sendHlc, isNot(hlc));
expect(sendHlc.dateTime, hlc.dateTime);
expect(sendHlc.counter, 18);
expect(sendHlc.nodeId, hlc.nodeId);
});
});
});
group('Given an HLC with canonical time more than one minute ahead of wall time', () {
final hlc = Hlc(_dateTime.add(const Duration(minutes: 1, seconds: 5)), 0, 'abc');
final wallTime = _dateTime;
test('when incrementing then ClockDriftException is thrown.', () {
withClock(Clock.fixed(wallTime), () {
expect(hlc.increment, throwsA(isA<ClockDriftException>()));
});
});
});
group('Given an HLC with counter at maximum and canonical time at wall time', () {
final hlc = Hlc(_dateTime, 0xFFFF, 'abc');
final wallTime = _dateTime;
test('when incrementing at same time then OverflowException is thrown.', () {
withClock(Clock.fixed(wallTime), () {
expect(hlc.increment, throwsA(isA<OverflowException>()));
});
});
});
}
extension on DateTime {
DateTime advance() => add(const Duration(milliseconds: 1));
DateTime retreat() => subtract(const Duration(milliseconds: 1));
}