2026-07-10 13:16:43 -03:00
|
|
|
import 'package:serverpod_offline_sync/serverpod_offline_sync.dart';
|
2026-05-03 09:09:23 -03:00
|
|
|
import 'package:test/test.dart';
|
|
|
|
|
|
2026-09-04 18:09:20 +02:00
|
|
|
import 'hlc_fixtures.dart';
|
|
|
|
|
|
|
|
|
|
final _isoTime = hlcTime.toIso8601String();
|
2026-05-03 09:09:23 -03:00
|
|
|
|
|
|
|
|
void main() {
|
|
|
|
|
test('Given an HLC built with dateTime, counter and nodeId '
|
|
|
|
|
'when reading properties '
|
|
|
|
|
'then dateTime, counter and nodeId match.', () {
|
2026-09-04 18:09:20 +02:00
|
|
|
final hlc = Hlc(hlcTime, 0x17, hlcNodeId);
|
2026-05-03 09:09:23 -03:00
|
|
|
|
2026-09-04 18:09:20 +02:00
|
|
|
expect(hlc.datetime, hlcTime);
|
2026-05-03 09:09:23 -03:00
|
|
|
expect(hlc.counter, 0x17);
|
2026-09-04 18:09:20 +02:00
|
|
|
expect(hlc.nodeId, hlcNodeId);
|
2026-05-03 09:09:23 -03:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('Given Hlc.zero '
|
|
|
|
|
'when building for a nodeId '
|
|
|
|
|
'then it equals epoch with zero counter.', () {
|
2026-09-04 18:09:20 +02:00
|
|
|
final zero = Hlc.zero(hlcNodeId);
|
2026-05-03 09:09:23 -03:00
|
|
|
|
|
|
|
|
expect(zero.unixTimestamp, 0);
|
|
|
|
|
expect(zero.counter, 0);
|
2026-09-04 18:09:20 +02:00
|
|
|
expect(zero.nodeId, hlcNodeId);
|
2026-05-03 09:09:23 -03:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('Given an ISO HLC string '
|
|
|
|
|
'when parsing '
|
|
|
|
|
'then it returns the equivalent Hlc.', () {
|
2026-09-04 18:09:20 +02:00
|
|
|
final hlc = Hlc(hlcTime, 0x17, hlcNodeId);
|
2026-05-03 09:09:23 -03:00
|
|
|
|
2026-09-04 18:09:20 +02:00
|
|
|
expect(Hlc.parse('${hlcTime.toIso8601String()}-0017-$hlcNodeId'), hlc);
|
2026-05-03 09:09:23 -03:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('Given a Unix timestamp HLC string '
|
|
|
|
|
'when parsing '
|
|
|
|
|
'then it returns the equivalent Hlc.', () {
|
2026-09-04 18:09:20 +02:00
|
|
|
final hlc = Hlc(hlcTime, 0x17, hlcNodeId);
|
2026-05-03 09:09:23 -03:00
|
|
|
|
2026-09-04 18:09:20 +02:00
|
|
|
expect(Hlc.parse('${hlcTime.millisecondsSinceEpoch}-0017-$hlcNodeId'), hlc);
|
2026-05-03 09:09:23 -03:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('Given legacy string with :worker suffix '
|
|
|
|
|
'when parsing '
|
|
|
|
|
'then node id is the uuid only.', () {
|
2026-09-04 18:09:20 +02:00
|
|
|
final hlc = Hlc.parse('$_isoTime-0017-$hlcNodeId:7');
|
|
|
|
|
expect(hlc.nodeId, hlcNodeId);
|
2026-05-03 09:09:23 -03:00
|
|
|
expect(hlc.counter, 0x17);
|
2026-09-04 18:09:20 +02:00
|
|
|
expect(hlc.datetime, hlcTime);
|
2026-05-03 09:09:23 -03:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('Given a parsed Hlc '
|
|
|
|
|
'when calling toString '
|
|
|
|
|
'then it returns the ISO HLC string.', () {
|
2026-09-04 18:09:20 +02:00
|
|
|
final hlc = Hlc.parse('$_isoTime-0017-$hlcNodeId');
|
2026-05-03 09:09:23 -03:00
|
|
|
|
2026-09-04 18:09:20 +02:00
|
|
|
expect(hlc.toString(), '$_isoTime-0017-$hlcNodeId');
|
2026-05-03 09:09:23 -03:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('Given an HLC '
|
|
|
|
|
'when converting to JSON '
|
|
|
|
|
'then it returns the equivalent string.', () {
|
2026-09-04 18:09:20 +02:00
|
|
|
final hlc = Hlc(hlcTime, 0x17, hlcNodeId);
|
2026-05-03 09:09:23 -03:00
|
|
|
|
|
|
|
|
expect(hlc.toJson(), {
|
2026-09-04 18:09:20 +02:00
|
|
|
'datetime': hlcTime.toIso8601String(),
|
2026-05-03 09:09:23 -03:00
|
|
|
'counter': '0017',
|
2026-09-04 18:09:20 +02:00
|
|
|
'node': hlcNodeId.toString(),
|
2026-05-03 09:09:23 -03:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|