test: Add a DST and tests reproducing found failures (#110)

Co-authored-by: Marcelo Soares <marcelo.me.soares@gmail.com>
This commit is contained in:
Kasper Overgård Nielsen 2026-09-04 18:09:20 +02:00 • committed by GitHub
commit c78af247fe
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,74 +1,73 @@
import 'package:serverpod_offline_sync/serverpod_offline_sync.dart';
import 'package:test/test.dart';
import 'package:uuid/uuid.dart';
const _isoTime = '2001-09-09T01:46:40.000Z';
final _dateTime = DateTime.parse(_isoTime);
final _nodeId = const Uuid().v4obj();
import 'hlc_fixtures.dart';
final _isoTime = hlcTime.toIso8601String();
void main() {
test('Given an HLC built with dateTime, counter and nodeId '
'when reading properties '
'then dateTime, counter and nodeId match.', () {
final hlc = Hlc(_dateTime, 0x17, _nodeId);
final hlc = Hlc(hlcTime, 0x17, hlcNodeId);
expect(hlc.datetime, _dateTime);
expect(hlc.datetime, hlcTime);
expect(hlc.counter, 0x17);
expect(hlc.nodeId, _nodeId);
expect(hlc.nodeId, hlcNodeId);
});
test('Given Hlc.zero '
'when building for a nodeId '
'then it equals epoch with zero counter.', () {
final zero = Hlc.zero(_nodeId);
final zero = Hlc.zero(hlcNodeId);
expect(zero.unixTimestamp, 0);
expect(zero.counter, 0);
expect(zero.nodeId, _nodeId);
expect(zero.nodeId, hlcNodeId);
});
test('Given an ISO HLC string '
'when parsing '
'then it returns the equivalent Hlc.', () {
final hlc = Hlc(_dateTime, 0x17, _nodeId);
final hlc = Hlc(hlcTime, 0x17, hlcNodeId);
expect(Hlc.parse('${_dateTime.toIso8601String()}-0017-$_nodeId'), hlc);
expect(Hlc.parse('${hlcTime.toIso8601String()}-0017-$hlcNodeId'), hlc);
});
test('Given a Unix timestamp HLC string '
'when parsing '
'then it returns the equivalent Hlc.', () {
final hlc = Hlc(_dateTime, 0x17, _nodeId);
final hlc = Hlc(hlcTime, 0x17, hlcNodeId);
expect(Hlc.parse('${_dateTime.millisecondsSinceEpoch}-0017-$_nodeId'), hlc);
expect(Hlc.parse('${hlcTime.millisecondsSinceEpoch}-0017-$hlcNodeId'), hlc);
});
test('Given legacy string with :worker suffix '
'when parsing '
'then node id is the uuid only.', () {
final hlc = Hlc.parse('$_isoTime-0017-$_nodeId:7');
expect(hlc.nodeId, _nodeId);
final hlc = Hlc.parse('$_isoTime-0017-$hlcNodeId:7');
expect(hlc.nodeId, hlcNodeId);
expect(hlc.counter, 0x17);
expect(hlc.datetime, _dateTime);
expect(hlc.datetime, hlcTime);
});
test('Given a parsed Hlc '
'when calling toString '
'then it returns the ISO HLC string.', () {
final hlc = Hlc.parse('$_isoTime-0017-$_nodeId');
final hlc = Hlc.parse('$_isoTime-0017-$hlcNodeId');
expect(hlc.toString(), '$_isoTime-0017-$_nodeId');
expect(hlc.toString(), '$_isoTime-0017-$hlcNodeId');
});
test('Given an HLC '
'when converting to JSON '
'then it returns the equivalent string.', () {
final hlc = Hlc(_dateTime, 0x17, _nodeId);
final hlc = Hlc(hlcTime, 0x17, hlcNodeId);
expect(hlc.toJson(), {
'datetime': _dateTime.toIso8601String(),
'datetime': hlcTime.toIso8601String(),
'counter': '0017',
'node': _nodeId.toString(),
'node': hlcNodeId.toString(),
});
});
}