Skip to content

Commit 995f248

Browse files
committed
Start with some unit tests
1 parent ab62c80 commit 995f248

12 files changed

+352
-68
lines changed

packages/powersync_core/lib/powersync_core.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export 'src/log.dart';
1111
export 'src/open_factory.dart';
1212
export 'src/schema.dart';
1313
export 'src/sync/options.dart' hide ResolvedSyncOptions;
14+
export 'src/sync/stream.dart' hide CoreActiveStreamSubscription;
1415
export 'src/sync/sync_status.dart'
1516
hide BucketProgress, InternalSyncDownloadProgress;
1617
export 'src/uuid.dart';

packages/powersync_core/lib/src/sync/connection_manager.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import 'package:powersync_core/src/abort_controller.dart';
77
import 'package:powersync_core/src/database/active_instances.dart';
88
import 'package:powersync_core/src/database/powersync_db_mixin.dart';
99
import 'package:powersync_core/src/sync/options.dart';
10-
import 'package:powersync_core/src/sync/stream.dart';
1110

1211
import 'streaming_sync.dart';
1312

@@ -189,6 +188,7 @@ final class ConnectionManager {
189188
hasSynced: status.lastSyncedAt != null
190189
? true
191190
: status.hasSynced ?? currentStatus.hasSynced,
191+
streamSubscriptions: status.internalSubscriptions,
192192
);
193193

194194
// If the absence of hasSynced was the only difference, the new states
@@ -226,7 +226,7 @@ final class ConnectionManager {
226226

227227
Future<void> _subscriptionsCommand(Object? command) async {
228228
await db.writeTransaction((tx) {
229-
return db.execute(
229+
return tx.execute(
230230
'SELECT powersync_control(?, ?)',
231231
['subscriptions', json.encode(command)],
232232
);

packages/powersync_core/lib/src/sync/instruction.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ final class CoreSyncStatus {
8585
null => null,
8686
final raw as Map<String, Object?> => DownloadProgress.fromJson(raw),
8787
},
88-
streams: (json['stream'] as List<Object?>?)
88+
streams: (json['streams'] as List<Object?>?)
8989
?.map((e) =>
9090
CoreActiveStreamSubscription.fromJson(e as Map<String, Object?>))
9191
.toList(),

packages/powersync_core/lib/src/sync/mutable_sync_status.dart

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ final class MutableSyncStatus {
1616

1717
InternalSyncDownloadProgress? downloadProgress;
1818
List<SyncPriorityStatus> priorityStatusEntries = const [];
19-
List<CoreActiveStreamSubscription> streams = const [];
19+
List<CoreActiveStreamSubscription>? streams;
2020

2121
DateTime? lastSyncedAt;
2222

@@ -53,9 +53,9 @@ final class MutableSyncStatus {
5353
hasSynced: true,
5454
lastSyncedAt: now,
5555
priority: maxBy(
56-
applied.checksums.map((cs) => BucketPriority(cs.priority)),
56+
applied.checksums.map((cs) => StreamPriority(cs.priority)),
5757
(priority) => priority,
58-
compare: BucketPriority.comparator,
58+
compare: StreamPriority.comparator,
5959
)!,
6060
)
6161
];
@@ -92,8 +92,9 @@ final class MutableSyncStatus {
9292
final downloading => InternalSyncDownloadProgress(downloading.buckets),
9393
};
9494
lastSyncedAt = status.priorityStatus
95-
.firstWhereOrNull((s) => s.priority == BucketPriority.fullSyncPriority)
95+
.firstWhereOrNull((s) => s.priority == StreamPriority.fullSyncPriority)
9696
?.lastSyncedAt;
97+
streams = status.streams;
9798
}
9899

99100
SyncStatus immutableSnapshot() {
@@ -108,6 +109,7 @@ final class MutableSyncStatus {
108109
hasSynced: null, // Stream client is not supposed to set this value.
109110
uploadError: uploadError,
110111
downloadError: downloadError,
112+
streamSubscriptions: streams,
111113
);
112114
}
113115
}

packages/powersync_core/lib/src/sync/options.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ final class SyncOptions {
4747
retryDelay: retryDelay,
4848
params: params ?? this.params,
4949
syncImplementation: syncImplementation,
50+
includeDefaultStreams: includeDefaultStreams,
5051
);
5152
}
5253
}
@@ -106,11 +107,14 @@ extension type ResolvedSyncOptions(SyncOptions source) {
106107
crudThrottleTime: other.crudThrottleTime ?? crudThrottleTime,
107108
retryDelay: other.retryDelay ?? retryDelay,
108109
params: other.params ?? params,
110+
includeDefaultStreams:
111+
other.includeDefaultStreams ?? includeDefaultStreams,
109112
);
110113

111114
final didChange = !_mapEquality.equals(newOptions.params, params) ||
112115
newOptions.crudThrottleTime != crudThrottleTime ||
113-
newOptions.retryDelay != retryDelay;
116+
newOptions.retryDelay != retryDelay ||
117+
newOptions.includeDefaultStreams != includeDefaultStreams;
114118
return (ResolvedSyncOptions(newOptions), didChange);
115119
}
116120

