Skip to content

Commit a2322df

Browse files
committed
Add tests
1 parent 8a3dded commit a2322df

File tree

6 files changed

+327
-42
lines changed

6 files changed

+327
-42
lines changed

Diff for: packages/powersync_core/lib/src/database/powersync_db_mixin.dart

+21-10
Original file line numberDiff line numberDiff line change
@@ -175,16 +175,27 @@ mixin PowerSyncDatabaseMixin implements SqliteConnection {
175175
@visibleForTesting
176176
void setStatus(SyncStatus status) {
177177
if (status != currentStatus) {
178-
// Note that currently the streaming sync implementation will never set hasSynced.
179-
// lastSyncedAt implies that syncing has completed at some point (hasSynced = true).
180-
// The previous values of hasSynced should be preserved here.
181-
final newStatus = status.copyWith(
182-
hasSynced: status.lastSyncedAt != null
183-
? true
184-
: status.hasSynced ?? currentStatus.hasSynced,
185-
lastSyncedAt: status.lastSyncedAt ?? currentStatus.lastSyncedAt);
186-
// If the absence of hasSync was the only difference, the new states would be equal
187-
// and don't require an event. So, check again.
178+
final newStatus = SyncStatus(
179+
connected: status.connected,
180+
downloading: status.downloading,
181+
uploading: status.uploading,
182+
connecting: status.connecting,
183+
uploadError: status.uploadError,
184+
downloadError: status.downloadError,
185+
priorityStatusEntries: status.priorityStatusEntries,
186+
downloadProgress: status.downloadProgress,
187+
// Note that currently the streaming sync implementation will never set
188+
// hasSynced. lastSyncedAt implies that syncing has completed at some
189+
// point (hasSynced = true).
190+
// The previous values of hasSynced should be preserved here.
191+
lastSyncedAt: status.lastSyncedAt ?? currentStatus.lastSyncedAt,
192+
hasSynced: status.lastSyncedAt != null
193+
? true
194+
: status.hasSynced ?? currentStatus.hasSynced,
195+
);
196+
197+
// If the absence of hasSynced was the only difference, the new states
198+
// would be equal and don't require an event. So, check again.
188199
if (newStatus != currentStatus) {
189200
currentStatus = newStatus;
190201
statusStreamController.add(currentStatus);

Diff for: packages/powersync_core/lib/src/sync/protocol.dart

+1
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ final class Checkpoint extends StreamingSyncLine {
140140
'bucket': c.bucket,
141141
'checksum': c.checksum,
142142
'priority': c.priority,
143+
'count': c.count,
143144
})
144145
.toList(growable: false)
145146
};

Diff for: packages/powersync_core/lib/src/sync/streaming_sync.dart

-1
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,6 @@ class StreamingSyncImplementation implements StreamingSync {
394394
// TODO: This increments the counters before actually saving sync
395395
// data. Might be fine though?
396396
_state.updateStatus((s) => s.applyBatchReceived(line));
397-
_state.updateStatus((s) => s.downloading = true);
398397
await adapter.saveSyncData(line);
399398
case StreamingSyncKeepalive(:final tokenExpiresIn):
400399
if (tokenExpiresIn == 0) {

Diff for: packages/powersync_core/lib/src/sync/sync_status.dart

+30-16
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,11 @@ final class SyncStatus {
7777
other.lastSyncedAt == lastSyncedAt &&
7878
other.hasSynced == hasSynced &&
7979
_statusEquality.equals(
80-
other.priorityStatusEntries, priorityStatusEntries));
80+
other.priorityStatusEntries, priorityStatusEntries) &&
81+
other.downloadProgress == downloadProgress);
8182
}
8283

84+
// Deprecated because it can't set fields back to null
8385
@Deprecated('Should not be used in user code')
8486
SyncStatus copyWith({
8587
bool? connected,
@@ -148,19 +150,21 @@ final class SyncStatus {
148150
@override
149151
int get hashCode {
150152
return Object.hash(
151-
connected,
152-
downloading,
153-
uploading,
154-
connecting,
155-
uploadError,
156-
downloadError,
157-
lastSyncedAt,
158-
_statusEquality.hash(priorityStatusEntries));
153+
connected,
154+
downloading,
155+
uploading,
156+
connecting,
157+
uploadError,
158+
downloadError,
159+
lastSyncedAt,
160+
_statusEquality.hash(priorityStatusEntries),
161+
downloadProgress,
162+
);
159163
}
160164

161165
@override
162166
String toString() {
163-
return "SyncStatus<connected: $connected connecting: $connecting downloading: $downloading uploading: $uploading lastSyncedAt: $lastSyncedAt, hasSynced: $hasSynced, error: $anyError>";
167+
return "SyncStatus<connected: $connected connecting: $connecting downloading: $downloading (progress: $downloadProgress) uploading: $uploading lastSyncedAt: $lastSyncedAt, hasSynced: $hasSynced, error: $anyError>";
164168
}
165169

166170
// This should be a ListEquality<SyncPriorityStatus>, but that appears to
@@ -266,11 +270,14 @@ final class InternalSyncDownloadProgress {
266270
/// Sums the total target and completed operations for all buckets up until
267271
/// the given [priority] (inclusive).
268272
(int, int) targetAndCompletedCounts(BucketPriority priority) {
269-
return buckets.values.fold((0, 0), (prev, entry) {
270-
final downloaded = entry.sinceLast;
271-
final total = entry.targetCount - entry.atLast;
272-
return (prev.$1 + total, prev.$2 + downloaded);
273-
});
273+
return buckets.values.where((e) => e.priority >= priority).fold(
274+
(0, 0),
275+
(prev, entry) {
276+
final downloaded = entry.sinceLast;
277+
final total = entry.targetCount - entry.atLast;
278+
return (prev.$1 + total, prev.$2 + downloaded);
279+
},
280+
);
274281
}
275282

276283
InternalSyncDownloadProgress incrementDownloaded(SyncDataBatch batch) {
@@ -280,7 +287,7 @@ final class InternalSyncDownloadProgress {
280287
newBucketStates[dataForBucket.bucket] = (
281288
priority: previous.priority,
282289
atLast: previous.atLast,
283-
sinceLast: previous.sinceLast,
290+
sinceLast: previous.sinceLast + dataForBucket.data.length,
284291
targetCount: previous.targetCount,
285292
);
286293
}
@@ -304,6 +311,13 @@ final class InternalSyncDownloadProgress {
304311
_mapEquality.equals(buckets, other.buckets);
305312
}
306313

314+
@override
315+
String toString() {
316+
final asView = asSyncDownloadProgress;
317+
final all = asView.untilCompletion;
318+
return 'for total: ${all.completed} / ${all.total}';
319+
}
320+
307321
static const _mapEquality = MapEquality<Object?, Object?>();
308322
}
309323

0 commit comments

Comments
 (0)