2023-03-27 02:35:52 +00:00
|
|
|
import 'package:flutter/widgets.dart';
|
|
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
|
|
import 'package:immich_mobile/shared/models/album.dart';
|
|
|
|
import 'package:immich_mobile/shared/models/asset.dart';
|
2023-09-10 12:51:18 +00:00
|
|
|
import 'package:immich_mobile/shared/models/etag.dart';
|
2023-03-27 02:35:52 +00:00
|
|
|
import 'package:immich_mobile/shared/models/exif_info.dart';
|
|
|
|
import 'package:immich_mobile/shared/models/logger_message.model.dart';
|
|
|
|
import 'package:immich_mobile/shared/models/store.dart';
|
|
|
|
import 'package:immich_mobile/shared/models/user.dart';
|
2023-06-10 18:13:59 +00:00
|
|
|
import 'package:immich_mobile/shared/services/hash.service.dart';
|
2023-03-27 02:35:52 +00:00
|
|
|
import 'package:immich_mobile/shared/services/immich_logger.service.dart';
|
|
|
|
import 'package:immich_mobile/shared/services/sync.service.dart';
|
|
|
|
import 'package:isar/isar.dart';
|
2023-06-10 18:13:59 +00:00
|
|
|
import 'package:mockito/mockito.dart';
|
2023-03-27 02:35:52 +00:00
|
|
|
|
|
|
|
void main() {
|
|
|
|
Asset makeAsset({
|
2023-06-10 18:13:59 +00:00
|
|
|
required String checksum,
|
|
|
|
String? localId,
|
2023-03-27 02:35:52 +00:00
|
|
|
String? remoteId,
|
|
|
|
int ownerId = 590700560494856554, // hash of "1"
|
|
|
|
}) {
|
|
|
|
final DateTime date = DateTime(2000);
|
|
|
|
return Asset(
|
2023-06-10 18:13:59 +00:00
|
|
|
checksum: checksum,
|
2023-03-27 02:35:52 +00:00
|
|
|
localId: localId,
|
|
|
|
remoteId: remoteId,
|
|
|
|
ownerId: ownerId,
|
|
|
|
fileCreatedAt: date,
|
|
|
|
fileModifiedAt: date,
|
|
|
|
updatedAt: date,
|
|
|
|
durationInSeconds: 0,
|
|
|
|
type: AssetType.image,
|
2023-06-10 18:13:59 +00:00
|
|
|
fileName: localId ?? remoteId ?? "",
|
2023-03-27 02:35:52 +00:00
|
|
|
isFavorite: false,
|
2023-04-17 05:02:07 +00:00
|
|
|
isArchived: false,
|
2023-10-06 07:01:14 +00:00
|
|
|
isTrashed: false,
|
2023-10-22 02:38:07 +00:00
|
|
|
stackCount: 0,
|
2023-03-27 02:35:52 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Isar loadDb() {
|
|
|
|
return Isar.openSync(
|
|
|
|
[
|
|
|
|
ExifInfoSchema,
|
|
|
|
AssetSchema,
|
|
|
|
AlbumSchema,
|
|
|
|
UserSchema,
|
|
|
|
StoreValueSchema,
|
2023-08-18 22:52:40 +00:00
|
|
|
LoggerMessageSchema,
|
2023-09-10 12:51:18 +00:00
|
|
|
ETagSchema,
|
2023-03-27 02:35:52 +00:00
|
|
|
],
|
|
|
|
maxSizeMiB: 256,
|
2023-05-26 13:09:44 +00:00
|
|
|
directory: ".",
|
2023-03-27 02:35:52 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
group('Test SyncService grouped', () {
|
|
|
|
late final Isar db;
|
2023-06-10 18:13:59 +00:00
|
|
|
final MockHashService hs = MockHashService();
|
2023-05-25 03:52:43 +00:00
|
|
|
final owner = User(
|
|
|
|
id: "1",
|
|
|
|
updatedAt: DateTime.now(),
|
|
|
|
email: "a@b.c",
|
2023-11-12 01:03:32 +00:00
|
|
|
name: "first last",
|
2023-05-25 03:52:43 +00:00
|
|
|
isAdmin: false,
|
|
|
|
);
|
2023-03-27 02:35:52 +00:00
|
|
|
setUpAll(() async {
|
|
|
|
WidgetsFlutterBinding.ensureInitialized();
|
|
|
|
await Isar.initializeIsarCore(download: true);
|
|
|
|
db = loadDb();
|
|
|
|
ImmichLogger();
|
|
|
|
db.writeTxnSync(() => db.clearSync());
|
|
|
|
Store.init(db);
|
2023-05-25 03:52:43 +00:00
|
|
|
await Store.put(StoreKey.currentUser, owner);
|
2023-03-27 02:35:52 +00:00
|
|
|
});
|
|
|
|
final List<Asset> initialAssets = [
|
2023-09-10 12:51:18 +00:00
|
|
|
makeAsset(checksum: "a", remoteId: "0-1"),
|
|
|
|
makeAsset(checksum: "b", remoteId: "2-1"),
|
2023-06-10 18:13:59 +00:00
|
|
|
makeAsset(checksum: "c", localId: "1", remoteId: "1-1"),
|
|
|
|
makeAsset(checksum: "d", localId: "2"),
|
|
|
|
makeAsset(checksum: "e", localId: "3"),
|
2023-03-27 02:35:52 +00:00
|
|
|
];
|
|
|
|
setUp(() {
|
|
|
|
db.writeTxnSync(() {
|
|
|
|
db.assets.clearSync();
|
|
|
|
db.assets.putAllSync(initialAssets);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
test('test inserting existing assets', () async {
|
2023-06-10 18:13:59 +00:00
|
|
|
SyncService s = SyncService(db, hs);
|
2023-03-27 02:35:52 +00:00
|
|
|
final List<Asset> remoteAssets = [
|
2023-09-10 12:51:18 +00:00
|
|
|
makeAsset(checksum: "a", remoteId: "0-1"),
|
|
|
|
makeAsset(checksum: "b", remoteId: "2-1"),
|
2023-06-10 18:13:59 +00:00
|
|
|
makeAsset(checksum: "c", remoteId: "1-1"),
|
2023-03-27 02:35:52 +00:00
|
|
|
];
|
|
|
|
expect(db.assets.countSync(), 5);
|
2023-09-10 12:51:18 +00:00
|
|
|
final bool c1 =
|
|
|
|
await s.syncRemoteAssetsToDb(owner, _failDiff, (u) => remoteAssets);
|
2023-03-27 02:35:52 +00:00
|
|
|
expect(c1, false);
|
|
|
|
expect(db.assets.countSync(), 5);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('test inserting new assets', () async {
|
2023-06-10 18:13:59 +00:00
|
|
|
SyncService s = SyncService(db, hs);
|
2023-03-27 02:35:52 +00:00
|
|
|
final List<Asset> remoteAssets = [
|
2023-09-10 12:51:18 +00:00
|
|
|
makeAsset(checksum: "a", remoteId: "0-1"),
|
|
|
|
makeAsset(checksum: "b", remoteId: "2-1"),
|
2023-06-10 18:13:59 +00:00
|
|
|
makeAsset(checksum: "c", remoteId: "1-1"),
|
|
|
|
makeAsset(checksum: "d", remoteId: "1-2"),
|
|
|
|
makeAsset(checksum: "f", remoteId: "1-4"),
|
2023-09-10 12:51:18 +00:00
|
|
|
makeAsset(checksum: "g", remoteId: "3-1"),
|
2023-03-27 02:35:52 +00:00
|
|
|
];
|
|
|
|
expect(db.assets.countSync(), 5);
|
2023-09-10 12:51:18 +00:00
|
|
|
final bool c1 =
|
|
|
|
await s.syncRemoteAssetsToDb(owner, _failDiff, (u) => remoteAssets);
|
2023-03-27 02:35:52 +00:00
|
|
|
expect(c1, true);
|
|
|
|
expect(db.assets.countSync(), 7);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('test syncing duplicate assets', () async {
|
2023-06-10 18:13:59 +00:00
|
|
|
SyncService s = SyncService(db, hs);
|
2023-03-27 02:35:52 +00:00
|
|
|
final List<Asset> remoteAssets = [
|
2023-09-10 12:51:18 +00:00
|
|
|
makeAsset(checksum: "a", remoteId: "0-1"),
|
2023-06-10 18:13:59 +00:00
|
|
|
makeAsset(checksum: "b", remoteId: "1-1"),
|
2023-09-10 12:51:18 +00:00
|
|
|
makeAsset(checksum: "c", remoteId: "2-1"),
|
|
|
|
makeAsset(checksum: "h", remoteId: "2-1b"),
|
|
|
|
makeAsset(checksum: "i", remoteId: "2-1c"),
|
|
|
|
makeAsset(checksum: "j", remoteId: "2-1d"),
|
2023-03-27 02:35:52 +00:00
|
|
|
];
|
|
|
|
expect(db.assets.countSync(), 5);
|
2023-09-10 12:51:18 +00:00
|
|
|
final bool c1 =
|
|
|
|
await s.syncRemoteAssetsToDb(owner, _failDiff, (u) => remoteAssets);
|
2023-03-27 02:35:52 +00:00
|
|
|
expect(c1, true);
|
|
|
|
expect(db.assets.countSync(), 8);
|
2023-09-10 12:51:18 +00:00
|
|
|
final bool c2 =
|
|
|
|
await s.syncRemoteAssetsToDb(owner, _failDiff, (u) => remoteAssets);
|
2023-03-27 02:35:52 +00:00
|
|
|
expect(c2, false);
|
|
|
|
expect(db.assets.countSync(), 8);
|
|
|
|
remoteAssets.removeAt(4);
|
2023-09-10 12:51:18 +00:00
|
|
|
final bool c3 =
|
|
|
|
await s.syncRemoteAssetsToDb(owner, _failDiff, (u) => remoteAssets);
|
2023-03-27 02:35:52 +00:00
|
|
|
expect(c3, true);
|
|
|
|
expect(db.assets.countSync(), 7);
|
2023-09-10 12:51:18 +00:00
|
|
|
remoteAssets.add(makeAsset(checksum: "k", remoteId: "2-1e"));
|
|
|
|
remoteAssets.add(makeAsset(checksum: "l", remoteId: "2-2"));
|
|
|
|
final bool c4 =
|
|
|
|
await s.syncRemoteAssetsToDb(owner, _failDiff, (u) => remoteAssets);
|
2023-03-27 02:35:52 +00:00
|
|
|
expect(c4, true);
|
|
|
|
expect(db.assets.countSync(), 9);
|
|
|
|
});
|
2023-09-10 12:51:18 +00:00
|
|
|
|
|
|
|
test('test efficient sync', () async {
|
|
|
|
SyncService s = SyncService(db, hs);
|
|
|
|
final List<Asset> toUpsert = [
|
|
|
|
makeAsset(checksum: "a", remoteId: "0-1"), // changed
|
|
|
|
makeAsset(checksum: "f", remoteId: "0-2"), // new
|
|
|
|
makeAsset(checksum: "g", remoteId: "0-3"), // new
|
|
|
|
];
|
|
|
|
toUpsert[0].isFavorite = true;
|
|
|
|
final List<String> toDelete = ["2-1", "1-1"];
|
|
|
|
final bool c = await s.syncRemoteAssetsToDb(
|
|
|
|
owner,
|
|
|
|
(user, since) async => (toUpsert, toDelete),
|
|
|
|
(user) => throw Exception(),
|
|
|
|
);
|
|
|
|
expect(c, true);
|
|
|
|
expect(db.assets.countSync(), 6);
|
|
|
|
});
|
2023-03-27 02:35:52 +00:00
|
|
|
});
|
|
|
|
}
|
2023-06-10 18:13:59 +00:00
|
|
|
|
2023-09-10 12:51:18 +00:00
|
|
|
Future<(List<Asset>?, List<String>?)> _failDiff(User user, DateTime time) =>
|
|
|
|
Future.value((null, null));
|
|
|
|
|
2023-06-10 18:13:59 +00:00
|
|
|
class MockHashService extends Mock implements HashService {}
|