packages/powersync_core/lib/src/sync/stream.dart

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,11 @@ final class CoreActiveStreamSubscription implements SyncSubscriptionDefinition {
123123
factory CoreActiveStreamSubscription.fromJson(Map<String, Object?> json) {
124124
return CoreActiveStreamSubscription._(
125125
name: json['name'] as String,
126-
parameters: json['parameters'] as Map<String, Object?>,
127-
priority: StreamPriority(json['priority'] as int),
126+
parameters: json['parameters'] as Map<String, Object?>?,
127+
priority: switch (json['priority'] as int?) {
128+
final prio? => StreamPriority(prio),
129+
null => StreamPriority.fullSyncPriority,
130+
},
128131
associatedBuckets: (json['associated_buckets'] as List).cast(),
129132
active: json['active'] as bool,
130133
isDefault: json['is_default'] as bool,

packages/powersync_core/lib/src/sync/sync_status.dart

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,12 @@ final class SyncStatus {
208208
static const _mapEquality = MapEquality<Object?, Object?>();
209209
}
210210

211+
@internal
212+
extension InternalSyncStatusAccess on SyncStatus {
213+
List<CoreActiveStreamSubscription>? get internalSubscriptions =>
214+
_internalSubscriptions;
215+
}
216+
211217
final class SyncStreamStatus {
212218
final ProgressWithOperations? progress;
213219
final CoreActiveStreamSubscription _internal;
@@ -276,7 +282,7 @@ class UploadQueueStats {
276282
/// Per-bucket download progress information.
277283
@internal
278284
typedef BucketProgress = ({
279-
BucketPriority priority,
285+
StreamPriority priority,
280286
int atLast,
281287
int sinceLast,
282288
int targetCount,

packages/powersync_core/lib/src/web/sync_worker_protocol.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ extension type StartSynchronization._(JSObject _) implements JSObject {
9292
}
9393

9494
@anonymous
95-
extension type UpdateSubscriptions.__(JSObject _inner) implements JSObject {
95+
extension type UpdateSubscriptions._raw(JSObject _inner) implements JSObject {
9696
external factory UpdateSubscriptions._({
9797
required int requestId,
9898
required JSArray content,

packages/powersync_core/test/bucket_storage_test.dart renamed to packages/powersync_core/test/sync/bucket_storage_test.dart

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ import 'package:powersync_core/src/sync/protocol.dart';
44
import 'package:sqlite_async/sqlite3_common.dart';
55
import 'package:test/test.dart';
66

7-
import 'utils/abstract_test_utils.dart';
8-
import 'utils/test_utils_impl.dart';
7+
import '../utils/abstract_test_utils.dart';
8+
import '../utils/test_utils_impl.dart';
9+
import 'utils.dart';
910

1011
final testUtils = TestUtils();
1112

@@ -39,11 +40,6 @@ const removeAsset1_4 = OplogEntry(
3940
const removeAsset1_5 = OplogEntry(
4041
opId: '5', op: OpType.remove, rowType: 'assets', rowId: 'O1', checksum: 5);
4142

42-
BucketChecksum checksum(
43-
{required String bucket, required int checksum, int priority = 1}) {
44-
return BucketChecksum(bucket: bucket, priority: priority, checksum: checksum);
45-
}
46-
4743
SyncDataBatch syncDataBatch(List<SyncBucketData> data) {
4844
return SyncDataBatch(data);
4945
}

packages/powersync_core/test/sync/in_memory_sync_test.dart

Lines changed: 1 addition & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ import 'package:shelf/shelf.dart';
1010
import 'package:shelf_router/shelf_router.dart';
1111
import 'package:test/test.dart';
1212

13-
import '../bucket_storage_test.dart';
1413
import '../server/sync_server/in_memory_sync_server.dart';
1514
import '../utils/abstract_test_utils.dart';
1615
import '../utils/in_memory_http.dart';
1716
import '../utils/test_utils_impl.dart';
17+
import 'utils.dart';
1818

1919
void main() {
2020
_declareTests(
@@ -968,51 +968,3 @@ void _declareTests(String name, SyncOptions options, bool bson) {
968968
});
969969
});
970970
}
971-
972-
TypeMatcher<SyncStatus> isSyncStatus({
973-
Object? downloading,
974-
Object? connected,
975-
Object? connecting,
976-
Object? hasSynced,
977-
Object? downloadProgress,
978-
}) {
979-
var matcher = isA<SyncStatus>();
980-
if (downloading != null) {
981-
matcher = matcher.having((e) => e.downloading, 'downloading', downloading);
982-
}
983-
if (connected != null) {
984-
matcher = matcher.having((e) => e.connected, 'connected', connected);
985-
}
986-
if (connecting != null) {
987-
matcher = matcher.having((e) => e.connecting, 'connecting', connecting);
988-
}
989-
if (hasSynced != null) {
990-
matcher = matcher.having((e) => e.hasSynced, 'hasSynced', hasSynced);
991-
}
992-
if (downloadProgress != null) {
993-
matcher = matcher.having(
994-
(e) => e.downloadProgress, 'downloadProgress', downloadProgress);
995-
}
996-
997-
return matcher;
998-
}
999-
1000-
TypeMatcher<SyncDownloadProgress> isSyncDownloadProgress({
1001-
required Object progress,
1002-
Map<BucketPriority, Object> priorities = const {},
1003-
}) {
1004-
var matcher =
1005-
isA<SyncDownloadProgress>().having((e) => e, 'untilCompletion', progress);
1006-
priorities.forEach((priority, expected) {
1007-
matcher = matcher.having(
1008-
(e) => e.untilPriority(priority), 'untilPriority($priority)', expected);
1009-
});
1010-
1011-
return matcher;
1012-
}
1013-
1014-
TypeMatcher<ProgressWithOperations> progress(int completed, int total) {
1015-
return isA<ProgressWithOperations>()
1016-
.having((e) => e.downloadedOperations, 'completed', completed)
1017-
.having((e) => e.totalOperations, 'total', total);
1018-
}

0 commit comments

Comments
 (0)