Source Code added
This commit is contained in:
parent
800376eafd
commit
9efa9bc6dd
3912 changed files with 754770 additions and 2 deletions
8
mobile/test/api.mocks.dart
Normal file
8
mobile/test/api.mocks.dart
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
import 'package:mocktail/mocktail.dart';
|
||||
import 'package:openapi/api.dart';
|
||||
|
||||
class MockAssetsApi extends Mock implements AssetsApi {}
|
||||
|
||||
class MockSyncApi extends Mock implements SyncApi {}
|
||||
|
||||
class MockServerApi extends Mock implements ServerApi {}
|
||||
186
mobile/test/domain/repositories/sync_stream_repository_test.dart
Normal file
186
mobile/test/domain/repositories/sync_stream_repository_test.dart
Normal file
|
|
@ -0,0 +1,186 @@
|
|||
import 'package:drift/drift.dart' as drift;
|
||||
import 'package:drift/native.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:immich_mobile/infrastructure/repositories/db.repository.dart';
|
||||
import 'package:immich_mobile/infrastructure/repositories/sync_stream.repository.dart';
|
||||
import 'package:openapi/api.dart';
|
||||
|
||||
SyncUserV1 _createUser({String id = 'user-1'}) {
|
||||
return SyncUserV1(
|
||||
id: id,
|
||||
name: 'Test User',
|
||||
email: 'test@test.com',
|
||||
deletedAt: null,
|
||||
avatarColor: null,
|
||||
hasProfileImage: false,
|
||||
profileChangedAt: DateTime(2024, 1, 1),
|
||||
);
|
||||
}
|
||||
|
||||
SyncAssetV1 _createAsset({
|
||||
required String id,
|
||||
required String checksum,
|
||||
required String fileName,
|
||||
String ownerId = 'user-1',
|
||||
int? width,
|
||||
int? height,
|
||||
}) {
|
||||
return SyncAssetV1(
|
||||
id: id,
|
||||
checksum: checksum,
|
||||
originalFileName: fileName,
|
||||
type: AssetTypeEnum.IMAGE,
|
||||
ownerId: ownerId,
|
||||
isFavorite: false,
|
||||
fileCreatedAt: DateTime(2024, 1, 1),
|
||||
fileModifiedAt: DateTime(2024, 1, 1),
|
||||
localDateTime: DateTime(2024, 1, 1),
|
||||
visibility: AssetVisibility.timeline,
|
||||
width: width,
|
||||
height: height,
|
||||
deletedAt: null,
|
||||
duration: null,
|
||||
libraryId: null,
|
||||
livePhotoVideoId: null,
|
||||
stackId: null,
|
||||
thumbhash: null,
|
||||
isEdited: false,
|
||||
);
|
||||
}
|
||||
|
||||
SyncAssetExifV1 _createExif({
|
||||
required String assetId,
|
||||
required int width,
|
||||
required int height,
|
||||
required String orientation,
|
||||
}) {
|
||||
return SyncAssetExifV1(
|
||||
assetId: assetId,
|
||||
exifImageWidth: width,
|
||||
exifImageHeight: height,
|
||||
orientation: orientation,
|
||||
city: null,
|
||||
country: null,
|
||||
dateTimeOriginal: null,
|
||||
description: null,
|
||||
exposureTime: null,
|
||||
fNumber: null,
|
||||
fileSizeInByte: null,
|
||||
focalLength: null,
|
||||
fps: null,
|
||||
iso: null,
|
||||
latitude: null,
|
||||
lensModel: null,
|
||||
longitude: null,
|
||||
make: null,
|
||||
model: null,
|
||||
modifyDate: null,
|
||||
profileDescription: null,
|
||||
projectionType: null,
|
||||
rating: null,
|
||||
state: null,
|
||||
timeZone: null,
|
||||
);
|
||||
}
|
||||
|
||||
void main() {
|
||||
late Drift db;
|
||||
late SyncStreamRepository sut;
|
||||
|
||||
setUp(() async {
|
||||
db = Drift(drift.DatabaseConnection(NativeDatabase.memory(), closeStreamsSynchronously: true));
|
||||
sut = SyncStreamRepository(db);
|
||||
});
|
||||
|
||||
tearDown(() async {
|
||||
await db.close();
|
||||
});
|
||||
|
||||
group('SyncStreamRepository - Dimension swapping based on orientation', () {
|
||||
test('swaps dimensions for asset with rotated orientation', () async {
|
||||
final flippedOrientations = ['5', '6', '7', '8', '90', '-90'];
|
||||
|
||||
for (final orientation in flippedOrientations) {
|
||||
final assetId = 'asset-$orientation-degrees';
|
||||
|
||||
await sut.updateUsersV1([_createUser()]);
|
||||
|
||||
final asset = _createAsset(
|
||||
id: assetId,
|
||||
checksum: 'checksum-$orientation',
|
||||
fileName: 'rotated_$orientation.jpg',
|
||||
);
|
||||
await sut.updateAssetsV1([asset]);
|
||||
|
||||
final exif = _createExif(
|
||||
assetId: assetId,
|
||||
width: 1920,
|
||||
height: 1080,
|
||||
orientation: orientation, // EXIF orientation value for 90 degrees CW
|
||||
);
|
||||
await sut.updateAssetsExifV1([exif]);
|
||||
|
||||
final query = db.remoteAssetEntity.select()..where((tbl) => tbl.id.equals(assetId));
|
||||
final result = await query.getSingle();
|
||||
|
||||
expect(result.width, equals(1080));
|
||||
expect(result.height, equals(1920));
|
||||
}
|
||||
});
|
||||
|
||||
test('does not swap dimensions for asset with normal orientation', () async {
|
||||
final nonFlippedOrientations = ['1', '2', '3', '4'];
|
||||
for (final orientation in nonFlippedOrientations) {
|
||||
final assetId = 'asset-$orientation-degrees';
|
||||
|
||||
await sut.updateUsersV1([_createUser()]);
|
||||
|
||||
final asset = _createAsset(id: assetId, checksum: 'checksum-$orientation', fileName: 'normal_$orientation.jpg');
|
||||
await sut.updateAssetsV1([asset]);
|
||||
|
||||
final exif = _createExif(
|
||||
assetId: assetId,
|
||||
width: 1920,
|
||||
height: 1080,
|
||||
orientation: orientation, // EXIF orientation value for normal
|
||||
);
|
||||
await sut.updateAssetsExifV1([exif]);
|
||||
|
||||
final query = db.remoteAssetEntity.select()..where((tbl) => tbl.id.equals(assetId));
|
||||
final result = await query.getSingle();
|
||||
|
||||
expect(result.width, equals(1920));
|
||||
expect(result.height, equals(1080));
|
||||
}
|
||||
});
|
||||
|
||||
test('does not update dimensions if asset already has width and height', () async {
|
||||
const assetId = 'asset-with-dimensions';
|
||||
const existingWidth = 1920;
|
||||
const existingHeight = 1080;
|
||||
const exifWidth = 3840;
|
||||
const exifHeight = 2160;
|
||||
|
||||
await sut.updateUsersV1([_createUser()]);
|
||||
|
||||
final asset = _createAsset(
|
||||
id: assetId,
|
||||
checksum: 'checksum-with-dims',
|
||||
fileName: 'with_dimensions.jpg',
|
||||
width: existingWidth,
|
||||
height: existingHeight,
|
||||
);
|
||||
await sut.updateAssetsV1([asset]);
|
||||
|
||||
final exif = _createExif(assetId: assetId, width: exifWidth, height: exifHeight, orientation: '6');
|
||||
await sut.updateAssetsExifV1([exif]);
|
||||
|
||||
// Verify the asset still has original dimensions (not updated from EXIF)
|
||||
final query = db.remoteAssetEntity.select()..where((tbl) => tbl.id.equals(assetId));
|
||||
final result = await query.getSingle();
|
||||
|
||||
expect(result.width, equals(existingWidth), reason: 'Width should remain as originally set');
|
||||
expect(result.height, equals(existingHeight), reason: 'Height should remain as originally set');
|
||||
});
|
||||
});
|
||||
}
|
||||
20
mobile/test/domain/service.mock.dart
Normal file
20
mobile/test/domain/service.mock.dart
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
import 'package:immich_mobile/domain/services/store.service.dart';
|
||||
import 'package:immich_mobile/domain/services/user.service.dart';
|
||||
import 'package:immich_mobile/domain/utils/background_sync.dart';
|
||||
import 'package:immich_mobile/platform/native_sync_api.g.dart';
|
||||
import 'package:immich_mobile/services/app_settings.service.dart';
|
||||
import 'package:immich_mobile/services/background_upload.service.dart';
|
||||
import 'package:mocktail/mocktail.dart';
|
||||
|
||||
class MockStoreService extends Mock implements StoreService {}
|
||||
|
||||
class MockUserService extends Mock implements UserService {}
|
||||
|
||||
class MockBackgroundSyncManager extends Mock implements BackgroundSyncManager {}
|
||||
|
||||
class MockNativeSyncApi extends Mock implements NativeSyncApi {}
|
||||
|
||||
class MockAppSettingsService extends Mock implements AppSettingsService {}
|
||||
|
||||
class MockBackgroundUploadService extends Mock implements BackgroundUploadService {}
|
||||
|
||||
119
mobile/test/domain/services/album.service_test.dart
Normal file
119
mobile/test/domain/services/album.service_test.dart
Normal file
|
|
@ -0,0 +1,119 @@
|
|||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:immich_mobile/domain/models/album/album.model.dart';
|
||||
import 'package:immich_mobile/domain/services/remote_album.service.dart';
|
||||
import 'package:immich_mobile/infrastructure/repositories/remote_album.repository.dart';
|
||||
import 'package:immich_mobile/providers/album/album_sort_by_options.provider.dart';
|
||||
import 'package:immich_mobile/repositories/drift_album_api_repository.dart';
|
||||
import 'package:mocktail/mocktail.dart';
|
||||
|
||||
import '../../infrastructure/repository.mock.dart';
|
||||
|
||||
void main() {
|
||||
late RemoteAlbumService sut;
|
||||
late DriftRemoteAlbumRepository mockRemoteAlbumRepo;
|
||||
late DriftAlbumApiRepository mockAlbumApiRepo;
|
||||
|
||||
setUp(() {
|
||||
mockRemoteAlbumRepo = MockRemoteAlbumRepository();
|
||||
mockAlbumApiRepo = MockDriftAlbumApiRepository();
|
||||
sut = RemoteAlbumService(mockRemoteAlbumRepo, mockAlbumApiRepo);
|
||||
|
||||
when(() => mockRemoteAlbumRepo.getNewestAssetTimestamp(any())).thenAnswer((invocation) {
|
||||
// Simulate a timestamp for the newest asset in the album
|
||||
final albumID = invocation.positionalArguments[0] as String;
|
||||
|
||||
if (albumID == '1') {
|
||||
return Future.value(DateTime(2023, 1, 1));
|
||||
} else if (albumID == '2') {
|
||||
return Future.value(DateTime(2023, 2, 1));
|
||||
}
|
||||
|
||||
return Future.value(DateTime.fromMillisecondsSinceEpoch(0));
|
||||
});
|
||||
|
||||
when(() => mockRemoteAlbumRepo.getOldestAssetTimestamp(any())).thenAnswer((invocation) {
|
||||
// Simulate a timestamp for the oldest asset in the album
|
||||
final albumID = invocation.positionalArguments[0] as String;
|
||||
|
||||
if (albumID == '1') {
|
||||
return Future.value(DateTime(2019, 1, 1));
|
||||
} else if (albumID == '2') {
|
||||
return Future.value(DateTime(2019, 2, 1));
|
||||
}
|
||||
|
||||
return Future.value(DateTime.fromMillisecondsSinceEpoch(0));
|
||||
});
|
||||
});
|
||||
|
||||
final albumA = RemoteAlbum(
|
||||
id: '1',
|
||||
name: 'Album A',
|
||||
description: "",
|
||||
isActivityEnabled: false,
|
||||
order: AlbumAssetOrder.asc,
|
||||
assetCount: 1,
|
||||
createdAt: DateTime(2023, 1, 1),
|
||||
updatedAt: DateTime(2023, 1, 2),
|
||||
ownerId: 'owner1',
|
||||
ownerName: "Test User",
|
||||
isShared: false,
|
||||
);
|
||||
|
||||
final albumB = RemoteAlbum(
|
||||
id: '2',
|
||||
name: 'Album B',
|
||||
description: "",
|
||||
isActivityEnabled: false,
|
||||
order: AlbumAssetOrder.desc,
|
||||
assetCount: 2,
|
||||
createdAt: DateTime(2023, 2, 1),
|
||||
updatedAt: DateTime(2023, 2, 2),
|
||||
ownerId: 'owner2',
|
||||
ownerName: "Test User",
|
||||
isShared: false,
|
||||
);
|
||||
|
||||
group('sortAlbums', () {
|
||||
test('should sort correctly based on name', () async {
|
||||
final albums = [albumB, albumA];
|
||||
|
||||
final result = await sut.sortAlbums(albums, AlbumSortMode.title);
|
||||
expect(result, [albumA, albumB]);
|
||||
});
|
||||
|
||||
test('should sort correctly based on createdAt', () async {
|
||||
final albums = [albumB, albumA];
|
||||
|
||||
final result = await sut.sortAlbums(albums, AlbumSortMode.created);
|
||||
expect(result, [albumA, albumB]);
|
||||
});
|
||||
|
||||
test('should sort correctly based on updatedAt', () async {
|
||||
final albums = [albumB, albumA];
|
||||
|
||||
final result = await sut.sortAlbums(albums, AlbumSortMode.lastModified);
|
||||
expect(result, [albumA, albumB]);
|
||||
});
|
||||
|
||||
test('should sort correctly based on assetCount', () async {
|
||||
final albums = [albumB, albumA];
|
||||
|
||||
final result = await sut.sortAlbums(albums, AlbumSortMode.assetCount);
|
||||
expect(result, [albumA, albumB]);
|
||||
});
|
||||
|
||||
test('should sort correctly based on newestAssetTimestamp', () async {
|
||||
final albums = [albumB, albumA];
|
||||
|
||||
final result = await sut.sortAlbums(albums, AlbumSortMode.mostRecent);
|
||||
expect(result, [albumA, albumB]);
|
||||
});
|
||||
|
||||
test('should sort correctly based on oldestAssetTimestamp', () async {
|
||||
final albums = [albumB, albumA];
|
||||
|
||||
final result = await sut.sortAlbums(albums, AlbumSortMode.mostOldest);
|
||||
expect(result, [albumB, albumA]);
|
||||
});
|
||||
});
|
||||
}
|
||||
185
mobile/test/domain/services/asset.service_test.dart
Normal file
185
mobile/test/domain/services/asset.service_test.dart
Normal file
|
|
@ -0,0 +1,185 @@
|
|||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:immich_mobile/domain/models/exif.model.dart';
|
||||
import 'package:immich_mobile/domain/services/asset.service.dart';
|
||||
import 'package:mocktail/mocktail.dart';
|
||||
|
||||
import '../../infrastructure/repository.mock.dart';
|
||||
import '../../test_utils.dart';
|
||||
|
||||
void main() {
|
||||
late AssetService sut;
|
||||
late MockRemoteAssetRepository mockRemoteAssetRepository;
|
||||
late MockDriftLocalAssetRepository mockLocalAssetRepository;
|
||||
|
||||
setUp(() {
|
||||
mockRemoteAssetRepository = MockRemoteAssetRepository();
|
||||
mockLocalAssetRepository = MockDriftLocalAssetRepository();
|
||||
sut = AssetService(
|
||||
remoteAssetRepository: mockRemoteAssetRepository,
|
||||
localAssetRepository: mockLocalAssetRepository,
|
||||
);
|
||||
});
|
||||
|
||||
group('getAspectRatio', () {
|
||||
test('flips dimensions on Android for 90° and 270° orientations', () async {
|
||||
debugDefaultTargetPlatformOverride = TargetPlatform.android;
|
||||
addTearDown(() => debugDefaultTargetPlatformOverride = null);
|
||||
|
||||
for (final orientation in [90, 270]) {
|
||||
final localAsset = TestUtils.createLocalAsset(
|
||||
id: 'local-$orientation',
|
||||
width: 1920,
|
||||
height: 1080,
|
||||
orientation: orientation,
|
||||
);
|
||||
|
||||
final result = await sut.getAspectRatio(localAsset);
|
||||
|
||||
expect(result, 1080 / 1920, reason: 'Orientation $orientation should flip on Android');
|
||||
}
|
||||
});
|
||||
|
||||
test('does not flip dimensions on iOS regardless of orientation', () async {
|
||||
debugDefaultTargetPlatformOverride = TargetPlatform.iOS;
|
||||
addTearDown(() => debugDefaultTargetPlatformOverride = null);
|
||||
|
||||
for (final orientation in [0, 90, 270]) {
|
||||
final localAsset = TestUtils.createLocalAsset(
|
||||
id: 'local-$orientation',
|
||||
width: 1920,
|
||||
height: 1080,
|
||||
orientation: orientation,
|
||||
);
|
||||
|
||||
final result = await sut.getAspectRatio(localAsset);
|
||||
|
||||
expect(result, 1920 / 1080, reason: 'iOS should never flip dimensions');
|
||||
}
|
||||
});
|
||||
|
||||
test('fetches dimensions from remote repository when missing from asset', () async {
|
||||
final remoteAsset = TestUtils.createRemoteAsset(id: 'remote-1', width: null, height: null);
|
||||
|
||||
final exif = const ExifInfo(orientation: '1');
|
||||
|
||||
final fetchedAsset = TestUtils.createRemoteAsset(id: 'remote-1', width: 1920, height: 1080);
|
||||
|
||||
when(() => mockRemoteAssetRepository.getExif('remote-1')).thenAnswer((_) async => exif);
|
||||
when(() => mockRemoteAssetRepository.get('remote-1')).thenAnswer((_) async => fetchedAsset);
|
||||
|
||||
final result = await sut.getAspectRatio(remoteAsset);
|
||||
|
||||
expect(result, 1920 / 1080);
|
||||
verify(() => mockRemoteAssetRepository.get('remote-1')).called(1);
|
||||
});
|
||||
|
||||
test('fetches dimensions from local repository when missing from local asset', () async {
|
||||
final localAsset = TestUtils.createLocalAsset(id: 'local-1', width: null, height: null, orientation: 0);
|
||||
|
||||
final fetchedAsset = TestUtils.createLocalAsset(id: 'local-1', width: 1920, height: 1080, orientation: 0);
|
||||
|
||||
when(() => mockLocalAssetRepository.get('local-1')).thenAnswer((_) async => fetchedAsset);
|
||||
|
||||
final result = await sut.getAspectRatio(localAsset);
|
||||
|
||||
expect(result, 1920 / 1080);
|
||||
verify(() => mockLocalAssetRepository.get('local-1')).called(1);
|
||||
});
|
||||
|
||||
test('uses fetched asset orientation when dimensions are missing on Android', () async {
|
||||
debugDefaultTargetPlatformOverride = TargetPlatform.android;
|
||||
addTearDown(() => debugDefaultTargetPlatformOverride = null);
|
||||
|
||||
// Original asset has default orientation 0, but dimensions are missing
|
||||
final localAsset = TestUtils.createLocalAsset(id: 'local-1', width: null, height: null, orientation: 0);
|
||||
|
||||
// Fetched asset has 90° orientation and proper dimensions
|
||||
final fetchedAsset = TestUtils.createLocalAsset(id: 'local-1', width: 1920, height: 1080, orientation: 90);
|
||||
|
||||
when(() => mockLocalAssetRepository.get('local-1')).thenAnswer((_) async => fetchedAsset);
|
||||
|
||||
final result = await sut.getAspectRatio(localAsset);
|
||||
|
||||
// Should flip dimensions since fetched asset has 90° orientation
|
||||
expect(result, 1080 / 1920);
|
||||
verify(() => mockLocalAssetRepository.get('local-1')).called(1);
|
||||
});
|
||||
|
||||
test('returns 1.0 when dimensions are still unavailable after fetching', () async {
|
||||
final remoteAsset = TestUtils.createRemoteAsset(id: 'remote-1', width: null, height: null);
|
||||
|
||||
final exif = const ExifInfo(orientation: '1');
|
||||
|
||||
when(() => mockRemoteAssetRepository.getExif('remote-1')).thenAnswer((_) async => exif);
|
||||
when(() => mockRemoteAssetRepository.get('remote-1')).thenAnswer((_) async => null);
|
||||
|
||||
final result = await sut.getAspectRatio(remoteAsset);
|
||||
|
||||
expect(result, 1.0);
|
||||
});
|
||||
|
||||
test('returns 1.0 when height is zero', () async {
|
||||
final remoteAsset = TestUtils.createRemoteAsset(id: 'remote-1', width: 1920, height: 0);
|
||||
|
||||
final exif = const ExifInfo(orientation: '1');
|
||||
|
||||
when(() => mockRemoteAssetRepository.getExif('remote-1')).thenAnswer((_) async => exif);
|
||||
|
||||
final result = await sut.getAspectRatio(remoteAsset);
|
||||
|
||||
expect(result, 1.0);
|
||||
});
|
||||
|
||||
test('handles local asset with remoteId using local orientation not remote exif', () async {
|
||||
// When a LocalAsset has a remoteId (merged), we should use local orientation
|
||||
// because the width/height come from the local asset (pre-corrected on iOS)
|
||||
final localAsset = TestUtils.createLocalAsset(
|
||||
id: 'local-1',
|
||||
remoteId: 'remote-1',
|
||||
width: 1920,
|
||||
height: 1080,
|
||||
orientation: 0,
|
||||
);
|
||||
|
||||
final result = await sut.getAspectRatio(localAsset);
|
||||
|
||||
expect(result, 1920 / 1080);
|
||||
// Should not call remote exif for LocalAsset
|
||||
verifyNever(() => mockRemoteAssetRepository.getExif(any()));
|
||||
});
|
||||
|
||||
test('handles local asset with remoteId and 90 degree rotation on Android', () async {
|
||||
debugDefaultTargetPlatformOverride = TargetPlatform.android;
|
||||
addTearDown(() => debugDefaultTargetPlatformOverride = null);
|
||||
|
||||
final localAsset = TestUtils.createLocalAsset(
|
||||
id: 'local-1',
|
||||
remoteId: 'remote-1',
|
||||
width: 1920,
|
||||
height: 1080,
|
||||
orientation: 90,
|
||||
);
|
||||
|
||||
final result = await sut.getAspectRatio(localAsset);
|
||||
|
||||
expect(result, 1080 / 1920);
|
||||
});
|
||||
|
||||
test('should not flip remote asset dimensions', () async {
|
||||
final flippedOrientations = ['1', '2', '3', '4', '5', '6', '7', '8', '90', '-90'];
|
||||
|
||||
for (final orientation in flippedOrientations) {
|
||||
final remoteAsset = TestUtils.createRemoteAsset(id: 'remote-$orientation', width: 1920, height: 1080);
|
||||
|
||||
final exif = ExifInfo(orientation: orientation);
|
||||
|
||||
when(() => mockRemoteAssetRepository.getExif('remote-$orientation')).thenAnswer((_) async => exif);
|
||||
|
||||
final result = await sut.getAspectRatio(remoteAsset);
|
||||
|
||||
expect(result, 1920 / 1080, reason: 'Should not flipped remote asset dimensions for orientation $orientation');
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
194
mobile/test/domain/services/hash_service_test.dart
Normal file
194
mobile/test/domain/services/hash_service_test.dart
Normal file
|
|
@ -0,0 +1,194 @@
|
|||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:immich_mobile/domain/models/album/local_album.model.dart';
|
||||
import 'package:immich_mobile/domain/services/hash.service.dart';
|
||||
import 'package:immich_mobile/platform/native_sync_api.g.dart';
|
||||
import 'package:mocktail/mocktail.dart';
|
||||
|
||||
import '../../fixtures/album.stub.dart';
|
||||
import '../../fixtures/asset.stub.dart';
|
||||
import '../../infrastructure/repository.mock.dart';
|
||||
import '../service.mock.dart';
|
||||
|
||||
void main() {
|
||||
late HashService sut;
|
||||
late MockLocalAlbumRepository mockAlbumRepo;
|
||||
late MockLocalAssetRepository mockAssetRepo;
|
||||
late MockNativeSyncApi mockNativeApi;
|
||||
late MockTrashedLocalAssetRepository mockTrashedAssetRepo;
|
||||
|
||||
setUp(() {
|
||||
mockAlbumRepo = MockLocalAlbumRepository();
|
||||
mockAssetRepo = MockLocalAssetRepository();
|
||||
mockNativeApi = MockNativeSyncApi();
|
||||
mockTrashedAssetRepo = MockTrashedLocalAssetRepository();
|
||||
|
||||
sut = HashService(
|
||||
localAlbumRepository: mockAlbumRepo,
|
||||
localAssetRepository: mockAssetRepo,
|
||||
nativeSyncApi: mockNativeApi,
|
||||
trashedLocalAssetRepository: mockTrashedAssetRepo,
|
||||
);
|
||||
|
||||
registerFallbackValue(LocalAlbumStub.recent);
|
||||
registerFallbackValue(LocalAssetStub.image1);
|
||||
registerFallbackValue(<String, String>{});
|
||||
|
||||
when(() => mockAssetRepo.reconcileHashesFromCloudId()).thenAnswer((_) async => {});
|
||||
when(() => mockAssetRepo.updateHashes(any())).thenAnswer((_) async => {});
|
||||
});
|
||||
|
||||
group('HashService hashAssets', () {
|
||||
test('skips albums with no assets to hash', () async {
|
||||
when(
|
||||
() => mockAlbumRepo.getBackupAlbums(),
|
||||
).thenAnswer((_) async => [LocalAlbumStub.recent.copyWith(assetCount: 0)]);
|
||||
when(() => mockAlbumRepo.getAssetsToHash(LocalAlbumStub.recent.id)).thenAnswer((_) async => []);
|
||||
|
||||
await sut.hashAssets();
|
||||
|
||||
verifyNever(() => mockNativeApi.hashAssets(any(), allowNetworkAccess: any(named: 'allowNetworkAccess')));
|
||||
});
|
||||
});
|
||||
|
||||
group('HashService _hashAssets', () {
|
||||
test('skips empty batches', () async {
|
||||
final album = LocalAlbumStub.recent;
|
||||
when(() => mockAlbumRepo.getBackupAlbums()).thenAnswer((_) async => [album]);
|
||||
when(() => mockAlbumRepo.getAssetsToHash(album.id)).thenAnswer((_) async => []);
|
||||
|
||||
await sut.hashAssets();
|
||||
|
||||
verifyNever(() => mockNativeApi.hashAssets(any(), allowNetworkAccess: any(named: 'allowNetworkAccess')));
|
||||
});
|
||||
|
||||
test('processes assets when available', () async {
|
||||
final album = LocalAlbumStub.recent;
|
||||
final asset = LocalAssetStub.image1;
|
||||
|
||||
when(() => mockAlbumRepo.getBackupAlbums()).thenAnswer((_) async => [album]);
|
||||
when(() => mockAlbumRepo.getAssetsToHash(album.id)).thenAnswer((_) async => [asset]);
|
||||
when(
|
||||
() => mockNativeApi.hashAssets([asset.id], allowNetworkAccess: false),
|
||||
).thenAnswer((_) async => [HashResult(assetId: asset.id, hash: 'test-hash')]);
|
||||
|
||||
await sut.hashAssets();
|
||||
|
||||
verify(() => mockNativeApi.hashAssets([asset.id], allowNetworkAccess: false)).called(1);
|
||||
final captured = verify(() => mockAssetRepo.updateHashes(captureAny())).captured.first as Map<String, String>;
|
||||
expect(captured.length, 1);
|
||||
expect(captured[asset.id], 'test-hash');
|
||||
});
|
||||
|
||||
test('handles failed hashes', () async {
|
||||
final album = LocalAlbumStub.recent;
|
||||
final asset = LocalAssetStub.image1;
|
||||
|
||||
when(() => mockAlbumRepo.getBackupAlbums()).thenAnswer((_) async => [album]);
|
||||
when(() => mockAlbumRepo.getAssetsToHash(album.id)).thenAnswer((_) async => [asset]);
|
||||
when(
|
||||
() => mockNativeApi.hashAssets([asset.id], allowNetworkAccess: false),
|
||||
).thenAnswer((_) async => [HashResult(assetId: asset.id, error: 'Failed to hash')]);
|
||||
|
||||
await sut.hashAssets();
|
||||
|
||||
final captured = verify(() => mockAssetRepo.updateHashes(captureAny())).captured.first as Map<String, String>;
|
||||
expect(captured.length, 0);
|
||||
});
|
||||
|
||||
test('handles null hash results', () async {
|
||||
final album = LocalAlbumStub.recent;
|
||||
final asset = LocalAssetStub.image1;
|
||||
|
||||
when(() => mockAlbumRepo.getBackupAlbums()).thenAnswer((_) async => [album]);
|
||||
when(() => mockAlbumRepo.getAssetsToHash(album.id)).thenAnswer((_) async => [asset]);
|
||||
when(
|
||||
() => mockNativeApi.hashAssets([asset.id], allowNetworkAccess: false),
|
||||
).thenAnswer((_) async => [HashResult(assetId: asset.id, hash: null)]);
|
||||
|
||||
await sut.hashAssets();
|
||||
|
||||
final captured = verify(() => mockAssetRepo.updateHashes(captureAny())).captured.first as Map<String, String>;
|
||||
expect(captured.length, 0);
|
||||
});
|
||||
|
||||
test('batches by size limit', () async {
|
||||
const batchSize = 2;
|
||||
final sut = HashService(
|
||||
localAlbumRepository: mockAlbumRepo,
|
||||
localAssetRepository: mockAssetRepo,
|
||||
nativeSyncApi: mockNativeApi,
|
||||
batchSize: batchSize,
|
||||
trashedLocalAssetRepository: mockTrashedAssetRepo,
|
||||
);
|
||||
|
||||
final album = LocalAlbumStub.recent;
|
||||
final asset1 = LocalAssetStub.image1;
|
||||
final asset2 = LocalAssetStub.image2;
|
||||
final asset3 = LocalAssetStub.image1.copyWith(id: 'image3', name: 'image3.jpg');
|
||||
|
||||
final capturedCalls = <List<String>>[];
|
||||
|
||||
when(() => mockAssetRepo.updateHashes(any())).thenAnswer((_) async => {});
|
||||
when(() => mockAlbumRepo.getBackupAlbums()).thenAnswer((_) async => [album]);
|
||||
when(() => mockAlbumRepo.getAssetsToHash(album.id)).thenAnswer((_) async => [asset1, asset2, asset3]);
|
||||
when(() => mockNativeApi.hashAssets(any(), allowNetworkAccess: any(named: 'allowNetworkAccess'))).thenAnswer((
|
||||
invocation,
|
||||
) async {
|
||||
final assetIds = invocation.positionalArguments[0] as List<String>;
|
||||
capturedCalls.add(List<String>.from(assetIds));
|
||||
return assetIds.map((id) => HashResult(assetId: id, hash: '$id-hash')).toList();
|
||||
});
|
||||
|
||||
await sut.hashAssets();
|
||||
|
||||
expect(capturedCalls.length, 2, reason: 'Should make exactly 2 calls to hashAssets');
|
||||
expect(capturedCalls[0], [asset1.id, asset2.id], reason: 'First call should batch the first two assets');
|
||||
expect(capturedCalls[1], [asset3.id], reason: 'Second call should have the remaining asset');
|
||||
|
||||
verify(() => mockAssetRepo.updateHashes(any())).called(2);
|
||||
});
|
||||
|
||||
test('handles mixed success and failure in batch', () async {
|
||||
final album = LocalAlbumStub.recent;
|
||||
final asset1 = LocalAssetStub.image1;
|
||||
final asset2 = LocalAssetStub.image2;
|
||||
|
||||
when(() => mockAlbumRepo.getBackupAlbums()).thenAnswer((_) async => [album]);
|
||||
when(() => mockAlbumRepo.getAssetsToHash(album.id)).thenAnswer((_) async => [asset1, asset2]);
|
||||
when(() => mockNativeApi.hashAssets([asset1.id, asset2.id], allowNetworkAccess: false)).thenAnswer(
|
||||
(_) async => [
|
||||
HashResult(assetId: asset1.id, hash: 'asset1-hash'),
|
||||
HashResult(assetId: asset2.id, error: 'Failed to hash asset2'),
|
||||
],
|
||||
);
|
||||
|
||||
await sut.hashAssets();
|
||||
|
||||
final captured = verify(() => mockAssetRepo.updateHashes(captureAny())).captured.first as Map<String, String>;
|
||||
expect(captured.length, 1);
|
||||
expect(captured[asset1.id], 'asset1-hash');
|
||||
});
|
||||
|
||||
test('uses allowNetworkAccess based on album backup selection', () async {
|
||||
final selectedAlbum = LocalAlbumStub.recent.copyWith(backupSelection: BackupSelection.selected);
|
||||
final nonSelectedAlbum = LocalAlbumStub.recent.copyWith(id: 'album2', backupSelection: BackupSelection.excluded);
|
||||
final asset1 = LocalAssetStub.image1;
|
||||
final asset2 = LocalAssetStub.image2;
|
||||
|
||||
when(() => mockAlbumRepo.getBackupAlbums()).thenAnswer((_) async => [selectedAlbum, nonSelectedAlbum]);
|
||||
when(() => mockAlbumRepo.getAssetsToHash(selectedAlbum.id)).thenAnswer((_) async => [asset1]);
|
||||
when(() => mockAlbumRepo.getAssetsToHash(nonSelectedAlbum.id)).thenAnswer((_) async => [asset2]);
|
||||
when(() => mockNativeApi.hashAssets(any(), allowNetworkAccess: any(named: 'allowNetworkAccess'))).thenAnswer((
|
||||
invocation,
|
||||
) async {
|
||||
final assetIds = invocation.positionalArguments[0] as List<String>;
|
||||
return assetIds.map((id) => HashResult(assetId: id, hash: '$id-hash')).toList();
|
||||
});
|
||||
|
||||
await sut.hashAssets();
|
||||
|
||||
verify(() => mockNativeApi.hashAssets([asset1.id], allowNetworkAccess: true)).called(1);
|
||||
verify(() => mockNativeApi.hashAssets([asset2.id], allowNetworkAccess: false)).called(1);
|
||||
});
|
||||
});
|
||||
}
|
||||
226
mobile/test/domain/services/local_sync_service_test.dart
Normal file
226
mobile/test/domain/services/local_sync_service_test.dart
Normal file
|
|
@ -0,0 +1,226 @@
|
|||
import 'package:drift/drift.dart' as drift;
|
||||
import 'package:drift/native.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:immich_mobile/domain/models/asset/base_asset.model.dart';
|
||||
import 'package:immich_mobile/domain/models/store.model.dart';
|
||||
import 'package:immich_mobile/domain/services/local_sync.service.dart';
|
||||
import 'package:immich_mobile/domain/services/store.service.dart';
|
||||
import 'package:immich_mobile/entities/store.entity.dart';
|
||||
import 'package:immich_mobile/infrastructure/repositories/db.repository.dart';
|
||||
import 'package:immich_mobile/infrastructure/repositories/local_album.repository.dart';
|
||||
import 'package:immich_mobile/infrastructure/repositories/local_asset.repository.dart';
|
||||
import 'package:immich_mobile/infrastructure/repositories/storage.repository.dart';
|
||||
import 'package:immich_mobile/infrastructure/repositories/store.repository.dart';
|
||||
import 'package:immich_mobile/infrastructure/repositories/trashed_local_asset.repository.dart';
|
||||
import 'package:immich_mobile/platform/native_sync_api.g.dart';
|
||||
import 'package:immich_mobile/repositories/local_files_manager.repository.dart';
|
||||
import 'package:mocktail/mocktail.dart';
|
||||
|
||||
import '../../domain/service.mock.dart';
|
||||
import '../../fixtures/asset.stub.dart';
|
||||
import '../../infrastructure/repository.mock.dart';
|
||||
import '../../mocks/asset_entity.mock.dart';
|
||||
import '../../repository.mocks.dart';
|
||||
|
||||
void main() {
|
||||
late LocalSyncService sut;
|
||||
late DriftLocalAlbumRepository mockLocalAlbumRepository;
|
||||
late DriftLocalAssetRepository mockLocalAssetRepository;
|
||||
late DriftTrashedLocalAssetRepository mockTrashedLocalAssetRepository;
|
||||
late LocalFilesManagerRepository mockLocalFilesManager;
|
||||
late StorageRepository mockStorageRepository;
|
||||
late MockNativeSyncApi mockNativeSyncApi;
|
||||
late Drift db;
|
||||
|
||||
setUpAll(() async {
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
debugDefaultTargetPlatformOverride = TargetPlatform.android;
|
||||
|
||||
db = Drift(drift.DatabaseConnection(NativeDatabase.memory(), closeStreamsSynchronously: true));
|
||||
await StoreService.init(storeRepository: DriftStoreRepository(db));
|
||||
});
|
||||
|
||||
tearDownAll(() async {
|
||||
debugDefaultTargetPlatformOverride = null;
|
||||
await Store.clear();
|
||||
await db.close();
|
||||
});
|
||||
|
||||
setUp(() async {
|
||||
mockLocalAlbumRepository = MockLocalAlbumRepository();
|
||||
mockLocalAssetRepository = MockLocalAssetRepository();
|
||||
mockTrashedLocalAssetRepository = MockTrashedLocalAssetRepository();
|
||||
mockLocalFilesManager = MockLocalFilesManagerRepository();
|
||||
mockStorageRepository = MockStorageRepository();
|
||||
mockNativeSyncApi = MockNativeSyncApi();
|
||||
|
||||
when(() => mockNativeSyncApi.shouldFullSync()).thenAnswer((_) async => false);
|
||||
when(() => mockNativeSyncApi.getMediaChanges()).thenAnswer(
|
||||
(_) async => SyncDelta(hasChanges: false, updates: const [], deletes: const [], assetAlbums: const {}),
|
||||
);
|
||||
when(() => mockNativeSyncApi.getTrashedAssets()).thenAnswer((_) async => {});
|
||||
when(() => mockTrashedLocalAssetRepository.processTrashSnapshot(any())).thenAnswer((_) async {});
|
||||
when(() => mockTrashedLocalAssetRepository.getToRestore()).thenAnswer((_) async => []);
|
||||
when(() => mockTrashedLocalAssetRepository.getToTrash()).thenAnswer((_) async => {});
|
||||
when(() => mockTrashedLocalAssetRepository.applyRestoredAssets(any())).thenAnswer((_) async {});
|
||||
when(() => mockTrashedLocalAssetRepository.trashLocalAsset(any())).thenAnswer((_) async {});
|
||||
when(() => mockLocalFilesManager.moveToTrash(any<List<String>>())).thenAnswer((_) async => true);
|
||||
|
||||
sut = LocalSyncService(
|
||||
localAlbumRepository: mockLocalAlbumRepository,
|
||||
localAssetRepository: mockLocalAssetRepository,
|
||||
trashedLocalAssetRepository: mockTrashedLocalAssetRepository,
|
||||
localFilesManager: mockLocalFilesManager,
|
||||
storageRepository: mockStorageRepository,
|
||||
nativeSyncApi: mockNativeSyncApi,
|
||||
);
|
||||
|
||||
await Store.put(StoreKey.manageLocalMediaAndroid, false);
|
||||
when(() => mockLocalFilesManager.hasManageMediaPermission()).thenAnswer((_) async => false);
|
||||
});
|
||||
|
||||
group('LocalSyncService - syncTrashedAssets gating', () {
|
||||
test('invokes syncTrashedAssets when Android flag enabled and permission granted', () async {
|
||||
await Store.put(StoreKey.manageLocalMediaAndroid, true);
|
||||
when(() => mockLocalFilesManager.hasManageMediaPermission()).thenAnswer((_) async => true);
|
||||
|
||||
await sut.sync();
|
||||
|
||||
verify(() => mockNativeSyncApi.getTrashedAssets()).called(1);
|
||||
verify(() => mockTrashedLocalAssetRepository.processTrashSnapshot(any())).called(1);
|
||||
});
|
||||
|
||||
test('skips syncTrashedAssets when store flag disabled', () async {
|
||||
await Store.put(StoreKey.manageLocalMediaAndroid, false);
|
||||
when(() => mockLocalFilesManager.hasManageMediaPermission()).thenAnswer((_) async => true);
|
||||
|
||||
await sut.sync();
|
||||
|
||||
verifyNever(() => mockNativeSyncApi.getTrashedAssets());
|
||||
});
|
||||
|
||||
test('skips syncTrashedAssets when MANAGE_MEDIA permission absent', () async {
|
||||
await Store.put(StoreKey.manageLocalMediaAndroid, true);
|
||||
when(() => mockLocalFilesManager.hasManageMediaPermission()).thenAnswer((_) async => false);
|
||||
|
||||
await sut.sync();
|
||||
|
||||
verifyNever(() => mockNativeSyncApi.getTrashedAssets());
|
||||
});
|
||||
|
||||
test('skips syncTrashedAssets on non-Android platforms', () async {
|
||||
debugDefaultTargetPlatformOverride = TargetPlatform.iOS;
|
||||
addTearDown(() => debugDefaultTargetPlatformOverride = TargetPlatform.android);
|
||||
|
||||
await Store.put(StoreKey.manageLocalMediaAndroid, true);
|
||||
when(() => mockLocalFilesManager.hasManageMediaPermission()).thenAnswer((_) async => true);
|
||||
|
||||
await sut.sync();
|
||||
|
||||
verifyNever(() => mockNativeSyncApi.getTrashedAssets());
|
||||
});
|
||||
});
|
||||
|
||||
group('LocalSyncService - syncTrashedAssets behavior', () {
|
||||
test('processes trashed snapshot, restores assets, and trashes local files', () async {
|
||||
final platformAsset = PlatformAsset(
|
||||
id: 'remote-id',
|
||||
name: 'remote.jpg',
|
||||
type: AssetType.image.index,
|
||||
durationInSeconds: 0,
|
||||
orientation: 0,
|
||||
isFavorite: false,
|
||||
);
|
||||
|
||||
final assetsToRestore = [LocalAssetStub.image1];
|
||||
when(() => mockTrashedLocalAssetRepository.getToRestore()).thenAnswer((_) async => assetsToRestore);
|
||||
final restoredIds = ['image1'];
|
||||
when(() => mockLocalFilesManager.restoreAssetsFromTrash(any())).thenAnswer((invocation) async {
|
||||
final Iterable<LocalAsset> requested = invocation.positionalArguments.first as Iterable<LocalAsset>;
|
||||
expect(requested, orderedEquals(assetsToRestore));
|
||||
return restoredIds;
|
||||
});
|
||||
|
||||
final localAssetToTrash = LocalAssetStub.image2.copyWith(id: 'local-trash', checksum: 'checksum-trash');
|
||||
when(() => mockTrashedLocalAssetRepository.getToTrash()).thenAnswer(
|
||||
(_) async => {
|
||||
'album-a': [localAssetToTrash],
|
||||
},
|
||||
);
|
||||
|
||||
final assetEntity = MockAssetEntity();
|
||||
when(() => assetEntity.getMediaUrl()).thenAnswer((_) async => 'content://local-trash');
|
||||
when(() => mockStorageRepository.getAssetEntityForAsset(localAssetToTrash)).thenAnswer((_) async => assetEntity);
|
||||
|
||||
await sut.processTrashedAssets({
|
||||
'album-a': [platformAsset],
|
||||
});
|
||||
|
||||
final trashedSnapshot =
|
||||
verify(() => mockTrashedLocalAssetRepository.processTrashSnapshot(captureAny())).captured.single
|
||||
as Iterable<TrashedAsset>;
|
||||
expect(trashedSnapshot.length, 1);
|
||||
final trashedEntry = trashedSnapshot.single;
|
||||
expect(trashedEntry.albumId, 'album-a');
|
||||
expect(trashedEntry.asset.id, platformAsset.id);
|
||||
expect(trashedEntry.asset.name, platformAsset.name);
|
||||
verify(() => mockTrashedLocalAssetRepository.getToTrash()).called(1);
|
||||
|
||||
verify(() => mockLocalFilesManager.restoreAssetsFromTrash(any())).called(1);
|
||||
verify(() => mockTrashedLocalAssetRepository.applyRestoredAssets(restoredIds)).called(1);
|
||||
|
||||
verify(() => mockStorageRepository.getAssetEntityForAsset(localAssetToTrash)).called(1);
|
||||
final moveArgs = verify(() => mockLocalFilesManager.moveToTrash(captureAny())).captured.single as List<String>;
|
||||
expect(moveArgs, ['content://local-trash']);
|
||||
final trashArgs =
|
||||
verify(() => mockTrashedLocalAssetRepository.trashLocalAsset(captureAny())).captured.single
|
||||
as Map<String, List<LocalAsset>>;
|
||||
expect(trashArgs.keys, ['album-a']);
|
||||
expect(trashArgs['album-a'], [localAssetToTrash]);
|
||||
});
|
||||
|
||||
test('does not attempt restore when repository has no assets to restore', () async {
|
||||
when(() => mockTrashedLocalAssetRepository.getToRestore()).thenAnswer((_) async => []);
|
||||
|
||||
await sut.processTrashedAssets({});
|
||||
|
||||
final trashedSnapshot =
|
||||
verify(() => mockTrashedLocalAssetRepository.processTrashSnapshot(captureAny())).captured.single
|
||||
as Iterable<TrashedAsset>;
|
||||
expect(trashedSnapshot, isEmpty);
|
||||
verifyNever(() => mockLocalFilesManager.restoreAssetsFromTrash(any()));
|
||||
verifyNever(() => mockTrashedLocalAssetRepository.applyRestoredAssets(any()));
|
||||
});
|
||||
|
||||
test('does not move local assets when repository finds nothing to trash', () async {
|
||||
when(() => mockTrashedLocalAssetRepository.getToTrash()).thenAnswer((_) async => {});
|
||||
|
||||
await sut.processTrashedAssets({});
|
||||
|
||||
verifyNever(() => mockLocalFilesManager.moveToTrash(any()));
|
||||
verifyNever(() => mockTrashedLocalAssetRepository.trashLocalAsset(any()));
|
||||
});
|
||||
});
|
||||
|
||||
group('LocalSyncService - PlatformAsset conversion', () {
|
||||
test('toLocalAsset uses correct updatedAt timestamp', () {
|
||||
final platformAsset = PlatformAsset(
|
||||
id: 'test-id',
|
||||
name: 'test.jpg',
|
||||
type: AssetType.image.index,
|
||||
durationInSeconds: 0,
|
||||
orientation: 0,
|
||||
isFavorite: false,
|
||||
createdAt: 1700000000,
|
||||
updatedAt: 1732000000,
|
||||
);
|
||||
|
||||
final localAsset = platformAsset.toLocalAsset();
|
||||
|
||||
expect(localAsset.createdAt.millisecondsSinceEpoch ~/ 1000, 1700000000);
|
||||
expect(localAsset.updatedAt.millisecondsSinceEpoch ~/ 1000, 1732000000);
|
||||
expect(localAsset.updatedAt, isNot(localAsset.createdAt));
|
||||
});
|
||||
});
|
||||
}
|
||||
160
mobile/test/domain/services/log_service_test.dart
Normal file
160
mobile/test/domain/services/log_service_test.dart
Normal file
|
|
@ -0,0 +1,160 @@
|
|||
import 'package:collection/collection.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:immich_mobile/constants/constants.dart';
|
||||
import 'package:immich_mobile/domain/models/log.model.dart';
|
||||
import 'package:immich_mobile/domain/models/store.model.dart';
|
||||
import 'package:immich_mobile/domain/services/log.service.dart';
|
||||
import 'package:immich_mobile/infrastructure/repositories/log.repository.dart';
|
||||
import 'package:immich_mobile/infrastructure/repositories/store.repository.dart';
|
||||
import 'package:logging/logging.dart';
|
||||
import 'package:mocktail/mocktail.dart';
|
||||
|
||||
import '../../infrastructure/repository.mock.dart';
|
||||
import '../../test_utils.dart';
|
||||
|
||||
final _kInfoLog = LogMessage(
|
||||
message: '#Info Message',
|
||||
level: LogLevel.info,
|
||||
createdAt: DateTime(2025, 2, 26),
|
||||
logger: 'Info Logger',
|
||||
);
|
||||
|
||||
final _kWarnLog = LogMessage(
|
||||
message: '#Warn Message',
|
||||
level: LogLevel.warning,
|
||||
createdAt: DateTime(2025, 2, 27),
|
||||
logger: 'Warn Logger',
|
||||
);
|
||||
|
||||
void main() {
|
||||
late LogService sut;
|
||||
late LogRepository mockLogRepo;
|
||||
late IsarStoreRepository mockStoreRepo;
|
||||
|
||||
setUp(() async {
|
||||
mockLogRepo = MockLogRepository();
|
||||
mockStoreRepo = MockStoreRepository();
|
||||
|
||||
registerFallbackValue(_kInfoLog);
|
||||
|
||||
when(() => mockLogRepo.truncate(limit: any(named: 'limit'))).thenAnswer((_) async => {});
|
||||
when(() => mockStoreRepo.tryGet<int>(StoreKey.logLevel)).thenAnswer((_) async => LogLevel.fine.index);
|
||||
when(() => mockLogRepo.getAll()).thenAnswer((_) async => []);
|
||||
when(() => mockLogRepo.insert(any())).thenAnswer((_) async => true);
|
||||
when(() => mockLogRepo.insertAll(any())).thenAnswer((_) async => true);
|
||||
|
||||
sut = await LogService.create(logRepository: mockLogRepo, storeRepository: mockStoreRepo);
|
||||
});
|
||||
|
||||
tearDown(() async {
|
||||
await sut.dispose();
|
||||
});
|
||||
|
||||
group("Log Service Init:", () {
|
||||
test('Truncates the existing logs on init', () {
|
||||
final limit = verify(() => mockLogRepo.truncate(limit: captureAny(named: 'limit'))).captured.firstOrNull as int?;
|
||||
expect(limit, kLogTruncateLimit);
|
||||
});
|
||||
|
||||
test('Sets log level based on the store setting', () {
|
||||
verify(() => mockStoreRepo.tryGet<int>(StoreKey.logLevel)).called(1);
|
||||
expect(Logger.root.level, Level.FINE);
|
||||
});
|
||||
});
|
||||
|
||||
group("Log Service Set Level:", () {
|
||||
setUp(() async {
|
||||
when(() => mockStoreRepo.upsert<int>(StoreKey.logLevel, any())).thenAnswer((_) async => true);
|
||||
await sut.setLogLevel(LogLevel.shout);
|
||||
});
|
||||
|
||||
test('Updates the log level in store', () {
|
||||
final index = verify(() => mockStoreRepo.upsert<int>(StoreKey.logLevel, captureAny())).captured.firstOrNull;
|
||||
expect(index, LogLevel.shout.index);
|
||||
});
|
||||
|
||||
test('Sets log level on logger', () {
|
||||
expect(Logger.root.level, Level.SHOUT);
|
||||
});
|
||||
});
|
||||
|
||||
group("Log Service Buffer:", () {
|
||||
test('Buffers logs until timer elapses', () {
|
||||
TestUtils.fakeAsync((time) async {
|
||||
sut = await LogService.create(logRepository: mockLogRepo, storeRepository: mockStoreRepo, shouldBuffer: true);
|
||||
|
||||
final logger = Logger(_kInfoLog.logger!);
|
||||
logger.info(_kInfoLog.message);
|
||||
expect(await sut.getMessages(), hasLength(1));
|
||||
logger.warning(_kWarnLog.message);
|
||||
expect(await sut.getMessages(), hasLength(2));
|
||||
time.elapse(const Duration(seconds: 6));
|
||||
expect(await sut.getMessages(), isEmpty);
|
||||
});
|
||||
});
|
||||
|
||||
test('Batch inserts all logs on timer', () {
|
||||
TestUtils.fakeAsync((time) async {
|
||||
sut = await LogService.create(logRepository: mockLogRepo, storeRepository: mockStoreRepo, shouldBuffer: true);
|
||||
|
||||
final logger = Logger(_kInfoLog.logger!);
|
||||
logger.info(_kInfoLog.message);
|
||||
time.elapse(const Duration(seconds: 6));
|
||||
final insert = verify(() => mockLogRepo.insertAll(captureAny()));
|
||||
insert.called(1);
|
||||
final captured = insert.captured.firstOrNull as List<LogMessage>;
|
||||
expect(captured.firstOrNull?.message, _kInfoLog.message);
|
||||
expect(captured.firstOrNull?.logger, _kInfoLog.logger);
|
||||
|
||||
verifyNever(() => mockLogRepo.insert(captureAny()));
|
||||
});
|
||||
});
|
||||
|
||||
test('Does not buffer when off', () {
|
||||
TestUtils.fakeAsync((time) async {
|
||||
sut = await LogService.create(logRepository: mockLogRepo, storeRepository: mockStoreRepo, shouldBuffer: false);
|
||||
|
||||
final logger = Logger(_kInfoLog.logger!);
|
||||
logger.info(_kInfoLog.message);
|
||||
// Ensure nothing gets buffer. This works because we mock log repo getAll to return nothing
|
||||
expect(await sut.getMessages(), isEmpty);
|
||||
|
||||
final insert = verify(() => mockLogRepo.insert(captureAny()));
|
||||
insert.called(1);
|
||||
final captured = insert.captured.firstOrNull as LogMessage;
|
||||
expect(captured.message, _kInfoLog.message);
|
||||
expect(captured.logger, _kInfoLog.logger);
|
||||
|
||||
verifyNever(() => mockLogRepo.insertAll(captureAny()));
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
group("Log Service Get messages:", () {
|
||||
setUp(() {
|
||||
when(() => mockLogRepo.getAll()).thenAnswer((_) async => [_kInfoLog]);
|
||||
});
|
||||
|
||||
test('Fetches result from DB', () async {
|
||||
expect(await sut.getMessages(), hasLength(1));
|
||||
verify(() => mockLogRepo.getAll()).called(1);
|
||||
});
|
||||
|
||||
test('Combines result from both DB + Buffer', () {
|
||||
TestUtils.fakeAsync((time) async {
|
||||
sut = await LogService.create(logRepository: mockLogRepo, storeRepository: mockStoreRepo, shouldBuffer: true);
|
||||
|
||||
final logger = Logger(_kWarnLog.logger!);
|
||||
logger.warning(_kWarnLog.message);
|
||||
expect(await sut.getMessages(), hasLength(2)); // 1 - DB, 1 - Buff
|
||||
|
||||
final messages = await sut.getMessages();
|
||||
// Logged time is assigned in the service for messages in the buffer, so compare manually
|
||||
expect(messages.firstOrNull?.message, _kWarnLog.message);
|
||||
expect(messages.firstOrNull?.logger, _kWarnLog.logger);
|
||||
|
||||
expect(messages.elementAtOrNull(1), _kInfoLog);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
175
mobile/test/domain/services/store_service_test.dart
Normal file
175
mobile/test/domain/services/store_service_test.dart
Normal file
|
|
@ -0,0 +1,175 @@
|
|||
import 'dart:async';
|
||||
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:immich_mobile/domain/models/store.model.dart';
|
||||
import 'package:immich_mobile/domain/services/store.service.dart';
|
||||
import 'package:immich_mobile/infrastructure/repositories/store.repository.dart';
|
||||
import 'package:mocktail/mocktail.dart';
|
||||
|
||||
import '../../infrastructure/repository.mock.dart';
|
||||
|
||||
const _kAccessToken = '#ThisIsAToken';
|
||||
const _kBackgroundBackup = false;
|
||||
const _kGroupAssetsBy = 2;
|
||||
final _kBackupFailedSince = DateTime.utc(2023);
|
||||
|
||||
void main() {
|
||||
late StoreService sut;
|
||||
late IsarStoreRepository mockStoreRepo;
|
||||
late DriftStoreRepository mockDriftStoreRepo;
|
||||
late StreamController<List<StoreDto<Object>>> controller;
|
||||
|
||||
setUp(() async {
|
||||
controller = StreamController<List<StoreDto<Object>>>.broadcast();
|
||||
mockStoreRepo = MockStoreRepository();
|
||||
mockDriftStoreRepo = MockDriftStoreRepository();
|
||||
// For generics, we need to provide fallback to each concrete type to avoid runtime errors
|
||||
registerFallbackValue(StoreKey.accessToken);
|
||||
registerFallbackValue(StoreKey.backupTriggerDelay);
|
||||
registerFallbackValue(StoreKey.backgroundBackup);
|
||||
registerFallbackValue(StoreKey.backupFailedSince);
|
||||
|
||||
when(() => mockStoreRepo.getAll()).thenAnswer(
|
||||
(_) async => [
|
||||
const StoreDto(StoreKey.accessToken, _kAccessToken),
|
||||
const StoreDto(StoreKey.backgroundBackup, _kBackgroundBackup),
|
||||
const StoreDto(StoreKey.groupAssetsBy, _kGroupAssetsBy),
|
||||
StoreDto(StoreKey.backupFailedSince, _kBackupFailedSince),
|
||||
],
|
||||
);
|
||||
when(() => mockStoreRepo.watchAll()).thenAnswer((_) => controller.stream);
|
||||
|
||||
when(() => mockDriftStoreRepo.getAll()).thenAnswer(
|
||||
(_) async => [
|
||||
const StoreDto(StoreKey.accessToken, _kAccessToken),
|
||||
const StoreDto(StoreKey.backgroundBackup, _kBackgroundBackup),
|
||||
const StoreDto(StoreKey.groupAssetsBy, _kGroupAssetsBy),
|
||||
StoreDto(StoreKey.backupFailedSince, _kBackupFailedSince),
|
||||
],
|
||||
);
|
||||
when(() => mockDriftStoreRepo.watchAll()).thenAnswer((_) => controller.stream);
|
||||
|
||||
sut = await StoreService.create(storeRepository: mockStoreRepo);
|
||||
});
|
||||
|
||||
tearDown(() async {
|
||||
unawaited(sut.dispose());
|
||||
await controller.close();
|
||||
});
|
||||
|
||||
group("Store Service Init:", () {
|
||||
test('Populates the internal cache on init', () {
|
||||
verify(() => mockStoreRepo.getAll()).called(1);
|
||||
expect(sut.tryGet(StoreKey.accessToken), _kAccessToken);
|
||||
expect(sut.tryGet(StoreKey.backgroundBackup), _kBackgroundBackup);
|
||||
expect(sut.tryGet(StoreKey.groupAssetsBy), _kGroupAssetsBy);
|
||||
expect(sut.tryGet(StoreKey.backupFailedSince), _kBackupFailedSince);
|
||||
// Other keys should be null
|
||||
expect(sut.tryGet(StoreKey.currentUser), isNull);
|
||||
});
|
||||
|
||||
test('Listens to stream of store updates', () async {
|
||||
final event = StoreDto(StoreKey.accessToken, _kAccessToken.toUpperCase());
|
||||
controller.add([event]);
|
||||
|
||||
await pumpEventQueue();
|
||||
|
||||
verify(() => mockStoreRepo.watchAll()).called(1);
|
||||
expect(sut.tryGet(StoreKey.accessToken), _kAccessToken.toUpperCase());
|
||||
});
|
||||
});
|
||||
|
||||
group('Store Service get:', () {
|
||||
test('Returns the stored value for the given key', () {
|
||||
expect(sut.get(StoreKey.accessToken), _kAccessToken);
|
||||
});
|
||||
|
||||
test('Throws StoreKeyNotFoundException for nonexistent keys', () {
|
||||
expect(() => sut.get(StoreKey.currentUser), throwsA(isA<StoreKeyNotFoundException>()));
|
||||
});
|
||||
|
||||
test('Returns the stored value for the given key or the defaultValue', () {
|
||||
expect(sut.get(StoreKey.currentUser, 5), 5);
|
||||
});
|
||||
});
|
||||
|
||||
group('Store Service put:', () {
|
||||
setUp(() {
|
||||
when(() => mockStoreRepo.upsert<String>(any<StoreKey<String>>(), any())).thenAnswer((_) async => true);
|
||||
when(() => mockDriftStoreRepo.upsert<String>(any<StoreKey<String>>(), any())).thenAnswer((_) async => true);
|
||||
});
|
||||
|
||||
test('Skip insert when value is not modified', () async {
|
||||
await sut.put(StoreKey.accessToken, _kAccessToken);
|
||||
verifyNever(() => mockStoreRepo.upsert<String>(StoreKey.accessToken, any()));
|
||||
});
|
||||
|
||||
test('Insert value when modified', () async {
|
||||
final newAccessToken = _kAccessToken.toUpperCase();
|
||||
await sut.put(StoreKey.accessToken, newAccessToken);
|
||||
verify(() => mockStoreRepo.upsert<String>(StoreKey.accessToken, newAccessToken)).called(1);
|
||||
expect(sut.tryGet(StoreKey.accessToken), newAccessToken);
|
||||
});
|
||||
});
|
||||
|
||||
group('Store Service watch:', () {
|
||||
late StreamController<String?> valueController;
|
||||
|
||||
setUp(() {
|
||||
valueController = StreamController<String?>.broadcast();
|
||||
when(() => mockStoreRepo.watch<String>(any<StoreKey<String>>())).thenAnswer((_) => valueController.stream);
|
||||
when(() => mockDriftStoreRepo.watch<String>(any<StoreKey<String>>())).thenAnswer((_) => valueController.stream);
|
||||
});
|
||||
|
||||
tearDown(() async {
|
||||
await valueController.close();
|
||||
});
|
||||
|
||||
test('Watches a specific key for changes', () async {
|
||||
final stream = sut.watch(StoreKey.accessToken);
|
||||
final events = <String?>[_kAccessToken, _kAccessToken.toUpperCase(), null, _kAccessToken.toLowerCase()];
|
||||
|
||||
unawaited(expectLater(stream, emitsInOrder(events)));
|
||||
|
||||
for (final event in events) {
|
||||
valueController.add(event);
|
||||
}
|
||||
|
||||
await pumpEventQueue();
|
||||
verify(() => mockStoreRepo.watch<String>(StoreKey.accessToken)).called(1);
|
||||
});
|
||||
});
|
||||
|
||||
group('Store Service delete:', () {
|
||||
setUp(() {
|
||||
when(() => mockStoreRepo.delete<String>(any<StoreKey<String>>())).thenAnswer((_) async => true);
|
||||
when(() => mockDriftStoreRepo.delete<String>(any<StoreKey<String>>())).thenAnswer((_) async => true);
|
||||
});
|
||||
|
||||
test('Removes the value from the DB', () async {
|
||||
await sut.delete(StoreKey.accessToken);
|
||||
verify(() => mockStoreRepo.delete<String>(StoreKey.accessToken)).called(1);
|
||||
});
|
||||
|
||||
test('Removes the value from the cache', () async {
|
||||
await sut.delete(StoreKey.accessToken);
|
||||
expect(sut.tryGet(StoreKey.accessToken), isNull);
|
||||
});
|
||||
});
|
||||
|
||||
group('Store Service clear:', () {
|
||||
setUp(() {
|
||||
when(() => mockStoreRepo.deleteAll()).thenAnswer((_) async => true);
|
||||
when(() => mockDriftStoreRepo.deleteAll()).thenAnswer((_) async => true);
|
||||
});
|
||||
|
||||
test('Clears all values from the store', () async {
|
||||
await sut.clear();
|
||||
verify(() => mockStoreRepo.deleteAll()).called(1);
|
||||
expect(sut.tryGet(StoreKey.accessToken), isNull);
|
||||
expect(sut.tryGet(StoreKey.backgroundBackup), isNull);
|
||||
expect(sut.tryGet(StoreKey.groupAssetsBy), isNull);
|
||||
expect(sut.tryGet(StoreKey.backupFailedSince), isNull);
|
||||
});
|
||||
});
|
||||
}
|
||||
578
mobile/test/domain/services/sync_stream_service_test.dart
Normal file
578
mobile/test/domain/services/sync_stream_service_test.dart
Normal file
|
|
@ -0,0 +1,578 @@
|
|||
import 'dart:async';
|
||||
|
||||
import 'package:drift/drift.dart' as drift;
|
||||
import 'package:drift/native.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:immich_mobile/domain/models/asset/base_asset.model.dart';
|
||||
import 'package:immich_mobile/domain/models/store.model.dart';
|
||||
import 'package:immich_mobile/domain/models/sync_event.model.dart';
|
||||
import 'package:immich_mobile/domain/services/store.service.dart';
|
||||
import 'package:immich_mobile/domain/services/sync_stream.service.dart';
|
||||
import 'package:immich_mobile/entities/store.entity.dart';
|
||||
import 'package:immich_mobile/infrastructure/repositories/db.repository.dart';
|
||||
import 'package:immich_mobile/infrastructure/repositories/local_asset.repository.dart';
|
||||
import 'package:immich_mobile/infrastructure/repositories/storage.repository.dart';
|
||||
import 'package:immich_mobile/infrastructure/repositories/store.repository.dart';
|
||||
import 'package:immich_mobile/infrastructure/repositories/sync_api.repository.dart';
|
||||
import 'package:immich_mobile/infrastructure/repositories/sync_stream.repository.dart';
|
||||
import 'package:immich_mobile/infrastructure/repositories/trashed_local_asset.repository.dart';
|
||||
import 'package:immich_mobile/repositories/local_files_manager.repository.dart';
|
||||
import 'package:mocktail/mocktail.dart';
|
||||
import 'package:openapi/api.dart';
|
||||
|
||||
import '../../api.mocks.dart';
|
||||
import '../../fixtures/asset.stub.dart';
|
||||
import '../../fixtures/sync_stream.stub.dart';
|
||||
import '../../infrastructure/repository.mock.dart';
|
||||
import '../../mocks/asset_entity.mock.dart';
|
||||
import '../../repository.mocks.dart';
|
||||
import '../../service.mocks.dart';
|
||||
|
||||
class _AbortCallbackWrapper {
|
||||
const _AbortCallbackWrapper();
|
||||
|
||||
bool call() => false;
|
||||
}
|
||||
|
||||
class _MockAbortCallbackWrapper extends Mock implements _AbortCallbackWrapper {}
|
||||
|
||||
class _CancellationWrapper {
|
||||
const _CancellationWrapper();
|
||||
|
||||
bool call() => false;
|
||||
}
|
||||
|
||||
class _MockCancellationWrapper extends Mock implements _CancellationWrapper {}
|
||||
|
||||
void main() {
|
||||
late SyncStreamService sut;
|
||||
late SyncStreamRepository mockSyncStreamRepo;
|
||||
late SyncApiRepository mockSyncApiRepo;
|
||||
late DriftLocalAssetRepository mockLocalAssetRepo;
|
||||
late DriftTrashedLocalAssetRepository mockTrashedLocalAssetRepo;
|
||||
late LocalFilesManagerRepository mockLocalFilesManagerRepo;
|
||||
late StorageRepository mockStorageRepo;
|
||||
late MockApiService mockApi;
|
||||
late MockServerApi mockServerApi;
|
||||
late MockSyncMigrationRepository mockSyncMigrationRepo;
|
||||
late Future<void> Function(List<SyncEvent>, Function(), Function()) handleEventsCallback;
|
||||
late _MockAbortCallbackWrapper mockAbortCallbackWrapper;
|
||||
late _MockAbortCallbackWrapper mockResetCallbackWrapper;
|
||||
late Drift db;
|
||||
late bool hasManageMediaPermission;
|
||||
|
||||
setUpAll(() async {
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
debugDefaultTargetPlatformOverride = TargetPlatform.android;
|
||||
registerFallbackValue(LocalAssetStub.image1);
|
||||
|
||||
db = Drift(drift.DatabaseConnection(NativeDatabase.memory(), closeStreamsSynchronously: true));
|
||||
await StoreService.init(storeRepository: DriftStoreRepository(db));
|
||||
});
|
||||
|
||||
tearDownAll(() async {
|
||||
debugDefaultTargetPlatformOverride = null;
|
||||
await Store.clear();
|
||||
await db.close();
|
||||
});
|
||||
|
||||
successHandler(Invocation _) async => true;
|
||||
|
||||
setUp(() async {
|
||||
mockSyncStreamRepo = MockSyncStreamRepository();
|
||||
mockSyncApiRepo = MockSyncApiRepository();
|
||||
mockLocalAssetRepo = MockLocalAssetRepository();
|
||||
mockTrashedLocalAssetRepo = MockTrashedLocalAssetRepository();
|
||||
mockLocalFilesManagerRepo = MockLocalFilesManagerRepository();
|
||||
mockStorageRepo = MockStorageRepository();
|
||||
mockAbortCallbackWrapper = _MockAbortCallbackWrapper();
|
||||
mockResetCallbackWrapper = _MockAbortCallbackWrapper();
|
||||
mockApi = MockApiService();
|
||||
mockServerApi = MockServerApi();
|
||||
mockSyncMigrationRepo = MockSyncMigrationRepository();
|
||||
|
||||
when(() => mockAbortCallbackWrapper()).thenReturn(false);
|
||||
|
||||
when(() => mockSyncApiRepo.streamChanges(any())).thenAnswer((invocation) async {
|
||||
handleEventsCallback = invocation.positionalArguments.first;
|
||||
});
|
||||
|
||||
when(() => mockSyncApiRepo.streamChanges(any(), onReset: any(named: 'onReset'))).thenAnswer((invocation) async {
|
||||
handleEventsCallback = invocation.positionalArguments.first;
|
||||
});
|
||||
|
||||
when(() => mockSyncApiRepo.ack(any())).thenAnswer((_) async => {});
|
||||
when(() => mockSyncApiRepo.deleteSyncAck(any())).thenAnswer((_) async => {});
|
||||
|
||||
when(() => mockApi.serverInfoApi).thenReturn(mockServerApi);
|
||||
when(() => mockServerApi.getServerVersion()).thenAnswer(
|
||||
(_) async => ServerVersionResponseDto(major: 1, minor: 132, patch_: 0),
|
||||
);
|
||||
|
||||
when(() => mockSyncStreamRepo.updateUsersV1(any())).thenAnswer(successHandler);
|
||||
when(() => mockSyncStreamRepo.deleteUsersV1(any())).thenAnswer(successHandler);
|
||||
when(() => mockSyncStreamRepo.updatePartnerV1(any())).thenAnswer(successHandler);
|
||||
when(() => mockSyncStreamRepo.deletePartnerV1(any())).thenAnswer(successHandler);
|
||||
when(() => mockSyncStreamRepo.updateAssetsV1(any())).thenAnswer(successHandler);
|
||||
when(
|
||||
() => mockSyncStreamRepo.updateAssetsV1(any(), debugLabel: any(named: 'debugLabel')),
|
||||
).thenAnswer(successHandler);
|
||||
when(() => mockSyncStreamRepo.deleteAssetsV1(any())).thenAnswer(successHandler);
|
||||
when(
|
||||
() => mockSyncStreamRepo.deleteAssetsV1(any(), debugLabel: any(named: 'debugLabel')),
|
||||
).thenAnswer(successHandler);
|
||||
when(() => mockSyncStreamRepo.updateAssetsExifV1(any())).thenAnswer(successHandler);
|
||||
when(
|
||||
() => mockSyncStreamRepo.updateAssetsExifV1(any(), debugLabel: any(named: 'debugLabel')),
|
||||
).thenAnswer(successHandler);
|
||||
when(() => mockSyncStreamRepo.updateMemoriesV1(any())).thenAnswer(successHandler);
|
||||
when(() => mockSyncStreamRepo.deleteMemoriesV1(any())).thenAnswer(successHandler);
|
||||
when(() => mockSyncStreamRepo.updateMemoryAssetsV1(any())).thenAnswer(successHandler);
|
||||
when(() => mockSyncStreamRepo.deleteMemoryAssetsV1(any())).thenAnswer(successHandler);
|
||||
when(
|
||||
() => mockSyncStreamRepo.updateStacksV1(any(), debugLabel: any(named: 'debugLabel')),
|
||||
).thenAnswer(successHandler);
|
||||
when(
|
||||
() => mockSyncStreamRepo.deleteStacksV1(any(), debugLabel: any(named: 'debugLabel')),
|
||||
).thenAnswer(successHandler);
|
||||
when(() => mockSyncStreamRepo.updateUserMetadatasV1(any())).thenAnswer(successHandler);
|
||||
when(() => mockSyncStreamRepo.deleteUserMetadatasV1(any())).thenAnswer(successHandler);
|
||||
when(() => mockSyncStreamRepo.updatePeopleV1(any())).thenAnswer(successHandler);
|
||||
when(() => mockSyncStreamRepo.deletePeopleV1(any())).thenAnswer(successHandler);
|
||||
when(() => mockSyncStreamRepo.updateAssetFacesV1(any())).thenAnswer(successHandler);
|
||||
when(() => mockSyncStreamRepo.deleteAssetFacesV1(any())).thenAnswer(successHandler);
|
||||
when(() => mockSyncMigrationRepo.v20260128CopyExifWidthHeightToAsset()).thenAnswer(successHandler);
|
||||
|
||||
sut = SyncStreamService(
|
||||
syncApiRepository: mockSyncApiRepo,
|
||||
syncStreamRepository: mockSyncStreamRepo,
|
||||
localAssetRepository: mockLocalAssetRepo,
|
||||
trashedLocalAssetRepository: mockTrashedLocalAssetRepo,
|
||||
localFilesManager: mockLocalFilesManagerRepo,
|
||||
storageRepository: mockStorageRepo,
|
||||
api: mockApi,
|
||||
syncMigrationRepository: mockSyncMigrationRepo,
|
||||
);
|
||||
|
||||
when(() => mockLocalAssetRepo.getAssetsFromBackupAlbums(any())).thenAnswer((_) async => {});
|
||||
when(() => mockTrashedLocalAssetRepo.trashLocalAsset(any())).thenAnswer((_) async {});
|
||||
when(() => mockTrashedLocalAssetRepo.getToRestore()).thenAnswer((_) async => []);
|
||||
when(() => mockTrashedLocalAssetRepo.applyRestoredAssets(any())).thenAnswer((_) async {});
|
||||
hasManageMediaPermission = false;
|
||||
when(() => mockLocalFilesManagerRepo.hasManageMediaPermission()).thenAnswer((_) async => hasManageMediaPermission);
|
||||
when(() => mockLocalFilesManagerRepo.moveToTrash(any())).thenAnswer((_) async => true);
|
||||
when(() => mockLocalFilesManagerRepo.restoreAssetsFromTrash(any())).thenAnswer((_) async => []);
|
||||
when(() => mockStorageRepo.getAssetEntityForAsset(any())).thenAnswer((_) async => null);
|
||||
await Store.put(StoreKey.manageLocalMediaAndroid, false);
|
||||
});
|
||||
|
||||
Future<void> simulateEvents(List<SyncEvent> events) async {
|
||||
await sut.sync();
|
||||
await handleEventsCallback(events, mockAbortCallbackWrapper.call, mockResetCallbackWrapper.call);
|
||||
}
|
||||
|
||||
group("SyncStreamService - _handleEvents", () {
|
||||
test("processes events and acks successfully when handlers succeed", () async {
|
||||
final events = [
|
||||
SyncStreamStub.userDeleteV1,
|
||||
SyncStreamStub.userV1Admin,
|
||||
SyncStreamStub.userV1User,
|
||||
SyncStreamStub.partnerDeleteV1,
|
||||
SyncStreamStub.partnerV1,
|
||||
];
|
||||
|
||||
await simulateEvents(events);
|
||||
|
||||
verifyInOrder([
|
||||
() => mockSyncStreamRepo.deleteUsersV1(any()),
|
||||
() => mockSyncApiRepo.ack(["2"]),
|
||||
() => mockSyncStreamRepo.updateUsersV1(any()),
|
||||
() => mockSyncApiRepo.ack(["5"]),
|
||||
() => mockSyncStreamRepo.deletePartnerV1(any()),
|
||||
() => mockSyncApiRepo.ack(["4"]),
|
||||
() => mockSyncStreamRepo.updatePartnerV1(any()),
|
||||
() => mockSyncApiRepo.ack(["3"]),
|
||||
]);
|
||||
verifyNever(() => mockAbortCallbackWrapper());
|
||||
});
|
||||
|
||||
test("processes final batch correctly", () async {
|
||||
final events = [SyncStreamStub.userDeleteV1, SyncStreamStub.userV1Admin];
|
||||
|
||||
await simulateEvents(events);
|
||||
|
||||
verifyInOrder([
|
||||
() => mockSyncStreamRepo.deleteUsersV1(any()),
|
||||
() => mockSyncApiRepo.ack(["2"]),
|
||||
() => mockSyncStreamRepo.updateUsersV1(any()),
|
||||
() => mockSyncApiRepo.ack(["1"]),
|
||||
]);
|
||||
verifyNever(() => mockAbortCallbackWrapper());
|
||||
});
|
||||
|
||||
test("does not process or ack when event list is empty", () async {
|
||||
await simulateEvents([]);
|
||||
|
||||
verifyNever(() => mockSyncStreamRepo.updateUsersV1(any()));
|
||||
verifyNever(() => mockSyncStreamRepo.deleteUsersV1(any()));
|
||||
verifyNever(() => mockSyncStreamRepo.updatePartnerV1(any()));
|
||||
verifyNever(() => mockSyncStreamRepo.deletePartnerV1(any()));
|
||||
verifyNever(() => mockAbortCallbackWrapper());
|
||||
verifyNever(() => mockSyncApiRepo.ack(any()));
|
||||
});
|
||||
|
||||
test("aborts and stops processing if cancelled during iteration", () async {
|
||||
final cancellationChecker = _MockCancellationWrapper();
|
||||
when(() => cancellationChecker()).thenReturn(false);
|
||||
|
||||
sut = SyncStreamService(
|
||||
syncApiRepository: mockSyncApiRepo,
|
||||
syncStreamRepository: mockSyncStreamRepo,
|
||||
localAssetRepository: mockLocalAssetRepo,
|
||||
trashedLocalAssetRepository: mockTrashedLocalAssetRepo,
|
||||
localFilesManager: mockLocalFilesManagerRepo,
|
||||
storageRepository: mockStorageRepo,
|
||||
cancelChecker: cancellationChecker.call,
|
||||
api: mockApi,
|
||||
syncMigrationRepository: mockSyncMigrationRepo,
|
||||
);
|
||||
await sut.sync();
|
||||
|
||||
final events = [SyncStreamStub.userDeleteV1, SyncStreamStub.userV1Admin, SyncStreamStub.partnerDeleteV1];
|
||||
|
||||
when(() => mockSyncStreamRepo.deleteUsersV1(any())).thenAnswer((_) async {
|
||||
when(() => cancellationChecker()).thenReturn(true);
|
||||
});
|
||||
|
||||
await handleEventsCallback(events, mockAbortCallbackWrapper.call, mockResetCallbackWrapper.call);
|
||||
|
||||
verify(() => mockSyncStreamRepo.deleteUsersV1(any())).called(1);
|
||||
verifyNever(() => mockSyncStreamRepo.updateUsersV1(any()));
|
||||
verifyNever(() => mockSyncStreamRepo.deletePartnerV1(any()));
|
||||
|
||||
verify(() => mockAbortCallbackWrapper()).called(1);
|
||||
|
||||
verify(() => mockSyncApiRepo.ack(["2"])).called(1);
|
||||
});
|
||||
|
||||
test("aborts and stops processing if cancelled before processing batch", () async {
|
||||
final cancellationChecker = _MockCancellationWrapper();
|
||||
when(() => cancellationChecker()).thenReturn(false);
|
||||
|
||||
final processingCompleter = Completer<void>();
|
||||
bool handler1Started = false;
|
||||
when(() => mockSyncStreamRepo.deleteUsersV1(any())).thenAnswer((_) async {
|
||||
handler1Started = true;
|
||||
return processingCompleter.future;
|
||||
});
|
||||
|
||||
sut = SyncStreamService(
|
||||
syncApiRepository: mockSyncApiRepo,
|
||||
syncStreamRepository: mockSyncStreamRepo,
|
||||
localAssetRepository: mockLocalAssetRepo,
|
||||
trashedLocalAssetRepository: mockTrashedLocalAssetRepo,
|
||||
localFilesManager: mockLocalFilesManagerRepo,
|
||||
storageRepository: mockStorageRepo,
|
||||
cancelChecker: cancellationChecker.call,
|
||||
api: mockApi,
|
||||
syncMigrationRepository: mockSyncMigrationRepo,
|
||||
);
|
||||
|
||||
await sut.sync();
|
||||
|
||||
final events = [SyncStreamStub.userDeleteV1, SyncStreamStub.userV1Admin, SyncStreamStub.partnerDeleteV1];
|
||||
|
||||
final processingFuture = handleEventsCallback(
|
||||
events,
|
||||
mockAbortCallbackWrapper.call,
|
||||
mockResetCallbackWrapper.call,
|
||||
);
|
||||
await pumpEventQueue();
|
||||
|
||||
expect(handler1Started, isTrue);
|
||||
|
||||
// Signal cancellation while handler 1 is waiting
|
||||
when(() => cancellationChecker()).thenReturn(true);
|
||||
await pumpEventQueue();
|
||||
|
||||
processingCompleter.complete();
|
||||
await processingFuture;
|
||||
|
||||
verifyNever(() => mockSyncStreamRepo.updateUsersV1(any()));
|
||||
|
||||
verify(() => mockSyncApiRepo.ack(["2"])).called(1);
|
||||
});
|
||||
|
||||
test("processes memory sync events successfully", () async {
|
||||
final events = [
|
||||
SyncStreamStub.memoryV1,
|
||||
SyncStreamStub.memoryDeleteV1,
|
||||
SyncStreamStub.memoryToAssetV1,
|
||||
SyncStreamStub.memoryToAssetDeleteV1,
|
||||
];
|
||||
|
||||
await simulateEvents(events);
|
||||
|
||||
verifyInOrder([
|
||||
() => mockSyncStreamRepo.updateMemoriesV1(any()),
|
||||
() => mockSyncApiRepo.ack(["5"]),
|
||||
() => mockSyncStreamRepo.deleteMemoriesV1(any()),
|
||||
() => mockSyncApiRepo.ack(["6"]),
|
||||
() => mockSyncStreamRepo.updateMemoryAssetsV1(any()),
|
||||
() => mockSyncApiRepo.ack(["7"]),
|
||||
() => mockSyncStreamRepo.deleteMemoryAssetsV1(any()),
|
||||
() => mockSyncApiRepo.ack(["8"]),
|
||||
]);
|
||||
verifyNever(() => mockAbortCallbackWrapper());
|
||||
});
|
||||
|
||||
test("processes mixed memory and user events in correct order", () async {
|
||||
final events = [
|
||||
SyncStreamStub.memoryDeleteV1,
|
||||
SyncStreamStub.userV1Admin,
|
||||
SyncStreamStub.memoryToAssetV1,
|
||||
SyncStreamStub.memoryV1,
|
||||
];
|
||||
|
||||
await simulateEvents(events);
|
||||
|
||||
verifyInOrder([
|
||||
() => mockSyncStreamRepo.deleteMemoriesV1(any()),
|
||||
() => mockSyncApiRepo.ack(["6"]),
|
||||
() => mockSyncStreamRepo.updateUsersV1(any()),
|
||||
() => mockSyncApiRepo.ack(["1"]),
|
||||
() => mockSyncStreamRepo.updateMemoryAssetsV1(any()),
|
||||
() => mockSyncApiRepo.ack(["7"]),
|
||||
() => mockSyncStreamRepo.updateMemoriesV1(any()),
|
||||
() => mockSyncApiRepo.ack(["5"]),
|
||||
]);
|
||||
verifyNever(() => mockAbortCallbackWrapper());
|
||||
});
|
||||
|
||||
test("handles memory sync failure gracefully", () async {
|
||||
when(() => mockSyncStreamRepo.updateMemoriesV1(any())).thenThrow(Exception("Memory sync failed"));
|
||||
|
||||
final events = [SyncStreamStub.memoryV1, SyncStreamStub.userV1Admin];
|
||||
|
||||
expect(() async => await simulateEvents(events), throwsA(isA<Exception>()));
|
||||
});
|
||||
|
||||
test("processes memory asset events with correct data types", () async {
|
||||
final events = [SyncStreamStub.memoryToAssetV1];
|
||||
|
||||
await simulateEvents(events);
|
||||
|
||||
verify(() => mockSyncStreamRepo.updateMemoryAssetsV1(any())).called(1);
|
||||
verify(() => mockSyncApiRepo.ack(["7"])).called(1);
|
||||
});
|
||||
|
||||
test("processes memory delete events with correct data types", () async {
|
||||
final events = [SyncStreamStub.memoryDeleteV1];
|
||||
|
||||
await simulateEvents(events);
|
||||
|
||||
verify(() => mockSyncStreamRepo.deleteMemoriesV1(any())).called(1);
|
||||
verify(() => mockSyncApiRepo.ack(["6"])).called(1);
|
||||
});
|
||||
|
||||
test("processes memory create/update events with correct data types", () async {
|
||||
final events = [SyncStreamStub.memoryV1];
|
||||
|
||||
await simulateEvents(events);
|
||||
|
||||
verify(() => mockSyncStreamRepo.updateMemoriesV1(any())).called(1);
|
||||
verify(() => mockSyncApiRepo.ack(["5"])).called(1);
|
||||
});
|
||||
});
|
||||
|
||||
group("SyncStreamService - remote trash & restore", () {
|
||||
setUp(() async {
|
||||
await Store.put(StoreKey.manageLocalMediaAndroid, true);
|
||||
hasManageMediaPermission = true;
|
||||
});
|
||||
|
||||
tearDown(() async {
|
||||
await Store.put(StoreKey.manageLocalMediaAndroid, false);
|
||||
hasManageMediaPermission = false;
|
||||
});
|
||||
|
||||
test("moves backed up local and merged assets to device trash when remote trash events are received", () async {
|
||||
final localAsset = LocalAssetStub.image1.copyWith(id: 'local-only', checksum: 'checksum-local', remoteId: null);
|
||||
final mergedAsset = LocalAssetStub.image2.copyWith(
|
||||
id: 'merged-local',
|
||||
checksum: 'checksum-merged',
|
||||
remoteId: 'remote-merged',
|
||||
);
|
||||
final assetsByAlbum = {
|
||||
'album-a': [localAsset],
|
||||
'album-b': [mergedAsset],
|
||||
};
|
||||
when(() => mockLocalAssetRepo.getAssetsFromBackupAlbums(any())).thenAnswer((invocation) async {
|
||||
final Iterable<String> requestedChecksums = invocation.positionalArguments.first as Iterable<String>;
|
||||
expect(requestedChecksums.toSet(), equals({'checksum-local', 'checksum-merged', 'checksum-remote-only'}));
|
||||
return assetsByAlbum;
|
||||
});
|
||||
|
||||
final localEntity = MockAssetEntity();
|
||||
when(() => localEntity.getMediaUrl()).thenAnswer((_) async => 'content://local-only');
|
||||
when(() => mockStorageRepo.getAssetEntityForAsset(localAsset)).thenAnswer((_) async => localEntity);
|
||||
|
||||
final mergedEntity = MockAssetEntity();
|
||||
when(() => mergedEntity.getMediaUrl()).thenAnswer((_) async => 'content://merged-local');
|
||||
when(() => mockStorageRepo.getAssetEntityForAsset(mergedAsset)).thenAnswer((_) async => mergedEntity);
|
||||
|
||||
when(() => mockLocalFilesManagerRepo.moveToTrash(any())).thenAnswer((invocation) async {
|
||||
final urls = invocation.positionalArguments.first as List<String>;
|
||||
expect(urls, unorderedEquals(['content://local-only', 'content://merged-local']));
|
||||
return true;
|
||||
});
|
||||
|
||||
final events = [
|
||||
SyncStreamStub.assetTrashed(
|
||||
id: 'remote-1',
|
||||
checksum: localAsset.checksum!,
|
||||
ack: 'asset-remote-local-1',
|
||||
trashedAt: DateTime(2025, 5, 1),
|
||||
),
|
||||
SyncStreamStub.assetTrashed(
|
||||
id: 'remote-2',
|
||||
checksum: mergedAsset.checksum!,
|
||||
ack: 'asset-remote-merged-2',
|
||||
trashedAt: DateTime(2025, 5, 2),
|
||||
),
|
||||
SyncStreamStub.assetTrashed(
|
||||
id: 'remote-3',
|
||||
checksum: 'checksum-remote-only',
|
||||
ack: 'asset-remote-only-3',
|
||||
trashedAt: DateTime(2025, 5, 3),
|
||||
),
|
||||
];
|
||||
|
||||
await simulateEvents(events);
|
||||
|
||||
verify(() => mockTrashedLocalAssetRepo.trashLocalAsset(assetsByAlbum)).called(1);
|
||||
verify(() => mockSyncApiRepo.ack(['asset-remote-only-3'])).called(1);
|
||||
});
|
||||
|
||||
test("skips device trashing when no local assets match the remote trash payload", () async {
|
||||
final events = [
|
||||
SyncStreamStub.assetTrashed(
|
||||
id: 'remote-only',
|
||||
checksum: 'checksum-only',
|
||||
ack: 'asset-remote-only-9',
|
||||
trashedAt: DateTime(2025, 6, 1),
|
||||
),
|
||||
];
|
||||
|
||||
await simulateEvents(events);
|
||||
|
||||
verify(() => mockLocalAssetRepo.getAssetsFromBackupAlbums(any())).called(1);
|
||||
verifyNever(() => mockLocalFilesManagerRepo.moveToTrash(any()));
|
||||
verifyNever(() => mockTrashedLocalAssetRepo.trashLocalAsset(any()));
|
||||
});
|
||||
|
||||
test("does not request local deletions for permanent remote delete events", () async {
|
||||
final events = [SyncStreamStub.assetDeleteV1];
|
||||
|
||||
await simulateEvents(events);
|
||||
|
||||
verifyNever(() => mockLocalAssetRepo.getAssetsFromBackupAlbums(any()));
|
||||
verifyNever(() => mockLocalFilesManagerRepo.moveToTrash(any()));
|
||||
verify(() => mockSyncStreamRepo.deleteAssetsV1(any())).called(1);
|
||||
});
|
||||
|
||||
test("restores trashed local assets once the matching remote assets leave the trash", () async {
|
||||
final trashedAssets = [
|
||||
LocalAssetStub.image1.copyWith(id: 'trashed-1', checksum: 'checksum-trash', remoteId: 'remote-1'),
|
||||
];
|
||||
when(() => mockTrashedLocalAssetRepo.getToRestore()).thenAnswer((_) async => trashedAssets);
|
||||
|
||||
final restoredIds = ['trashed-1'];
|
||||
when(() => mockLocalFilesManagerRepo.restoreAssetsFromTrash(any())).thenAnswer((invocation) async {
|
||||
final Iterable<LocalAsset> requestedAssets = invocation.positionalArguments.first as Iterable<LocalAsset>;
|
||||
expect(requestedAssets, orderedEquals(trashedAssets));
|
||||
return restoredIds;
|
||||
});
|
||||
|
||||
final events = [
|
||||
SyncStreamStub.assetModified(id: 'remote-1', checksum: 'checksum-trash', ack: 'asset-remote-1-11'),
|
||||
];
|
||||
|
||||
await simulateEvents(events);
|
||||
|
||||
verify(() => mockTrashedLocalAssetRepo.applyRestoredAssets(restoredIds)).called(1);
|
||||
});
|
||||
});
|
||||
|
||||
group('SyncStreamService - Sync Migration', () {
|
||||
test('ensure that <2.5.0 migrations run', () async {
|
||||
await Store.put(StoreKey.syncMigrationStatus, "[]");
|
||||
when(
|
||||
() => mockServerApi.getServerVersion(),
|
||||
).thenAnswer((_) async => ServerVersionResponseDto(major: 2, minor: 4, patch_: 1));
|
||||
|
||||
await sut.sync();
|
||||
|
||||
verifyInOrder([
|
||||
() => mockSyncApiRepo.deleteSyncAck([
|
||||
SyncEntityType.assetExifV1,
|
||||
SyncEntityType.partnerAssetExifV1,
|
||||
SyncEntityType.albumAssetExifCreateV1,
|
||||
SyncEntityType.albumAssetExifUpdateV1,
|
||||
]),
|
||||
() => mockSyncMigrationRepo.v20260128CopyExifWidthHeightToAsset(),
|
||||
]);
|
||||
|
||||
// should only run on server >2.5.0
|
||||
verifyNever(
|
||||
() => mockSyncApiRepo.deleteSyncAck([
|
||||
SyncEntityType.assetV1,
|
||||
SyncEntityType.partnerAssetV1,
|
||||
SyncEntityType.albumAssetCreateV1,
|
||||
SyncEntityType.albumAssetUpdateV1,
|
||||
]),
|
||||
);
|
||||
});
|
||||
test('ensure that >=2.5.0 migrations run', () async {
|
||||
await Store.put(StoreKey.syncMigrationStatus, "[]");
|
||||
when(
|
||||
() => mockServerApi.getServerVersion(),
|
||||
).thenAnswer((_) async => ServerVersionResponseDto(major: 2, minor: 5, patch_: 0));
|
||||
await sut.sync();
|
||||
|
||||
verifyInOrder([
|
||||
() => mockSyncApiRepo.deleteSyncAck([
|
||||
SyncEntityType.assetExifV1,
|
||||
SyncEntityType.partnerAssetExifV1,
|
||||
SyncEntityType.albumAssetExifCreateV1,
|
||||
SyncEntityType.albumAssetExifUpdateV1,
|
||||
]),
|
||||
() => mockSyncApiRepo.deleteSyncAck([
|
||||
SyncEntityType.assetV1,
|
||||
SyncEntityType.partnerAssetV1,
|
||||
SyncEntityType.albumAssetCreateV1,
|
||||
SyncEntityType.albumAssetUpdateV1,
|
||||
]),
|
||||
]);
|
||||
|
||||
// v20260128_ResetAssetV1 writes that v20260128_CopyExifWidthHeightToAsset has been completed
|
||||
verifyNever(() => mockSyncMigrationRepo.v20260128CopyExifWidthHeightToAsset());
|
||||
});
|
||||
|
||||
test('ensure that migrations do not re-run', () async {
|
||||
await Store.put(
|
||||
StoreKey.syncMigrationStatus,
|
||||
'["${SyncMigrationTask.v20260128_CopyExifWidthHeightToAsset.name}"]',
|
||||
);
|
||||
|
||||
when(
|
||||
() => mockServerApi.getServerVersion(),
|
||||
).thenAnswer((_) async => ServerVersionResponseDto(major: 2, minor: 4, patch_: 1));
|
||||
|
||||
await sut.sync();
|
||||
|
||||
verifyNever(() => mockSyncMigrationRepo.v20260128CopyExifWidthHeightToAsset());
|
||||
});
|
||||
});
|
||||
}
|
||||
130
mobile/test/domain/services/user_service_test.dart
Normal file
130
mobile/test/domain/services/user_service_test.dart
Normal file
|
|
@ -0,0 +1,130 @@
|
|||
import 'dart:typed_data';
|
||||
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:immich_mobile/domain/models/store.model.dart';
|
||||
import 'package:immich_mobile/domain/services/store.service.dart';
|
||||
import 'package:immich_mobile/domain/services/user.service.dart';
|
||||
import 'package:immich_mobile/infrastructure/repositories/user.repository.dart';
|
||||
import 'package:immich_mobile/infrastructure/repositories/user_api.repository.dart';
|
||||
import 'package:mocktail/mocktail.dart';
|
||||
|
||||
import '../../fixtures/user.stub.dart';
|
||||
import '../../infrastructure/repository.mock.dart';
|
||||
import '../service.mock.dart';
|
||||
|
||||
void main() {
|
||||
late UserService sut;
|
||||
late IsarUserRepository mockUserRepo;
|
||||
late UserApiRepository mockUserApiRepo;
|
||||
late StoreService mockStoreService;
|
||||
|
||||
setUp(() {
|
||||
mockUserRepo = MockIsarUserRepository();
|
||||
mockUserApiRepo = MockUserApiRepository();
|
||||
mockStoreService = MockStoreService();
|
||||
sut = UserService(
|
||||
isarUserRepository: mockUserRepo,
|
||||
userApiRepository: mockUserApiRepo,
|
||||
storeService: mockStoreService,
|
||||
);
|
||||
|
||||
registerFallbackValue(UserStub.admin);
|
||||
when(() => mockStoreService.get(StoreKey.currentUser)).thenReturn(UserStub.admin);
|
||||
when(() => mockStoreService.tryGet(StoreKey.currentUser)).thenReturn(UserStub.admin);
|
||||
});
|
||||
|
||||
group('getMyUser', () {
|
||||
test('should return user from store', () {
|
||||
final result = sut.getMyUser();
|
||||
expect(result, UserStub.admin);
|
||||
});
|
||||
|
||||
test('should handle user not found scenario', () {
|
||||
when(() => mockStoreService.get(StoreKey.currentUser)).thenThrow(Exception('User not found'));
|
||||
|
||||
expect(() => sut.getMyUser(), throwsA(isA<Exception>()));
|
||||
});
|
||||
});
|
||||
|
||||
group('tryGetMyUser', () {
|
||||
test('should return user from store', () {
|
||||
final result = sut.tryGetMyUser();
|
||||
expect(result, UserStub.admin);
|
||||
});
|
||||
|
||||
test('should return null if user not found', () {
|
||||
when(() => mockStoreService.tryGet(StoreKey.currentUser)).thenReturn(null);
|
||||
final result = sut.tryGetMyUser();
|
||||
expect(result, isNull);
|
||||
});
|
||||
});
|
||||
|
||||
group('watchMyUser', () {
|
||||
test('should return user stream from store', () {
|
||||
when(() => mockStoreService.watch(StoreKey.currentUser)).thenAnswer((_) => Stream.value(UserStub.admin));
|
||||
final result = sut.watchMyUser();
|
||||
expect(result, emits(UserStub.admin));
|
||||
});
|
||||
|
||||
test('should return an empty stream if user not found', () {
|
||||
when(() => mockStoreService.watch(StoreKey.currentUser)).thenAnswer((_) => const Stream.empty());
|
||||
final result = sut.watchMyUser();
|
||||
expect(result, emitsInOrder([]));
|
||||
});
|
||||
});
|
||||
|
||||
group('refreshMyUser', () {
|
||||
test('should return user from api and store it', () async {
|
||||
when(() => mockUserApiRepo.getMyUser()).thenAnswer((_) async => UserStub.admin);
|
||||
when(() => mockStoreService.put(StoreKey.currentUser, UserStub.admin)).thenAnswer((_) async => true);
|
||||
when(() => mockUserRepo.update(UserStub.admin)).thenAnswer((_) async => UserStub.admin);
|
||||
|
||||
final result = await sut.refreshMyUser();
|
||||
verify(() => mockStoreService.put(StoreKey.currentUser, UserStub.admin)).called(1);
|
||||
verify(() => mockUserRepo.update(UserStub.admin)).called(1);
|
||||
expect(result, UserStub.admin);
|
||||
});
|
||||
|
||||
test('should return null if user not found', () async {
|
||||
when(() => mockUserApiRepo.getMyUser()).thenAnswer((_) async => null);
|
||||
|
||||
final result = await sut.refreshMyUser();
|
||||
verifyNever(() => mockStoreService.put(StoreKey.currentUser, UserStub.admin));
|
||||
verifyNever(() => mockUserRepo.update(UserStub.admin));
|
||||
expect(result, isNull);
|
||||
});
|
||||
});
|
||||
|
||||
group('createProfileImage', () {
|
||||
test('should return profile image path', () async {
|
||||
const profileImagePath = 'profile.jpg';
|
||||
final updatedUser = UserStub.admin;
|
||||
|
||||
when(
|
||||
() => mockUserApiRepo.createProfileImage(name: profileImagePath, data: Uint8List(0)),
|
||||
).thenAnswer((_) async => profileImagePath);
|
||||
when(() => mockStoreService.put(StoreKey.currentUser, updatedUser)).thenAnswer((_) async => true);
|
||||
when(() => mockUserRepo.update(updatedUser)).thenAnswer((_) async => UserStub.admin);
|
||||
|
||||
final result = await sut.createProfileImage(profileImagePath, Uint8List(0));
|
||||
|
||||
verify(() => mockStoreService.put(StoreKey.currentUser, updatedUser)).called(1);
|
||||
verify(() => mockUserRepo.update(updatedUser)).called(1);
|
||||
expect(result, profileImagePath);
|
||||
});
|
||||
|
||||
test('should return null if profile image creation fails', () async {
|
||||
const profileImagePath = 'profile.jpg';
|
||||
final updatedUser = UserStub.admin;
|
||||
|
||||
when(
|
||||
() => mockUserApiRepo.createProfileImage(name: profileImagePath, data: Uint8List(0)),
|
||||
).thenThrow(Exception('Failed to create profile image'));
|
||||
|
||||
final result = await sut.createProfileImage(profileImagePath, Uint8List(0));
|
||||
verifyNever(() => mockStoreService.put(StoreKey.currentUser, updatedUser));
|
||||
verifyNever(() => mockUserRepo.update(updatedUser));
|
||||
expect(result, isNull);
|
||||
});
|
||||
});
|
||||
}
|
||||
90
mobile/test/drift/main/generated/schema.dart
generated
Normal file
90
mobile/test/drift/main/generated/schema.dart
generated
Normal file
|
|
@ -0,0 +1,90 @@
|
|||
// dart format width=80
|
||||
// GENERATED CODE, DO NOT EDIT BY HAND.
|
||||
// ignore_for_file: type=lint
|
||||
import 'package:drift/drift.dart';
|
||||
import 'package:drift/internal/migrations.dart';
|
||||
import 'schema_v1.dart' as v1;
|
||||
import 'schema_v2.dart' as v2;
|
||||
import 'schema_v3.dart' as v3;
|
||||
import 'schema_v4.dart' as v4;
|
||||
import 'schema_v5.dart' as v5;
|
||||
import 'schema_v6.dart' as v6;
|
||||
import 'schema_v7.dart' as v7;
|
||||
import 'schema_v8.dart' as v8;
|
||||
import 'schema_v9.dart' as v9;
|
||||
import 'schema_v10.dart' as v10;
|
||||
import 'schema_v11.dart' as v11;
|
||||
import 'schema_v12.dart' as v12;
|
||||
import 'schema_v13.dart' as v13;
|
||||
import 'schema_v14.dart' as v14;
|
||||
import 'schema_v15.dart' as v15;
|
||||
import 'schema_v16.dart' as v16;
|
||||
import 'schema_v17.dart' as v17;
|
||||
import 'schema_v18.dart' as v18;
|
||||
|
||||
class GeneratedHelper implements SchemaInstantiationHelper {
|
||||
@override
|
||||
GeneratedDatabase databaseForVersion(QueryExecutor db, int version) {
|
||||
switch (version) {
|
||||
case 1:
|
||||
return v1.DatabaseAtV1(db);
|
||||
case 2:
|
||||
return v2.DatabaseAtV2(db);
|
||||
case 3:
|
||||
return v3.DatabaseAtV3(db);
|
||||
case 4:
|
||||
return v4.DatabaseAtV4(db);
|
||||
case 5:
|
||||
return v5.DatabaseAtV5(db);
|
||||
case 6:
|
||||
return v6.DatabaseAtV6(db);
|
||||
case 7:
|
||||
return v7.DatabaseAtV7(db);
|
||||
case 8:
|
||||
return v8.DatabaseAtV8(db);
|
||||
case 9:
|
||||
return v9.DatabaseAtV9(db);
|
||||
case 10:
|
||||
return v10.DatabaseAtV10(db);
|
||||
case 11:
|
||||
return v11.DatabaseAtV11(db);
|
||||
case 12:
|
||||
return v12.DatabaseAtV12(db);
|
||||
case 13:
|
||||
return v13.DatabaseAtV13(db);
|
||||
case 14:
|
||||
return v14.DatabaseAtV14(db);
|
||||
case 15:
|
||||
return v15.DatabaseAtV15(db);
|
||||
case 16:
|
||||
return v16.DatabaseAtV16(db);
|
||||
case 17:
|
||||
return v17.DatabaseAtV17(db);
|
||||
case 18:
|
||||
return v18.DatabaseAtV18(db);
|
||||
default:
|
||||
throw MissingSchemaException(version, versions);
|
||||
}
|
||||
}
|
||||
|
||||
static const versions = const [
|
||||
1,
|
||||
2,
|
||||
3,
|
||||
4,
|
||||
5,
|
||||
6,
|
||||
7,
|
||||
8,
|
||||
9,
|
||||
10,
|
||||
11,
|
||||
12,
|
||||
13,
|
||||
14,
|
||||
15,
|
||||
16,
|
||||
17,
|
||||
18,
|
||||
];
|
||||
}
|
||||
5995
mobile/test/drift/main/generated/schema_v1.dart
generated
Normal file
5995
mobile/test/drift/main/generated/schema_v1.dart
generated
Normal file
File diff suppressed because it is too large
Load diff
7159
mobile/test/drift/main/generated/schema_v10.dart
generated
Normal file
7159
mobile/test/drift/main/generated/schema_v10.dart
generated
Normal file
File diff suppressed because it is too large
Load diff
7198
mobile/test/drift/main/generated/schema_v11.dart
generated
Normal file
7198
mobile/test/drift/main/generated/schema_v11.dart
generated
Normal file
File diff suppressed because it is too large
Load diff
7198
mobile/test/drift/main/generated/schema_v12.dart
generated
Normal file
7198
mobile/test/drift/main/generated/schema_v12.dart
generated
Normal file
File diff suppressed because it is too large
Load diff
7765
mobile/test/drift/main/generated/schema_v13.dart
generated
Normal file
7765
mobile/test/drift/main/generated/schema_v13.dart
generated
Normal file
File diff suppressed because it is too large
Load diff
7878
mobile/test/drift/main/generated/schema_v14.dart
generated
Normal file
7878
mobile/test/drift/main/generated/schema_v14.dart
generated
Normal file
File diff suppressed because it is too large
Load diff
7913
mobile/test/drift/main/generated/schema_v15.dart
generated
Normal file
7913
mobile/test/drift/main/generated/schema_v15.dart
generated
Normal file
File diff suppressed because it is too large
Load diff
8299
mobile/test/drift/main/generated/schema_v16.dart
generated
Normal file
8299
mobile/test/drift/main/generated/schema_v16.dart
generated
Normal file
File diff suppressed because it is too large
Load diff
8337
mobile/test/drift/main/generated/schema_v17.dart
generated
Normal file
8337
mobile/test/drift/main/generated/schema_v17.dart
generated
Normal file
File diff suppressed because it is too large
Load diff
8342
mobile/test/drift/main/generated/schema_v18.dart
generated
Normal file
8342
mobile/test/drift/main/generated/schema_v18.dart
generated
Normal file
File diff suppressed because it is too large
Load diff
5995
mobile/test/drift/main/generated/schema_v2.dart
generated
Normal file
5995
mobile/test/drift/main/generated/schema_v2.dart
generated
Normal file
File diff suppressed because it is too large
Load diff
5992
mobile/test/drift/main/generated/schema_v3.dart
generated
Normal file
5992
mobile/test/drift/main/generated/schema_v3.dart
generated
Normal file
File diff suppressed because it is too large
Load diff
6441
mobile/test/drift/main/generated/schema_v4.dart
generated
Normal file
6441
mobile/test/drift/main/generated/schema_v4.dart
generated
Normal file
File diff suppressed because it is too large
Load diff
6402
mobile/test/drift/main/generated/schema_v5.dart
generated
Normal file
6402
mobile/test/drift/main/generated/schema_v5.dart
generated
Normal file
File diff suppressed because it is too large
Load diff
6448
mobile/test/drift/main/generated/schema_v6.dart
generated
Normal file
6448
mobile/test/drift/main/generated/schema_v6.dart
generated
Normal file
File diff suppressed because it is too large
Load diff
6453
mobile/test/drift/main/generated/schema_v7.dart
generated
Normal file
6453
mobile/test/drift/main/generated/schema_v7.dart
generated
Normal file
File diff suppressed because it is too large
Load diff
6663
mobile/test/drift/main/generated/schema_v8.dart
generated
Normal file
6663
mobile/test/drift/main/generated/schema_v8.dart
generated
Normal file
File diff suppressed because it is too large
Load diff
6712
mobile/test/drift/main/generated/schema_v9.dart
generated
Normal file
6712
mobile/test/drift/main/generated/schema_v9.dart
generated
Normal file
File diff suppressed because it is too large
Load diff
38
mobile/test/drift/main/migration_test.dart
Normal file
38
mobile/test/drift/main/migration_test.dart
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
// dart format width=80
|
||||
// ignore_for_file: unused_local_variable, unused_import
|
||||
import 'package:drift/drift.dart';
|
||||
import 'package:drift_dev/api/migrations_native.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:immich_mobile/infrastructure/repositories/db.repository.dart';
|
||||
|
||||
import 'generated/schema.dart';
|
||||
import 'generated/schema_v1.dart' as v1;
|
||||
import 'generated/schema_v2.dart' as v2;
|
||||
|
||||
void main() {
|
||||
driftRuntimeOptions.dontWarnAboutMultipleDatabases = true;
|
||||
late SchemaVerifier verifier;
|
||||
|
||||
setUpAll(() {
|
||||
verifier = SchemaVerifier(GeneratedHelper());
|
||||
});
|
||||
|
||||
group('simple database migrations', () {
|
||||
// These simple tests verify all possible schema updates with a simple (no
|
||||
// data) migration. This is a quick way to ensure that written database
|
||||
// migrations properly alter the schema.
|
||||
const versions = GeneratedHelper.versions;
|
||||
for (final (i, fromVersion) in versions.indexed) {
|
||||
group('from $fromVersion', () {
|
||||
for (final toVersion in versions.skip(i + 1)) {
|
||||
test('to $toVersion', () async {
|
||||
final schema = await verifier.schemaAt(fromVersion);
|
||||
final db = Drift(schema.newConnection());
|
||||
await verifier.migrateAndValidate(db, toVersion);
|
||||
await db.close();
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
6
mobile/test/dto.mocks.dart
Normal file
6
mobile/test/dto.mocks.dart
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
import 'package:mocktail/mocktail.dart';
|
||||
import 'package:openapi/api.dart';
|
||||
|
||||
class MockSmartSearchDto extends Mock implements SmartSearchDto {}
|
||||
|
||||
class MockMetadataSearchDto extends Mock implements MetadataSearchDto {}
|
||||
118
mobile/test/fixtures/album.stub.dart
vendored
Normal file
118
mobile/test/fixtures/album.stub.dart
vendored
Normal file
|
|
@ -0,0 +1,118 @@
|
|||
import 'package:immich_mobile/domain/models/album/local_album.model.dart';
|
||||
import 'package:immich_mobile/entities/album.entity.dart';
|
||||
import 'package:immich_mobile/infrastructure/entities/user.entity.dart';
|
||||
|
||||
import 'asset.stub.dart';
|
||||
import 'user.stub.dart';
|
||||
|
||||
final class AlbumStub {
|
||||
const AlbumStub._();
|
||||
|
||||
static final emptyAlbum = Album(
|
||||
name: "empty-album",
|
||||
localId: "empty-album-local",
|
||||
remoteId: "empty-album-remote",
|
||||
createdAt: DateTime(2000),
|
||||
modifiedAt: DateTime(2023),
|
||||
shared: false,
|
||||
activityEnabled: false,
|
||||
startDate: DateTime(2020),
|
||||
);
|
||||
|
||||
static final sharedWithUser = Album(
|
||||
name: "empty-album-shared-with-user",
|
||||
localId: "empty-album-shared-with-user-local",
|
||||
remoteId: "empty-album-shared-with-user-remote",
|
||||
createdAt: DateTime(2023),
|
||||
modifiedAt: DateTime(2023),
|
||||
shared: true,
|
||||
activityEnabled: false,
|
||||
endDate: DateTime(2020),
|
||||
)..sharedUsers.addAll([User.fromDto(UserStub.admin)]);
|
||||
|
||||
static final oneAsset = Album(
|
||||
name: "album-with-single-asset",
|
||||
localId: "album-with-single-asset-local",
|
||||
remoteId: "album-with-single-asset-remote",
|
||||
createdAt: DateTime(2022),
|
||||
modifiedAt: DateTime(2023),
|
||||
shared: false,
|
||||
activityEnabled: false,
|
||||
startDate: DateTime(2020),
|
||||
endDate: DateTime(2023),
|
||||
)..assets.addAll([AssetStub.image1]);
|
||||
|
||||
static final twoAsset =
|
||||
Album(
|
||||
name: "album-with-two-assets",
|
||||
localId: "album-with-two-assets-local",
|
||||
remoteId: "album-with-two-assets-remote",
|
||||
createdAt: DateTime(2001),
|
||||
modifiedAt: DateTime(2010),
|
||||
shared: false,
|
||||
activityEnabled: false,
|
||||
startDate: DateTime(2019),
|
||||
endDate: DateTime(2020),
|
||||
)
|
||||
..assets.addAll([AssetStub.image1, AssetStub.image2])
|
||||
..activityEnabled = true
|
||||
..owner.value = User.fromDto(UserStub.admin);
|
||||
|
||||
static final create2020end2020Album = Album(
|
||||
name: "create2020update2020Album",
|
||||
localId: "create2020update2020Album-local",
|
||||
remoteId: "create2020update2020Album-remote",
|
||||
createdAt: DateTime(2020),
|
||||
modifiedAt: DateTime(2020),
|
||||
shared: false,
|
||||
activityEnabled: false,
|
||||
startDate: DateTime(2020),
|
||||
endDate: DateTime(2020),
|
||||
);
|
||||
static final create2020end2022Album = Album(
|
||||
name: "create2020update2021Album",
|
||||
localId: "create2020update2021Album-local",
|
||||
remoteId: "create2020update2021Album-remote",
|
||||
createdAt: DateTime(2020),
|
||||
modifiedAt: DateTime(2022),
|
||||
shared: false,
|
||||
activityEnabled: false,
|
||||
startDate: DateTime(2020),
|
||||
endDate: DateTime(2022),
|
||||
);
|
||||
static final create2020end2024Album = Album(
|
||||
name: "create2020update2022Album",
|
||||
localId: "create2020update2022Album-local",
|
||||
remoteId: "create2020update2022Album-remote",
|
||||
createdAt: DateTime(2020),
|
||||
modifiedAt: DateTime(2024),
|
||||
shared: false,
|
||||
activityEnabled: false,
|
||||
startDate: DateTime(2020),
|
||||
endDate: DateTime(2024),
|
||||
);
|
||||
static final create2020end2026Album = Album(
|
||||
name: "create2020update2023Album",
|
||||
localId: "create2020update2023Album-local",
|
||||
remoteId: "create2020update2023Album-remote",
|
||||
createdAt: DateTime(2020),
|
||||
modifiedAt: DateTime(2026),
|
||||
shared: false,
|
||||
activityEnabled: false,
|
||||
startDate: DateTime(2020),
|
||||
endDate: DateTime(2026),
|
||||
);
|
||||
}
|
||||
|
||||
abstract final class LocalAlbumStub {
|
||||
const LocalAlbumStub._();
|
||||
|
||||
static final recent = LocalAlbum(
|
||||
id: "recent-local-id",
|
||||
name: "Recent",
|
||||
updatedAt: DateTime(2023),
|
||||
assetCount: 1000,
|
||||
backupSelection: BackupSelection.none,
|
||||
isIosSharedAlbum: false,
|
||||
);
|
||||
}
|
||||
78
mobile/test/fixtures/asset.stub.dart
vendored
Normal file
78
mobile/test/fixtures/asset.stub.dart
vendored
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
import 'package:immich_mobile/domain/models/asset/base_asset.model.dart';
|
||||
import 'package:immich_mobile/domain/models/exif.model.dart';
|
||||
import 'package:immich_mobile/entities/asset.entity.dart' as old;
|
||||
|
||||
final class AssetStub {
|
||||
const AssetStub._();
|
||||
|
||||
static final image1 = old.Asset(
|
||||
checksum: "image1-checksum",
|
||||
localId: "image1",
|
||||
remoteId: 'image1-remote',
|
||||
ownerId: 1,
|
||||
fileCreatedAt: DateTime(2019),
|
||||
fileModifiedAt: DateTime(2020),
|
||||
updatedAt: DateTime.now(),
|
||||
durationInSeconds: 0,
|
||||
type: old.AssetType.image,
|
||||
fileName: "image1.jpg",
|
||||
isFavorite: true,
|
||||
isArchived: false,
|
||||
isTrashed: false,
|
||||
exifInfo: const ExifInfo(isFlipped: false),
|
||||
);
|
||||
|
||||
static final image2 = old.Asset(
|
||||
checksum: "image2-checksum",
|
||||
localId: "image2",
|
||||
remoteId: 'image2-remote',
|
||||
ownerId: 1,
|
||||
fileCreatedAt: DateTime(2000),
|
||||
fileModifiedAt: DateTime(2010),
|
||||
updatedAt: DateTime.now(),
|
||||
durationInSeconds: 60,
|
||||
type: old.AssetType.video,
|
||||
fileName: "image2.jpg",
|
||||
isFavorite: false,
|
||||
isArchived: false,
|
||||
isTrashed: false,
|
||||
exifInfo: const ExifInfo(isFlipped: true),
|
||||
);
|
||||
|
||||
static final image3 = old.Asset(
|
||||
checksum: "image3-checksum",
|
||||
localId: "image3",
|
||||
ownerId: 1,
|
||||
fileCreatedAt: DateTime(2025),
|
||||
fileModifiedAt: DateTime(2025),
|
||||
updatedAt: DateTime.now(),
|
||||
durationInSeconds: 60,
|
||||
type: old.AssetType.image,
|
||||
fileName: "image3.jpg",
|
||||
isFavorite: true,
|
||||
isArchived: false,
|
||||
isTrashed: false,
|
||||
);
|
||||
}
|
||||
|
||||
abstract final class LocalAssetStub {
|
||||
const LocalAssetStub._();
|
||||
|
||||
static final image1 = LocalAsset(
|
||||
id: "image1",
|
||||
name: "image1.jpg",
|
||||
type: AssetType.image,
|
||||
createdAt: DateTime(2025),
|
||||
updatedAt: DateTime(2025, 2),
|
||||
isEdited: false,
|
||||
);
|
||||
|
||||
static final image2 = LocalAsset(
|
||||
id: "image2",
|
||||
name: "image2.jpg",
|
||||
type: AssetType.image,
|
||||
createdAt: DateTime(2000),
|
||||
updatedAt: DateTime(20021),
|
||||
isEdited: false,
|
||||
);
|
||||
}
|
||||
18
mobile/test/fixtures/exif.stub.dart
vendored
Normal file
18
mobile/test/fixtures/exif.stub.dart
vendored
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
import 'package:immich_mobile/domain/models/exif.model.dart';
|
||||
|
||||
abstract final class ExifStub {
|
||||
static final size = const ExifInfo(assetId: 1, fileSize: 1000);
|
||||
|
||||
static final gps = const ExifInfo(
|
||||
assetId: 2,
|
||||
latitude: 20,
|
||||
longitude: 20,
|
||||
city: 'city',
|
||||
state: 'state',
|
||||
country: 'country',
|
||||
);
|
||||
|
||||
static final rotated90CW = const ExifInfo(assetId: 3, orientation: "90");
|
||||
|
||||
static final rotated270CW = const ExifInfo(assetId: 4, orientation: "-90");
|
||||
}
|
||||
136
mobile/test/fixtures/sync_stream.stub.dart
vendored
Normal file
136
mobile/test/fixtures/sync_stream.stub.dart
vendored
Normal file
|
|
@ -0,0 +1,136 @@
|
|||
import 'package:immich_mobile/domain/models/sync_event.model.dart';
|
||||
import 'package:openapi/api.dart';
|
||||
|
||||
abstract final class SyncStreamStub {
|
||||
static final userV1Admin = SyncEvent(
|
||||
type: SyncEntityType.userV1,
|
||||
data: SyncUserV1(
|
||||
deletedAt: DateTime(2020),
|
||||
email: "admin@admin",
|
||||
id: "1",
|
||||
name: "Admin",
|
||||
avatarColor: null,
|
||||
hasProfileImage: false,
|
||||
profileChangedAt: DateTime(2025),
|
||||
),
|
||||
ack: "1",
|
||||
);
|
||||
static final userV1User = SyncEvent(
|
||||
type: SyncEntityType.userV1,
|
||||
data: SyncUserV1(
|
||||
deletedAt: DateTime(2021),
|
||||
email: "user@user",
|
||||
id: "5",
|
||||
name: "User",
|
||||
avatarColor: null,
|
||||
hasProfileImage: false,
|
||||
profileChangedAt: DateTime(2025),
|
||||
),
|
||||
ack: "5",
|
||||
);
|
||||
static final userDeleteV1 = SyncEvent(
|
||||
type: SyncEntityType.userDeleteV1,
|
||||
data: SyncUserDeleteV1(userId: "2"),
|
||||
ack: "2",
|
||||
);
|
||||
|
||||
static final partnerV1 = SyncEvent(
|
||||
type: SyncEntityType.partnerV1,
|
||||
data: SyncPartnerV1(inTimeline: true, sharedById: "1", sharedWithId: "2"),
|
||||
ack: "3",
|
||||
);
|
||||
static final partnerDeleteV1 = SyncEvent(
|
||||
type: SyncEntityType.partnerDeleteV1,
|
||||
data: SyncPartnerDeleteV1(sharedById: "3", sharedWithId: "4"),
|
||||
ack: "4",
|
||||
);
|
||||
|
||||
static final memoryV1 = SyncEvent(
|
||||
type: SyncEntityType.memoryV1,
|
||||
data: SyncMemoryV1(
|
||||
createdAt: DateTime(2023, 1, 1),
|
||||
data: {"year": 2023, "title": "Test Memory"},
|
||||
deletedAt: null,
|
||||
hideAt: null,
|
||||
id: "memory-1",
|
||||
isSaved: false,
|
||||
memoryAt: DateTime(2023, 1, 1),
|
||||
ownerId: "user-1",
|
||||
seenAt: null,
|
||||
showAt: DateTime(2023, 1, 1),
|
||||
type: MemoryType.onThisDay,
|
||||
updatedAt: DateTime(2023, 1, 1),
|
||||
),
|
||||
ack: "5",
|
||||
);
|
||||
|
||||
static final memoryDeleteV1 = SyncEvent(
|
||||
type: SyncEntityType.memoryDeleteV1,
|
||||
data: SyncMemoryDeleteV1(memoryId: "memory-2"),
|
||||
ack: "6",
|
||||
);
|
||||
|
||||
static final memoryToAssetV1 = SyncEvent(
|
||||
type: SyncEntityType.memoryToAssetV1,
|
||||
data: SyncMemoryAssetV1(assetId: "asset-1", memoryId: "memory-1"),
|
||||
ack: "7",
|
||||
);
|
||||
|
||||
static final memoryToAssetDeleteV1 = SyncEvent(
|
||||
type: SyncEntityType.memoryToAssetDeleteV1,
|
||||
data: SyncMemoryAssetDeleteV1(assetId: "asset-2", memoryId: "memory-1"),
|
||||
ack: "8",
|
||||
);
|
||||
|
||||
static final assetDeleteV1 = SyncEvent(
|
||||
type: SyncEntityType.assetDeleteV1,
|
||||
data: SyncAssetDeleteV1(assetId: "remote-asset"),
|
||||
ack: "asset-delete-ack",
|
||||
);
|
||||
|
||||
static SyncEvent assetTrashed({
|
||||
required String id,
|
||||
required String checksum,
|
||||
required String ack,
|
||||
DateTime? trashedAt,
|
||||
}) {
|
||||
return _assetV1(id: id, checksum: checksum, deletedAt: trashedAt ?? DateTime(2025, 1, 1), ack: ack);
|
||||
}
|
||||
|
||||
static SyncEvent assetModified({required String id, required String checksum, required String ack}) {
|
||||
return _assetV1(id: id, checksum: checksum, deletedAt: null, ack: ack);
|
||||
}
|
||||
|
||||
static SyncEvent _assetV1({
|
||||
required String id,
|
||||
required String checksum,
|
||||
required DateTime? deletedAt,
|
||||
required String ack,
|
||||
}) {
|
||||
return SyncEvent(
|
||||
type: SyncEntityType.assetV1,
|
||||
data: SyncAssetV1(
|
||||
checksum: checksum,
|
||||
deletedAt: deletedAt,
|
||||
duration: '0',
|
||||
fileCreatedAt: DateTime(2025),
|
||||
fileModifiedAt: DateTime(2025, 1, 2),
|
||||
id: id,
|
||||
isFavorite: false,
|
||||
libraryId: null,
|
||||
livePhotoVideoId: null,
|
||||
localDateTime: DateTime(2025, 1, 3),
|
||||
originalFileName: '$id.jpg',
|
||||
ownerId: 'owner',
|
||||
stackId: null,
|
||||
thumbhash: null,
|
||||
type: AssetTypeEnum.IMAGE,
|
||||
visibility: AssetVisibility.timeline,
|
||||
width: null,
|
||||
height: null,
|
||||
isEdited: false,
|
||||
),
|
||||
ack: ack,
|
||||
);
|
||||
}
|
||||
}
|
||||
35
mobile/test/fixtures/user.stub.dart
vendored
Normal file
35
mobile/test/fixtures/user.stub.dart
vendored
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
import 'package:immich_mobile/domain/models/user.model.dart';
|
||||
|
||||
abstract final class UserStub {
|
||||
const UserStub._();
|
||||
|
||||
static final admin = UserDto(
|
||||
id: "admin",
|
||||
email: "admin@test.com",
|
||||
name: "admin",
|
||||
isAdmin: true,
|
||||
updatedAt: DateTime(2021),
|
||||
profileChangedAt: DateTime(2021),
|
||||
avatarColor: AvatarColor.green,
|
||||
);
|
||||
|
||||
static final user1 = UserDto(
|
||||
id: "user1",
|
||||
email: "user1@test.com",
|
||||
name: "user1",
|
||||
isAdmin: false,
|
||||
updatedAt: DateTime(2022),
|
||||
profileChangedAt: DateTime(2022),
|
||||
avatarColor: AvatarColor.red,
|
||||
);
|
||||
|
||||
static final user2 = UserDto(
|
||||
id: "user2",
|
||||
email: "user2@test.com",
|
||||
name: "user2",
|
||||
isAdmin: false,
|
||||
updatedAt: DateTime(2023),
|
||||
profileChangedAt: DateTime(2023),
|
||||
avatarColor: AvatarColor.primary,
|
||||
);
|
||||
}
|
||||
|
|
@ -0,0 +1,49 @@
|
|||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:immich_mobile/infrastructure/entities/exif.entity.dart';
|
||||
import 'package:immich_mobile/infrastructure/repositories/exif.repository.dart';
|
||||
import 'package:isar/isar.dart';
|
||||
|
||||
import '../../fixtures/exif.stub.dart';
|
||||
import '../../test_utils.dart';
|
||||
|
||||
Future<void> _populateExifTable(Isar db) async {
|
||||
await db.writeTxn(() async {
|
||||
await db.exifInfos.putAll([
|
||||
ExifInfo.fromDto(ExifStub.size),
|
||||
ExifInfo.fromDto(ExifStub.gps),
|
||||
ExifInfo.fromDto(ExifStub.rotated90CW),
|
||||
ExifInfo.fromDto(ExifStub.rotated270CW),
|
||||
]);
|
||||
});
|
||||
}
|
||||
|
||||
void main() {
|
||||
late Isar db;
|
||||
late IsarExifRepository sut;
|
||||
|
||||
setUp(() async {
|
||||
db = await TestUtils.initIsar();
|
||||
sut = IsarExifRepository(db);
|
||||
});
|
||||
|
||||
group("Return with proper orientation", () {
|
||||
setUp(() async {
|
||||
await _populateExifTable(db);
|
||||
});
|
||||
|
||||
test("isFlipped true for 90CW", () async {
|
||||
final exif = await sut.get(ExifStub.rotated90CW.assetId!);
|
||||
expect(exif!.isFlipped, true);
|
||||
});
|
||||
|
||||
test("isFlipped true for 270CW", () async {
|
||||
final exif = await sut.get(ExifStub.rotated270CW.assetId!);
|
||||
expect(exif!.isFlipped, true);
|
||||
});
|
||||
|
||||
test("isFlipped false for the original non-rotated image", () async {
|
||||
final exif = await sut.get(ExifStub.size.assetId!);
|
||||
expect(exif!.isFlipped, false);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
|
@ -0,0 +1,38 @@
|
|||
import 'package:drift/drift.dart';
|
||||
import 'package:drift/native.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:immich_mobile/domain/models/album/local_album.model.dart';
|
||||
import 'package:immich_mobile/infrastructure/repositories/db.repository.dart';
|
||||
import 'package:immich_mobile/infrastructure/repositories/local_album.repository.dart';
|
||||
|
||||
import '../../test_utils/medium_factory.dart';
|
||||
|
||||
void main() {
|
||||
late Drift db;
|
||||
late MediumFactory mediumFactory;
|
||||
|
||||
setUp(() {
|
||||
db = Drift(DatabaseConnection(NativeDatabase.memory(), closeStreamsSynchronously: true));
|
||||
mediumFactory = MediumFactory(db);
|
||||
});
|
||||
|
||||
group('getAll', () {
|
||||
test('sorts albums by backupSelection & isIosSharedAlbum', () async {
|
||||
final localAlbumRepo = mediumFactory.getRepository<DriftLocalAlbumRepository>();
|
||||
await localAlbumRepo.upsert(mediumFactory.localAlbum(id: '1', backupSelection: BackupSelection.none));
|
||||
await localAlbumRepo.upsert(mediumFactory.localAlbum(id: '2', backupSelection: BackupSelection.excluded));
|
||||
await localAlbumRepo.upsert(
|
||||
mediumFactory.localAlbum(id: '3', backupSelection: BackupSelection.selected, isIosSharedAlbum: true),
|
||||
);
|
||||
await localAlbumRepo.upsert(mediumFactory.localAlbum(id: '4', backupSelection: BackupSelection.selected));
|
||||
final albums = await localAlbumRepo.getAll(
|
||||
sortBy: {SortLocalAlbumsBy.backupSelection, SortLocalAlbumsBy.isIosSharedAlbum},
|
||||
);
|
||||
expect(albums.length, 4);
|
||||
expect(albums[0].id, '4'); // selected
|
||||
expect(albums[1].id, '3'); // selected & isIosSharedAlbum
|
||||
expect(albums[2].id, '1'); // none
|
||||
expect(albums[3].id, '2'); // excluded
|
||||
});
|
||||
});
|
||||
}
|
||||
|
|
@ -0,0 +1,976 @@
|
|||
import 'package:drift/drift.dart' hide isNull;
|
||||
import 'package:drift/native.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:immich_mobile/constants/enums.dart';
|
||||
import 'package:immich_mobile/domain/models/album/local_album.model.dart';
|
||||
import 'package:immich_mobile/domain/models/asset/base_asset.model.dart';
|
||||
import 'package:immich_mobile/infrastructure/entities/local_album.entity.drift.dart';
|
||||
import 'package:immich_mobile/infrastructure/entities/local_album_asset.entity.drift.dart';
|
||||
import 'package:immich_mobile/infrastructure/entities/local_asset.entity.drift.dart';
|
||||
import 'package:immich_mobile/infrastructure/entities/remote_asset.entity.drift.dart';
|
||||
import 'package:immich_mobile/infrastructure/entities/remote_asset_cloud_id.entity.drift.dart';
|
||||
import 'package:immich_mobile/infrastructure/entities/user.entity.drift.dart';
|
||||
import 'package:immich_mobile/infrastructure/repositories/db.repository.dart';
|
||||
import 'package:immich_mobile/infrastructure/repositories/local_asset.repository.dart';
|
||||
|
||||
void main() {
|
||||
final now = DateTime(2024, 1, 15);
|
||||
late Drift db;
|
||||
late DriftLocalAssetRepository repository;
|
||||
|
||||
setUp(() {
|
||||
db = Drift(DatabaseConnection(NativeDatabase.memory(), closeStreamsSynchronously: true));
|
||||
repository = DriftLocalAssetRepository(db);
|
||||
});
|
||||
|
||||
tearDown(() async {
|
||||
await db.close();
|
||||
});
|
||||
|
||||
Future<void> insertLocalAsset({
|
||||
required String id,
|
||||
String? checksum,
|
||||
DateTime? createdAt,
|
||||
AssetType type = AssetType.image,
|
||||
bool isFavorite = false,
|
||||
String? iCloudId,
|
||||
DateTime? adjustmentTime,
|
||||
double? latitude,
|
||||
double? longitude,
|
||||
}) async {
|
||||
final created = createdAt ?? now;
|
||||
await db
|
||||
.into(db.localAssetEntity)
|
||||
.insert(
|
||||
LocalAssetEntityCompanion.insert(
|
||||
id: id,
|
||||
name: 'asset_$id.jpg',
|
||||
checksum: Value(checksum),
|
||||
type: type,
|
||||
createdAt: Value(created),
|
||||
updatedAt: Value(created),
|
||||
isFavorite: Value(isFavorite),
|
||||
iCloudId: Value(iCloudId),
|
||||
adjustmentTime: Value(adjustmentTime),
|
||||
latitude: Value(latitude),
|
||||
longitude: Value(longitude),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> insertRemoteAsset({
|
||||
required String id,
|
||||
required String checksum,
|
||||
required String ownerId,
|
||||
DateTime? deletedAt,
|
||||
}) async {
|
||||
await db
|
||||
.into(db.remoteAssetEntity)
|
||||
.insert(
|
||||
RemoteAssetEntityCompanion.insert(
|
||||
id: id,
|
||||
name: 'remote_$id.jpg',
|
||||
checksum: checksum,
|
||||
type: AssetType.image,
|
||||
createdAt: Value(now),
|
||||
updatedAt: Value(now),
|
||||
ownerId: ownerId,
|
||||
visibility: AssetVisibility.timeline,
|
||||
deletedAt: Value(deletedAt),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> insertRemoteAssetCloudId({
|
||||
required String assetId,
|
||||
required String? cloudId,
|
||||
DateTime? createdAt,
|
||||
DateTime? adjustmentTime,
|
||||
double? latitude,
|
||||
double? longitude,
|
||||
}) async {
|
||||
await db
|
||||
.into(db.remoteAssetCloudIdEntity)
|
||||
.insert(
|
||||
RemoteAssetCloudIdEntityCompanion.insert(
|
||||
assetId: assetId,
|
||||
cloudId: Value(cloudId),
|
||||
createdAt: Value(createdAt),
|
||||
adjustmentTime: Value(adjustmentTime),
|
||||
latitude: Value(latitude),
|
||||
longitude: Value(longitude),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> insertUser(String id, String email) async {
|
||||
await db.into(db.userEntity).insert(UserEntityCompanion.insert(id: id, email: email, name: email));
|
||||
}
|
||||
|
||||
group('getRemovalCandidates', () {
|
||||
final userId = 'user-123';
|
||||
final otherUserId = 'user-456';
|
||||
final cutoffDate = DateTime(2024, 1, 10);
|
||||
final beforeCutoff = DateTime(2024, 1, 5);
|
||||
final afterCutoff = DateTime(2024, 1, 12);
|
||||
|
||||
setUp(() async {
|
||||
await insertUser(userId, 'user@test.com');
|
||||
await insertUser(otherUserId, 'other@test.com');
|
||||
});
|
||||
|
||||
Future<void> insertLocalAlbum({required String id, required String name, required bool isIosSharedAlbum}) async {
|
||||
await db
|
||||
.into(db.localAlbumEntity)
|
||||
.insert(
|
||||
LocalAlbumEntityCompanion.insert(
|
||||
id: id,
|
||||
name: name,
|
||||
updatedAt: Value(now),
|
||||
backupSelection: BackupSelection.none,
|
||||
isIosSharedAlbum: Value(isIosSharedAlbum),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> insertLocalAlbumAsset({required String albumId, required String assetId}) async {
|
||||
await db
|
||||
.into(db.localAlbumAssetEntity)
|
||||
.insert(LocalAlbumAssetEntityCompanion.insert(albumId: albumId, assetId: assetId));
|
||||
}
|
||||
|
||||
test('returns only assets that match all criteria', () async {
|
||||
// Asset 1: Should be included - backed up, before cutoff, correct owner, not deleted, not favorite
|
||||
await insertLocalAsset(
|
||||
id: 'local-1',
|
||||
checksum: 'checksum-1',
|
||||
createdAt: beforeCutoff,
|
||||
type: AssetType.image,
|
||||
isFavorite: false,
|
||||
);
|
||||
await insertRemoteAsset(id: 'remote-1', checksum: 'checksum-1', ownerId: userId);
|
||||
|
||||
// Asset 2: Should NOT be included - not backed up (no remote asset)
|
||||
await insertLocalAsset(
|
||||
id: 'local-2',
|
||||
checksum: 'checksum-2',
|
||||
createdAt: beforeCutoff,
|
||||
type: AssetType.image,
|
||||
isFavorite: false,
|
||||
);
|
||||
|
||||
// Asset 3: Should NOT be included - after cutoff date
|
||||
await insertLocalAsset(
|
||||
id: 'local-3',
|
||||
checksum: 'checksum-3',
|
||||
createdAt: afterCutoff,
|
||||
type: AssetType.image,
|
||||
isFavorite: false,
|
||||
);
|
||||
await insertRemoteAsset(id: 'remote-3', checksum: 'checksum-3', ownerId: userId);
|
||||
|
||||
// Asset 4: Should NOT be included - different owner
|
||||
await insertLocalAsset(
|
||||
id: 'local-4',
|
||||
checksum: 'checksum-4',
|
||||
createdAt: beforeCutoff,
|
||||
type: AssetType.image,
|
||||
isFavorite: false,
|
||||
);
|
||||
await insertRemoteAsset(id: 'remote-4', checksum: 'checksum-4', ownerId: otherUserId);
|
||||
|
||||
// Asset 5: Should NOT be included - remote asset is deleted
|
||||
await insertLocalAsset(
|
||||
id: 'local-5',
|
||||
checksum: 'checksum-5',
|
||||
createdAt: beforeCutoff,
|
||||
type: AssetType.image,
|
||||
isFavorite: false,
|
||||
);
|
||||
await insertRemoteAsset(id: 'remote-5', checksum: 'checksum-5', ownerId: userId, deletedAt: now);
|
||||
|
||||
// Asset 6: Should NOT be included - is favorite (when keepFavorites=true)
|
||||
await insertLocalAsset(
|
||||
id: 'local-6',
|
||||
checksum: 'checksum-6',
|
||||
createdAt: beforeCutoff,
|
||||
type: AssetType.image,
|
||||
isFavorite: true,
|
||||
);
|
||||
await insertRemoteAsset(id: 'remote-6', checksum: 'checksum-6', ownerId: userId);
|
||||
|
||||
final result = await repository.getRemovalCandidates(userId, cutoffDate, keepFavorites: true);
|
||||
|
||||
expect(result.assets.length, 1);
|
||||
expect(result.assets[0].id, 'local-1');
|
||||
});
|
||||
|
||||
test('includes favorites when keepFavorites is false', () async {
|
||||
await insertLocalAsset(
|
||||
id: 'local-favorite',
|
||||
checksum: 'checksum-fav',
|
||||
createdAt: beforeCutoff,
|
||||
type: AssetType.image,
|
||||
isFavorite: true,
|
||||
);
|
||||
await insertRemoteAsset(id: 'remote-favorite', checksum: 'checksum-fav', ownerId: userId);
|
||||
|
||||
final result = await repository.getRemovalCandidates(userId, cutoffDate, keepFavorites: false);
|
||||
|
||||
expect(result.assets.length, 1);
|
||||
expect(result.assets[0].id, 'local-favorite');
|
||||
expect(result.assets[0].isFavorite, true);
|
||||
});
|
||||
|
||||
test('keepMediaType photosOnly returns only videos for deletion', () async {
|
||||
// Photo - should be kept
|
||||
await insertLocalAsset(
|
||||
id: 'local-photo',
|
||||
checksum: 'checksum-photo',
|
||||
createdAt: beforeCutoff,
|
||||
type: AssetType.image,
|
||||
isFavorite: false,
|
||||
);
|
||||
await insertRemoteAsset(id: 'remote-photo', checksum: 'checksum-photo', ownerId: userId);
|
||||
|
||||
// Video - should be deleted
|
||||
await insertLocalAsset(
|
||||
id: 'local-video',
|
||||
checksum: 'checksum-video',
|
||||
createdAt: beforeCutoff,
|
||||
type: AssetType.video,
|
||||
isFavorite: false,
|
||||
);
|
||||
await insertRemoteAsset(id: 'remote-video', checksum: 'checksum-video', ownerId: userId);
|
||||
|
||||
final result = await repository.getRemovalCandidates(userId, cutoffDate, keepMediaType: AssetKeepType.photosOnly);
|
||||
|
||||
expect(result.assets.length, 1);
|
||||
expect(result.assets[0].id, 'local-video');
|
||||
expect(result.assets[0].type, AssetType.video);
|
||||
});
|
||||
|
||||
test('keepMediaType videosOnly returns only photos for deletion', () async {
|
||||
// Photo - should be deleted
|
||||
await insertLocalAsset(
|
||||
id: 'local-photo',
|
||||
checksum: 'checksum-photo',
|
||||
createdAt: beforeCutoff,
|
||||
type: AssetType.image,
|
||||
isFavorite: false,
|
||||
);
|
||||
await insertRemoteAsset(id: 'remote-photo', checksum: 'checksum-photo', ownerId: userId);
|
||||
|
||||
// Video - should be kept
|
||||
await insertLocalAsset(
|
||||
id: 'local-video',
|
||||
checksum: 'checksum-video',
|
||||
createdAt: beforeCutoff,
|
||||
type: AssetType.video,
|
||||
isFavorite: false,
|
||||
);
|
||||
await insertRemoteAsset(id: 'remote-video', checksum: 'checksum-video', ownerId: userId);
|
||||
|
||||
final result = await repository.getRemovalCandidates(userId, cutoffDate, keepMediaType: AssetKeepType.videosOnly);
|
||||
|
||||
expect(result.assets.length, 1);
|
||||
expect(result.assets[0].id, 'local-photo');
|
||||
expect(result.assets[0].type, AssetType.image);
|
||||
});
|
||||
|
||||
test('returns both photos and videos with keepMediaType.all', () async {
|
||||
// Photo
|
||||
await insertLocalAsset(
|
||||
id: 'local-photo',
|
||||
checksum: 'checksum-photo',
|
||||
createdAt: beforeCutoff,
|
||||
type: AssetType.image,
|
||||
isFavorite: false,
|
||||
);
|
||||
await insertRemoteAsset(id: 'remote-photo', checksum: 'checksum-photo', ownerId: userId);
|
||||
|
||||
// Video
|
||||
await insertLocalAsset(
|
||||
id: 'local-video',
|
||||
checksum: 'checksum-video',
|
||||
createdAt: beforeCutoff,
|
||||
type: AssetType.video,
|
||||
isFavorite: false,
|
||||
);
|
||||
await insertRemoteAsset(id: 'remote-video', checksum: 'checksum-video', ownerId: userId);
|
||||
|
||||
final result = await repository.getRemovalCandidates(userId, cutoffDate, keepMediaType: AssetKeepType.none);
|
||||
|
||||
expect(result.assets.length, 2);
|
||||
final ids = result.assets.map((a) => a.id).toSet();
|
||||
expect(ids, containsAll(['local-photo', 'local-video']));
|
||||
});
|
||||
|
||||
test('excludes assets in iOS shared albums', () async {
|
||||
// Regular album
|
||||
await insertLocalAlbum(id: 'album-regular', name: 'Regular Album', isIosSharedAlbum: false);
|
||||
|
||||
// iOS shared album
|
||||
await insertLocalAlbum(id: 'album-shared', name: 'Shared Album', isIosSharedAlbum: true);
|
||||
|
||||
// Asset in regular album (should be included)
|
||||
await insertLocalAsset(
|
||||
id: 'local-regular',
|
||||
checksum: 'checksum-regular',
|
||||
createdAt: beforeCutoff,
|
||||
type: AssetType.image,
|
||||
isFavorite: false,
|
||||
);
|
||||
await insertRemoteAsset(id: 'remote-regular', checksum: 'checksum-regular', ownerId: userId);
|
||||
await insertLocalAlbumAsset(albumId: 'album-regular', assetId: 'local-regular');
|
||||
|
||||
// Asset in iOS shared album (should be excluded)
|
||||
await insertLocalAsset(
|
||||
id: 'local-shared',
|
||||
checksum: 'checksum-shared',
|
||||
createdAt: beforeCutoff,
|
||||
type: AssetType.image,
|
||||
isFavorite: false,
|
||||
);
|
||||
await insertRemoteAsset(id: 'remote-shared', checksum: 'checksum-shared', ownerId: userId);
|
||||
await insertLocalAlbumAsset(albumId: 'album-shared', assetId: 'local-shared');
|
||||
|
||||
final result = await repository.getRemovalCandidates(userId, cutoffDate);
|
||||
|
||||
expect(result.assets.length, 1);
|
||||
expect(result.assets[0].id, 'local-regular');
|
||||
});
|
||||
|
||||
test('includes assets at exact cutoff date', () async {
|
||||
await insertLocalAsset(
|
||||
id: 'local-exact',
|
||||
checksum: 'checksum-exact',
|
||||
createdAt: cutoffDate,
|
||||
type: AssetType.image,
|
||||
isFavorite: false,
|
||||
);
|
||||
await insertRemoteAsset(id: 'remote-exact', checksum: 'checksum-exact', ownerId: userId);
|
||||
|
||||
final result = await repository.getRemovalCandidates(userId, cutoffDate);
|
||||
|
||||
expect(result.assets.length, 1);
|
||||
expect(result.assets[0].id, 'local-exact');
|
||||
});
|
||||
|
||||
test('returns empty list when no assets match criteria', () async {
|
||||
// Only assets after cutoff
|
||||
await insertLocalAsset(
|
||||
id: 'local-after',
|
||||
checksum: 'checksum-after',
|
||||
createdAt: afterCutoff,
|
||||
type: AssetType.image,
|
||||
isFavorite: false,
|
||||
);
|
||||
await insertRemoteAsset(id: 'remote-after', checksum: 'checksum-after', ownerId: userId);
|
||||
|
||||
final result = await repository.getRemovalCandidates(userId, cutoffDate);
|
||||
|
||||
expect(result.assets, isEmpty);
|
||||
});
|
||||
|
||||
test('handles multiple assets with same checksum', () async {
|
||||
// Two local assets with same checksum (edge case, but should handle it)
|
||||
await insertLocalAsset(
|
||||
id: 'local-dup1',
|
||||
checksum: 'checksum-dup',
|
||||
createdAt: beforeCutoff,
|
||||
type: AssetType.image,
|
||||
isFavorite: false,
|
||||
);
|
||||
await insertLocalAsset(
|
||||
id: 'local-dup2',
|
||||
checksum: 'checksum-dup',
|
||||
createdAt: beforeCutoff,
|
||||
type: AssetType.image,
|
||||
isFavorite: false,
|
||||
);
|
||||
await insertRemoteAsset(id: 'remote-dup', checksum: 'checksum-dup', ownerId: userId);
|
||||
|
||||
final result = await repository.getRemovalCandidates(userId, cutoffDate);
|
||||
|
||||
expect(result.assets.length, 2);
|
||||
expect(result.assets.map((a) => a.checksum).toSet(), equals({'checksum-dup'}));
|
||||
});
|
||||
|
||||
test('includes assets not in any album', () async {
|
||||
// Asset not in any album should be included
|
||||
await insertLocalAsset(
|
||||
id: 'local-no-album',
|
||||
checksum: 'checksum-no-album',
|
||||
createdAt: beforeCutoff,
|
||||
type: AssetType.image,
|
||||
isFavorite: false,
|
||||
);
|
||||
await insertRemoteAsset(id: 'remote-no-album', checksum: 'checksum-no-album', ownerId: userId);
|
||||
|
||||
final result = await repository.getRemovalCandidates(userId, cutoffDate);
|
||||
|
||||
expect(result.assets.length, 1);
|
||||
expect(result.assets[0].id, 'local-no-album');
|
||||
});
|
||||
|
||||
test('excludes asset that is in both regular and iOS shared album', () async {
|
||||
// Regular album
|
||||
await insertLocalAlbum(id: 'album-regular', name: 'Regular Album', isIosSharedAlbum: false);
|
||||
|
||||
// iOS shared album
|
||||
await insertLocalAlbum(id: 'album-shared', name: 'Shared Album', isIosSharedAlbum: true);
|
||||
|
||||
// Asset in BOTH albums - should be excluded because it's in an iOS shared album
|
||||
await insertLocalAsset(
|
||||
id: 'local-both',
|
||||
checksum: 'checksum-both',
|
||||
createdAt: beforeCutoff,
|
||||
type: AssetType.image,
|
||||
isFavorite: false,
|
||||
);
|
||||
await insertRemoteAsset(id: 'remote-both', checksum: 'checksum-both', ownerId: userId);
|
||||
await insertLocalAlbumAsset(albumId: 'album-regular', assetId: 'local-both');
|
||||
await insertLocalAlbumAsset(albumId: 'album-shared', assetId: 'local-both');
|
||||
|
||||
final result = await repository.getRemovalCandidates(userId, cutoffDate);
|
||||
|
||||
expect(result.assets, isEmpty);
|
||||
});
|
||||
|
||||
test('excludes assets with null checksum (not backed up)', () async {
|
||||
// Asset with null checksum cannot be matched to remote asset
|
||||
await db
|
||||
.into(db.localAssetEntity)
|
||||
.insert(
|
||||
LocalAssetEntityCompanion.insert(
|
||||
id: 'local-null-checksum',
|
||||
name: 'asset_null.jpg',
|
||||
checksum: const Value.absent(), // null checksum
|
||||
type: AssetType.image,
|
||||
createdAt: Value(beforeCutoff),
|
||||
updatedAt: Value(beforeCutoff),
|
||||
isFavorite: const Value(false),
|
||||
),
|
||||
);
|
||||
|
||||
final result = await repository.getRemovalCandidates(userId, cutoffDate);
|
||||
|
||||
expect(result.assets, isEmpty);
|
||||
});
|
||||
|
||||
test('excludes assets in user-excluded albums', () async {
|
||||
// Create two regular albums
|
||||
await insertLocalAlbum(id: 'album-include', name: 'Include Album', isIosSharedAlbum: false);
|
||||
await insertLocalAlbum(id: 'album-exclude', name: 'Exclude Album', isIosSharedAlbum: false);
|
||||
|
||||
// Asset in included album - should be included
|
||||
await insertLocalAsset(
|
||||
id: 'local-in-included',
|
||||
checksum: 'checksum-included',
|
||||
createdAt: beforeCutoff,
|
||||
type: AssetType.image,
|
||||
isFavorite: false,
|
||||
);
|
||||
await insertRemoteAsset(id: 'remote-included', checksum: 'checksum-included', ownerId: userId);
|
||||
await insertLocalAlbumAsset(albumId: 'album-include', assetId: 'local-in-included');
|
||||
|
||||
// Asset in excluded album - should NOT be included
|
||||
await insertLocalAsset(
|
||||
id: 'local-in-excluded',
|
||||
checksum: 'checksum-excluded',
|
||||
createdAt: beforeCutoff,
|
||||
type: AssetType.image,
|
||||
isFavorite: false,
|
||||
);
|
||||
await insertRemoteAsset(id: 'remote-excluded', checksum: 'checksum-excluded', ownerId: userId);
|
||||
await insertLocalAlbumAsset(albumId: 'album-exclude', assetId: 'local-in-excluded');
|
||||
|
||||
final result = await repository.getRemovalCandidates(userId, cutoffDate, keepAlbumIds: {'album-exclude'});
|
||||
|
||||
expect(result.assets.length, 1);
|
||||
expect(result.assets[0].id, 'local-in-included');
|
||||
});
|
||||
|
||||
test('excludes assets that are in any of multiple excluded albums', () async {
|
||||
// Create multiple albums
|
||||
await insertLocalAlbum(id: 'album-1', name: 'Album 1', isIosSharedAlbum: false);
|
||||
await insertLocalAlbum(id: 'album-2', name: 'Album 2', isIosSharedAlbum: false);
|
||||
await insertLocalAlbum(id: 'album-3', name: 'Album 3', isIosSharedAlbum: false);
|
||||
|
||||
// Asset in album-1 (excluded) - should NOT be included
|
||||
await insertLocalAsset(
|
||||
id: 'local-1',
|
||||
checksum: 'checksum-1',
|
||||
createdAt: beforeCutoff,
|
||||
type: AssetType.image,
|
||||
isFavorite: false,
|
||||
);
|
||||
await insertRemoteAsset(id: 'remote-1', checksum: 'checksum-1', ownerId: userId);
|
||||
await insertLocalAlbumAsset(albumId: 'album-1', assetId: 'local-1');
|
||||
|
||||
// Asset in album-2 (excluded) - should NOT be included
|
||||
await insertLocalAsset(
|
||||
id: 'local-2',
|
||||
checksum: 'checksum-2',
|
||||
createdAt: beforeCutoff,
|
||||
type: AssetType.image,
|
||||
isFavorite: false,
|
||||
);
|
||||
await insertRemoteAsset(id: 'remote-2', checksum: 'checksum-2', ownerId: userId);
|
||||
await insertLocalAlbumAsset(albumId: 'album-2', assetId: 'local-2');
|
||||
|
||||
// Asset in album-3 (not excluded) - should be included
|
||||
await insertLocalAsset(
|
||||
id: 'local-3',
|
||||
checksum: 'checksum-3',
|
||||
createdAt: beforeCutoff,
|
||||
type: AssetType.image,
|
||||
isFavorite: false,
|
||||
);
|
||||
await insertRemoteAsset(id: 'remote-3', checksum: 'checksum-3', ownerId: userId);
|
||||
await insertLocalAlbumAsset(albumId: 'album-3', assetId: 'local-3');
|
||||
|
||||
final result = await repository.getRemovalCandidates(userId, cutoffDate, keepAlbumIds: {'album-1', 'album-2'});
|
||||
|
||||
expect(result.assets.length, 1);
|
||||
expect(result.assets[0].id, 'local-3');
|
||||
});
|
||||
|
||||
test('excludes asset that is in both excluded and non-excluded album', () async {
|
||||
await insertLocalAlbum(id: 'album-included', name: 'Included Album', isIosSharedAlbum: false);
|
||||
await insertLocalAlbum(id: 'album-excluded', name: 'Excluded Album', isIosSharedAlbum: false);
|
||||
|
||||
// Asset in BOTH albums - should be excluded because it's in an excluded album
|
||||
await insertLocalAsset(
|
||||
id: 'local-both',
|
||||
checksum: 'checksum-both',
|
||||
createdAt: beforeCutoff,
|
||||
type: AssetType.image,
|
||||
isFavorite: false,
|
||||
);
|
||||
await insertRemoteAsset(id: 'remote-both', checksum: 'checksum-both', ownerId: userId);
|
||||
await insertLocalAlbumAsset(albumId: 'album-included', assetId: 'local-both');
|
||||
await insertLocalAlbumAsset(albumId: 'album-excluded', assetId: 'local-both');
|
||||
|
||||
final result = await repository.getRemovalCandidates(userId, cutoffDate, keepAlbumIds: {'album-excluded'});
|
||||
|
||||
expect(result.assets, isEmpty);
|
||||
});
|
||||
|
||||
test('includes all assets when excludedAlbumIds is empty', () async {
|
||||
await insertLocalAlbum(id: 'album-1', name: 'Album 1', isIosSharedAlbum: false);
|
||||
|
||||
await insertLocalAsset(
|
||||
id: 'local-1',
|
||||
checksum: 'checksum-1',
|
||||
createdAt: beforeCutoff,
|
||||
type: AssetType.image,
|
||||
isFavorite: false,
|
||||
);
|
||||
await insertRemoteAsset(id: 'remote-1', checksum: 'checksum-1', ownerId: userId);
|
||||
await insertLocalAlbumAsset(albumId: 'album-1', assetId: 'local-1');
|
||||
|
||||
await insertLocalAsset(
|
||||
id: 'local-2',
|
||||
checksum: 'checksum-2',
|
||||
createdAt: beforeCutoff,
|
||||
type: AssetType.image,
|
||||
isFavorite: false,
|
||||
);
|
||||
await insertRemoteAsset(id: 'remote-2', checksum: 'checksum-2', ownerId: userId);
|
||||
|
||||
// Empty excludedAlbumIds should include all eligible assets
|
||||
final result = await repository.getRemovalCandidates(userId, cutoffDate, keepAlbumIds: {});
|
||||
|
||||
expect(result.assets.length, 2);
|
||||
});
|
||||
|
||||
test('excludes asset not in any album when album is excluded', () async {
|
||||
await insertLocalAlbum(id: 'album-excluded', name: 'Excluded Album', isIosSharedAlbum: false);
|
||||
|
||||
// Asset NOT in any album - should be included
|
||||
await insertLocalAsset(
|
||||
id: 'local-no-album',
|
||||
checksum: 'checksum-no-album',
|
||||
createdAt: beforeCutoff,
|
||||
type: AssetType.image,
|
||||
isFavorite: false,
|
||||
);
|
||||
await insertRemoteAsset(id: 'remote-no-album', checksum: 'checksum-no-album', ownerId: userId);
|
||||
|
||||
// Asset in excluded album - should NOT be included
|
||||
await insertLocalAsset(
|
||||
id: 'local-in-excluded',
|
||||
checksum: 'checksum-in-excluded',
|
||||
createdAt: beforeCutoff,
|
||||
type: AssetType.image,
|
||||
isFavorite: false,
|
||||
);
|
||||
await insertRemoteAsset(id: 'remote-in-excluded', checksum: 'checksum-in-excluded', ownerId: userId);
|
||||
await insertLocalAlbumAsset(albumId: 'album-excluded', assetId: 'local-in-excluded');
|
||||
|
||||
final result = await repository.getRemovalCandidates(userId, cutoffDate, keepAlbumIds: {'album-excluded'});
|
||||
|
||||
expect(result.assets.length, 1);
|
||||
expect(result.assets[0].id, 'local-no-album');
|
||||
});
|
||||
|
||||
test('combines excludedAlbumIds with keepMediaType correctly', () async {
|
||||
await insertLocalAlbum(id: 'album-excluded', name: 'Excluded Album', isIosSharedAlbum: false);
|
||||
await insertLocalAlbum(id: 'album-regular', name: 'Regular Album', isIosSharedAlbum: false);
|
||||
|
||||
// Photo in excluded album - should NOT be included (album excluded)
|
||||
await insertLocalAsset(
|
||||
id: 'local-photo-excluded',
|
||||
checksum: 'checksum-photo-excluded',
|
||||
createdAt: beforeCutoff,
|
||||
type: AssetType.image,
|
||||
isFavorite: false,
|
||||
);
|
||||
await insertRemoteAsset(id: 'remote-photo-excluded', checksum: 'checksum-photo-excluded', ownerId: userId);
|
||||
await insertLocalAlbumAsset(albumId: 'album-excluded', assetId: 'local-photo-excluded');
|
||||
|
||||
// Video in regular album - should be included (keepMediaType photosOnly = delete videos)
|
||||
await insertLocalAsset(
|
||||
id: 'local-video',
|
||||
checksum: 'checksum-video',
|
||||
createdAt: beforeCutoff,
|
||||
type: AssetType.video,
|
||||
isFavorite: false,
|
||||
);
|
||||
await insertRemoteAsset(id: 'remote-video', checksum: 'checksum-video', ownerId: userId);
|
||||
await insertLocalAlbumAsset(albumId: 'album-regular', assetId: 'local-video');
|
||||
|
||||
// Photo in regular album - should NOT be included (keepMediaType photosOnly = keep photos)
|
||||
await insertLocalAsset(
|
||||
id: 'local-photo-regular',
|
||||
checksum: 'checksum-photo-regular',
|
||||
createdAt: beforeCutoff,
|
||||
type: AssetType.image,
|
||||
isFavorite: false,
|
||||
);
|
||||
await insertRemoteAsset(id: 'remote-photo-regular', checksum: 'checksum-photo-regular', ownerId: userId);
|
||||
await insertLocalAlbumAsset(albumId: 'album-regular', assetId: 'local-photo-regular');
|
||||
|
||||
final result = await repository.getRemovalCandidates(
|
||||
userId,
|
||||
cutoffDate,
|
||||
keepMediaType: AssetKeepType.photosOnly,
|
||||
keepAlbumIds: {'album-excluded'},
|
||||
);
|
||||
|
||||
expect(result.assets.length, 1);
|
||||
expect(result.assets[0].id, 'local-video');
|
||||
});
|
||||
});
|
||||
|
||||
group('reconcileHashesFromCloudId', () {
|
||||
final userId = 'user-123';
|
||||
final createdAt = DateTime(2024, 1, 10);
|
||||
final adjustmentTime = DateTime(2024, 1, 11);
|
||||
const latitude = 37.7749;
|
||||
const longitude = -122.4194;
|
||||
|
||||
setUp(() async {
|
||||
await insertUser(userId, 'user@test.com');
|
||||
});
|
||||
|
||||
test('updates local asset checksum when all metadata matches', () async {
|
||||
await insertLocalAsset(
|
||||
id: 'local-1',
|
||||
checksum: null,
|
||||
iCloudId: 'cloud-123',
|
||||
createdAt: createdAt,
|
||||
adjustmentTime: adjustmentTime,
|
||||
latitude: latitude,
|
||||
longitude: longitude,
|
||||
);
|
||||
|
||||
await insertRemoteAsset(id: 'remote-1', checksum: 'hash-abc123', ownerId: userId);
|
||||
|
||||
await insertRemoteAssetCloudId(
|
||||
assetId: 'remote-1',
|
||||
cloudId: 'cloud-123',
|
||||
createdAt: createdAt,
|
||||
adjustmentTime: adjustmentTime,
|
||||
latitude: latitude,
|
||||
longitude: longitude,
|
||||
);
|
||||
|
||||
await repository.reconcileHashesFromCloudId();
|
||||
|
||||
final updated = await repository.getById('local-1');
|
||||
expect(updated?.checksum, 'hash-abc123');
|
||||
});
|
||||
|
||||
test('does not update when local asset already has checksum', () async {
|
||||
await insertLocalAsset(
|
||||
id: 'local-1',
|
||||
checksum: 'existing-checksum',
|
||||
iCloudId: 'cloud-123',
|
||||
createdAt: createdAt,
|
||||
adjustmentTime: adjustmentTime,
|
||||
latitude: latitude,
|
||||
longitude: longitude,
|
||||
);
|
||||
|
||||
await insertRemoteAsset(id: 'remote-1', checksum: 'hash-abc123', ownerId: userId);
|
||||
|
||||
await insertRemoteAssetCloudId(
|
||||
assetId: 'remote-1',
|
||||
cloudId: 'cloud-123',
|
||||
createdAt: createdAt,
|
||||
adjustmentTime: adjustmentTime,
|
||||
latitude: latitude,
|
||||
longitude: longitude,
|
||||
);
|
||||
|
||||
await repository.reconcileHashesFromCloudId();
|
||||
|
||||
final updated = await repository.getById('local-1');
|
||||
expect(updated?.checksum, 'existing-checksum');
|
||||
});
|
||||
|
||||
test('does not update when adjustment_time does not match', () async {
|
||||
await insertLocalAsset(
|
||||
id: 'local-1',
|
||||
checksum: null,
|
||||
iCloudId: 'cloud-123',
|
||||
createdAt: createdAt,
|
||||
adjustmentTime: adjustmentTime,
|
||||
latitude: latitude,
|
||||
longitude: longitude,
|
||||
);
|
||||
|
||||
await insertRemoteAsset(id: 'remote-1', checksum: 'hash-abc123', ownerId: userId);
|
||||
|
||||
await insertRemoteAssetCloudId(
|
||||
assetId: 'remote-1',
|
||||
cloudId: 'cloud-123',
|
||||
createdAt: createdAt,
|
||||
adjustmentTime: DateTime(2024, 1, 12),
|
||||
latitude: latitude,
|
||||
longitude: longitude,
|
||||
);
|
||||
|
||||
await repository.reconcileHashesFromCloudId();
|
||||
|
||||
final updated = await repository.getById('local-1');
|
||||
expect(updated?.checksum, isNull);
|
||||
});
|
||||
|
||||
test('does not update when latitude does not match', () async {
|
||||
await insertLocalAsset(
|
||||
id: 'local-1',
|
||||
checksum: null,
|
||||
iCloudId: 'cloud-123',
|
||||
createdAt: createdAt,
|
||||
adjustmentTime: adjustmentTime,
|
||||
latitude: latitude,
|
||||
longitude: longitude,
|
||||
);
|
||||
|
||||
await insertRemoteAsset(id: 'remote-1', checksum: 'hash-abc123', ownerId: userId);
|
||||
|
||||
await insertRemoteAssetCloudId(
|
||||
assetId: 'remote-1',
|
||||
cloudId: 'cloud-123',
|
||||
createdAt: createdAt,
|
||||
adjustmentTime: adjustmentTime,
|
||||
latitude: 40.7128,
|
||||
longitude: longitude,
|
||||
);
|
||||
|
||||
await repository.reconcileHashesFromCloudId();
|
||||
|
||||
final updated = await repository.getById('local-1');
|
||||
expect(updated?.checksum, isNull);
|
||||
});
|
||||
|
||||
test('does not update when longitude does not match', () async {
|
||||
await insertLocalAsset(
|
||||
id: 'local-1',
|
||||
checksum: null,
|
||||
iCloudId: 'cloud-123',
|
||||
createdAt: createdAt,
|
||||
adjustmentTime: adjustmentTime,
|
||||
latitude: latitude,
|
||||
longitude: longitude,
|
||||
);
|
||||
|
||||
await insertRemoteAsset(id: 'remote-1', checksum: 'hash-abc123', ownerId: userId);
|
||||
|
||||
await insertRemoteAssetCloudId(
|
||||
assetId: 'remote-1',
|
||||
cloudId: 'cloud-123',
|
||||
createdAt: createdAt,
|
||||
adjustmentTime: adjustmentTime,
|
||||
latitude: latitude,
|
||||
longitude: -74.0060,
|
||||
);
|
||||
|
||||
await repository.reconcileHashesFromCloudId();
|
||||
|
||||
final updated = await repository.getById('local-1');
|
||||
expect(updated?.checksum, isNull);
|
||||
});
|
||||
|
||||
test('does not update when createdAt does not match', () async {
|
||||
await insertLocalAsset(
|
||||
id: 'local-1',
|
||||
checksum: null,
|
||||
iCloudId: 'cloud-123',
|
||||
createdAt: createdAt,
|
||||
adjustmentTime: adjustmentTime,
|
||||
latitude: latitude,
|
||||
longitude: longitude,
|
||||
);
|
||||
|
||||
await insertRemoteAsset(id: 'remote-1', checksum: 'hash-abc123', ownerId: userId);
|
||||
|
||||
await insertRemoteAssetCloudId(
|
||||
assetId: 'remote-1',
|
||||
cloudId: 'cloud-123',
|
||||
createdAt: DateTime(2024, 1, 5),
|
||||
adjustmentTime: adjustmentTime,
|
||||
latitude: latitude,
|
||||
longitude: longitude,
|
||||
);
|
||||
|
||||
await repository.reconcileHashesFromCloudId();
|
||||
|
||||
final updated = await repository.getById('local-1');
|
||||
expect(updated?.checksum, isNull);
|
||||
});
|
||||
|
||||
test('does not update when iCloudId is null', () async {
|
||||
await insertLocalAsset(
|
||||
id: 'local-1',
|
||||
checksum: null,
|
||||
iCloudId: null,
|
||||
createdAt: createdAt,
|
||||
adjustmentTime: adjustmentTime,
|
||||
latitude: latitude,
|
||||
longitude: longitude,
|
||||
);
|
||||
|
||||
await insertRemoteAsset(id: 'remote-1', checksum: 'hash-abc123', ownerId: userId);
|
||||
|
||||
await insertRemoteAssetCloudId(
|
||||
assetId: 'remote-1',
|
||||
cloudId: 'cloud-123',
|
||||
createdAt: createdAt,
|
||||
adjustmentTime: adjustmentTime,
|
||||
latitude: latitude,
|
||||
longitude: longitude,
|
||||
);
|
||||
|
||||
await repository.reconcileHashesFromCloudId();
|
||||
|
||||
final updated = await repository.getById('local-1');
|
||||
expect(updated?.checksum, isNull);
|
||||
});
|
||||
|
||||
test('does not update when cloudId does not match iCloudId', () async {
|
||||
await insertLocalAsset(
|
||||
id: 'local-1',
|
||||
checksum: null,
|
||||
iCloudId: 'cloud-123',
|
||||
createdAt: createdAt,
|
||||
adjustmentTime: adjustmentTime,
|
||||
latitude: latitude,
|
||||
longitude: longitude,
|
||||
);
|
||||
|
||||
await insertRemoteAsset(id: 'remote-1', checksum: 'hash-abc123', ownerId: userId);
|
||||
|
||||
await insertRemoteAssetCloudId(
|
||||
assetId: 'remote-1',
|
||||
cloudId: 'cloud-456',
|
||||
createdAt: createdAt,
|
||||
adjustmentTime: adjustmentTime,
|
||||
latitude: latitude,
|
||||
longitude: longitude,
|
||||
);
|
||||
|
||||
await repository.reconcileHashesFromCloudId();
|
||||
|
||||
final updated = await repository.getById('local-1');
|
||||
expect(updated?.checksum, isNull);
|
||||
});
|
||||
|
||||
test('handles partial null metadata fields matching correctly', () async {
|
||||
await insertLocalAsset(
|
||||
id: 'local-1',
|
||||
checksum: null,
|
||||
iCloudId: 'cloud-123',
|
||||
createdAt: createdAt,
|
||||
adjustmentTime: null,
|
||||
latitude: latitude,
|
||||
longitude: longitude,
|
||||
);
|
||||
|
||||
await insertRemoteAsset(id: 'remote-1', checksum: 'hash-abc123', ownerId: userId);
|
||||
|
||||
await insertRemoteAssetCloudId(
|
||||
assetId: 'remote-1',
|
||||
cloudId: 'cloud-123',
|
||||
createdAt: createdAt,
|
||||
adjustmentTime: null,
|
||||
latitude: latitude,
|
||||
longitude: longitude,
|
||||
);
|
||||
|
||||
await repository.reconcileHashesFromCloudId();
|
||||
|
||||
final updated = await repository.getById('local-1');
|
||||
expect(updated?.checksum, 'hash-abc123');
|
||||
});
|
||||
|
||||
test('does not update when one has null and other has value', () async {
|
||||
await insertLocalAsset(
|
||||
id: 'local-1',
|
||||
checksum: null,
|
||||
iCloudId: 'cloud-123',
|
||||
createdAt: createdAt,
|
||||
adjustmentTime: adjustmentTime,
|
||||
latitude: null,
|
||||
longitude: longitude,
|
||||
);
|
||||
|
||||
await insertRemoteAsset(id: 'remote-1', checksum: 'hash-abc123', ownerId: userId);
|
||||
|
||||
await insertRemoteAssetCloudId(
|
||||
assetId: 'remote-1',
|
||||
cloudId: 'cloud-123',
|
||||
createdAt: createdAt,
|
||||
adjustmentTime: adjustmentTime,
|
||||
latitude: latitude,
|
||||
longitude: longitude,
|
||||
);
|
||||
|
||||
await repository.reconcileHashesFromCloudId();
|
||||
|
||||
final updated = await repository.getById('local-1');
|
||||
expect(updated?.checksum, isNull);
|
||||
});
|
||||
|
||||
test('handles no matching assets gracefully', () async {
|
||||
await insertLocalAsset(
|
||||
id: 'local-1',
|
||||
checksum: null,
|
||||
iCloudId: 'cloud-999',
|
||||
createdAt: createdAt,
|
||||
adjustmentTime: adjustmentTime,
|
||||
latitude: latitude,
|
||||
longitude: longitude,
|
||||
);
|
||||
|
||||
await repository.reconcileHashesFromCloudId();
|
||||
|
||||
final updated = await repository.getById('local-1');
|
||||
expect(updated?.checksum, isNull);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
|
@ -0,0 +1,158 @@
|
|||
import 'dart:async';
|
||||
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:immich_mobile/domain/models/store.model.dart';
|
||||
import 'package:immich_mobile/domain/models/user.model.dart';
|
||||
import 'package:immich_mobile/infrastructure/entities/store.entity.dart';
|
||||
import 'package:immich_mobile/infrastructure/repositories/store.repository.dart';
|
||||
import 'package:isar/isar.dart';
|
||||
|
||||
import '../../fixtures/user.stub.dart';
|
||||
import '../../test_utils.dart';
|
||||
|
||||
const _kTestAccessToken = "#TestToken";
|
||||
final _kTestBackupFailed = DateTime(2025, 2, 20, 11, 45);
|
||||
const _kTestVersion = 10;
|
||||
const _kTestColorfulInterface = false;
|
||||
final _kTestUser = UserStub.admin;
|
||||
|
||||
Future<void> _addIntStoreValue(Isar db, StoreKey key, int? value) async {
|
||||
await db.storeValues.put(StoreValue(key.id, intValue: value, strValue: null));
|
||||
}
|
||||
|
||||
Future<void> _addStrStoreValue(Isar db, StoreKey key, String? value) async {
|
||||
await db.storeValues.put(StoreValue(key.id, intValue: null, strValue: value));
|
||||
}
|
||||
|
||||
Future<void> _populateStore(Isar db) async {
|
||||
await db.writeTxn(() async {
|
||||
await _addIntStoreValue(db, StoreKey.colorfulInterface, _kTestColorfulInterface ? 1 : 0);
|
||||
await _addIntStoreValue(db, StoreKey.backupFailedSince, _kTestBackupFailed.millisecondsSinceEpoch);
|
||||
await _addStrStoreValue(db, StoreKey.accessToken, _kTestAccessToken);
|
||||
await _addIntStoreValue(db, StoreKey.version, _kTestVersion);
|
||||
});
|
||||
}
|
||||
|
||||
void main() {
|
||||
late Isar db;
|
||||
late IsarStoreRepository sut;
|
||||
|
||||
setUp(() async {
|
||||
db = await TestUtils.initIsar();
|
||||
sut = IsarStoreRepository(db);
|
||||
});
|
||||
|
||||
group('Store Repository converters:', () {
|
||||
test('converts int', () async {
|
||||
int? version = await sut.tryGet(StoreKey.version);
|
||||
expect(version, isNull);
|
||||
await sut.upsert(StoreKey.version, _kTestVersion);
|
||||
version = await sut.tryGet(StoreKey.version);
|
||||
expect(version, _kTestVersion);
|
||||
});
|
||||
|
||||
test('converts string', () async {
|
||||
String? accessToken = await sut.tryGet(StoreKey.accessToken);
|
||||
expect(accessToken, isNull);
|
||||
await sut.upsert(StoreKey.accessToken, _kTestAccessToken);
|
||||
accessToken = await sut.tryGet(StoreKey.accessToken);
|
||||
expect(accessToken, _kTestAccessToken);
|
||||
});
|
||||
|
||||
test('converts datetime', () async {
|
||||
DateTime? backupFailedSince = await sut.tryGet(StoreKey.backupFailedSince);
|
||||
expect(backupFailedSince, isNull);
|
||||
await sut.upsert(StoreKey.backupFailedSince, _kTestBackupFailed);
|
||||
backupFailedSince = await sut.tryGet(StoreKey.backupFailedSince);
|
||||
expect(backupFailedSince, _kTestBackupFailed);
|
||||
});
|
||||
|
||||
test('converts bool', () async {
|
||||
bool? colorfulInterface = await sut.tryGet(StoreKey.colorfulInterface);
|
||||
expect(colorfulInterface, isNull);
|
||||
await sut.upsert(StoreKey.colorfulInterface, _kTestColorfulInterface);
|
||||
colorfulInterface = await sut.tryGet(StoreKey.colorfulInterface);
|
||||
expect(colorfulInterface, _kTestColorfulInterface);
|
||||
});
|
||||
|
||||
test('converts user', () async {
|
||||
UserDto? user = await sut.tryGet(StoreKey.currentUser);
|
||||
expect(user, isNull);
|
||||
await sut.upsert(StoreKey.currentUser, _kTestUser);
|
||||
user = await sut.tryGet(StoreKey.currentUser);
|
||||
expect(user, _kTestUser);
|
||||
});
|
||||
});
|
||||
|
||||
group('Store Repository Deletes:', () {
|
||||
setUp(() async {
|
||||
await _populateStore(db);
|
||||
});
|
||||
|
||||
test('delete()', () async {
|
||||
bool? isColorful = await sut.tryGet(StoreKey.colorfulInterface);
|
||||
expect(isColorful, isFalse);
|
||||
await sut.delete(StoreKey.colorfulInterface);
|
||||
isColorful = await sut.tryGet(StoreKey.colorfulInterface);
|
||||
expect(isColorful, isNull);
|
||||
});
|
||||
|
||||
test('deleteAll()', () async {
|
||||
final count = await db.storeValues.count();
|
||||
expect(count, isNot(isZero));
|
||||
await sut.deleteAll();
|
||||
unawaited(expectLater(await db.storeValues.count(), isZero));
|
||||
});
|
||||
});
|
||||
|
||||
group('Store Repository Updates:', () {
|
||||
setUp(() async {
|
||||
await _populateStore(db);
|
||||
});
|
||||
|
||||
test('upsert()', () async {
|
||||
int? version = await sut.tryGet(StoreKey.version);
|
||||
expect(version, _kTestVersion);
|
||||
await sut.upsert(StoreKey.version, _kTestVersion + 10);
|
||||
version = await sut.tryGet(StoreKey.version);
|
||||
expect(version, _kTestVersion + 10);
|
||||
});
|
||||
});
|
||||
|
||||
group('Store Repository Watchers:', () {
|
||||
setUp(() async {
|
||||
await _populateStore(db);
|
||||
});
|
||||
|
||||
test('watch()', () async {
|
||||
final stream = sut.watch(StoreKey.version);
|
||||
unawaited(expectLater(stream, emitsInOrder([_kTestVersion, _kTestVersion + 10])));
|
||||
await pumpEventQueue();
|
||||
await sut.upsert(StoreKey.version, _kTestVersion + 10);
|
||||
});
|
||||
|
||||
test('watchAll()', () async {
|
||||
final stream = sut.watchAll();
|
||||
unawaited(
|
||||
expectLater(
|
||||
stream,
|
||||
emitsInOrder([
|
||||
[
|
||||
const StoreDto<Object>(StoreKey.version, _kTestVersion),
|
||||
StoreDto<Object>(StoreKey.backupFailedSince, _kTestBackupFailed),
|
||||
const StoreDto<Object>(StoreKey.accessToken, _kTestAccessToken),
|
||||
const StoreDto<Object>(StoreKey.colorfulInterface, _kTestColorfulInterface),
|
||||
],
|
||||
[
|
||||
const StoreDto<Object>(StoreKey.version, _kTestVersion + 10),
|
||||
StoreDto<Object>(StoreKey.backupFailedSince, _kTestBackupFailed),
|
||||
const StoreDto<Object>(StoreKey.accessToken, _kTestAccessToken),
|
||||
const StoreDto<Object>(StoreKey.colorfulInterface, _kTestColorfulInterface),
|
||||
],
|
||||
]),
|
||||
),
|
||||
);
|
||||
await sut.upsert(StoreKey.version, _kTestVersion + 10);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
|
@ -0,0 +1,291 @@
|
|||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:immich_mobile/domain/models/sync_event.model.dart';
|
||||
import 'package:immich_mobile/domain/services/store.service.dart';
|
||||
import 'package:immich_mobile/infrastructure/repositories/store.repository.dart';
|
||||
import 'package:immich_mobile/infrastructure/repositories/sync_api.repository.dart';
|
||||
import 'package:mocktail/mocktail.dart';
|
||||
import 'package:openapi/api.dart';
|
||||
|
||||
import '../../api.mocks.dart';
|
||||
import '../../service.mocks.dart';
|
||||
import '../../test_utils.dart';
|
||||
|
||||
class MockHttpClient extends Mock implements http.Client {}
|
||||
|
||||
class MockApiClient extends Mock implements ApiClient {}
|
||||
|
||||
class MockStreamedResponse extends Mock implements http.StreamedResponse {}
|
||||
|
||||
class FakeBaseRequest extends Fake implements http.BaseRequest {}
|
||||
|
||||
String _createJsonLine(String type, Map<String, dynamic> data, String ack) {
|
||||
return '${jsonEncode({'type': type, 'data': data, 'ack': ack})}\n';
|
||||
}
|
||||
|
||||
void main() {
|
||||
late SyncApiRepository sut;
|
||||
late MockApiService mockApiService;
|
||||
late MockApiClient mockApiClient;
|
||||
late MockSyncApi mockSyncApi;
|
||||
late MockHttpClient mockHttpClient;
|
||||
late MockStreamedResponse mockStreamedResponse;
|
||||
late StreamController<List<int>> responseStreamController;
|
||||
late int testBatchSize = 3;
|
||||
|
||||
setUpAll(() async {
|
||||
await StoreService.init(storeRepository: IsarStoreRepository(await TestUtils.initIsar()));
|
||||
});
|
||||
|
||||
setUp(() {
|
||||
mockApiService = MockApiService();
|
||||
mockApiClient = MockApiClient();
|
||||
mockSyncApi = MockSyncApi();
|
||||
mockHttpClient = MockHttpClient();
|
||||
mockStreamedResponse = MockStreamedResponse();
|
||||
responseStreamController = StreamController<List<int>>.broadcast(sync: true);
|
||||
|
||||
registerFallbackValue(FakeBaseRequest());
|
||||
|
||||
when(() => mockApiService.apiClient).thenReturn(mockApiClient);
|
||||
when(() => mockApiService.syncApi).thenReturn(mockSyncApi);
|
||||
when(() => mockApiClient.basePath).thenReturn('http://demo.immich.app/api');
|
||||
when(() => mockApiService.applyToParams(any(), any())).thenAnswer((_) async => {});
|
||||
|
||||
// Mock HTTP client behavior
|
||||
when(() => mockHttpClient.send(any())).thenAnswer((_) async => mockStreamedResponse);
|
||||
when(() => mockStreamedResponse.statusCode).thenReturn(200);
|
||||
when(() => mockStreamedResponse.stream).thenAnswer((_) => http.ByteStream(responseStreamController.stream));
|
||||
when(() => mockHttpClient.close()).thenAnswer((_) => {});
|
||||
|
||||
sut = SyncApiRepository(mockApiService);
|
||||
});
|
||||
|
||||
tearDown(() async {
|
||||
if (!responseStreamController.isClosed) {
|
||||
await responseStreamController.close();
|
||||
}
|
||||
});
|
||||
|
||||
Future<void> streamChanges(
|
||||
Future<void> Function(List<SyncEvent>, Function() abort, Function() reset) onDataCallback,
|
||||
) {
|
||||
return sut.streamChanges(onDataCallback, batchSize: testBatchSize, httpClient: mockHttpClient);
|
||||
}
|
||||
|
||||
test('streamChanges stops processing stream when abort is called', () async {
|
||||
int onDataCallCount = 0;
|
||||
bool abortWasCalledInCallback = false;
|
||||
List<SyncEvent> receivedEventsBatch1 = [];
|
||||
final Completer<void> firstBatchReceived = Completer<void>();
|
||||
|
||||
Future<void> onDataCallback(List<SyncEvent> events, Function() abort, Function() _) async {
|
||||
onDataCallCount++;
|
||||
if (onDataCallCount == 1) {
|
||||
receivedEventsBatch1 = events;
|
||||
abort();
|
||||
abortWasCalledInCallback = true;
|
||||
firstBatchReceived.complete();
|
||||
} else {
|
||||
fail("onData called more than once after abort was invoked");
|
||||
}
|
||||
}
|
||||
|
||||
final streamChangesFuture = streamChanges(onDataCallback);
|
||||
|
||||
// Give the stream subscription time to start (longer delay to account for mock delay)
|
||||
await Future.delayed(const Duration(milliseconds: 50));
|
||||
|
||||
for (int i = 0; i < testBatchSize; i++) {
|
||||
responseStreamController.add(
|
||||
utf8.encode(
|
||||
_createJsonLine(SyncEntityType.userDeleteV1.toString(), SyncUserDeleteV1(userId: "user$i").toJson(), 'ack$i'),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
await firstBatchReceived.future.timeout(
|
||||
const Duration(seconds: 5),
|
||||
onTimeout: () => fail('First batch was not processed within timeout'),
|
||||
);
|
||||
|
||||
for (int i = testBatchSize; i < testBatchSize * 2; i++) {
|
||||
responseStreamController.add(
|
||||
utf8.encode(
|
||||
_createJsonLine(SyncEntityType.userDeleteV1.toString(), SyncUserDeleteV1(userId: "user$i").toJson(), 'ack$i'),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
await responseStreamController.close();
|
||||
await expectLater(streamChangesFuture, completes);
|
||||
|
||||
expect(onDataCallCount, 1);
|
||||
expect(abortWasCalledInCallback, isTrue);
|
||||
expect(receivedEventsBatch1.length, testBatchSize);
|
||||
verify(() => mockHttpClient.close()).called(1);
|
||||
});
|
||||
|
||||
test('streamChanges does not process remaining lines in finally block if aborted', () async {
|
||||
int onDataCallCount = 0;
|
||||
bool abortWasCalledInCallback = false;
|
||||
final Completer<void> firstBatchReceived = Completer<void>();
|
||||
|
||||
Future<void> onDataCallback(List<SyncEvent> events, Function() abort, Function() _) async {
|
||||
onDataCallCount++;
|
||||
if (onDataCallCount == 1) {
|
||||
abort();
|
||||
abortWasCalledInCallback = true;
|
||||
firstBatchReceived.complete();
|
||||
} else {
|
||||
fail("onData called more than once after abort was invoked");
|
||||
}
|
||||
}
|
||||
|
||||
final streamChangesFuture = streamChanges(onDataCallback);
|
||||
|
||||
await Future.delayed(const Duration(milliseconds: 50));
|
||||
|
||||
for (int i = 0; i < testBatchSize; i++) {
|
||||
responseStreamController.add(
|
||||
utf8.encode(
|
||||
_createJsonLine(SyncEntityType.userDeleteV1.toString(), SyncUserDeleteV1(userId: "user$i").toJson(), 'ack$i'),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
await firstBatchReceived.future.timeout(
|
||||
const Duration(seconds: 5),
|
||||
onTimeout: () => fail('First batch was not processed within timeout'),
|
||||
);
|
||||
|
||||
// emit a single event to skip batching and trigger finally
|
||||
responseStreamController.add(
|
||||
utf8.encode(
|
||||
_createJsonLine(SyncEntityType.userDeleteV1.toString(), SyncUserDeleteV1(userId: "user100").toJson(), 'ack100'),
|
||||
),
|
||||
);
|
||||
|
||||
await responseStreamController.close();
|
||||
await expectLater(streamChangesFuture, completes);
|
||||
|
||||
expect(onDataCallCount, 1);
|
||||
expect(abortWasCalledInCallback, isTrue);
|
||||
verify(() => mockHttpClient.close()).called(1);
|
||||
});
|
||||
|
||||
test('streamChanges processes remaining lines in finally block if not aborted', () async {
|
||||
int onDataCallCount = 0;
|
||||
List<SyncEvent> receivedEventsBatch1 = [];
|
||||
List<SyncEvent> receivedEventsBatch2 = [];
|
||||
final Completer<void> firstBatchReceived = Completer<void>();
|
||||
final Completer<void> secondBatchReceived = Completer<void>();
|
||||
|
||||
Future<void> onDataCallback(List<SyncEvent> events, Function() _, Function() __) async {
|
||||
onDataCallCount++;
|
||||
if (onDataCallCount == 1) {
|
||||
receivedEventsBatch1 = events;
|
||||
firstBatchReceived.complete();
|
||||
} else if (onDataCallCount == 2) {
|
||||
receivedEventsBatch2 = events;
|
||||
secondBatchReceived.complete();
|
||||
} else {
|
||||
fail("onData called more than expected");
|
||||
}
|
||||
}
|
||||
|
||||
final streamChangesFuture = streamChanges(onDataCallback);
|
||||
|
||||
await Future.delayed(const Duration(milliseconds: 50));
|
||||
|
||||
// Batch 1
|
||||
for (int i = 0; i < testBatchSize; i++) {
|
||||
responseStreamController.add(
|
||||
utf8.encode(
|
||||
_createJsonLine(SyncEntityType.userDeleteV1.toString(), SyncUserDeleteV1(userId: "user$i").toJson(), 'ack$i'),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
await firstBatchReceived.future.timeout(
|
||||
const Duration(seconds: 5),
|
||||
onTimeout: () => fail('First batch was not processed within timeout'),
|
||||
);
|
||||
|
||||
responseStreamController.add(
|
||||
utf8.encode(
|
||||
_createJsonLine(SyncEntityType.userDeleteV1.toString(), SyncUserDeleteV1(userId: "user100").toJson(), 'ack100'),
|
||||
),
|
||||
);
|
||||
|
||||
await responseStreamController.close();
|
||||
|
||||
await secondBatchReceived.future.timeout(
|
||||
const Duration(seconds: 5),
|
||||
onTimeout: () => fail('Second batch was not processed within timeout'),
|
||||
);
|
||||
|
||||
await expectLater(streamChangesFuture, completes);
|
||||
|
||||
expect(onDataCallCount, 2);
|
||||
expect(receivedEventsBatch1.length, testBatchSize);
|
||||
expect(receivedEventsBatch2.length, 1);
|
||||
verify(() => mockHttpClient.close()).called(1);
|
||||
});
|
||||
|
||||
test('streamChanges handles stream error gracefully', () async {
|
||||
final streamError = Exception("Network Error");
|
||||
int onDataCallCount = 0;
|
||||
|
||||
Future<void> onDataCallback(List<SyncEvent> events, Function() _, Function() __) async {
|
||||
onDataCallCount++;
|
||||
}
|
||||
|
||||
final streamChangesFuture = streamChanges(onDataCallback);
|
||||
|
||||
await Future.delayed(const Duration(milliseconds: 50));
|
||||
|
||||
responseStreamController.add(
|
||||
utf8.encode(
|
||||
_createJsonLine(SyncEntityType.userDeleteV1.toString(), SyncUserDeleteV1(userId: "user1").toJson(), 'ack1'),
|
||||
),
|
||||
);
|
||||
|
||||
responseStreamController.addError(streamError);
|
||||
await expectLater(streamChangesFuture, throwsA(streamError));
|
||||
|
||||
expect(onDataCallCount, 0);
|
||||
verify(() => mockHttpClient.close()).called(1);
|
||||
});
|
||||
|
||||
test('streamChanges throws ApiException on non-200 status code', () async {
|
||||
when(() => mockStreamedResponse.statusCode).thenReturn(401);
|
||||
final errorBodyController = StreamController<List<int>>(sync: true);
|
||||
when(() => mockStreamedResponse.stream).thenAnswer((_) => http.ByteStream(errorBodyController.stream));
|
||||
|
||||
int onDataCallCount = 0;
|
||||
Future<void> onDataCallback(List<SyncEvent> events, Function() _, Function() __) async {
|
||||
onDataCallCount++;
|
||||
}
|
||||
|
||||
final future = streamChanges(onDataCallback);
|
||||
|
||||
errorBodyController.add(utf8.encode('{"error":"Unauthorized"}'));
|
||||
await errorBodyController.close();
|
||||
|
||||
await expectLater(
|
||||
future,
|
||||
throwsA(
|
||||
isA<ApiException>()
|
||||
.having((e) => e.code, 'code', 401)
|
||||
.having((e) => e.message, 'message', contains('Unauthorized')),
|
||||
),
|
||||
);
|
||||
|
||||
expect(onDataCallCount, 0);
|
||||
verify(() => mockHttpClient.close()).called(1);
|
||||
});
|
||||
}
|
||||
57
mobile/test/infrastructure/repository.mock.dart
Normal file
57
mobile/test/infrastructure/repository.mock.dart
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
import 'package:immich_mobile/infrastructure/repositories/backup.repository.dart';
|
||||
import 'package:immich_mobile/infrastructure/repositories/device_asset.repository.dart';
|
||||
import 'package:immich_mobile/infrastructure/repositories/local_album.repository.dart';
|
||||
import 'package:immich_mobile/infrastructure/repositories/local_asset.repository.dart';
|
||||
import 'package:immich_mobile/infrastructure/repositories/log.repository.dart';
|
||||
import 'package:immich_mobile/infrastructure/repositories/remote_album.repository.dart';
|
||||
import 'package:immich_mobile/infrastructure/repositories/remote_asset.repository.dart';
|
||||
import 'package:immich_mobile/infrastructure/repositories/storage.repository.dart';
|
||||
import 'package:immich_mobile/infrastructure/repositories/store.repository.dart';
|
||||
import 'package:immich_mobile/infrastructure/repositories/sync_api.repository.dart';
|
||||
import 'package:immich_mobile/infrastructure/repositories/sync_migration.repository.dart';
|
||||
import 'package:immich_mobile/infrastructure/repositories/sync_stream.repository.dart';
|
||||
import 'package:immich_mobile/infrastructure/repositories/trashed_local_asset.repository.dart';
|
||||
import 'package:immich_mobile/infrastructure/repositories/user.repository.dart';
|
||||
import 'package:immich_mobile/infrastructure/repositories/user_api.repository.dart';
|
||||
import 'package:immich_mobile/repositories/drift_album_api_repository.dart';
|
||||
import 'package:immich_mobile/repositories/upload.repository.dart';
|
||||
import 'package:mocktail/mocktail.dart';
|
||||
|
||||
class MockStoreRepository extends Mock implements IsarStoreRepository {}
|
||||
|
||||
class MockDriftStoreRepository extends Mock implements DriftStoreRepository {}
|
||||
|
||||
class MockLogRepository extends Mock implements LogRepository {}
|
||||
|
||||
class MockIsarUserRepository extends Mock implements IsarUserRepository {}
|
||||
|
||||
class MockDeviceAssetRepository extends Mock implements IsarDeviceAssetRepository {}
|
||||
|
||||
class MockSyncStreamRepository extends Mock implements SyncStreamRepository {}
|
||||
|
||||
class MockLocalAlbumRepository extends Mock implements DriftLocalAlbumRepository {}
|
||||
|
||||
class MockRemoteAlbumRepository extends Mock implements DriftRemoteAlbumRepository {}
|
||||
|
||||
class MockLocalAssetRepository extends Mock implements DriftLocalAssetRepository {}
|
||||
|
||||
class MockDriftLocalAssetRepository extends Mock implements DriftLocalAssetRepository {}
|
||||
|
||||
class MockRemoteAssetRepository extends Mock implements RemoteAssetRepository {}
|
||||
|
||||
class MockTrashedLocalAssetRepository extends Mock implements DriftTrashedLocalAssetRepository {}
|
||||
|
||||
class MockStorageRepository extends Mock implements StorageRepository {}
|
||||
|
||||
class MockDriftBackupRepository extends Mock implements DriftBackupRepository {}
|
||||
|
||||
class MockUploadRepository extends Mock implements UploadRepository {}
|
||||
|
||||
class MockSyncMigrationRepository extends Mock implements SyncMigrationRepository {}
|
||||
|
||||
// API Repos
|
||||
class MockUserApiRepository extends Mock implements UserApiRepository {}
|
||||
|
||||
class MockSyncApiRepository extends Mock implements SyncApiRepository {}
|
||||
|
||||
class MockDriftAlbumApiRepository extends Mock implements DriftAlbumApiRepository {}
|
||||
58
mobile/test/mock_http_override.dart
Normal file
58
mobile/test/mock_http_override.dart
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
import 'dart:io';
|
||||
|
||||
import 'package:immich_mobile/widgets/common/transparent_image.dart';
|
||||
import 'package:mocktail/mocktail.dart';
|
||||
|
||||
/// Mocks the http client to always return a transparent image for all the requests. Only useful in widget
|
||||
/// tests to return network images
|
||||
class MockHttpOverrides extends HttpOverrides {
|
||||
@override
|
||||
HttpClient createHttpClient(SecurityContext? context) {
|
||||
final client = _MockHttpClient();
|
||||
final request = _MockHttpClientRequest();
|
||||
final response = _MockHttpClientResponse();
|
||||
final headers = _MockHttpHeaders();
|
||||
|
||||
// Client mocks
|
||||
when(() => client.autoUncompress).thenReturn(true);
|
||||
|
||||
// Request mocks
|
||||
when(() => request.headers).thenAnswer((_) => headers);
|
||||
when(() => request.close()).thenAnswer((_) => Future<HttpClientResponse>.value(response));
|
||||
|
||||
// Response mocks
|
||||
when(() => response.statusCode).thenReturn(HttpStatus.ok);
|
||||
when(() => response.compressionState).thenReturn(HttpClientResponseCompressionState.decompressed);
|
||||
when(() => response.contentLength).thenAnswer((_) => kTransparentImage.length);
|
||||
when(
|
||||
() => response.listen(
|
||||
captureAny(),
|
||||
cancelOnError: captureAny(named: 'cancelOnError'),
|
||||
onDone: captureAny(named: 'onDone'),
|
||||
onError: captureAny(named: 'onError'),
|
||||
),
|
||||
).thenAnswer((invocation) {
|
||||
final onData = invocation.positionalArguments[0] as void Function(List<int>);
|
||||
|
||||
final onDone = invocation.namedArguments[#onDone] as void Function();
|
||||
|
||||
final onError = invocation.namedArguments[#onError] as void Function(Object, [StackTrace]);
|
||||
|
||||
final cancelOnError = invocation.namedArguments[#cancelOnError] as bool;
|
||||
|
||||
return Stream<List<int>>.fromIterable([
|
||||
kTransparentImage.toList(),
|
||||
]).listen(onData, onDone: onDone, onError: onError, cancelOnError: cancelOnError);
|
||||
});
|
||||
|
||||
return client;
|
||||
}
|
||||
}
|
||||
|
||||
class _MockHttpClient extends Mock implements HttpClient {}
|
||||
|
||||
class _MockHttpClientRequest extends Mock implements HttpClientRequest {}
|
||||
|
||||
class _MockHttpClientResponse extends Mock implements HttpClientResponse {}
|
||||
|
||||
class _MockHttpHeaders extends Mock implements HttpHeaders {}
|
||||
4
mobile/test/mocks/asset_entity.mock.dart
Normal file
4
mobile/test/mocks/asset_entity.mock.dart
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
import 'package:mocktail/mocktail.dart';
|
||||
import 'package:photo_manager/photo_manager.dart';
|
||||
|
||||
class MockAssetEntity extends Mock implements AssetEntity {}
|
||||
175
mobile/test/modules/activity/activities_page_test.dart
Normal file
175
mobile/test/modules/activity/activities_page_test.dart
Normal file
|
|
@ -0,0 +1,175 @@
|
|||
@Skip('currently failing due to mock HTTP client to download ISAR binaries')
|
||||
@Tags(['widget'])
|
||||
library;
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:immich_mobile/domain/models/store.model.dart';
|
||||
import 'package:immich_mobile/domain/services/store.service.dart';
|
||||
import 'package:immich_mobile/entities/album.entity.dart';
|
||||
import 'package:immich_mobile/entities/asset.entity.dart';
|
||||
import 'package:immich_mobile/entities/store.entity.dart';
|
||||
import 'package:immich_mobile/infrastructure/entities/user.entity.dart';
|
||||
import 'package:immich_mobile/infrastructure/repositories/store.repository.dart';
|
||||
import 'package:immich_mobile/models/activities/activity.model.dart';
|
||||
import 'package:immich_mobile/pages/common/activities.page.dart';
|
||||
import 'package:immich_mobile/providers/activity.provider.dart';
|
||||
import 'package:immich_mobile/providers/album/current_album.provider.dart';
|
||||
import 'package:immich_mobile/providers/asset_viewer/current_asset.provider.dart';
|
||||
import 'package:immich_mobile/providers/user.provider.dart';
|
||||
import 'package:immich_mobile/widgets/activities/activity_text_field.dart';
|
||||
import 'package:immich_mobile/widgets/activities/dismissible_activity.dart';
|
||||
import 'package:isar/isar.dart';
|
||||
import 'package:mocktail/mocktail.dart';
|
||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
|
||||
import '../../fixtures/album.stub.dart';
|
||||
import '../../fixtures/asset.stub.dart';
|
||||
import '../../fixtures/user.stub.dart';
|
||||
import '../../test_utils.dart';
|
||||
import '../../widget_tester_extensions.dart';
|
||||
import '../album/album_mocks.dart';
|
||||
import '../asset_viewer/asset_viewer_mocks.dart';
|
||||
import '../shared/shared_mocks.dart';
|
||||
import 'activity_mocks.dart';
|
||||
|
||||
final _activities = [
|
||||
Activity(
|
||||
id: '1',
|
||||
createdAt: DateTime(100),
|
||||
type: ActivityType.comment,
|
||||
comment: 'First Activity',
|
||||
assetId: 'asset-2',
|
||||
user: UserStub.admin,
|
||||
),
|
||||
Activity(
|
||||
id: '2',
|
||||
createdAt: DateTime(200),
|
||||
type: ActivityType.comment,
|
||||
comment: 'Second Activity',
|
||||
user: UserStub.user1,
|
||||
),
|
||||
Activity(id: '3', createdAt: DateTime(300), type: ActivityType.like, assetId: 'asset-1', user: UserStub.user2),
|
||||
Activity(id: '4', createdAt: DateTime(400), type: ActivityType.like, user: UserStub.user1),
|
||||
];
|
||||
|
||||
void main() {
|
||||
late MockAlbumActivity activityMock;
|
||||
late MockCurrentAlbumProvider mockCurrentAlbumProvider;
|
||||
late MockCurrentAssetProvider mockCurrentAssetProvider;
|
||||
late List<Override> overrides;
|
||||
late Isar db;
|
||||
|
||||
setUpAll(() async {
|
||||
TestUtils.init();
|
||||
db = await TestUtils.initIsar();
|
||||
await StoreService.init(storeRepository: IsarStoreRepository(db));
|
||||
await Store.put(StoreKey.currentUser, UserStub.admin);
|
||||
await Store.put(StoreKey.serverEndpoint, '');
|
||||
await Store.put(StoreKey.accessToken, '');
|
||||
});
|
||||
|
||||
setUp(() async {
|
||||
mockCurrentAlbumProvider = MockCurrentAlbumProvider(AlbumStub.twoAsset);
|
||||
mockCurrentAssetProvider = MockCurrentAssetProvider(AssetStub.image1);
|
||||
activityMock = MockAlbumActivity(_activities);
|
||||
overrides = [
|
||||
albumActivityProvider(AlbumStub.twoAsset.remoteId!, AssetStub.image1.remoteId!).overrideWith(() => activityMock),
|
||||
currentAlbumProvider.overrideWith(() => mockCurrentAlbumProvider),
|
||||
currentAssetProvider.overrideWith(() => mockCurrentAssetProvider),
|
||||
];
|
||||
|
||||
await db.writeTxn(() async {
|
||||
await db.clear();
|
||||
// Save all assets
|
||||
await db.users.put(User.fromDto(UserStub.admin));
|
||||
await db.assets.putAll([AssetStub.image1, AssetStub.image2]);
|
||||
await db.albums.put(AlbumStub.twoAsset);
|
||||
await AlbumStub.twoAsset.owner.save();
|
||||
await AlbumStub.twoAsset.assets.save();
|
||||
});
|
||||
expect(db.albums.countSync(), 1);
|
||||
expect(db.assets.countSync(), 2);
|
||||
expect(db.users.countSync(), 1);
|
||||
});
|
||||
|
||||
group("App bar", () {
|
||||
testWidgets("No title when currentAsset != null", (tester) async {
|
||||
await tester.pumpConsumerWidget(const ActivitiesPage(), overrides: overrides);
|
||||
|
||||
final listTile = tester.widget<AppBar>(find.byType(AppBar));
|
||||
expect(listTile.title, isNull);
|
||||
});
|
||||
|
||||
testWidgets("Album name as title when currentAsset == null", (tester) async {
|
||||
await tester.pumpConsumerWidget(const ActivitiesPage(), overrides: overrides);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
mockCurrentAssetProvider.state = null;
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.text(AlbumStub.twoAsset.name), findsOneWidget);
|
||||
final listTile = tester.widget<AppBar>(find.byType(AppBar));
|
||||
expect(listTile.title, isNotNull);
|
||||
});
|
||||
});
|
||||
|
||||
group("Body", () {
|
||||
testWidgets("Contains a stack with Activity List and Activity Input", (tester) async {
|
||||
await tester.pumpConsumerWidget(const ActivitiesPage(), overrides: overrides);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.descendant(of: find.byType(Stack), matching: find.byType(ActivityTextField)), findsOneWidget);
|
||||
|
||||
expect(find.descendant(of: find.byType(Stack), matching: find.byType(ListView)), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets("List Contains all dismissible activities", (tester) async {
|
||||
await tester.pumpConsumerWidget(const ActivitiesPage(), overrides: overrides);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
final listFinder = find.descendant(of: find.byType(Stack), matching: find.byType(ListView));
|
||||
final listChildren = find.descendant(of: listFinder, matching: find.byType(DismissibleActivity));
|
||||
expect(listChildren, findsNWidgets(_activities.length));
|
||||
});
|
||||
|
||||
testWidgets("Submitting text input adds a comment with the text", (tester) async {
|
||||
await tester.pumpConsumerWidget(const ActivitiesPage(), overrides: overrides);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
when(() => activityMock.addComment(any())).thenAnswer((_) => Future.value());
|
||||
|
||||
final textField = find.byType(TextField);
|
||||
await tester.enterText(textField, 'Test comment');
|
||||
await tester.testTextInput.receiveAction(TextInputAction.done);
|
||||
|
||||
verify(() => activityMock.addComment('Test comment'));
|
||||
});
|
||||
|
||||
testWidgets("Owner can remove all activities", (tester) async {
|
||||
await tester.pumpConsumerWidget(const ActivitiesPage(), overrides: overrides);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
final deletableActivityFinder = find.byWidgetPredicate(
|
||||
(widget) => widget is DismissibleActivity && widget.onDismiss != null,
|
||||
);
|
||||
expect(deletableActivityFinder, findsNWidgets(_activities.length));
|
||||
});
|
||||
|
||||
testWidgets("Non-Owner can remove only their activities", (tester) async {
|
||||
final mockCurrentUser = MockCurrentUserProvider();
|
||||
|
||||
await tester.pumpConsumerWidget(
|
||||
const ActivitiesPage(),
|
||||
overrides: [...overrides, currentUserProvider.overrideWith((ref) => mockCurrentUser)],
|
||||
);
|
||||
mockCurrentUser.state = UserStub.user1;
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
final deletableActivityFinder = find.byWidgetPredicate(
|
||||
(widget) => widget is DismissibleActivity && widget.onDismiss != null,
|
||||
);
|
||||
expect(deletableActivityFinder, findsNWidgets(_activities.where((a) => a.user == UserStub.user1).length));
|
||||
});
|
||||
});
|
||||
}
|
||||
19
mobile/test/modules/activity/activity_mocks.dart
Normal file
19
mobile/test/modules/activity/activity_mocks.dart
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
import 'package:immich_mobile/models/activities/activity.model.dart';
|
||||
import 'package:immich_mobile/providers/activity.provider.dart';
|
||||
import 'package:immich_mobile/providers/activity_statistics.provider.dart';
|
||||
import 'package:immich_mobile/services/activity.service.dart';
|
||||
import 'package:mocktail/mocktail.dart';
|
||||
|
||||
class ActivityServiceMock extends Mock implements ActivityService {}
|
||||
|
||||
class MockAlbumActivity extends AlbumActivityInternal with Mock implements AlbumActivity {
|
||||
List<Activity>? initActivities;
|
||||
MockAlbumActivity([this.initActivities]);
|
||||
|
||||
@override
|
||||
Future<List<Activity>> build(String albumId, [String? assetId]) async {
|
||||
return initActivities ?? [];
|
||||
}
|
||||
}
|
||||
|
||||
class ActivityStatisticsMock extends ActivityStatisticsInternal with Mock implements ActivityStatistics {}
|
||||
331
mobile/test/modules/activity/activity_provider_test.dart
Normal file
331
mobile/test/modules/activity/activity_provider_test.dart
Normal file
|
|
@ -0,0 +1,331 @@
|
|||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
import 'package:immich_mobile/models/activities/activity.model.dart';
|
||||
import 'package:immich_mobile/providers/activity.provider.dart';
|
||||
import 'package:immich_mobile/providers/activity_service.provider.dart';
|
||||
import 'package:immich_mobile/providers/activity_statistics.provider.dart';
|
||||
import 'package:mocktail/mocktail.dart';
|
||||
|
||||
import '../../fixtures/user.stub.dart';
|
||||
import '../../test_utils.dart';
|
||||
import 'activity_mocks.dart';
|
||||
|
||||
final _activities = [
|
||||
Activity(
|
||||
id: '1',
|
||||
createdAt: DateTime(100),
|
||||
type: ActivityType.comment,
|
||||
comment: 'First Activity',
|
||||
assetId: 'asset-2',
|
||||
user: UserStub.admin,
|
||||
),
|
||||
Activity(
|
||||
id: '2',
|
||||
createdAt: DateTime(200),
|
||||
type: ActivityType.comment,
|
||||
comment: 'Second Activity',
|
||||
user: UserStub.user1,
|
||||
),
|
||||
Activity(id: '3', createdAt: DateTime(300), type: ActivityType.like, assetId: 'asset-1', user: UserStub.admin),
|
||||
Activity(id: '4', createdAt: DateTime(400), type: ActivityType.like, user: UserStub.user1),
|
||||
];
|
||||
|
||||
void main() {
|
||||
late ActivityServiceMock activityMock;
|
||||
late ActivityStatisticsMock activityStatisticsMock;
|
||||
late ActivityStatisticsMock albumActivityStatisticsMock;
|
||||
late ProviderContainer container;
|
||||
late AlbumActivityProvider provider;
|
||||
late ListenerMock<AsyncValue<List<Activity>>> listener;
|
||||
|
||||
setUpAll(() {
|
||||
registerFallbackValue(AsyncData<List<Activity>>([..._activities]));
|
||||
});
|
||||
|
||||
setUp(() async {
|
||||
activityMock = ActivityServiceMock();
|
||||
activityStatisticsMock = ActivityStatisticsMock();
|
||||
albumActivityStatisticsMock = ActivityStatisticsMock();
|
||||
|
||||
container = TestUtils.createContainer(
|
||||
overrides: [
|
||||
activityServiceProvider.overrideWith((ref) => activityMock),
|
||||
activityStatisticsProvider('test-album', 'test-asset').overrideWith(() => activityStatisticsMock),
|
||||
activityStatisticsProvider('test-album').overrideWith(() => albumActivityStatisticsMock),
|
||||
],
|
||||
);
|
||||
|
||||
// Mock values
|
||||
when(() => activityStatisticsMock.build(any(), any())).thenReturn(0);
|
||||
when(() => albumActivityStatisticsMock.build(any())).thenReturn(0);
|
||||
when(
|
||||
() => activityMock.getAllActivities('test-album', assetId: 'test-asset'),
|
||||
).thenAnswer((_) async => [..._activities]);
|
||||
when(() => activityMock.getAllActivities('test-album')).thenAnswer((_) async => [..._activities]);
|
||||
|
||||
// Init and wait for providers future to complete
|
||||
provider = albumActivityProvider('test-album', 'test-asset');
|
||||
listener = ListenerMock();
|
||||
container.listen(provider, listener.call, fireImmediately: true);
|
||||
|
||||
await container.read(provider.future);
|
||||
});
|
||||
|
||||
test('Returns a list of activity', () async {
|
||||
verifyInOrder([
|
||||
() => listener.call(null, const AsyncLoading()),
|
||||
() => listener.call(
|
||||
const AsyncLoading(),
|
||||
any(
|
||||
that: allOf([
|
||||
isA<AsyncData<List<Activity>>>(),
|
||||
predicate((AsyncData<List<Activity>> ad) => ad.requireValue.every((e) => _activities.contains(e))),
|
||||
]),
|
||||
),
|
||||
),
|
||||
]);
|
||||
|
||||
verifyNoMoreInteractions(listener);
|
||||
});
|
||||
|
||||
group('addLike()', () {
|
||||
test('Like successfully added', () async {
|
||||
final like = Activity(id: '5', createdAt: DateTime(2023), type: ActivityType.like, user: UserStub.admin);
|
||||
|
||||
when(
|
||||
() => activityMock.addActivity('test-album', ActivityType.like, assetId: 'test-asset'),
|
||||
).thenAnswer((_) async => AsyncData(like));
|
||||
|
||||
final albumProvider = albumActivityProvider('test-album');
|
||||
container.read(albumProvider.notifier);
|
||||
await container.read(albumProvider.future);
|
||||
|
||||
await container.read(provider.notifier).addLike();
|
||||
|
||||
verify(() => activityMock.addActivity('test-album', ActivityType.like, assetId: 'test-asset'));
|
||||
|
||||
final activities = await container.read(provider.future);
|
||||
expect(activities, hasLength(5));
|
||||
expect(activities, contains(like));
|
||||
|
||||
// Never bump activity count for new likes
|
||||
verifyNever(() => activityStatisticsMock.addActivity());
|
||||
verifyNever(() => albumActivityStatisticsMock.addActivity());
|
||||
|
||||
final albumActivities = container.read(albumProvider).requireValue;
|
||||
expect(albumActivities, hasLength(5));
|
||||
expect(albumActivities, contains(like));
|
||||
});
|
||||
|
||||
test('Like failed', () async {
|
||||
final like = Activity(id: '5', createdAt: DateTime(2023), type: ActivityType.like, user: UserStub.admin);
|
||||
when(
|
||||
() => activityMock.addActivity('test-album', ActivityType.like, assetId: 'test-asset'),
|
||||
).thenAnswer((_) async => AsyncError(Exception('Mock'), StackTrace.current));
|
||||
|
||||
final albumProvider = albumActivityProvider('test-album');
|
||||
container.read(albumProvider.notifier);
|
||||
await container.read(albumProvider.future);
|
||||
|
||||
await container.read(provider.notifier).addLike();
|
||||
|
||||
verify(() => activityMock.addActivity('test-album', ActivityType.like, assetId: 'test-asset'));
|
||||
|
||||
final activities = await container.read(provider.future);
|
||||
expect(activities, hasLength(4));
|
||||
expect(activities, isNot(contains(like)));
|
||||
|
||||
verifyNever(() => albumActivityStatisticsMock.addActivity());
|
||||
|
||||
final albumActivities = container.read(albumProvider).requireValue;
|
||||
expect(albumActivities, hasLength(4));
|
||||
expect(albumActivities, isNot(contains(like)));
|
||||
});
|
||||
});
|
||||
|
||||
group('removeActivity()', () {
|
||||
test('Like successfully removed', () async {
|
||||
when(() => activityMock.removeActivity('3')).thenAnswer((_) async => true);
|
||||
|
||||
await container.read(provider.notifier).removeActivity('3');
|
||||
|
||||
verify(() => activityMock.removeActivity('3'));
|
||||
|
||||
final activities = await container.read(provider.future);
|
||||
expect(activities, hasLength(3));
|
||||
expect(activities, isNot(anyElement(predicate((Activity a) => a.id == '3'))));
|
||||
|
||||
verifyNever(() => activityStatisticsMock.removeActivity());
|
||||
verifyNever(() => albumActivityStatisticsMock.removeActivity());
|
||||
});
|
||||
|
||||
test('Remove Like failed', () async {
|
||||
when(() => activityMock.removeActivity('3')).thenAnswer((_) async => false);
|
||||
|
||||
await container.read(provider.notifier).removeActivity('3');
|
||||
|
||||
final activities = await container.read(provider.future);
|
||||
expect(activities, hasLength(4));
|
||||
expect(activities, anyElement(predicate((Activity a) => a.id == '3')));
|
||||
|
||||
verifyNever(() => activityStatisticsMock.removeActivity());
|
||||
verifyNever(() => albumActivityStatisticsMock.removeActivity());
|
||||
});
|
||||
|
||||
test('Comment successfully removed', () async {
|
||||
when(() => activityMock.removeActivity('1')).thenAnswer((_) async => true);
|
||||
|
||||
await container.read(provider.notifier).removeActivity('1');
|
||||
|
||||
final activities = await container.read(provider.future);
|
||||
expect(activities, isNot(anyElement(predicate((Activity a) => a.id == '1'))));
|
||||
|
||||
verify(() => activityStatisticsMock.removeActivity());
|
||||
verify(() => albumActivityStatisticsMock.removeActivity());
|
||||
});
|
||||
|
||||
test('Removes activity from album state when asset scoped', () async {
|
||||
when(() => activityMock.removeActivity('3')).thenAnswer((_) async => true);
|
||||
when(() => activityMock.getAllActivities('test-album')).thenAnswer((_) async => [..._activities]);
|
||||
|
||||
final albumProvider = albumActivityProvider('test-album');
|
||||
container.read(albumProvider.notifier);
|
||||
await container.read(albumProvider.future);
|
||||
|
||||
await container.read(provider.notifier).removeActivity('3');
|
||||
|
||||
final assetActivities = container.read(provider).requireValue;
|
||||
final albumActivities = container.read(albumProvider).requireValue;
|
||||
|
||||
expect(assetActivities, hasLength(3));
|
||||
expect(assetActivities, isNot(anyElement(predicate((Activity a) => a.id == '3'))));
|
||||
|
||||
expect(albumActivities, hasLength(3));
|
||||
expect(albumActivities, isNot(anyElement(predicate((Activity a) => a.id == '3'))));
|
||||
|
||||
verify(() => activityMock.removeActivity('3'));
|
||||
verifyNever(() => activityStatisticsMock.removeActivity());
|
||||
verifyNever(() => albumActivityStatisticsMock.removeActivity());
|
||||
});
|
||||
});
|
||||
|
||||
group('addComment()', () {
|
||||
test('Comment successfully added', () async {
|
||||
final comment = Activity(
|
||||
id: '5',
|
||||
createdAt: DateTime(2023),
|
||||
type: ActivityType.comment,
|
||||
user: UserStub.admin,
|
||||
comment: 'Test-Comment',
|
||||
assetId: 'test-asset',
|
||||
);
|
||||
|
||||
final albumProvider = albumActivityProvider('test-album');
|
||||
container.read(albumProvider.notifier);
|
||||
await container.read(albumProvider.future);
|
||||
|
||||
when(
|
||||
() => activityMock.addActivity(
|
||||
'test-album',
|
||||
ActivityType.comment,
|
||||
assetId: 'test-asset',
|
||||
comment: 'Test-Comment',
|
||||
),
|
||||
).thenAnswer((_) async => AsyncData(comment));
|
||||
when(() => activityStatisticsMock.build('test-album', 'test-asset')).thenReturn(4);
|
||||
when(() => albumActivityStatisticsMock.build('test-album')).thenReturn(2);
|
||||
|
||||
await container.read(provider.notifier).addComment('Test-Comment');
|
||||
|
||||
verify(
|
||||
() => activityMock.addActivity(
|
||||
'test-album',
|
||||
ActivityType.comment,
|
||||
assetId: 'test-asset',
|
||||
comment: 'Test-Comment',
|
||||
),
|
||||
);
|
||||
|
||||
final activities = await container.read(provider.future);
|
||||
expect(activities, hasLength(5));
|
||||
expect(activities, contains(comment));
|
||||
|
||||
verify(() => activityStatisticsMock.addActivity());
|
||||
verify(() => albumActivityStatisticsMock.addActivity());
|
||||
|
||||
final albumActivities = container.read(albumProvider).requireValue;
|
||||
expect(albumActivities, hasLength(5));
|
||||
expect(albumActivities, contains(comment));
|
||||
});
|
||||
|
||||
test('Comment successfully added without assetId', () async {
|
||||
final comment = Activity(
|
||||
id: '5',
|
||||
createdAt: DateTime(2023),
|
||||
type: ActivityType.comment,
|
||||
user: UserStub.admin,
|
||||
assetId: 'test-asset',
|
||||
comment: 'Test-Comment',
|
||||
);
|
||||
|
||||
when(
|
||||
() => activityMock.addActivity('test-album', ActivityType.comment, comment: 'Test-Comment'),
|
||||
).thenAnswer((_) async => AsyncData(comment));
|
||||
when(() => albumActivityStatisticsMock.build('test-album')).thenReturn(2);
|
||||
when(() => activityMock.getAllActivities('test-album')).thenAnswer((_) async => [..._activities]);
|
||||
|
||||
final albumProvider = albumActivityProvider('test-album');
|
||||
container.read(albumProvider.notifier);
|
||||
await container.read(albumProvider.future);
|
||||
await container.read(albumProvider.notifier).addComment('Test-Comment');
|
||||
|
||||
verify(
|
||||
() => activityMock.addActivity('test-album', ActivityType.comment, assetId: null, comment: 'Test-Comment'),
|
||||
);
|
||||
|
||||
final activities = await container.read(albumProvider.future);
|
||||
expect(activities, hasLength(5));
|
||||
expect(activities, contains(comment));
|
||||
|
||||
verifyNever(() => activityStatisticsMock.addActivity());
|
||||
verify(() => albumActivityStatisticsMock.addActivity());
|
||||
});
|
||||
|
||||
test('Comment failed', () async {
|
||||
final comment = Activity(
|
||||
id: '5',
|
||||
createdAt: DateTime(2023),
|
||||
type: ActivityType.comment,
|
||||
user: UserStub.admin,
|
||||
comment: 'Test-Comment',
|
||||
assetId: 'test-asset',
|
||||
);
|
||||
|
||||
when(
|
||||
() => activityMock.addActivity(
|
||||
'test-album',
|
||||
ActivityType.comment,
|
||||
assetId: 'test-asset',
|
||||
comment: 'Test-Comment',
|
||||
),
|
||||
).thenAnswer((_) async => AsyncError(Exception('Error'), StackTrace.current));
|
||||
|
||||
final albumProvider = albumActivityProvider('test-album');
|
||||
container.read(albumProvider.notifier);
|
||||
await container.read(albumProvider.future);
|
||||
|
||||
await container.read(provider.notifier).addComment('Test-Comment');
|
||||
|
||||
final activities = await container.read(provider.future);
|
||||
expect(activities, hasLength(4));
|
||||
expect(activities, isNot(contains(comment)));
|
||||
|
||||
verifyNever(() => activityStatisticsMock.addActivity());
|
||||
verifyNever(() => albumActivityStatisticsMock.addActivity());
|
||||
|
||||
final albumActivities = container.read(albumProvider).requireValue;
|
||||
expect(albumActivities, hasLength(4));
|
||||
expect(albumActivities, isNot(contains(comment)));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
|
@ -0,0 +1,71 @@
|
|||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
import 'package:immich_mobile/models/activities/activity.model.dart';
|
||||
import 'package:immich_mobile/providers/activity_service.provider.dart';
|
||||
import 'package:immich_mobile/providers/activity_statistics.provider.dart';
|
||||
import 'package:mocktail/mocktail.dart';
|
||||
|
||||
import '../../test_utils.dart';
|
||||
import 'activity_mocks.dart';
|
||||
|
||||
void main() {
|
||||
late ActivityServiceMock activityMock;
|
||||
late ProviderContainer container;
|
||||
late ListenerMock<int> listener;
|
||||
|
||||
setUp(() async {
|
||||
activityMock = ActivityServiceMock();
|
||||
container = TestUtils.createContainer(overrides: [activityServiceProvider.overrideWith((ref) => activityMock)]);
|
||||
listener = ListenerMock();
|
||||
});
|
||||
|
||||
test('Returns the proper count family', () async {
|
||||
when(
|
||||
() => activityMock.getStatistics('test-album', assetId: 'test-asset'),
|
||||
).thenAnswer((_) async => const ActivityStats(comments: 5));
|
||||
|
||||
// Read here to make the getStatistics call
|
||||
container.read(activityStatisticsProvider('test-album', 'test-asset'));
|
||||
|
||||
container.listen(activityStatisticsProvider('test-album', 'test-asset'), listener.call, fireImmediately: true);
|
||||
|
||||
// Sleep for the getStatistics future to resolve
|
||||
await Future.delayed(const Duration(milliseconds: 1));
|
||||
|
||||
verifyInOrder([() => listener.call(null, 0), () => listener.call(0, 5)]);
|
||||
|
||||
verifyNoMoreInteractions(listener);
|
||||
});
|
||||
|
||||
test('Adds activity', () async {
|
||||
when(() => activityMock.getStatistics('test-album')).thenAnswer((_) async => const ActivityStats(comments: 10));
|
||||
|
||||
final provider = activityStatisticsProvider('test-album');
|
||||
container.listen(provider, listener.call, fireImmediately: true);
|
||||
|
||||
// Sleep for the getStatistics future to resolve
|
||||
await Future.delayed(const Duration(milliseconds: 1));
|
||||
|
||||
container.read(provider.notifier).addActivity();
|
||||
container.read(provider.notifier).addActivity();
|
||||
|
||||
expect(container.read(provider), 12);
|
||||
});
|
||||
|
||||
test('Removes activity', () async {
|
||||
when(
|
||||
() => activityMock.getStatistics('new-album', assetId: 'test-asset'),
|
||||
).thenAnswer((_) async => const ActivityStats(comments: 10));
|
||||
|
||||
final provider = activityStatisticsProvider('new-album', 'test-asset');
|
||||
container.listen(provider, listener.call, fireImmediately: true);
|
||||
|
||||
// Sleep for the getStatistics future to resolve
|
||||
await Future.delayed(const Duration(milliseconds: 1));
|
||||
|
||||
container.read(provider.notifier).removeActivity();
|
||||
container.read(provider.notifier).removeActivity();
|
||||
|
||||
expect(container.read(provider), 8);
|
||||
});
|
||||
}
|
||||
149
mobile/test/modules/activity/activity_text_field_test.dart
Normal file
149
mobile/test/modules/activity/activity_text_field_test.dart
Normal file
|
|
@ -0,0 +1,149 @@
|
|||
@Skip('currently failing due to mock HTTP client to download ISAR binaries')
|
||||
@Tags(['widget'])
|
||||
library;
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:immich_mobile/domain/models/store.model.dart';
|
||||
import 'package:immich_mobile/domain/services/store.service.dart';
|
||||
import 'package:immich_mobile/entities/store.entity.dart';
|
||||
import 'package:immich_mobile/infrastructure/repositories/store.repository.dart';
|
||||
import 'package:immich_mobile/providers/activity.provider.dart';
|
||||
import 'package:immich_mobile/providers/album/current_album.provider.dart';
|
||||
import 'package:immich_mobile/providers/user.provider.dart';
|
||||
import 'package:immich_mobile/widgets/activities/activity_text_field.dart';
|
||||
import 'package:immich_mobile/widgets/common/user_circle_avatar.dart';
|
||||
import 'package:isar/isar.dart';
|
||||
import 'package:mocktail/mocktail.dart';
|
||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
|
||||
import '../../fixtures/album.stub.dart';
|
||||
import '../../fixtures/user.stub.dart';
|
||||
import '../../test_utils.dart';
|
||||
import '../../widget_tester_extensions.dart';
|
||||
import '../album/album_mocks.dart';
|
||||
import '../shared/shared_mocks.dart';
|
||||
import 'activity_mocks.dart';
|
||||
|
||||
void main() {
|
||||
late Isar db;
|
||||
late MockCurrentAlbumProvider mockCurrentAlbumProvider;
|
||||
late MockAlbumActivity activityMock;
|
||||
late List<Override> overrides;
|
||||
|
||||
setUpAll(() async {
|
||||
TestUtils.init();
|
||||
db = await TestUtils.initIsar();
|
||||
await StoreService.init(storeRepository: IsarStoreRepository(db));
|
||||
await Store.put(StoreKey.currentUser, UserStub.admin);
|
||||
await Store.put(StoreKey.serverEndpoint, '');
|
||||
});
|
||||
|
||||
setUp(() {
|
||||
mockCurrentAlbumProvider = MockCurrentAlbumProvider(AlbumStub.twoAsset);
|
||||
activityMock = MockAlbumActivity();
|
||||
overrides = [
|
||||
currentAlbumProvider.overrideWith(() => mockCurrentAlbumProvider),
|
||||
albumActivityProvider(AlbumStub.twoAsset.remoteId!).overrideWith(() => activityMock),
|
||||
];
|
||||
});
|
||||
|
||||
testWidgets('Returns an Input text field', (tester) async {
|
||||
await tester.pumpConsumerWidget(ActivityTextField(onSubmit: (_) {}), overrides: overrides);
|
||||
|
||||
expect(find.byType(TextField), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('No UserCircleAvatar when user == null', (tester) async {
|
||||
final userProvider = MockCurrentUserProvider();
|
||||
|
||||
await tester.pumpConsumerWidget(
|
||||
ActivityTextField(onSubmit: (_) {}),
|
||||
overrides: [currentUserProvider.overrideWith((ref) => userProvider), ...overrides],
|
||||
);
|
||||
|
||||
expect(find.byType(UserCircleAvatar), findsNothing);
|
||||
});
|
||||
|
||||
testWidgets('UserCircleAvatar displayed when user != null', (tester) async {
|
||||
await tester.pumpConsumerWidget(ActivityTextField(onSubmit: (_) {}), overrides: overrides);
|
||||
|
||||
expect(find.byType(UserCircleAvatar), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('Filled icon if likedId != null', (tester) async {
|
||||
await tester.pumpConsumerWidget(
|
||||
ActivityTextField(onSubmit: (_) {}, likeId: '1'),
|
||||
overrides: overrides,
|
||||
);
|
||||
|
||||
expect(find.widgetWithIcon(IconButton, Icons.thumb_up), findsOneWidget);
|
||||
expect(find.widgetWithIcon(IconButton, Icons.thumb_up_off_alt), findsNothing);
|
||||
});
|
||||
|
||||
testWidgets('Bordered icon if likedId == null', (tester) async {
|
||||
await tester.pumpConsumerWidget(ActivityTextField(onSubmit: (_) {}), overrides: overrides);
|
||||
|
||||
expect(find.widgetWithIcon(IconButton, Icons.thumb_up_off_alt), findsOneWidget);
|
||||
expect(find.widgetWithIcon(IconButton, Icons.thumb_up), findsNothing);
|
||||
});
|
||||
|
||||
testWidgets('Adds new like', (tester) async {
|
||||
await tester.pumpConsumerWidget(ActivityTextField(onSubmit: (_) {}), overrides: overrides);
|
||||
|
||||
when(() => activityMock.addLike()).thenAnswer((_) => Future.value());
|
||||
|
||||
final suffixIcon = find.byType(IconButton);
|
||||
await tester.tap(suffixIcon);
|
||||
|
||||
verify(() => activityMock.addLike());
|
||||
});
|
||||
|
||||
testWidgets('Removes like if already liked', (tester) async {
|
||||
await tester.pumpConsumerWidget(
|
||||
ActivityTextField(onSubmit: (_) {}, likeId: 'test-suffix'),
|
||||
overrides: overrides,
|
||||
);
|
||||
|
||||
when(() => activityMock.removeActivity(any())).thenAnswer((_) => Future.value());
|
||||
|
||||
final suffixIcon = find.byType(IconButton);
|
||||
await tester.tap(suffixIcon);
|
||||
|
||||
verify(() => activityMock.removeActivity('test-suffix'));
|
||||
});
|
||||
|
||||
testWidgets('Passes text entered to onSubmit on submit', (tester) async {
|
||||
String? receivedText;
|
||||
|
||||
await tester.pumpConsumerWidget(
|
||||
ActivityTextField(onSubmit: (text) => receivedText = text, likeId: 'test-suffix'),
|
||||
overrides: overrides,
|
||||
);
|
||||
|
||||
final textField = find.byType(TextField);
|
||||
await tester.enterText(textField, 'This is a test comment');
|
||||
await tester.testTextInput.receiveAction(TextInputAction.done);
|
||||
expect(receivedText, 'This is a test comment');
|
||||
});
|
||||
|
||||
testWidgets('Input disabled when isEnabled false', (tester) async {
|
||||
String? receviedText;
|
||||
|
||||
await tester.pumpConsumerWidget(
|
||||
ActivityTextField(onSubmit: (text) => receviedText = text, isEnabled: false, likeId: 'test-suffix'),
|
||||
overrides: overrides,
|
||||
);
|
||||
|
||||
final suffixIcon = find.byType(IconButton);
|
||||
await tester.tap(suffixIcon, warnIfMissed: false);
|
||||
|
||||
final textField = find.byType(TextField);
|
||||
await tester.enterText(textField, 'This is a test comment');
|
||||
await tester.testTextInput.receiveAction(TextInputAction.done);
|
||||
|
||||
expect(receviedText, isNull);
|
||||
verifyNever(() => activityMock.addLike());
|
||||
verifyNever(() => activityMock.removeActivity(any()));
|
||||
});
|
||||
}
|
||||
165
mobile/test/modules/activity/activity_tile_test.dart
Normal file
165
mobile/test/modules/activity/activity_tile_test.dart
Normal file
|
|
@ -0,0 +1,165 @@
|
|||
@Skip('currently failing due to mock HTTP client to download ISAR binaries')
|
||||
@Tags(['widget'])
|
||||
library;
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
import 'package:immich_mobile/domain/models/store.model.dart';
|
||||
import 'package:immich_mobile/domain/services/store.service.dart';
|
||||
import 'package:immich_mobile/entities/store.entity.dart';
|
||||
import 'package:immich_mobile/infrastructure/repositories/store.repository.dart';
|
||||
import 'package:immich_mobile/models/activities/activity.model.dart';
|
||||
import 'package:immich_mobile/providers/asset_viewer/current_asset.provider.dart';
|
||||
import 'package:immich_mobile/widgets/activities/activity_tile.dart';
|
||||
import 'package:immich_mobile/widgets/common/user_circle_avatar.dart';
|
||||
import 'package:isar/isar.dart';
|
||||
|
||||
import '../../fixtures/asset.stub.dart';
|
||||
import '../../fixtures/user.stub.dart';
|
||||
import '../../test_utils.dart';
|
||||
import '../../widget_tester_extensions.dart';
|
||||
import '../asset_viewer/asset_viewer_mocks.dart';
|
||||
|
||||
void main() {
|
||||
late MockCurrentAssetProvider assetProvider;
|
||||
late List<Override> overrides;
|
||||
late Isar db;
|
||||
|
||||
setUpAll(() async {
|
||||
TestUtils.init();
|
||||
db = await TestUtils.initIsar();
|
||||
// For UserCircleAvatar
|
||||
await StoreService.init(storeRepository: IsarStoreRepository(db));
|
||||
await Store.put(StoreKey.currentUser, UserStub.admin);
|
||||
await Store.put(StoreKey.serverEndpoint, '');
|
||||
await Store.put(StoreKey.accessToken, '');
|
||||
});
|
||||
|
||||
setUp(() {
|
||||
assetProvider = MockCurrentAssetProvider();
|
||||
overrides = [currentAssetProvider.overrideWith(() => assetProvider)];
|
||||
});
|
||||
|
||||
testWidgets('Returns a ListTile', (tester) async {
|
||||
await tester.pumpConsumerWidget(
|
||||
ActivityTile(Activity(id: '1', createdAt: DateTime(100), type: ActivityType.like, user: UserStub.admin)),
|
||||
overrides: overrides,
|
||||
);
|
||||
|
||||
expect(find.byType(ListTile), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('No trailing widget when activity assetId == null', (tester) async {
|
||||
await tester.pumpConsumerWidget(
|
||||
ActivityTile(Activity(id: '1', createdAt: DateTime(100), type: ActivityType.like, user: UserStub.admin)),
|
||||
overrides: overrides,
|
||||
);
|
||||
|
||||
final listTile = tester.widget<ListTile>(find.byType(ListTile));
|
||||
expect(listTile.trailing, isNull);
|
||||
});
|
||||
|
||||
testWidgets('Asset Thumbanil as trailing widget when activity assetId != null', (tester) async {
|
||||
await tester.pumpConsumerWidget(
|
||||
ActivityTile(
|
||||
Activity(id: '1', createdAt: DateTime(100), type: ActivityType.like, user: UserStub.admin, assetId: '1'),
|
||||
),
|
||||
overrides: overrides,
|
||||
);
|
||||
|
||||
final listTile = tester.widget<ListTile>(find.byType(ListTile));
|
||||
expect(listTile.trailing, isNotNull);
|
||||
// TODO: Validate this to be the common class after migrating ActivityTile#_ActivityAssetThumbnail to a common class
|
||||
});
|
||||
|
||||
testWidgets('No trailing widget when current asset != null', (tester) async {
|
||||
await tester.pumpConsumerWidget(
|
||||
ActivityTile(
|
||||
Activity(id: '1', createdAt: DateTime(100), type: ActivityType.like, user: UserStub.admin, assetId: '1'),
|
||||
),
|
||||
overrides: overrides,
|
||||
);
|
||||
|
||||
assetProvider.state = AssetStub.image1;
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
final listTile = tester.widget<ListTile>(find.byType(ListTile));
|
||||
expect(listTile.trailing, isNull);
|
||||
});
|
||||
|
||||
group('Like Activity', () {
|
||||
final activity = Activity(id: '1', createdAt: DateTime(100), type: ActivityType.like, user: UserStub.admin);
|
||||
|
||||
testWidgets('Like contains filled thumbs-up as leading', (tester) async {
|
||||
await tester.pumpConsumerWidget(ActivityTile(activity), overrides: overrides);
|
||||
|
||||
// Leading widget should not be null
|
||||
final listTile = tester.widget<ListTile>(find.byType(ListTile));
|
||||
expect(listTile.leading, isNotNull);
|
||||
|
||||
// And should have a thumb_up icon
|
||||
final thumbUpIconFinder = find.widgetWithIcon(listTile.leading!.runtimeType, Icons.thumb_up);
|
||||
|
||||
expect(thumbUpIconFinder, findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('Like title is center aligned', (tester) async {
|
||||
await tester.pumpConsumerWidget(ActivityTile(activity), overrides: overrides);
|
||||
|
||||
final listTile = tester.widget<ListTile>(find.byType(ListTile));
|
||||
|
||||
expect(listTile.titleAlignment, ListTileTitleAlignment.center);
|
||||
});
|
||||
|
||||
testWidgets('No subtitle for likes', (tester) async {
|
||||
await tester.pumpConsumerWidget(ActivityTile(activity), overrides: overrides);
|
||||
|
||||
final listTile = tester.widget<ListTile>(find.byType(ListTile));
|
||||
|
||||
expect(listTile.subtitle, isNull);
|
||||
});
|
||||
});
|
||||
|
||||
group('Comment Activity', () {
|
||||
final activity = Activity(
|
||||
id: '1',
|
||||
createdAt: DateTime(100),
|
||||
type: ActivityType.comment,
|
||||
comment: 'This is a test comment',
|
||||
user: UserStub.admin,
|
||||
);
|
||||
|
||||
testWidgets('Comment contains User Circle Avatar as leading', (tester) async {
|
||||
await tester.pumpConsumerWidget(ActivityTile(activity), overrides: overrides);
|
||||
|
||||
final userAvatarFinder = find.byType(UserCircleAvatar);
|
||||
expect(userAvatarFinder, findsOneWidget);
|
||||
|
||||
// Leading widget should not be null
|
||||
final listTile = tester.widget<ListTile>(find.byType(ListTile));
|
||||
expect(listTile.leading, isNotNull);
|
||||
|
||||
// Make sure that the leading widget is the UserCircleAvatar
|
||||
final userAvatar = tester.widget<UserCircleAvatar>(userAvatarFinder);
|
||||
expect(listTile.leading, userAvatar);
|
||||
});
|
||||
|
||||
testWidgets('Comment title is top aligned', (tester) async {
|
||||
await tester.pumpConsumerWidget(ActivityTile(activity), overrides: overrides);
|
||||
|
||||
final listTile = tester.widget<ListTile>(find.byType(ListTile));
|
||||
|
||||
expect(listTile.titleAlignment, ListTileTitleAlignment.top);
|
||||
});
|
||||
|
||||
testWidgets('Contains comment text as subtitle', (tester) async {
|
||||
await tester.pumpConsumerWidget(ActivityTile(activity), overrides: overrides);
|
||||
|
||||
final listTile = tester.widget<ListTile>(find.byType(ListTile));
|
||||
|
||||
expect(listTile.subtitle, isNotNull);
|
||||
expect(find.descendant(of: find.byType(ListTile), matching: find.text(activity.comment!)), findsOneWidget);
|
||||
});
|
||||
});
|
||||
}
|
||||
99
mobile/test/modules/activity/dismissible_activity_test.dart
Normal file
99
mobile/test/modules/activity/dismissible_activity_test.dart
Normal file
|
|
@ -0,0 +1,99 @@
|
|||
@Tags(['widget'])
|
||||
library;
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:immich_mobile/models/activities/activity.model.dart';
|
||||
import 'package:immich_mobile/widgets/activities/activity_tile.dart';
|
||||
import 'package:immich_mobile/widgets/activities/dismissible_activity.dart';
|
||||
import 'package:immich_mobile/providers/asset_viewer/current_asset.provider.dart';
|
||||
import 'package:immich_mobile/widgets/common/confirm_dialog.dart';
|
||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
|
||||
import '../../fixtures/user.stub.dart';
|
||||
import '../../test_utils.dart';
|
||||
import '../../widget_tester_extensions.dart';
|
||||
import '../asset_viewer/asset_viewer_mocks.dart';
|
||||
|
||||
final activity = Activity(id: '1', createdAt: DateTime(100), type: ActivityType.like, user: UserStub.admin);
|
||||
|
||||
void main() {
|
||||
late MockCurrentAssetProvider assetProvider;
|
||||
late List<Override> overrides;
|
||||
|
||||
setUpAll(() => TestUtils.init());
|
||||
|
||||
setUp(() {
|
||||
assetProvider = MockCurrentAssetProvider();
|
||||
overrides = [currentAssetProvider.overrideWith(() => assetProvider)];
|
||||
});
|
||||
|
||||
testWidgets('Returns a Dismissible', (tester) async {
|
||||
await tester.pumpConsumerWidget(
|
||||
DismissibleActivity('1', ActivityTile(activity), onDismiss: (_) {}),
|
||||
overrides: overrides,
|
||||
);
|
||||
|
||||
expect(find.byType(Dismissible), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('Dialog displayed when onDismiss is set', (tester) async {
|
||||
await tester.pumpConsumerWidget(
|
||||
DismissibleActivity('1', ActivityTile(activity), onDismiss: (_) {}),
|
||||
overrides: overrides,
|
||||
);
|
||||
|
||||
final dismissible = find.byType(Dismissible);
|
||||
await tester.drag(dismissible, const Offset(500, 0));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.byType(ConfirmDialog), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('Ok action in ConfirmDialog should call onDismiss with activityId', (tester) async {
|
||||
String? receivedActivityId;
|
||||
await tester.pumpConsumerWidget(
|
||||
DismissibleActivity('1', ActivityTile(activity), onDismiss: (id) => receivedActivityId = id),
|
||||
overrides: overrides,
|
||||
);
|
||||
|
||||
final dismissible = find.byType(Dismissible);
|
||||
await tester.drag(dismissible, const Offset(-500, 0));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
final okButton = find.text('delete');
|
||||
await tester.tap(okButton);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(receivedActivityId, '1');
|
||||
});
|
||||
|
||||
testWidgets('Delete icon for background if onDismiss is set', (tester) async {
|
||||
await tester.pumpConsumerWidget(
|
||||
DismissibleActivity('1', ActivityTile(activity), onDismiss: (_) {}),
|
||||
overrides: overrides,
|
||||
);
|
||||
|
||||
final dismissible = find.byType(Dismissible);
|
||||
await tester.drag(dismissible, const Offset(500, 0));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.byIcon(Icons.delete_sweep_rounded), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('No delete dialog if onDismiss is not set', (tester) async {
|
||||
await tester.pumpConsumerWidget(DismissibleActivity('1', ActivityTile(activity)), overrides: overrides);
|
||||
|
||||
// When onDismiss is not set, the widget should not be wrapped by a Dismissible
|
||||
expect(find.byType(Dismissible), findsNothing);
|
||||
expect(find.byType(ConfirmDialog), findsNothing);
|
||||
});
|
||||
|
||||
testWidgets('No icon for background if onDismiss is not set', (tester) async {
|
||||
await tester.pumpConsumerWidget(DismissibleActivity('1', ActivityTile(activity)), overrides: overrides);
|
||||
|
||||
// No Dismissible should exist when onDismiss is not provided, so no delete icon either
|
||||
expect(find.byType(Dismissible), findsNothing);
|
||||
expect(find.byIcon(Icons.delete_sweep_rounded), findsNothing);
|
||||
});
|
||||
}
|
||||
13
mobile/test/modules/album/album_mocks.dart
Normal file
13
mobile/test/modules/album/album_mocks.dart
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
import 'package:immich_mobile/providers/album/current_album.provider.dart';
|
||||
import 'package:immich_mobile/entities/album.entity.dart';
|
||||
import 'package:mocktail/mocktail.dart';
|
||||
|
||||
class MockCurrentAlbumProvider extends CurrentAlbum with Mock implements CurrentAlbumInternal {
|
||||
Album? initAlbum;
|
||||
MockCurrentAlbumProvider([this.initAlbum]);
|
||||
|
||||
@override
|
||||
Album? build() {
|
||||
return initAlbum;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,270 @@
|
|||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
import 'package:immich_mobile/entities/album.entity.dart';
|
||||
import 'package:immich_mobile/entities/asset.entity.dart';
|
||||
import 'package:immich_mobile/providers/album/album_sort_by_options.provider.dart';
|
||||
import 'package:immich_mobile/providers/app_settings.provider.dart';
|
||||
import 'package:immich_mobile/services/app_settings.service.dart';
|
||||
import 'package:isar/isar.dart';
|
||||
import 'package:mocktail/mocktail.dart';
|
||||
|
||||
import '../../fixtures/album.stub.dart';
|
||||
import '../../fixtures/asset.stub.dart';
|
||||
import '../../test_utils.dart';
|
||||
import '../settings/settings_mocks.dart';
|
||||
|
||||
void main() {
|
||||
/// Verify the sort modes
|
||||
group("AlbumSortMode", () {
|
||||
late final Isar db;
|
||||
|
||||
setUpAll(() async {
|
||||
db = await TestUtils.initIsar();
|
||||
});
|
||||
|
||||
final albums = [AlbumStub.emptyAlbum, AlbumStub.sharedWithUser, AlbumStub.oneAsset, AlbumStub.twoAsset];
|
||||
|
||||
setUp(() {
|
||||
db.writeTxnSync(() {
|
||||
db.clearSync();
|
||||
// Save all assets
|
||||
db.assets.putAllSync([AssetStub.image1, AssetStub.image2]);
|
||||
db.albums.putAllSync(albums);
|
||||
for (final album in albums) {
|
||||
album.sharedUsers.saveSync();
|
||||
album.assets.saveSync();
|
||||
}
|
||||
});
|
||||
expect(db.albums.countSync(), 4);
|
||||
expect(db.assets.countSync(), 2);
|
||||
});
|
||||
|
||||
group("Album sort - Created Time", () {
|
||||
const created = AlbumSortMode.created;
|
||||
test("Created time - ASC", () {
|
||||
final sorted = created.sortFn(albums, false);
|
||||
final sortedList = [AlbumStub.emptyAlbum, AlbumStub.twoAsset, AlbumStub.oneAsset, AlbumStub.sharedWithUser];
|
||||
expect(sorted, orderedEquals(sortedList));
|
||||
});
|
||||
|
||||
test("Created time - DESC", () {
|
||||
final sorted = created.sortFn(albums, true);
|
||||
final sortedList = [AlbumStub.sharedWithUser, AlbumStub.oneAsset, AlbumStub.twoAsset, AlbumStub.emptyAlbum];
|
||||
expect(sorted, orderedEquals(sortedList));
|
||||
});
|
||||
});
|
||||
|
||||
group("Album sort - Asset count", () {
|
||||
const assetCount = AlbumSortMode.assetCount;
|
||||
test("Asset Count - ASC", () {
|
||||
final sorted = assetCount.sortFn(albums, false);
|
||||
final sortedList = [AlbumStub.emptyAlbum, AlbumStub.sharedWithUser, AlbumStub.oneAsset, AlbumStub.twoAsset];
|
||||
expect(sorted, orderedEquals(sortedList));
|
||||
});
|
||||
|
||||
test("Asset Count - DESC", () {
|
||||
final sorted = assetCount.sortFn(albums, true);
|
||||
final sortedList = [AlbumStub.twoAsset, AlbumStub.oneAsset, AlbumStub.sharedWithUser, AlbumStub.emptyAlbum];
|
||||
expect(sorted, orderedEquals(sortedList));
|
||||
});
|
||||
});
|
||||
|
||||
group("Album sort - Last modified", () {
|
||||
const lastModified = AlbumSortMode.lastModified;
|
||||
test("Last modified - ASC", () {
|
||||
final sorted = lastModified.sortFn(albums, false);
|
||||
final sortedList = [AlbumStub.twoAsset, AlbumStub.emptyAlbum, AlbumStub.sharedWithUser, AlbumStub.oneAsset];
|
||||
expect(sorted, orderedEquals(sortedList));
|
||||
});
|
||||
|
||||
test("Last modified - DESC", () {
|
||||
final sorted = lastModified.sortFn(albums, true);
|
||||
final sortedList = [AlbumStub.oneAsset, AlbumStub.sharedWithUser, AlbumStub.emptyAlbum, AlbumStub.twoAsset];
|
||||
expect(sorted, orderedEquals(sortedList));
|
||||
});
|
||||
});
|
||||
|
||||
group("Album sort - Created", () {
|
||||
const created = AlbumSortMode.created;
|
||||
test("Created - ASC", () {
|
||||
final sorted = created.sortFn(albums, false);
|
||||
final sortedList = [AlbumStub.emptyAlbum, AlbumStub.twoAsset, AlbumStub.oneAsset, AlbumStub.sharedWithUser];
|
||||
expect(sorted, orderedEquals(sortedList));
|
||||
});
|
||||
|
||||
test("Created - DESC", () {
|
||||
final sorted = created.sortFn(albums, true);
|
||||
final sortedList = [AlbumStub.sharedWithUser, AlbumStub.oneAsset, AlbumStub.twoAsset, AlbumStub.emptyAlbum];
|
||||
expect(sorted, orderedEquals(sortedList));
|
||||
});
|
||||
});
|
||||
|
||||
group("Album sort - Most Recent", () {
|
||||
const mostRecent = AlbumSortMode.mostRecent;
|
||||
|
||||
test("Most Recent - DESC", () {
|
||||
final sorted = mostRecent.sortFn([
|
||||
AlbumStub.create2020end2020Album,
|
||||
AlbumStub.create2020end2022Album,
|
||||
AlbumStub.create2020end2024Album,
|
||||
AlbumStub.create2020end2026Album,
|
||||
], false);
|
||||
final sortedList = [
|
||||
AlbumStub.create2020end2026Album,
|
||||
AlbumStub.create2020end2024Album,
|
||||
AlbumStub.create2020end2022Album,
|
||||
AlbumStub.create2020end2020Album,
|
||||
];
|
||||
expect(sorted, orderedEquals(sortedList));
|
||||
});
|
||||
|
||||
test("Most Recent - ASC", () {
|
||||
final sorted = mostRecent.sortFn([
|
||||
AlbumStub.create2020end2020Album,
|
||||
AlbumStub.create2020end2022Album,
|
||||
AlbumStub.create2020end2024Album,
|
||||
AlbumStub.create2020end2026Album,
|
||||
], true);
|
||||
final sortedList = [
|
||||
AlbumStub.create2020end2020Album,
|
||||
AlbumStub.create2020end2022Album,
|
||||
AlbumStub.create2020end2024Album,
|
||||
AlbumStub.create2020end2026Album,
|
||||
];
|
||||
expect(sorted, orderedEquals(sortedList));
|
||||
});
|
||||
});
|
||||
|
||||
group("Album sort - Most Oldest", () {
|
||||
const mostOldest = AlbumSortMode.mostOldest;
|
||||
|
||||
test("Most Oldest - ASC", () {
|
||||
final sorted = mostOldest.sortFn(albums, false);
|
||||
final sortedList = [AlbumStub.twoAsset, AlbumStub.emptyAlbum, AlbumStub.oneAsset, AlbumStub.sharedWithUser];
|
||||
expect(sorted, orderedEquals(sortedList));
|
||||
});
|
||||
|
||||
test("Most Oldest - DESC", () {
|
||||
final sorted = mostOldest.sortFn(albums, true);
|
||||
final sortedList = [AlbumStub.sharedWithUser, AlbumStub.oneAsset, AlbumStub.emptyAlbum, AlbumStub.twoAsset];
|
||||
expect(sorted, orderedEquals(sortedList));
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
/// Verify the sort mode provider
|
||||
group('AlbumSortByOptions', () {
|
||||
late AppSettingsService settingsMock;
|
||||
late ProviderContainer container;
|
||||
|
||||
setUp(() async {
|
||||
settingsMock = MockAppSettingsService();
|
||||
container = TestUtils.createContainer(
|
||||
overrides: [appSettingsServiceProvider.overrideWith((ref) => settingsMock)],
|
||||
);
|
||||
when(
|
||||
() => settingsMock.setSetting<bool>(AppSettingsEnum.selectedAlbumSortReverse, any()),
|
||||
).thenAnswer((_) async => {});
|
||||
when(
|
||||
() => settingsMock.setSetting<int>(AppSettingsEnum.selectedAlbumSortOrder, any()),
|
||||
).thenAnswer((_) async => {});
|
||||
});
|
||||
|
||||
test('Returns the default sort mode when none set', () {
|
||||
// Returns the default value when nothing is set
|
||||
when(() => settingsMock.getSetting(AppSettingsEnum.selectedAlbumSortOrder)).thenReturn(0);
|
||||
|
||||
expect(container.read(albumSortByOptionsProvider), AlbumSortMode.created);
|
||||
});
|
||||
|
||||
test('Returns the correct sort mode with index from Store', () {
|
||||
// Returns the default value when nothing is set
|
||||
when(() => settingsMock.getSetting(AppSettingsEnum.selectedAlbumSortOrder)).thenReturn(3);
|
||||
|
||||
expect(container.read(albumSortByOptionsProvider), AlbumSortMode.lastModified);
|
||||
});
|
||||
|
||||
test('Properly saves the correct store index of sort mode', () {
|
||||
container.read(albumSortByOptionsProvider.notifier).changeSortMode(AlbumSortMode.mostOldest);
|
||||
|
||||
verify(
|
||||
() => settingsMock.setSetting(AppSettingsEnum.selectedAlbumSortOrder, AlbumSortMode.mostOldest.storeIndex),
|
||||
);
|
||||
});
|
||||
|
||||
test('Notifies listeners on state change', () {
|
||||
when(() => settingsMock.getSetting(AppSettingsEnum.selectedAlbumSortOrder)).thenReturn(0);
|
||||
|
||||
final listener = ListenerMock<AlbumSortMode>();
|
||||
container.listen(albumSortByOptionsProvider, listener.call, fireImmediately: true);
|
||||
|
||||
// Created -> Most Oldest
|
||||
container.read(albumSortByOptionsProvider.notifier).changeSortMode(AlbumSortMode.mostOldest);
|
||||
|
||||
// Most Oldest -> Title
|
||||
container.read(albumSortByOptionsProvider.notifier).changeSortMode(AlbumSortMode.title);
|
||||
|
||||
verifyInOrder([
|
||||
() => listener.call(null, AlbumSortMode.created),
|
||||
() => listener.call(AlbumSortMode.created, AlbumSortMode.mostOldest),
|
||||
() => listener.call(AlbumSortMode.mostOldest, AlbumSortMode.title),
|
||||
]);
|
||||
|
||||
verifyNoMoreInteractions(listener);
|
||||
});
|
||||
});
|
||||
|
||||
/// Verify the sort order provider
|
||||
group('AlbumSortOrder', () {
|
||||
late AppSettingsService settingsMock;
|
||||
late ProviderContainer container;
|
||||
|
||||
registerFallbackValue(AppSettingsEnum.selectedAlbumSortReverse);
|
||||
|
||||
setUp(() async {
|
||||
settingsMock = MockAppSettingsService();
|
||||
container = TestUtils.createContainer(
|
||||
overrides: [appSettingsServiceProvider.overrideWith((ref) => settingsMock)],
|
||||
);
|
||||
when(
|
||||
() => settingsMock.setSetting<bool>(AppSettingsEnum.selectedAlbumSortReverse, any()),
|
||||
).thenAnswer((_) async => {});
|
||||
when(
|
||||
() => settingsMock.setSetting<int>(AppSettingsEnum.selectedAlbumSortOrder, any()),
|
||||
).thenAnswer((_) async => {});
|
||||
});
|
||||
|
||||
test('Returns the default sort order when none set - false', () {
|
||||
when(() => settingsMock.getSetting(AppSettingsEnum.selectedAlbumSortReverse)).thenReturn(false);
|
||||
|
||||
expect(container.read(albumSortOrderProvider), isFalse);
|
||||
});
|
||||
|
||||
test('Properly saves the correct order', () {
|
||||
container.read(albumSortOrderProvider.notifier).changeSortDirection(true);
|
||||
|
||||
verify(() => settingsMock.setSetting(AppSettingsEnum.selectedAlbumSortReverse, true));
|
||||
});
|
||||
|
||||
test('Notifies listeners on state change', () {
|
||||
when(() => settingsMock.getSetting(AppSettingsEnum.selectedAlbumSortReverse)).thenReturn(false);
|
||||
|
||||
final listener = ListenerMock<bool>();
|
||||
container.listen(albumSortOrderProvider, listener.call, fireImmediately: true);
|
||||
|
||||
// false -> true
|
||||
container.read(albumSortOrderProvider.notifier).changeSortDirection(true);
|
||||
|
||||
// true -> false
|
||||
container.read(albumSortOrderProvider.notifier).changeSortDirection(false);
|
||||
|
||||
verifyInOrder([
|
||||
() => listener.call(null, false),
|
||||
() => listener.call(false, true),
|
||||
() => listener.call(true, false),
|
||||
]);
|
||||
|
||||
verifyNoMoreInteractions(listener);
|
||||
});
|
||||
});
|
||||
}
|
||||
13
mobile/test/modules/asset_viewer/asset_viewer_mocks.dart
Normal file
13
mobile/test/modules/asset_viewer/asset_viewer_mocks.dart
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
import 'package:immich_mobile/providers/asset_viewer/current_asset.provider.dart';
|
||||
import 'package:immich_mobile/entities/asset.entity.dart';
|
||||
import 'package:mocktail/mocktail.dart';
|
||||
|
||||
class MockCurrentAssetProvider extends CurrentAssetInternal with Mock implements CurrentAsset {
|
||||
Asset? initAsset;
|
||||
MockCurrentAssetProvider([this.initAsset]);
|
||||
|
||||
@override
|
||||
Asset? build() {
|
||||
return initAsset;
|
||||
}
|
||||
}
|
||||
113
mobile/test/modules/extensions/asset_extensions_test.dart
Normal file
113
mobile/test/modules/extensions/asset_extensions_test.dart
Normal file
|
|
@ -0,0 +1,113 @@
|
|||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:immich_mobile/domain/models/exif.model.dart';
|
||||
import 'package:immich_mobile/entities/asset.entity.dart';
|
||||
import 'package:immich_mobile/extensions/asset_extensions.dart';
|
||||
import 'package:timezone/data/latest.dart';
|
||||
import 'package:timezone/timezone.dart';
|
||||
|
||||
ExifInfo makeExif({DateTime? dateTimeOriginal, String? timeZone}) {
|
||||
return ExifInfo(dateTimeOriginal: dateTimeOriginal, timeZone: timeZone);
|
||||
}
|
||||
|
||||
Asset makeAsset({required String id, required DateTime createdAt, ExifInfo? exifInfo}) {
|
||||
return Asset(
|
||||
checksum: '',
|
||||
localId: id,
|
||||
remoteId: id,
|
||||
ownerId: 1,
|
||||
fileCreatedAt: createdAt,
|
||||
fileModifiedAt: DateTime.now(),
|
||||
updatedAt: DateTime.now(),
|
||||
durationInSeconds: 0,
|
||||
type: AssetType.image,
|
||||
fileName: id,
|
||||
isFavorite: false,
|
||||
isArchived: false,
|
||||
isTrashed: false,
|
||||
exifInfo: exifInfo,
|
||||
);
|
||||
}
|
||||
|
||||
void main() {
|
||||
// Init Timezone DB
|
||||
initializeTimeZones();
|
||||
|
||||
group("Returns local time and offset if no exifInfo", () {
|
||||
test('returns createdAt directly if in local', () {
|
||||
final createdAt = DateTime(2023, 12, 12, 12, 12, 12);
|
||||
final a = makeAsset(id: '1', createdAt: createdAt);
|
||||
final (dt, tz) = a.getTZAdjustedTimeAndOffset();
|
||||
|
||||
expect(createdAt, dt);
|
||||
expect(createdAt.timeZoneOffset, tz);
|
||||
});
|
||||
|
||||
test('returns createdAt in local if in utc', () {
|
||||
final createdAt = DateTime.utc(2023, 12, 12, 12, 12, 12);
|
||||
final a = makeAsset(id: '1', createdAt: createdAt);
|
||||
final (dt, tz) = a.getTZAdjustedTimeAndOffset();
|
||||
|
||||
final localCreatedAt = createdAt.toLocal();
|
||||
expect(localCreatedAt, dt);
|
||||
expect(localCreatedAt.timeZoneOffset, tz);
|
||||
});
|
||||
});
|
||||
|
||||
group("Returns dateTimeOriginal", () {
|
||||
test('Returns dateTimeOriginal in UTC from exifInfo without timezone', () {
|
||||
final createdAt = DateTime.parse("2023-01-27T14:00:00-0500");
|
||||
final dateTimeOriginal = DateTime.parse("2022-01-27T14:00:00+0530");
|
||||
final e = makeExif(dateTimeOriginal: dateTimeOriginal);
|
||||
final a = makeAsset(id: '1', createdAt: createdAt, exifInfo: e);
|
||||
final (dt, tz) = a.getTZAdjustedTimeAndOffset();
|
||||
|
||||
final dateTimeInUTC = dateTimeOriginal.toUtc();
|
||||
expect(dateTimeInUTC, dt);
|
||||
expect(dateTimeInUTC.timeZoneOffset, tz);
|
||||
});
|
||||
|
||||
test('Returns dateTimeOriginal in UTC from exifInfo with invalid timezone', () {
|
||||
final createdAt = DateTime.parse("2023-01-27T14:00:00-0500");
|
||||
final dateTimeOriginal = DateTime.parse("2022-01-27T14:00:00+0530");
|
||||
final e = makeExif(dateTimeOriginal: dateTimeOriginal, timeZone: "#_#"); // Invalid timezone
|
||||
final a = makeAsset(id: '1', createdAt: createdAt, exifInfo: e);
|
||||
final (dt, tz) = a.getTZAdjustedTimeAndOffset();
|
||||
|
||||
final dateTimeInUTC = dateTimeOriginal.toUtc();
|
||||
expect(dateTimeInUTC, dt);
|
||||
expect(dateTimeInUTC.timeZoneOffset, tz);
|
||||
});
|
||||
});
|
||||
|
||||
group("Returns adjusted time if timezone available", () {
|
||||
test('With timezone as location', () {
|
||||
final createdAt = DateTime.parse("2023-01-27T14:00:00-0500");
|
||||
final dateTimeOriginal = DateTime.parse("2022-01-27T14:00:00+0530");
|
||||
const location = "Asia/Hong_Kong";
|
||||
final e = makeExif(dateTimeOriginal: dateTimeOriginal, timeZone: location);
|
||||
final a = makeAsset(id: '1', createdAt: createdAt, exifInfo: e);
|
||||
final (dt, tz) = a.getTZAdjustedTimeAndOffset();
|
||||
|
||||
final adjustedTime = TZDateTime.from(dateTimeOriginal.toUtc(), getLocation(location));
|
||||
expect(adjustedTime, dt);
|
||||
expect(adjustedTime.timeZoneOffset, tz);
|
||||
});
|
||||
|
||||
test('With timezone as offset', () {
|
||||
final createdAt = DateTime.parse("2023-01-27T14:00:00-0500");
|
||||
final dateTimeOriginal = DateTime.parse("2022-01-27T14:00:00+0530");
|
||||
const offset = "utc+08:00";
|
||||
final e = makeExif(dateTimeOriginal: dateTimeOriginal, timeZone: offset);
|
||||
final a = makeAsset(id: '1', createdAt: createdAt, exifInfo: e);
|
||||
final (dt, tz) = a.getTZAdjustedTimeAndOffset();
|
||||
|
||||
final location = getLocation("Asia/Hong_Kong");
|
||||
final offsetFromLocation = Duration(milliseconds: location.currentTimeZone.offset);
|
||||
final adjustedTime = dateTimeOriginal.toUtc().add(offsetFromLocation);
|
||||
|
||||
// Adds the offset to the actual time and returns the offset separately
|
||||
expect(adjustedTime, dt);
|
||||
expect(offsetFromLocation, tz);
|
||||
});
|
||||
});
|
||||
}
|
||||
50
mobile/test/modules/extensions/builtin_extensions_test.dart
Normal file
50
mobile/test/modules/extensions/builtin_extensions_test.dart
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:immich_mobile/extensions/collection_extensions.dart';
|
||||
import 'package:immich_mobile/extensions/string_extensions.dart';
|
||||
|
||||
void main() {
|
||||
group('Test toDuration', () {
|
||||
test('ok', () {
|
||||
expect("1:02:33".toDuration(), const Duration(hours: 1, minutes: 2, seconds: 33));
|
||||
});
|
||||
test('malformed', () {
|
||||
expect("".toDuration(), isNull);
|
||||
expect("1:2".toDuration(), isNull);
|
||||
expect("a:b:c".toDuration(), isNull);
|
||||
});
|
||||
});
|
||||
group('Test uniqueConsecutive', () {
|
||||
test('empty', () {
|
||||
final a = [];
|
||||
expect(a.uniqueConsecutive(), []);
|
||||
});
|
||||
|
||||
test('singleElement', () {
|
||||
final a = [5];
|
||||
expect(a.uniqueConsecutive(), [5]);
|
||||
});
|
||||
|
||||
test('noDuplicates', () {
|
||||
final a = [1, 2, 3];
|
||||
expect(a.uniqueConsecutive(), orderedEquals([1, 2, 3]));
|
||||
});
|
||||
|
||||
test('unsortedDuplicates', () {
|
||||
final a = [1, 2, 1, 3];
|
||||
expect(a.uniqueConsecutive(), orderedEquals([1, 2, 1, 3]));
|
||||
});
|
||||
|
||||
test('sortedDuplicates', () {
|
||||
final a = [6, 6, 2, 3, 3, 3, 4, 5, 1, 1];
|
||||
expect(a.uniqueConsecutive(), orderedEquals([6, 2, 3, 4, 5, 1]));
|
||||
});
|
||||
|
||||
test('withKey', () {
|
||||
final a = ["a", "bb", "cc", "ddd"];
|
||||
expect(
|
||||
a.uniqueConsecutive(compare: (s1, s2) => s1.length.compareTo(s2.length)),
|
||||
orderedEquals(["a", "bb", "ddd"]),
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
||||
46
mobile/test/modules/extensions/datetime_extensions_test.dart
Normal file
46
mobile/test/modules/extensions/datetime_extensions_test.dart
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:immich_mobile/extensions/datetime_extensions.dart';
|
||||
import 'package:intl/date_symbol_data_local.dart';
|
||||
|
||||
void main() {
|
||||
setUpAll(() async {
|
||||
await initializeDateFormatting();
|
||||
});
|
||||
|
||||
group('DateRangeFormatting.formatDateRange', () {
|
||||
final currentYear = DateTime.now().year;
|
||||
|
||||
test('returns single date format for this year', () {
|
||||
final date = DateTime(currentYear, 8, 28); // Aug 28 this year
|
||||
final result = DateRangeFormatting.formatDateRange(date, date, null);
|
||||
expect(result, 'Aug 28');
|
||||
});
|
||||
|
||||
test('returns single date format for other year', () {
|
||||
final date = DateTime(2023, 8, 28); // Aug 28, 2023
|
||||
final result = DateRangeFormatting.formatDateRange(date, date, null);
|
||||
expect(result, 'Aug 28, 2023');
|
||||
});
|
||||
|
||||
test('returns date range format for this year', () {
|
||||
final startDate = DateTime(currentYear, 3, 23); // Mar 23
|
||||
final endDate = DateTime(currentYear, 5, 31); // May 31
|
||||
final result = DateRangeFormatting.formatDateRange(startDate, endDate, null);
|
||||
expect(result, 'Mar 23 - May 31');
|
||||
});
|
||||
|
||||
test('returns date range format for other year (same year)', () {
|
||||
final startDate = DateTime(2023, 8, 28); // Aug 28
|
||||
final endDate = DateTime(2023, 9, 30); // Sep 30
|
||||
final result = DateRangeFormatting.formatDateRange(startDate, endDate, null);
|
||||
expect(result, 'Aug 28 - Sep 30, 2023');
|
||||
});
|
||||
|
||||
test('returns date range format over multiple years', () {
|
||||
final startDate = DateTime(2021, 4, 17); // Apr 17, 2021
|
||||
final endDate = DateTime(2022, 4, 9); // Apr 9, 2022
|
||||
final result = DateRangeFormatting.formatDateRange(startDate, endDate, null);
|
||||
expect(result, 'Apr 17, 2021 - Apr 9, 2022');
|
||||
});
|
||||
});
|
||||
}
|
||||
113
mobile/test/modules/home/asset_grid_data_structure_test.dart
Normal file
113
mobile/test/modules/home/asset_grid_data_structure_test.dart
Normal file
|
|
@ -0,0 +1,113 @@
|
|||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:immich_mobile/widgets/asset_grid/asset_grid_data_structure.dart';
|
||||
import 'package:immich_mobile/entities/asset.entity.dart';
|
||||
|
||||
void main() {
|
||||
final List<Asset> testAssets = [];
|
||||
|
||||
for (int i = 0; i < 150; i++) {
|
||||
int month = i ~/ 31;
|
||||
int day = (i % 31).toInt();
|
||||
|
||||
DateTime date = DateTime(2022, month, day);
|
||||
|
||||
testAssets.add(
|
||||
Asset(
|
||||
checksum: "",
|
||||
localId: '$i',
|
||||
ownerId: 1,
|
||||
fileCreatedAt: date,
|
||||
fileModifiedAt: date,
|
||||
updatedAt: date,
|
||||
durationInSeconds: 0,
|
||||
type: AssetType.image,
|
||||
fileName: '',
|
||||
isFavorite: false,
|
||||
isArchived: false,
|
||||
isTrashed: false,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
final List<Asset> assets = [];
|
||||
|
||||
assets.addAll(
|
||||
testAssets.sublist(0, 5).map((e) {
|
||||
e.fileCreatedAt = DateTime(2022, 1, 5);
|
||||
return e;
|
||||
}).toList(),
|
||||
);
|
||||
assets.addAll(
|
||||
testAssets.sublist(5, 10).map((e) {
|
||||
e.fileCreatedAt = DateTime(2022, 1, 10);
|
||||
return e;
|
||||
}).toList(),
|
||||
);
|
||||
assets.addAll(
|
||||
testAssets.sublist(10, 15).map((e) {
|
||||
e.fileCreatedAt = DateTime(2022, 2, 17);
|
||||
return e;
|
||||
}).toList(),
|
||||
);
|
||||
assets.addAll(
|
||||
testAssets.sublist(15, 30).map((e) {
|
||||
e.fileCreatedAt = DateTime(2022, 10, 15);
|
||||
return e;
|
||||
}).toList(),
|
||||
);
|
||||
|
||||
group('Test grouped', () {
|
||||
test('test grouped check months', () async {
|
||||
final renderList = await RenderList.fromAssets(assets, GroupAssetsBy.day);
|
||||
|
||||
// Oct
|
||||
// Day 1
|
||||
// 15 Assets => 5 Rows
|
||||
// Feb
|
||||
// Day 1
|
||||
// 5 Assets => 2 Rows
|
||||
// Jan
|
||||
// Day 2
|
||||
// 5 Assets => 2 Rows
|
||||
// Day 1
|
||||
// 5 Assets => 2 Rows
|
||||
expect(renderList.elements, hasLength(4));
|
||||
expect(renderList.elements[0].type, RenderAssetGridElementType.monthTitle);
|
||||
expect(renderList.elements[0].date.month, 1);
|
||||
expect(renderList.elements[1].type, RenderAssetGridElementType.groupDividerTitle);
|
||||
expect(renderList.elements[1].date.month, 1);
|
||||
expect(renderList.elements[2].type, RenderAssetGridElementType.monthTitle);
|
||||
expect(renderList.elements[2].date.month, 2);
|
||||
expect(renderList.elements[3].type, RenderAssetGridElementType.monthTitle);
|
||||
expect(renderList.elements[3].date.month, 10);
|
||||
});
|
||||
|
||||
test('test grouped check types', () async {
|
||||
final renderList = await RenderList.fromAssets(assets, GroupAssetsBy.day);
|
||||
|
||||
// Oct
|
||||
// Day 1
|
||||
// 15 Assets => 3 Rows
|
||||
// Feb
|
||||
// Day 1
|
||||
// 5 Assets => 1 Row
|
||||
// Jan
|
||||
// Day 2
|
||||
// 5 Assets => 1 Row
|
||||
// Day 1
|
||||
// 5 Assets => 1 Row
|
||||
final types = [
|
||||
RenderAssetGridElementType.monthTitle,
|
||||
RenderAssetGridElementType.groupDividerTitle,
|
||||
RenderAssetGridElementType.monthTitle,
|
||||
RenderAssetGridElementType.monthTitle,
|
||||
];
|
||||
|
||||
expect(renderList.elements, hasLength(types.length));
|
||||
|
||||
for (int i = 0; i < renderList.elements.length; i++) {
|
||||
expect(renderList.elements[i].type, types[i]);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
16
mobile/test/modules/map/map_mocks.dart
Normal file
16
mobile/test/modules/map/map_mocks.dart
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
import 'package:immich_mobile/models/map/map_state.model.dart';
|
||||
import 'package:immich_mobile/providers/map/map_state.provider.dart';
|
||||
import 'package:mocktail/mocktail.dart';
|
||||
|
||||
class MockMapStateNotifier extends Notifier<MapState> with Mock implements MapStateNotifier {
|
||||
final MapState initState;
|
||||
|
||||
MockMapStateNotifier(this.initState);
|
||||
|
||||
@override
|
||||
MapState build() => initState;
|
||||
|
||||
@override
|
||||
set state(MapState mapState) => super.state = mapState;
|
||||
}
|
||||
163
mobile/test/modules/map/map_theme_override_test.dart
Normal file
163
mobile/test/modules/map/map_theme_override_test.dart
Normal file
|
|
@ -0,0 +1,163 @@
|
|||
@Skip('Flaky test, needs investigation')
|
||||
@Tags(['widget'])
|
||||
library;
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
import 'package:immich_mobile/domain/services/store.service.dart';
|
||||
import 'package:immich_mobile/infrastructure/repositories/store.repository.dart';
|
||||
import 'package:immich_mobile/models/map/map_state.model.dart';
|
||||
import 'package:immich_mobile/providers/locale_provider.dart';
|
||||
import 'package:immich_mobile/providers/map/map_state.provider.dart';
|
||||
import 'package:immich_mobile/widgets/map/map_theme_override.dart';
|
||||
import 'package:isar/isar.dart';
|
||||
|
||||
import '../../test_utils.dart';
|
||||
import '../../widget_tester_extensions.dart';
|
||||
import 'map_mocks.dart';
|
||||
|
||||
void main() {
|
||||
late MockMapStateNotifier mapStateNotifier;
|
||||
late List<Override> overrides;
|
||||
late MapState mapState;
|
||||
late Isar db;
|
||||
|
||||
setUpAll(() async {
|
||||
db = await TestUtils.initIsar();
|
||||
TestUtils.init();
|
||||
});
|
||||
|
||||
setUp(() async {
|
||||
mapState = const MapState(themeMode: ThemeMode.dark);
|
||||
mapStateNotifier = MockMapStateNotifier(mapState);
|
||||
await StoreService.init(storeRepository: IsarStoreRepository(db));
|
||||
overrides = [
|
||||
mapStateNotifierProvider.overrideWith(() => mapStateNotifier),
|
||||
localeProvider.overrideWithValue(const Locale("en")),
|
||||
];
|
||||
});
|
||||
|
||||
testWidgets("Return dark theme style when theme mode is dark", (tester) async {
|
||||
AsyncValue<String>? mapStyle;
|
||||
await tester.pumpConsumerWidget(
|
||||
MapThemeOverride(
|
||||
mapBuilder: (AsyncValue<String> style) {
|
||||
mapStyle = style;
|
||||
return const Text("Mock");
|
||||
},
|
||||
),
|
||||
overrides: overrides,
|
||||
);
|
||||
|
||||
mapStateNotifier.state = mapState.copyWith(darkStyleFetched: const AsyncData("dark"));
|
||||
await tester.pumpAndSettle();
|
||||
expect(mapStyle?.valueOrNull, "dark");
|
||||
});
|
||||
|
||||
testWidgets("Return error when style is not fetched", (tester) async {
|
||||
AsyncValue<String>? mapStyle;
|
||||
await tester.pumpConsumerWidget(
|
||||
MapThemeOverride(
|
||||
mapBuilder: (AsyncValue<String> style) {
|
||||
mapStyle = style;
|
||||
return const Text("Mock");
|
||||
},
|
||||
),
|
||||
overrides: overrides,
|
||||
);
|
||||
|
||||
mapStateNotifier.state = mapState.copyWith(darkStyleFetched: const AsyncError("Error", StackTrace.empty));
|
||||
await tester.pumpAndSettle();
|
||||
expect(mapStyle?.hasError, isTrue);
|
||||
});
|
||||
|
||||
testWidgets("Return light theme style when theme mode is light", (tester) async {
|
||||
AsyncValue<String>? mapStyle;
|
||||
await tester.pumpConsumerWidget(
|
||||
MapThemeOverride(
|
||||
mapBuilder: (AsyncValue<String> style) {
|
||||
mapStyle = style;
|
||||
return const Text("Mock");
|
||||
},
|
||||
),
|
||||
overrides: overrides,
|
||||
);
|
||||
|
||||
mapStateNotifier.state = mapState.copyWith(themeMode: ThemeMode.light, lightStyleFetched: const AsyncData("light"));
|
||||
await tester.pumpAndSettle();
|
||||
expect(mapStyle?.valueOrNull, "light");
|
||||
});
|
||||
|
||||
group("System mode", () {
|
||||
testWidgets("Return dark theme style when system is dark", (tester) async {
|
||||
AsyncValue<String>? mapStyle;
|
||||
await tester.pumpConsumerWidget(
|
||||
MapThemeOverride(
|
||||
mapBuilder: (AsyncValue<String> style) {
|
||||
mapStyle = style;
|
||||
return const Text("Mock");
|
||||
},
|
||||
),
|
||||
overrides: overrides,
|
||||
);
|
||||
|
||||
tester.binding.platformDispatcher.platformBrightnessTestValue = Brightness.dark;
|
||||
mapStateNotifier.state = mapState.copyWith(
|
||||
themeMode: ThemeMode.system,
|
||||
darkStyleFetched: const AsyncData("dark"),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(mapStyle?.valueOrNull, "dark");
|
||||
});
|
||||
|
||||
testWidgets("Return light theme style when system is light", (tester) async {
|
||||
AsyncValue<String>? mapStyle;
|
||||
await tester.pumpConsumerWidget(
|
||||
MapThemeOverride(
|
||||
mapBuilder: (AsyncValue<String> style) {
|
||||
mapStyle = style;
|
||||
return const Text("Mock");
|
||||
},
|
||||
),
|
||||
overrides: overrides,
|
||||
);
|
||||
|
||||
tester.binding.platformDispatcher.platformBrightnessTestValue = Brightness.light;
|
||||
mapStateNotifier.state = mapState.copyWith(
|
||||
themeMode: ThemeMode.system,
|
||||
lightStyleFetched: const AsyncData("light"),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(mapStyle?.valueOrNull, "light");
|
||||
});
|
||||
|
||||
testWidgets("Switches style when system brightness changes", (tester) async {
|
||||
AsyncValue<String>? mapStyle;
|
||||
await tester.pumpConsumerWidget(
|
||||
MapThemeOverride(
|
||||
mapBuilder: (AsyncValue<String> style) {
|
||||
mapStyle = style;
|
||||
return const Text("Mock");
|
||||
},
|
||||
),
|
||||
overrides: overrides,
|
||||
);
|
||||
|
||||
tester.binding.platformDispatcher.platformBrightnessTestValue = Brightness.light;
|
||||
mapStateNotifier.state = mapState.copyWith(
|
||||
themeMode: ThemeMode.system,
|
||||
lightStyleFetched: const AsyncData("light"),
|
||||
darkStyleFetched: const AsyncData("dark"),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
expect(mapStyle?.valueOrNull, "light");
|
||||
|
||||
tester.binding.platformDispatcher.platformBrightnessTestValue = Brightness.dark;
|
||||
await tester.pumpAndSettle();
|
||||
expect(mapStyle?.valueOrNull, "dark");
|
||||
});
|
||||
});
|
||||
}
|
||||
4
mobile/test/modules/settings/settings_mocks.dart
Normal file
4
mobile/test/modules/settings/settings_mocks.dart
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
import 'package:immich_mobile/services/app_settings.service.dart';
|
||||
import 'package:mocktail/mocktail.dart';
|
||||
|
||||
class MockAppSettingsService extends Mock implements AppSettingsService {}
|
||||
11
mobile/test/modules/shared/shared_mocks.dart
Normal file
11
mobile/test/modules/shared/shared_mocks.dart
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
import 'package:immich_mobile/domain/models/user.model.dart';
|
||||
import 'package:immich_mobile/providers/user.provider.dart';
|
||||
import 'package:mocktail/mocktail.dart';
|
||||
|
||||
class MockCurrentUserProvider extends StateNotifier<UserDto?> with Mock implements CurrentUserProvider {
|
||||
MockCurrentUserProvider() : super(null);
|
||||
|
||||
@override
|
||||
set state(UserDto? user) => super.state = user;
|
||||
}
|
||||
285
mobile/test/modules/shared/sync_service_test.dart
Normal file
285
mobile/test/modules/shared/sync_service_test.dart
Normal file
|
|
@ -0,0 +1,285 @@
|
|||
import 'package:drift/drift.dart';
|
||||
import 'package:drift/native.dart';
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:immich_mobile/constants/enums.dart';
|
||||
import 'package:immich_mobile/domain/models/store.model.dart';
|
||||
import 'package:immich_mobile/domain/models/user.model.dart';
|
||||
import 'package:immich_mobile/domain/services/log.service.dart';
|
||||
import 'package:immich_mobile/domain/services/store.service.dart';
|
||||
import 'package:immich_mobile/entities/asset.entity.dart';
|
||||
import 'package:immich_mobile/entities/etag.entity.dart';
|
||||
import 'package:immich_mobile/entities/store.entity.dart';
|
||||
import 'package:immich_mobile/infrastructure/repositories/log.repository.dart';
|
||||
import 'package:immich_mobile/infrastructure/repositories/logger_db.repository.dart';
|
||||
import 'package:immich_mobile/infrastructure/repositories/store.repository.dart';
|
||||
import 'package:immich_mobile/repositories/asset.repository.dart';
|
||||
import 'package:immich_mobile/repositories/partner_api.repository.dart';
|
||||
import 'package:immich_mobile/services/sync.service.dart';
|
||||
import 'package:mocktail/mocktail.dart';
|
||||
|
||||
import '../../domain/service.mock.dart';
|
||||
import '../../fixtures/asset.stub.dart';
|
||||
import '../../infrastructure/repository.mock.dart';
|
||||
import '../../repository.mocks.dart';
|
||||
import '../../service.mocks.dart';
|
||||
import '../../test_utils.dart';
|
||||
|
||||
void main() {
|
||||
int assetIdCounter = 0;
|
||||
Asset makeAsset({
|
||||
required String checksum,
|
||||
String? localId,
|
||||
String? remoteId,
|
||||
int ownerId = 590700560494856554, // hash of "1"
|
||||
}) {
|
||||
final DateTime date = DateTime(2000);
|
||||
return Asset(
|
||||
id: assetIdCounter++,
|
||||
checksum: checksum,
|
||||
localId: localId,
|
||||
remoteId: remoteId,
|
||||
ownerId: ownerId,
|
||||
fileCreatedAt: date,
|
||||
fileModifiedAt: date,
|
||||
updatedAt: date,
|
||||
durationInSeconds: 0,
|
||||
type: AssetType.image,
|
||||
fileName: localId ?? remoteId ?? "",
|
||||
isFavorite: false,
|
||||
isArchived: false,
|
||||
isTrashed: false,
|
||||
);
|
||||
}
|
||||
|
||||
final owner = UserDto(
|
||||
id: "1",
|
||||
updatedAt: DateTime.now(),
|
||||
email: "a@b.c",
|
||||
name: "first last",
|
||||
isAdmin: false,
|
||||
profileChangedAt: DateTime.now(),
|
||||
);
|
||||
|
||||
setUpAll(() async {
|
||||
final loggerDb = DriftLogger(DatabaseConnection(NativeDatabase.memory(), closeStreamsSynchronously: true));
|
||||
final LogRepository logRepository = LogRepository(loggerDb);
|
||||
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
final db = await TestUtils.initIsar();
|
||||
|
||||
db.writeTxnSync(() => db.clearSync());
|
||||
await StoreService.init(storeRepository: IsarStoreRepository(db));
|
||||
await Store.put(StoreKey.currentUser, owner);
|
||||
await LogService.init(logRepository: logRepository, storeRepository: IsarStoreRepository(db));
|
||||
});
|
||||
|
||||
group('Test SyncService grouped', () {
|
||||
final MockHashService hs = MockHashService();
|
||||
final MockEntityService entityService = MockEntityService();
|
||||
final MockAlbumRepository albumRepository = MockAlbumRepository();
|
||||
final MockAssetRepository assetRepository = MockAssetRepository();
|
||||
final MockExifInfoRepository exifInfoRepository = MockExifInfoRepository();
|
||||
final MockIsarUserRepository userRepository = MockIsarUserRepository();
|
||||
final MockETagRepository eTagRepository = MockETagRepository();
|
||||
final MockAlbumMediaRepository albumMediaRepository = MockAlbumMediaRepository();
|
||||
final MockAlbumApiRepository albumApiRepository = MockAlbumApiRepository();
|
||||
final MockAppSettingService appSettingService = MockAppSettingService();
|
||||
final MockLocalFilesManagerRepository localFilesManagerRepository = MockLocalFilesManagerRepository();
|
||||
final MockPartnerApiRepository partnerApiRepository = MockPartnerApiRepository();
|
||||
final MockUserApiRepository userApiRepository = MockUserApiRepository();
|
||||
final MockPartnerRepository partnerRepository = MockPartnerRepository();
|
||||
final MockUserService userService = MockUserService();
|
||||
|
||||
final owner = UserDto(
|
||||
id: "1",
|
||||
updatedAt: DateTime.now(),
|
||||
email: "a@b.c",
|
||||
name: "first last",
|
||||
isAdmin: false,
|
||||
profileChangedAt: DateTime(2021),
|
||||
);
|
||||
|
||||
late SyncService s;
|
||||
|
||||
final List<Asset> initialAssets = [
|
||||
makeAsset(checksum: "a", remoteId: "0-1"),
|
||||
makeAsset(checksum: "b", remoteId: "2-1"),
|
||||
makeAsset(checksum: "c", localId: "1", remoteId: "1-1"),
|
||||
makeAsset(checksum: "d", localId: "2"),
|
||||
makeAsset(checksum: "e", localId: "3"),
|
||||
];
|
||||
setUp(() {
|
||||
s = SyncService(
|
||||
hs,
|
||||
entityService,
|
||||
albumMediaRepository,
|
||||
albumApiRepository,
|
||||
albumRepository,
|
||||
assetRepository,
|
||||
exifInfoRepository,
|
||||
partnerRepository,
|
||||
userRepository,
|
||||
userService,
|
||||
eTagRepository,
|
||||
appSettingService,
|
||||
localFilesManagerRepository,
|
||||
partnerApiRepository,
|
||||
userApiRepository,
|
||||
);
|
||||
when(() => userService.getMyUser()).thenReturn(owner);
|
||||
when(() => eTagRepository.get(owner.id)).thenAnswer((_) async => ETag(id: owner.id, time: DateTime.now()));
|
||||
when(() => eTagRepository.deleteByIds(["1"])).thenAnswer((_) async {});
|
||||
when(() => eTagRepository.upsertAll(any())).thenAnswer((_) async {});
|
||||
when(() => partnerRepository.getSharedWith()).thenAnswer((_) async => []);
|
||||
when(() => userRepository.getAll(sortBy: SortUserBy.id)).thenAnswer((_) async => [owner]);
|
||||
when(() => userRepository.getAll()).thenAnswer((_) async => [owner]);
|
||||
when(
|
||||
() => assetRepository.getAll(ownerId: owner.id, sortBy: AssetSort.checksum),
|
||||
).thenAnswer((_) async => initialAssets);
|
||||
when(
|
||||
() => assetRepository.getAllByOwnerIdChecksum(any(), any()),
|
||||
).thenAnswer((_) async => [initialAssets[3], null, null]);
|
||||
when(() => assetRepository.updateAll(any())).thenAnswer((_) async => []);
|
||||
when(() => assetRepository.deleteByIds(any())).thenAnswer((_) async {});
|
||||
when(() => exifInfoRepository.updateAll(any())).thenAnswer((_) async => []);
|
||||
when(
|
||||
() => assetRepository.transaction<void>(any()),
|
||||
).thenAnswer((call) => (call.positionalArguments.first as Function).call());
|
||||
when(
|
||||
() => assetRepository.transaction<Null>(any()),
|
||||
).thenAnswer((call) => (call.positionalArguments.first as Function).call());
|
||||
when(() => userApiRepository.getAll()).thenAnswer((_) async => [owner]);
|
||||
registerFallbackValue(Direction.sharedByMe);
|
||||
when(() => partnerApiRepository.getAll(any())).thenAnswer((_) async => []);
|
||||
});
|
||||
test('test inserting existing assets', () async {
|
||||
final List<Asset> remoteAssets = [
|
||||
makeAsset(checksum: "a", remoteId: "0-1"),
|
||||
makeAsset(checksum: "b", remoteId: "2-1"),
|
||||
makeAsset(checksum: "c", remoteId: "1-1"),
|
||||
];
|
||||
final bool c1 = await s.syncRemoteAssetsToDb(
|
||||
users: [owner],
|
||||
getChangedAssets: _failDiff,
|
||||
loadAssets: (u, d) => remoteAssets,
|
||||
);
|
||||
expect(c1, isFalse);
|
||||
verifyNever(() => assetRepository.updateAll(any()));
|
||||
});
|
||||
|
||||
test('test inserting new assets', () async {
|
||||
final List<Asset> remoteAssets = [
|
||||
makeAsset(checksum: "a", remoteId: "0-1"),
|
||||
makeAsset(checksum: "b", remoteId: "2-1"),
|
||||
makeAsset(checksum: "c", remoteId: "1-1"),
|
||||
makeAsset(checksum: "d", remoteId: "1-2"),
|
||||
makeAsset(checksum: "f", remoteId: "1-4"),
|
||||
makeAsset(checksum: "g", remoteId: "3-1"),
|
||||
];
|
||||
final bool c1 = await s.syncRemoteAssetsToDb(
|
||||
users: [owner],
|
||||
getChangedAssets: _failDiff,
|
||||
loadAssets: (u, d) => remoteAssets,
|
||||
);
|
||||
expect(c1, isTrue);
|
||||
final updatedAsset = initialAssets[3].updatedCopy(remoteAssets[3]);
|
||||
verify(() => assetRepository.updateAll([remoteAssets[4], remoteAssets[5], updatedAsset]));
|
||||
});
|
||||
|
||||
test('test syncing duplicate assets', () async {
|
||||
final List<Asset> remoteAssets = [
|
||||
makeAsset(checksum: "a", remoteId: "0-1"),
|
||||
makeAsset(checksum: "b", remoteId: "1-1"),
|
||||
makeAsset(checksum: "c", remoteId: "2-1"),
|
||||
makeAsset(checksum: "h", remoteId: "2-1b"),
|
||||
makeAsset(checksum: "i", remoteId: "2-1c"),
|
||||
makeAsset(checksum: "j", remoteId: "2-1d"),
|
||||
];
|
||||
final bool c1 = await s.syncRemoteAssetsToDb(
|
||||
users: [owner],
|
||||
getChangedAssets: _failDiff,
|
||||
loadAssets: (u, d) => remoteAssets,
|
||||
);
|
||||
expect(c1, isTrue);
|
||||
when(
|
||||
() => assetRepository.getAll(ownerId: owner.id, sortBy: AssetSort.checksum),
|
||||
).thenAnswer((_) async => remoteAssets);
|
||||
final bool c2 = await s.syncRemoteAssetsToDb(
|
||||
users: [owner],
|
||||
getChangedAssets: _failDiff,
|
||||
loadAssets: (u, d) => remoteAssets,
|
||||
);
|
||||
expect(c2, isFalse);
|
||||
final currentState = [...remoteAssets];
|
||||
when(
|
||||
() => assetRepository.getAll(ownerId: owner.id, sortBy: AssetSort.checksum),
|
||||
).thenAnswer((_) async => currentState);
|
||||
remoteAssets.removeAt(4);
|
||||
final bool c3 = await s.syncRemoteAssetsToDb(
|
||||
users: [owner],
|
||||
getChangedAssets: _failDiff,
|
||||
loadAssets: (u, d) => remoteAssets,
|
||||
);
|
||||
expect(c3, isTrue);
|
||||
remoteAssets.add(makeAsset(checksum: "k", remoteId: "2-1e"));
|
||||
remoteAssets.add(makeAsset(checksum: "l", remoteId: "2-2"));
|
||||
final bool c4 = await s.syncRemoteAssetsToDb(
|
||||
users: [owner],
|
||||
getChangedAssets: _failDiff,
|
||||
loadAssets: (u, d) => remoteAssets,
|
||||
);
|
||||
expect(c4, isTrue);
|
||||
});
|
||||
|
||||
test('test efficient sync', () async {
|
||||
when(
|
||||
() => assetRepository.deleteAllByRemoteId([
|
||||
initialAssets[1].remoteId!,
|
||||
initialAssets[2].remoteId!,
|
||||
], state: AssetState.remote),
|
||||
).thenAnswer((_) async {
|
||||
return;
|
||||
});
|
||||
when(
|
||||
() => assetRepository.getAllByRemoteId(["2-1", "1-1"], state: AssetState.merged),
|
||||
).thenAnswer((_) async => [initialAssets[2]]);
|
||||
when(
|
||||
() => assetRepository.getAllByOwnerIdChecksum(any(), any()),
|
||||
).thenAnswer((_) async => [initialAssets[0], null, null]); //afg
|
||||
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 expected = [...toUpsert];
|
||||
expected[0].id = initialAssets[0].id;
|
||||
final bool c = await s.syncRemoteAssetsToDb(
|
||||
users: [owner],
|
||||
getChangedAssets: (user, since) async => (toUpsert, toDelete),
|
||||
loadAssets: (user, date) => throw Exception(),
|
||||
);
|
||||
expect(c, isTrue);
|
||||
verify(() => assetRepository.updateAll(expected));
|
||||
});
|
||||
|
||||
group("upsertAssetsWithExif", () {
|
||||
test('test upsert with EXIF data', () async {
|
||||
final assets = [AssetStub.image1, AssetStub.image2];
|
||||
|
||||
expect(assets.map((a) => a.exifInfo?.assetId), List.filled(assets.length, null));
|
||||
await s.upsertAssetsWithExif(assets);
|
||||
verify(
|
||||
() => exifInfoRepository.updateAll(
|
||||
any(that: containsAll(assets.map((a) => a.exifInfo!.copyWith(assetId: a.id)))),
|
||||
),
|
||||
);
|
||||
expect(assets.map((a) => a.exifInfo?.assetId), assets.map((a) => a.id));
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Future<(List<Asset>?, List<String>?)> _failDiff(List<UserDto> user, DateTime time) => Future.value((null, null));
|
||||
23
mobile/test/modules/utils/async_mutex_test.dart
Normal file
23
mobile/test/modules/utils/async_mutex_test.dart
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
import 'dart:async';
|
||||
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:immich_mobile/utils/async_mutex.dart';
|
||||
|
||||
void main() {
|
||||
group('Test AsyncMutex grouped', () {
|
||||
test('test ordered execution', () async {
|
||||
AsyncMutex lock = AsyncMutex();
|
||||
List<int> events = [];
|
||||
expect(0, lock.enqueued);
|
||||
unawaited(lock.run(() => Future.delayed(const Duration(milliseconds: 10), () => events.add(1))));
|
||||
expect(1, lock.enqueued);
|
||||
unawaited(lock.run(() => Future.delayed(const Duration(milliseconds: 3), () => events.add(2))));
|
||||
expect(2, lock.enqueued);
|
||||
unawaited(lock.run(() => Future.delayed(const Duration(milliseconds: 1), () => events.add(3))));
|
||||
expect(3, lock.enqueued);
|
||||
await lock.run(() => Future.delayed(const Duration(milliseconds: 10), () => events.add(4)));
|
||||
expect(0, lock.enqueued);
|
||||
expect(events, [1, 2, 3, 4]);
|
||||
});
|
||||
});
|
||||
}
|
||||
58
mobile/test/modules/utils/datetime_helpers_test.dart
Normal file
58
mobile/test/modules/utils/datetime_helpers_test.dart
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:immich_mobile/utils/datetime_helpers.dart';
|
||||
|
||||
void main() {
|
||||
group('tryFromSecondsSinceEpoch', () {
|
||||
test('returns null for null input', () {
|
||||
final result = tryFromSecondsSinceEpoch(null);
|
||||
expect(result, isNull);
|
||||
});
|
||||
|
||||
test('returns null for value below minimum allowed range', () {
|
||||
// _minMillisecondsSinceEpoch = -62135596800000
|
||||
final seconds = -62135596800000 ~/ 1000 - 1; // One second before min allowed
|
||||
final result = tryFromSecondsSinceEpoch(seconds);
|
||||
expect(result, isNull);
|
||||
});
|
||||
|
||||
test('returns null for value above maximum allowed range', () {
|
||||
// _maxMillisecondsSinceEpoch = 8640000000000000
|
||||
final seconds = 8640000000000000 ~/ 1000 + 1; // One second after max allowed
|
||||
final result = tryFromSecondsSinceEpoch(seconds);
|
||||
expect(result, isNull);
|
||||
});
|
||||
|
||||
test('returns correct DateTime for minimum allowed value', () {
|
||||
final seconds = -62135596800000 ~/ 1000; // Minimum allowed timestamp
|
||||
final result = tryFromSecondsSinceEpoch(seconds);
|
||||
expect(result, DateTime.fromMillisecondsSinceEpoch(-62135596800000));
|
||||
});
|
||||
|
||||
test('returns correct DateTime for maximum allowed value', () {
|
||||
final seconds = 8640000000000000 ~/ 1000; // Maximum allowed timestamp
|
||||
final result = tryFromSecondsSinceEpoch(seconds);
|
||||
expect(result, DateTime.fromMillisecondsSinceEpoch(8640000000000000));
|
||||
});
|
||||
|
||||
test('returns correct DateTime for negative timestamp', () {
|
||||
final seconds = -1577836800; // Dec 31, 1919 (pre-epoch)
|
||||
final result = tryFromSecondsSinceEpoch(seconds);
|
||||
expect(result, DateTime.fromMillisecondsSinceEpoch(-1577836800 * 1000));
|
||||
});
|
||||
|
||||
test('returns correct DateTime for zero timestamp', () {
|
||||
final seconds = 0; // Jan 1, 1970 (epoch)
|
||||
final result = tryFromSecondsSinceEpoch(seconds);
|
||||
expect(result, DateTime.fromMillisecondsSinceEpoch(0));
|
||||
});
|
||||
|
||||
test('returns correct DateTime for recent timestamp', () {
|
||||
final now = DateTime.now();
|
||||
final seconds = now.millisecondsSinceEpoch ~/ 1000;
|
||||
final result = tryFromSecondsSinceEpoch(seconds);
|
||||
expect(result?.year, now.year);
|
||||
expect(result?.month, now.month);
|
||||
expect(result?.day, now.day);
|
||||
});
|
||||
});
|
||||
}
|
||||
41
mobile/test/modules/utils/debouncer_test.dart
Normal file
41
mobile/test/modules/utils/debouncer_test.dart
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:immich_mobile/utils/debounce.dart';
|
||||
|
||||
class _Counter {
|
||||
int _count = 0;
|
||||
_Counter();
|
||||
|
||||
int get count => _count;
|
||||
void increment() => _count = _count + 1;
|
||||
}
|
||||
|
||||
void main() {
|
||||
test('Executes the method after the interval', () async {
|
||||
var counter = _Counter();
|
||||
final debouncer = Debouncer(interval: const Duration(milliseconds: 300));
|
||||
debouncer.run(() => counter.increment());
|
||||
expect(counter.count, 0);
|
||||
await Future.delayed(const Duration(milliseconds: 300));
|
||||
expect(counter.count, 1);
|
||||
});
|
||||
|
||||
test('Executes the method immediately if zero interval', () async {
|
||||
var counter = _Counter();
|
||||
final debouncer = Debouncer(interval: const Duration(milliseconds: 0));
|
||||
debouncer.run(() => counter.increment());
|
||||
// Even though it is supposed to be executed immediately, it is added to the async queue and so
|
||||
// we need this delay to make sure the actual debounced method is called
|
||||
await Future.delayed(const Duration(milliseconds: 0));
|
||||
expect(counter.count, 1);
|
||||
});
|
||||
|
||||
test('Delayes method execution after all the calls are completed', () async {
|
||||
var counter = _Counter();
|
||||
final debouncer = Debouncer(interval: const Duration(milliseconds: 100));
|
||||
debouncer.run(() => counter.increment());
|
||||
debouncer.run(() => counter.increment());
|
||||
debouncer.run(() => counter.increment());
|
||||
await Future.delayed(const Duration(milliseconds: 300));
|
||||
expect(counter.count, 1);
|
||||
});
|
||||
}
|
||||
50
mobile/test/modules/utils/diff_test.dart
Normal file
50
mobile/test/modules/utils/diff_test.dart
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:immich_mobile/utils/diff.dart';
|
||||
|
||||
void main() {
|
||||
final List<int> listA = [1, 2, 3, 4, 6];
|
||||
final List<int> listB = [1, 3, 5, 7];
|
||||
|
||||
group('Test grouped', () {
|
||||
test('test partial overlap', () async {
|
||||
final List<int> onlyInA = [];
|
||||
final List<int> onlyInB = [];
|
||||
final List<int> inBoth = [];
|
||||
final changes = await diffSortedLists(
|
||||
listA,
|
||||
listB,
|
||||
compare: (int a, int b) => a.compareTo(b),
|
||||
both: (int a, int b) {
|
||||
inBoth.add(b);
|
||||
return false;
|
||||
},
|
||||
onlyFirst: (int a) => onlyInA.add(a),
|
||||
onlySecond: (int b) => onlyInB.add(b),
|
||||
);
|
||||
expect(changes, true);
|
||||
expect(onlyInA, [2, 4, 6]);
|
||||
expect(onlyInB, [5, 7]);
|
||||
expect(inBoth, [1, 3]);
|
||||
});
|
||||
test('test partial overlap sync', () {
|
||||
final List<int> onlyInA = [];
|
||||
final List<int> onlyInB = [];
|
||||
final List<int> inBoth = [];
|
||||
final changes = diffSortedListsSync(
|
||||
listA,
|
||||
listB,
|
||||
compare: (int a, int b) => a.compareTo(b),
|
||||
both: (int a, int b) {
|
||||
inBoth.add(b);
|
||||
return false;
|
||||
},
|
||||
onlyFirst: (int a) => onlyInA.add(a),
|
||||
onlySecond: (int b) => onlyInB.add(b),
|
||||
);
|
||||
expect(changes, true);
|
||||
expect(onlyInA, [2, 4, 6]);
|
||||
expect(onlyInB, [5, 7]);
|
||||
expect(inBoth, [1, 3]);
|
||||
});
|
||||
});
|
||||
}
|
||||
131
mobile/test/modules/utils/migration_test.dart
Normal file
131
mobile/test/modules/utils/migration_test.dart
Normal file
|
|
@ -0,0 +1,131 @@
|
|||
import 'package:drift/drift.dart' hide isNull;
|
||||
import 'package:drift/native.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:immich_mobile/domain/models/store.model.dart';
|
||||
import 'package:immich_mobile/domain/services/store.service.dart';
|
||||
import 'package:immich_mobile/entities/store.entity.dart';
|
||||
import 'package:immich_mobile/infrastructure/repositories/db.repository.dart';
|
||||
import 'package:immich_mobile/infrastructure/repositories/store.repository.dart';
|
||||
import 'package:immich_mobile/infrastructure/repositories/sync_stream.repository.dart';
|
||||
import 'package:immich_mobile/utils/migration.dart';
|
||||
import 'package:mocktail/mocktail.dart';
|
||||
|
||||
import '../../infrastructure/repository.mock.dart';
|
||||
|
||||
void main() {
|
||||
late Drift db;
|
||||
late SyncStreamRepository mockSyncStreamRepository;
|
||||
|
||||
setUpAll(() async {
|
||||
db = Drift(DatabaseConnection(NativeDatabase.memory(), closeStreamsSynchronously: true));
|
||||
await StoreService.init(storeRepository: DriftStoreRepository(db));
|
||||
mockSyncStreamRepository = MockSyncStreamRepository();
|
||||
when(() => mockSyncStreamRepository.reset()).thenAnswer((_) async => {});
|
||||
});
|
||||
|
||||
tearDown(() async {
|
||||
await Store.clear();
|
||||
});
|
||||
|
||||
group('handleBetaMigration Tests', () {
|
||||
group("version < 15", () {
|
||||
test('already on new timeline', () async {
|
||||
await Store.put(StoreKey.betaTimeline, true);
|
||||
|
||||
await handleBetaMigration(14, false, mockSyncStreamRepository);
|
||||
|
||||
expect(Store.tryGet(StoreKey.betaTimeline), true);
|
||||
expect(Store.tryGet(StoreKey.needBetaMigration), false);
|
||||
});
|
||||
|
||||
test('already on old timeline', () async {
|
||||
await Store.put(StoreKey.betaTimeline, false);
|
||||
|
||||
await handleBetaMigration(14, false, mockSyncStreamRepository);
|
||||
|
||||
expect(Store.tryGet(StoreKey.needBetaMigration), true);
|
||||
});
|
||||
|
||||
test('fresh install', () async {
|
||||
await Store.delete(StoreKey.betaTimeline);
|
||||
await handleBetaMigration(14, true, mockSyncStreamRepository);
|
||||
|
||||
expect(Store.tryGet(StoreKey.betaTimeline), true);
|
||||
expect(Store.tryGet(StoreKey.needBetaMigration), false);
|
||||
});
|
||||
});
|
||||
|
||||
group("version == 15", () {
|
||||
test('already on new timeline', () async {
|
||||
await Store.put(StoreKey.betaTimeline, true);
|
||||
|
||||
await handleBetaMigration(15, false, mockSyncStreamRepository);
|
||||
|
||||
expect(Store.tryGet(StoreKey.betaTimeline), true);
|
||||
expect(Store.tryGet(StoreKey.needBetaMigration), false);
|
||||
});
|
||||
|
||||
test('already on old timeline', () async {
|
||||
await Store.put(StoreKey.betaTimeline, false);
|
||||
|
||||
await handleBetaMigration(15, false, mockSyncStreamRepository);
|
||||
|
||||
expect(Store.tryGet(StoreKey.needBetaMigration), true);
|
||||
});
|
||||
|
||||
test('fresh install', () async {
|
||||
await Store.delete(StoreKey.betaTimeline);
|
||||
await handleBetaMigration(15, true, mockSyncStreamRepository);
|
||||
|
||||
expect(Store.tryGet(StoreKey.betaTimeline), true);
|
||||
expect(Store.tryGet(StoreKey.needBetaMigration), false);
|
||||
});
|
||||
});
|
||||
|
||||
group("version > 15", () {
|
||||
test('already on new timeline', () async {
|
||||
await Store.put(StoreKey.betaTimeline, true);
|
||||
|
||||
await handleBetaMigration(16, false, mockSyncStreamRepository);
|
||||
|
||||
expect(Store.tryGet(StoreKey.betaTimeline), true);
|
||||
expect(Store.tryGet(StoreKey.needBetaMigration), false);
|
||||
});
|
||||
|
||||
test('already on old timeline', () async {
|
||||
await Store.put(StoreKey.betaTimeline, false);
|
||||
|
||||
await handleBetaMigration(16, false, mockSyncStreamRepository);
|
||||
|
||||
expect(Store.tryGet(StoreKey.betaTimeline), false);
|
||||
expect(Store.tryGet(StoreKey.needBetaMigration), false);
|
||||
});
|
||||
|
||||
test('fresh install', () async {
|
||||
await Store.delete(StoreKey.betaTimeline);
|
||||
await handleBetaMigration(16, true, mockSyncStreamRepository);
|
||||
|
||||
expect(Store.tryGet(StoreKey.betaTimeline), true);
|
||||
expect(Store.tryGet(StoreKey.needBetaMigration), false);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
group('sync reset tests', () {
|
||||
test('version < 16', () async {
|
||||
await Store.put(StoreKey.shouldResetSync, false);
|
||||
|
||||
await handleBetaMigration(15, false, mockSyncStreamRepository);
|
||||
|
||||
expect(Store.tryGet(StoreKey.shouldResetSync), true);
|
||||
});
|
||||
|
||||
test('version >= 16', () async {
|
||||
await Store.put(StoreKey.shouldResetSync, false);
|
||||
|
||||
await handleBetaMigration(16, false, mockSyncStreamRepository);
|
||||
|
||||
expect(Store.tryGet(StoreKey.shouldResetSync), false);
|
||||
});
|
||||
});
|
||||
}
|
||||
61
mobile/test/modules/utils/openapi_patching_test.dart
Normal file
61
mobile/test/modules/utils/openapi_patching_test.dart
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:openapi/api.dart';
|
||||
import 'package:immich_mobile/utils/openapi_patching.dart';
|
||||
|
||||
void main() {
|
||||
group('Test OpenApi Patching', () {
|
||||
test('upgradeDto', () {
|
||||
dynamic value;
|
||||
String targetType;
|
||||
|
||||
targetType = 'UserPreferencesResponseDto';
|
||||
value = jsonDecode("""
|
||||
{
|
||||
"download": {
|
||||
"archiveSize": 4294967296,
|
||||
"includeEmbeddedVideos": false
|
||||
}
|
||||
}
|
||||
""");
|
||||
|
||||
upgradeDto(value, targetType);
|
||||
expect(value['tags'], TagsResponse().toJson());
|
||||
expect(value['download']['includeEmbeddedVideos'], false);
|
||||
});
|
||||
|
||||
test('addDefault', () {
|
||||
dynamic value = jsonDecode("""
|
||||
{
|
||||
"download": {
|
||||
"archiveSize": 4294967296,
|
||||
"includeEmbeddedVideos": false
|
||||
}
|
||||
}
|
||||
""");
|
||||
String keys = 'download.unknownKey';
|
||||
dynamic defaultValue = 69420;
|
||||
|
||||
addDefault(value, keys, defaultValue);
|
||||
expect(value['download']['unknownKey'], 69420);
|
||||
|
||||
keys = 'alpha.beta';
|
||||
defaultValue = 'gamma';
|
||||
addDefault(value, keys, defaultValue);
|
||||
expect(value['alpha']['beta'], 'gamma');
|
||||
});
|
||||
|
||||
test('addDefault with null', () {
|
||||
dynamic value = jsonDecode("""
|
||||
{
|
||||
"download": {
|
||||
"archiveSize": 4294967296,
|
||||
"includeEmbeddedVideos": false
|
||||
}
|
||||
}
|
||||
""");
|
||||
expect(value['download']['unknownKey'], isNull);
|
||||
});
|
||||
});
|
||||
}
|
||||
46
mobile/test/modules/utils/throttler_test.dart
Normal file
46
mobile/test/modules/utils/throttler_test.dart
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:immich_mobile/utils/throttle.dart';
|
||||
import 'package:immich_mobile/utils/debug_print.dart';
|
||||
|
||||
class _Counter {
|
||||
int _count = 0;
|
||||
_Counter();
|
||||
|
||||
int get count => _count;
|
||||
void increment() {
|
||||
dPrint(() => "Counter inside increment: $count");
|
||||
_count = _count + 1;
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
test('Executes the method immediately if no calls received previously', () async {
|
||||
var counter = _Counter();
|
||||
final throttler = Throttler(interval: const Duration(milliseconds: 300));
|
||||
throttler.run(() => counter.increment());
|
||||
expect(counter.count, 1);
|
||||
});
|
||||
|
||||
test('Does not execute calls before throttle interval', () async {
|
||||
var counter = _Counter();
|
||||
final throttler = Throttler(interval: const Duration(milliseconds: 100));
|
||||
throttler.run(() => counter.increment());
|
||||
throttler.run(() => counter.increment());
|
||||
throttler.run(() => counter.increment());
|
||||
throttler.run(() => counter.increment());
|
||||
throttler.run(() => counter.increment());
|
||||
await Future.delayed(const Duration(seconds: 1));
|
||||
expect(counter.count, 1);
|
||||
});
|
||||
|
||||
test('Executes the method if received in intervals', () async {
|
||||
var counter = _Counter();
|
||||
final throttler = Throttler(interval: const Duration(milliseconds: 100));
|
||||
for (final _ in Iterable<int>.generate(10)) {
|
||||
throttler.run(() => counter.increment());
|
||||
await Future.delayed(const Duration(milliseconds: 50));
|
||||
}
|
||||
await Future.delayed(const Duration(seconds: 1));
|
||||
expect(counter.count, 5);
|
||||
});
|
||||
}
|
||||
63
mobile/test/modules/utils/thumbnail_utils_test.dart
Normal file
63
mobile/test/modules/utils/thumbnail_utils_test.dart
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
import 'package:easy_localization/easy_localization.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:immich_mobile/domain/models/exif.model.dart';
|
||||
import 'package:immich_mobile/entities/asset.entity.dart';
|
||||
import 'package:immich_mobile/utils/thumbnail_utils.dart';
|
||||
|
||||
void main() {
|
||||
final dateTime = DateTime(2025, 04, 25, 12, 13, 14);
|
||||
final dateTimeString = DateFormat.yMMMMd().format(dateTime);
|
||||
|
||||
test('returns description if it has one', () {
|
||||
final result = getAltText(const ExifInfo(description: 'description'), dateTime, AssetType.image, []);
|
||||
expect(result, 'description');
|
||||
});
|
||||
|
||||
test('returns image alt text with date if no location', () {
|
||||
final (template, args) = getAltTextTemplate(const ExifInfo(), dateTime, AssetType.image, []);
|
||||
expect(template, "image_alt_text_date");
|
||||
expect(args["isVideo"], "false");
|
||||
expect(args["date"], dateTimeString);
|
||||
});
|
||||
|
||||
test('returns image alt text with date and place', () {
|
||||
final (template, args) = getAltTextTemplate(
|
||||
const ExifInfo(city: 'city', country: 'country'),
|
||||
dateTime,
|
||||
AssetType.video,
|
||||
[],
|
||||
);
|
||||
expect(template, "image_alt_text_date_place");
|
||||
expect(args["isVideo"], "true");
|
||||
expect(args["date"], dateTimeString);
|
||||
expect(args["city"], "city");
|
||||
expect(args["country"], "country");
|
||||
});
|
||||
|
||||
test('returns image alt text with date and some people', () {
|
||||
final (template, args) = getAltTextTemplate(const ExifInfo(), dateTime, AssetType.image, ["Alice", "Bob"]);
|
||||
expect(template, "image_alt_text_date_2_people");
|
||||
expect(args["isVideo"], "false");
|
||||
expect(args["date"], dateTimeString);
|
||||
expect(args["person1"], "Alice");
|
||||
expect(args["person2"], "Bob");
|
||||
});
|
||||
|
||||
test('returns image alt text with date and location and many people', () {
|
||||
final (template, args) = getAltTextTemplate(
|
||||
const ExifInfo(city: "city", country: 'country'),
|
||||
dateTime,
|
||||
AssetType.video,
|
||||
["Alice", "Bob", "Carol", "David", "Eve"],
|
||||
);
|
||||
expect(template, "image_alt_text_date_place_4_or_more_people");
|
||||
expect(args["isVideo"], "true");
|
||||
expect(args["date"], dateTimeString);
|
||||
expect(args["city"], "city");
|
||||
expect(args["country"], "country");
|
||||
expect(args["person1"], "Alice");
|
||||
expect(args["person2"], "Bob");
|
||||
expect(args["person3"], "Carol");
|
||||
expect(args["additionalCount"], "2");
|
||||
});
|
||||
}
|
||||
136
mobile/test/modules/utils/url_helper_test.dart
Normal file
136
mobile/test/modules/utils/url_helper_test.dart
Normal file
|
|
@ -0,0 +1,136 @@
|
|||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:immich_mobile/utils/url_helper.dart';
|
||||
|
||||
void main() {
|
||||
group('punycodeEncodeUrl', () {
|
||||
test('should return empty string for invalid URL', () {
|
||||
expect(punycodeEncodeUrl('not a url'), equals(''));
|
||||
});
|
||||
|
||||
test('should handle empty input', () {
|
||||
expect(punycodeEncodeUrl(''), equals(''));
|
||||
});
|
||||
|
||||
test('should return ASCII-only URL unchanged', () {
|
||||
const url = 'https://example.com';
|
||||
expect(punycodeEncodeUrl(url), equals(url));
|
||||
});
|
||||
|
||||
test('should encode single-segment Unicode host', () {
|
||||
const url = 'https://bücher';
|
||||
const expected = 'https://xn--bcher-kva';
|
||||
expect(punycodeEncodeUrl(url), equals(expected));
|
||||
});
|
||||
|
||||
test('should encode multi-segment Unicode host', () {
|
||||
const url = 'https://bücher.de';
|
||||
const expected = 'https://xn--bcher-kva.de';
|
||||
expect(punycodeEncodeUrl(url), equals(expected));
|
||||
});
|
||||
|
||||
test('should encode multi-segment Unicode host with multiple non-ASCII segments', () {
|
||||
const url = 'https://bücher.münchen';
|
||||
const expected = 'https://xn--bcher-kva.xn--mnchen-3ya';
|
||||
expect(punycodeEncodeUrl(url), equals(expected));
|
||||
});
|
||||
|
||||
test('should handle URL with port', () {
|
||||
const url = 'https://bücher.de:8080';
|
||||
const expected = 'https://xn--bcher-kva.de:8080';
|
||||
expect(punycodeEncodeUrl(url), equals(expected));
|
||||
});
|
||||
|
||||
test('should handle URL with path', () {
|
||||
const url = 'https://bücher.de/path/to/resource';
|
||||
const expected = 'https://xn--bcher-kva.de/path/to/resource';
|
||||
expect(punycodeEncodeUrl(url), equals(expected));
|
||||
});
|
||||
|
||||
test('should handle URL with port and path', () {
|
||||
const url = 'https://bücher.de:3000/path';
|
||||
const expected = 'https://xn--bcher-kva.de:3000/path';
|
||||
expect(punycodeEncodeUrl(url), equals(expected));
|
||||
});
|
||||
|
||||
test('should not encode ASCII segment in multi-segment host', () {
|
||||
const url = 'https://shop.bücher.de';
|
||||
const expected = 'https://shop.xn--bcher-kva.de';
|
||||
expect(punycodeEncodeUrl(url), equals(expected));
|
||||
});
|
||||
|
||||
test('should handle host with hyphen in Unicode segment', () {
|
||||
const url = 'https://bü-cher.de';
|
||||
const expected = 'https://xn--b-cher-3ya.de';
|
||||
expect(punycodeEncodeUrl(url), equals(expected));
|
||||
});
|
||||
|
||||
test('should handle host with numbers in Unicode segment', () {
|
||||
const url = 'https://bücher123.de';
|
||||
const expected = 'https://xn--bcher123-65a.de';
|
||||
expect(punycodeEncodeUrl(url), equals(expected));
|
||||
});
|
||||
|
||||
test('should encode the domain of the original issue poster :)', () {
|
||||
const url = 'https://фото.большойчлен.рф/';
|
||||
const expected = 'https://xn--n1aalg.xn--90ailhbncb6fh7b.xn--p1ai/';
|
||||
expect(punycodeEncodeUrl(url), expected);
|
||||
});
|
||||
});
|
||||
|
||||
group('punycodeDecodeUrl', () {
|
||||
test('should return null for null input', () {
|
||||
expect(punycodeDecodeUrl(null), isNull);
|
||||
});
|
||||
|
||||
test('should return null for an invalid URL', () {
|
||||
// "not a url" should fail to parse.
|
||||
expect(punycodeDecodeUrl('not a url'), isNull);
|
||||
});
|
||||
|
||||
test('should return null for a URL with empty host', () {
|
||||
// "https://" is a valid scheme but with no host.
|
||||
expect(punycodeDecodeUrl('https://'), isNull);
|
||||
});
|
||||
|
||||
test('should return ASCII-only URL unchanged', () {
|
||||
const url = 'https://example.com';
|
||||
expect(punycodeDecodeUrl(url), equals(url));
|
||||
});
|
||||
|
||||
test('should decode a single-segment Punycode domain', () {
|
||||
const input = 'https://xn--bcher-kva.de';
|
||||
const expected = 'https://bücher.de';
|
||||
expect(punycodeDecodeUrl(input), equals(expected));
|
||||
});
|
||||
|
||||
test('should decode a multi-segment Punycode domain', () {
|
||||
const input = 'https://shop.xn--bcher-kva.de';
|
||||
const expected = 'https://shop.bücher.de';
|
||||
expect(punycodeDecodeUrl(input), equals(expected));
|
||||
});
|
||||
|
||||
test('should decode URL with port', () {
|
||||
const input = 'https://xn--bcher-kva.de:8080';
|
||||
const expected = 'https://bücher.de:8080';
|
||||
expect(punycodeDecodeUrl(input), equals(expected));
|
||||
});
|
||||
|
||||
test('should decode domains with uppercase punycode prefix correctly', () {
|
||||
const input = 'https://XN--BCHER-KVA.de';
|
||||
const expected = 'https://bücher.de';
|
||||
expect(punycodeDecodeUrl(input), equals(expected));
|
||||
});
|
||||
|
||||
test('should handle mixed segments with no punycode in some parts', () {
|
||||
const input = 'https://news.xn--bcher-kva.de';
|
||||
const expected = 'https://news.bücher.de';
|
||||
expect(punycodeDecodeUrl(input), equals(expected));
|
||||
});
|
||||
|
||||
test('should decode the domain of the original issue poster :)', () {
|
||||
const url = 'https://xn--n1aalg.xn--90ailhbncb6fh7b.xn--p1ai/';
|
||||
const expected = 'https://фото.большойчлен.рф/';
|
||||
expect(punycodeDecodeUrl(url), expected);
|
||||
});
|
||||
});
|
||||
}
|
||||
32
mobile/test/modules/utils/version_compatibility_test.dart
Normal file
32
mobile/test/modules/utils/version_compatibility_test.dart
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:immich_mobile/utils/version_compatibility.dart';
|
||||
|
||||
void main() {
|
||||
test('getVersionCompatibilityMessage', () {
|
||||
String? result;
|
||||
|
||||
result = getVersionCompatibilityMessage(1, 0, 2, 0);
|
||||
expect(result, 'Your app major version is not compatible with the server!');
|
||||
|
||||
result = getVersionCompatibilityMessage(1, 106, 1, 105);
|
||||
expect(
|
||||
result,
|
||||
'Your app minor version is not compatible with the server! Please update your server to version v1.106.0 or newer to login',
|
||||
);
|
||||
|
||||
result = getVersionCompatibilityMessage(1, 107, 1, 105);
|
||||
expect(
|
||||
result,
|
||||
'Your app minor version is not compatible with the server! Please update your server to version v1.106.0 or newer to login',
|
||||
);
|
||||
|
||||
result = getVersionCompatibilityMessage(1, 106, 1, 106);
|
||||
expect(result, null);
|
||||
|
||||
result = getVersionCompatibilityMessage(1, 107, 1, 106);
|
||||
expect(result, null);
|
||||
|
||||
result = getVersionCompatibilityMessage(1, 107, 1, 108);
|
||||
expect(result, null);
|
||||
});
|
||||
}
|
||||
98
mobile/test/pages/search/search.page_test.dart
Normal file
98
mobile/test/pages/search/search.page_test.dart
Normal file
|
|
@ -0,0 +1,98 @@
|
|||
@Skip('currently failing due to mock HTTP client to download ISAR binaries')
|
||||
@Tags(['pages'])
|
||||
library;
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
import 'package:immich_mobile/domain/services/store.service.dart';
|
||||
import 'package:immich_mobile/infrastructure/repositories/store.repository.dart';
|
||||
import 'package:immich_mobile/pages/search/search.page.dart';
|
||||
import 'package:immich_mobile/providers/api.provider.dart';
|
||||
import 'package:immich_mobile/providers/db.provider.dart';
|
||||
import 'package:immich_mobile/providers/infrastructure/db.provider.dart';
|
||||
import 'package:isar/isar.dart';
|
||||
import 'package:mocktail/mocktail.dart';
|
||||
import 'package:openapi/api.dart';
|
||||
|
||||
import '../../dto.mocks.dart';
|
||||
import '../../service.mocks.dart';
|
||||
import '../../test_utils.dart';
|
||||
import '../../widget_tester_extensions.dart';
|
||||
|
||||
void main() {
|
||||
late List<Override> overrides;
|
||||
late Isar db;
|
||||
late MockApiService mockApiService;
|
||||
late MockSearchApi mockSearchApi;
|
||||
|
||||
setUpAll(() async {
|
||||
TestUtils.init();
|
||||
db = await TestUtils.initIsar();
|
||||
await StoreService.init(storeRepository: IsarStoreRepository(db));
|
||||
mockApiService = MockApiService();
|
||||
mockSearchApi = MockSearchApi();
|
||||
when(() => mockApiService.searchApi).thenReturn(mockSearchApi);
|
||||
registerFallbackValue(MockSmartSearchDto());
|
||||
registerFallbackValue(MockMetadataSearchDto());
|
||||
overrides = [
|
||||
dbProvider.overrideWithValue(db),
|
||||
isarProvider.overrideWithValue(db),
|
||||
apiServiceProvider.overrideWithValue(mockApiService),
|
||||
];
|
||||
});
|
||||
|
||||
final emptyTextSearch = isA<MetadataSearchDto>().having((s) => s.originalFileName, 'originalFileName', null);
|
||||
|
||||
testWidgets('contextual search with/without text', (tester) async {
|
||||
await tester.pumpConsumerWidget(const SearchPage(), overrides: overrides);
|
||||
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.byIcon(Icons.abc_rounded), findsOneWidget, reason: 'Should have contextual search icon');
|
||||
|
||||
final searchField = find.byKey(const Key('search_text_field'));
|
||||
expect(searchField, findsOneWidget);
|
||||
|
||||
await tester.enterText(searchField, 'test');
|
||||
await tester.testTextInput.receiveAction(TextInputAction.search);
|
||||
|
||||
var captured = verify(() => mockSearchApi.searchSmart(captureAny())).captured;
|
||||
|
||||
expect(captured.first, isA<SmartSearchDto>().having((s) => s.query, 'query', 'test'));
|
||||
|
||||
await tester.enterText(searchField, '');
|
||||
await tester.testTextInput.receiveAction(TextInputAction.search);
|
||||
|
||||
captured = verify(() => mockSearchApi.searchAssets(captureAny())).captured;
|
||||
expect(captured.first, emptyTextSearch);
|
||||
});
|
||||
|
||||
testWidgets('not contextual search with/without text', (tester) async {
|
||||
await tester.pumpConsumerWidget(const SearchPage(), overrides: overrides);
|
||||
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
await tester.tap(find.byKey(const Key('contextual_search_button')));
|
||||
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.byIcon(Icons.image_search_rounded), findsOneWidget, reason: 'Should not have contextual search icon');
|
||||
|
||||
final searchField = find.byKey(const Key('search_text_field'));
|
||||
expect(searchField, findsOneWidget);
|
||||
|
||||
await tester.enterText(searchField, 'test');
|
||||
await tester.testTextInput.receiveAction(TextInputAction.search);
|
||||
|
||||
var captured = verify(() => mockSearchApi.searchAssets(captureAny())).captured;
|
||||
|
||||
expect(captured.first, isA<MetadataSearchDto>().having((s) => s.originalFileName, 'originalFileName', 'test'));
|
||||
|
||||
await tester.enterText(searchField, '');
|
||||
await tester.testTextInput.receiveAction(TextInputAction.search);
|
||||
|
||||
captured = verify(() => mockSearchApi.searchAssets(captureAny())).captured;
|
||||
expect(captured.first, emptyTextSearch);
|
||||
});
|
||||
}
|
||||
|
|
@ -0,0 +1,500 @@
|
|||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:immich_mobile/presentation/widgets/remote_album/drift_album_option.widget.dart';
|
||||
|
||||
import '../../../widget_tester_extensions.dart';
|
||||
|
||||
void main() {
|
||||
group('DriftRemoteAlbumOption', () {
|
||||
testWidgets('shows kebab menu icon button', (tester) async {
|
||||
await tester.pumpConsumerWidget(
|
||||
const DriftRemoteAlbumOption(),
|
||||
);
|
||||
|
||||
expect(find.byIcon(Icons.more_vert_rounded), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('opens menu when icon button is tapped', (tester) async {
|
||||
await tester.pumpConsumerWidget(
|
||||
DriftRemoteAlbumOption(
|
||||
onEditAlbum: () {},
|
||||
),
|
||||
);
|
||||
|
||||
await tester.tap(find.byIcon(Icons.more_vert_rounded));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.byIcon(Icons.edit), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('shows edit album option when onEditAlbum is provided',
|
||||
(tester) async {
|
||||
bool editCalled = false;
|
||||
|
||||
await tester.pumpConsumerWidget(
|
||||
DriftRemoteAlbumOption(
|
||||
onEditAlbum: () => editCalled = true,
|
||||
),
|
||||
);
|
||||
|
||||
await tester.tap(find.byIcon(Icons.more_vert_rounded));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.byIcon(Icons.edit), findsOneWidget);
|
||||
|
||||
await tester.tap(find.byIcon(Icons.edit));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(editCalled, isTrue);
|
||||
});
|
||||
|
||||
testWidgets('hides edit album option when onEditAlbum is null',
|
||||
(tester) async {
|
||||
await tester.pumpConsumerWidget(
|
||||
DriftRemoteAlbumOption(
|
||||
onAddPhotos: () {},
|
||||
),
|
||||
);
|
||||
|
||||
await tester.tap(find.byIcon(Icons.more_vert_rounded));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.byIcon(Icons.edit), findsNothing);
|
||||
});
|
||||
|
||||
testWidgets('shows add photos option when onAddPhotos is provided',
|
||||
(tester) async {
|
||||
bool addPhotosCalled = false;
|
||||
|
||||
await tester.pumpConsumerWidget(
|
||||
DriftRemoteAlbumOption(
|
||||
onAddPhotos: () => addPhotosCalled = true,
|
||||
),
|
||||
);
|
||||
|
||||
await tester.tap(find.byIcon(Icons.more_vert_rounded));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.byIcon(Icons.add_a_photo), findsOneWidget);
|
||||
|
||||
await tester.tap(find.byIcon(Icons.add_a_photo));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(addPhotosCalled, isTrue);
|
||||
});
|
||||
|
||||
testWidgets('hides add photos option when onAddPhotos is null',
|
||||
(tester) async {
|
||||
await tester.pumpConsumerWidget(
|
||||
DriftRemoteAlbumOption(
|
||||
onEditAlbum: () {},
|
||||
),
|
||||
);
|
||||
|
||||
await tester.tap(find.byIcon(Icons.more_vert_rounded));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.byIcon(Icons.add_a_photo), findsNothing);
|
||||
});
|
||||
|
||||
testWidgets('shows add users option when onAddUsers is provided',
|
||||
(tester) async {
|
||||
bool addUsersCalled = false;
|
||||
|
||||
await tester.pumpConsumerWidget(
|
||||
DriftRemoteAlbumOption(
|
||||
onAddUsers: () => addUsersCalled = true,
|
||||
),
|
||||
);
|
||||
|
||||
await tester.tap(find.byIcon(Icons.more_vert_rounded));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.byIcon(Icons.group_add), findsOneWidget);
|
||||
|
||||
await tester.tap(find.byIcon(Icons.group_add));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(addUsersCalled, isTrue);
|
||||
});
|
||||
|
||||
testWidgets('hides add users option when onAddUsers is null',
|
||||
(tester) async {
|
||||
await tester.pumpConsumerWidget(
|
||||
DriftRemoteAlbumOption(
|
||||
onEditAlbum: () {},
|
||||
),
|
||||
);
|
||||
|
||||
await tester.tap(find.byIcon(Icons.more_vert_rounded));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.byIcon(Icons.group_add), findsNothing);
|
||||
});
|
||||
|
||||
testWidgets('shows leave album option when onLeaveAlbum is provided',
|
||||
(tester) async {
|
||||
bool leaveAlbumCalled = false;
|
||||
|
||||
await tester.pumpConsumerWidget(
|
||||
DriftRemoteAlbumOption(
|
||||
onLeaveAlbum: () => leaveAlbumCalled = true,
|
||||
),
|
||||
);
|
||||
|
||||
await tester.tap(find.byIcon(Icons.more_vert_rounded));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.byIcon(Icons.person_remove_rounded), findsOneWidget);
|
||||
|
||||
await tester.tap(find.byIcon(Icons.person_remove_rounded));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(leaveAlbumCalled, isTrue);
|
||||
});
|
||||
|
||||
testWidgets('hides leave album option when onLeaveAlbum is null',
|
||||
(tester) async {
|
||||
await tester.pumpConsumerWidget(
|
||||
DriftRemoteAlbumOption(
|
||||
onEditAlbum: () {},
|
||||
),
|
||||
);
|
||||
|
||||
await tester.tap(find.byIcon(Icons.more_vert_rounded));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.byIcon(Icons.person_remove_rounded), findsNothing);
|
||||
});
|
||||
|
||||
testWidgets(
|
||||
'shows toggle album order option when onToggleAlbumOrder is provided',
|
||||
(tester) async {
|
||||
bool toggleOrderCalled = false;
|
||||
|
||||
await tester.pumpConsumerWidget(
|
||||
DriftRemoteAlbumOption(
|
||||
onToggleAlbumOrder: () => toggleOrderCalled = true,
|
||||
),
|
||||
);
|
||||
|
||||
await tester.tap(find.byIcon(Icons.more_vert_rounded));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.byIcon(Icons.swap_vert_rounded), findsOneWidget);
|
||||
|
||||
await tester.tap(find.byIcon(Icons.swap_vert_rounded));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(toggleOrderCalled, isTrue);
|
||||
});
|
||||
|
||||
testWidgets('hides toggle album order option when onToggleAlbumOrder is null',
|
||||
(tester) async {
|
||||
await tester.pumpConsumerWidget(
|
||||
DriftRemoteAlbumOption(
|
||||
onEditAlbum: () {},
|
||||
),
|
||||
);
|
||||
|
||||
await tester.tap(find.byIcon(Icons.more_vert_rounded));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.byIcon(Icons.swap_vert_rounded), findsNothing);
|
||||
});
|
||||
|
||||
testWidgets(
|
||||
'shows create shared link option when onCreateSharedLink is provided',
|
||||
(tester) async {
|
||||
bool createSharedLinkCalled = false;
|
||||
|
||||
await tester.pumpConsumerWidget(
|
||||
DriftRemoteAlbumOption(
|
||||
onCreateSharedLink: () => createSharedLinkCalled = true,
|
||||
),
|
||||
);
|
||||
|
||||
await tester.tap(find.byIcon(Icons.more_vert_rounded));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.byIcon(Icons.link), findsOneWidget);
|
||||
|
||||
await tester.tap(find.byIcon(Icons.link));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(createSharedLinkCalled, isTrue);
|
||||
});
|
||||
|
||||
testWidgets('hides create shared link option when onCreateSharedLink is null',
|
||||
(tester) async {
|
||||
await tester.pumpConsumerWidget(
|
||||
DriftRemoteAlbumOption(
|
||||
onEditAlbum: () {},
|
||||
),
|
||||
);
|
||||
|
||||
await tester.tap(find.byIcon(Icons.more_vert_rounded));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.byIcon(Icons.link), findsNothing);
|
||||
});
|
||||
|
||||
testWidgets('shows options option when onShowOptions is provided',
|
||||
(tester) async {
|
||||
bool showOptionsCalled = false;
|
||||
|
||||
await tester.pumpConsumerWidget(
|
||||
DriftRemoteAlbumOption(
|
||||
onShowOptions: () => showOptionsCalled = true,
|
||||
),
|
||||
);
|
||||
|
||||
await tester.tap(find.byIcon(Icons.more_vert_rounded));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.byIcon(Icons.settings), findsOneWidget);
|
||||
|
||||
await tester.tap(find.byIcon(Icons.settings));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(showOptionsCalled, isTrue);
|
||||
});
|
||||
|
||||
testWidgets('hides options option when onShowOptions is null',
|
||||
(tester) async {
|
||||
await tester.pumpConsumerWidget(
|
||||
DriftRemoteAlbumOption(
|
||||
onEditAlbum: () {},
|
||||
),
|
||||
);
|
||||
|
||||
await tester.tap(find.byIcon(Icons.more_vert_rounded));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.byIcon(Icons.settings), findsNothing);
|
||||
});
|
||||
|
||||
testWidgets('shows delete album option when onDeleteAlbum is provided',
|
||||
(tester) async {
|
||||
bool deleteAlbumCalled = false;
|
||||
|
||||
await tester.pumpConsumerWidget(
|
||||
DriftRemoteAlbumOption(
|
||||
onDeleteAlbum: () => deleteAlbumCalled = true,
|
||||
),
|
||||
);
|
||||
|
||||
await tester.tap(find.byIcon(Icons.more_vert_rounded));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.byIcon(Icons.delete), findsOneWidget);
|
||||
|
||||
await tester.tap(find.byIcon(Icons.delete));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(deleteAlbumCalled, isTrue);
|
||||
});
|
||||
|
||||
testWidgets('hides delete album option when onDeleteAlbum is null',
|
||||
(tester) async {
|
||||
await tester.pumpConsumerWidget(
|
||||
DriftRemoteAlbumOption(
|
||||
onEditAlbum: () {},
|
||||
),
|
||||
);
|
||||
|
||||
await tester.tap(find.byIcon(Icons.more_vert_rounded));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.byIcon(Icons.delete), findsNothing);
|
||||
});
|
||||
|
||||
testWidgets('shows divider before delete album option', (tester) async {
|
||||
await tester.pumpConsumerWidget(
|
||||
DriftRemoteAlbumOption(
|
||||
onEditAlbum: () {},
|
||||
onDeleteAlbum: () {},
|
||||
),
|
||||
);
|
||||
|
||||
await tester.tap(find.byIcon(Icons.more_vert_rounded));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.byType(Divider), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('shows all options when all callbacks are provided',
|
||||
(tester) async {
|
||||
await tester.pumpConsumerWidget(
|
||||
DriftRemoteAlbumOption(
|
||||
onEditAlbum: () {},
|
||||
onAddPhotos: () {},
|
||||
onAddUsers: () {},
|
||||
onLeaveAlbum: () {},
|
||||
onToggleAlbumOrder: () {},
|
||||
onCreateSharedLink: () {},
|
||||
onShowOptions: () {},
|
||||
onDeleteAlbum: () {},
|
||||
),
|
||||
);
|
||||
|
||||
await tester.tap(find.byIcon(Icons.more_vert_rounded));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.byIcon(Icons.edit), findsOneWidget);
|
||||
expect(find.byIcon(Icons.add_a_photo), findsOneWidget);
|
||||
expect(find.byIcon(Icons.group_add), findsOneWidget);
|
||||
expect(find.byIcon(Icons.person_remove_rounded), findsOneWidget);
|
||||
expect(find.byIcon(Icons.swap_vert_rounded), findsOneWidget);
|
||||
expect(find.byIcon(Icons.link), findsOneWidget);
|
||||
expect(find.byIcon(Icons.settings), findsOneWidget);
|
||||
expect(find.byIcon(Icons.delete), findsOneWidget);
|
||||
expect(find.byType(Divider), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('shows no options when all callbacks are null', (tester) async {
|
||||
await tester.pumpConsumerWidget(
|
||||
const DriftRemoteAlbumOption(),
|
||||
);
|
||||
|
||||
await tester.tap(find.byIcon(Icons.more_vert_rounded));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.byIcon(Icons.edit), findsNothing);
|
||||
expect(find.byIcon(Icons.add_a_photo), findsNothing);
|
||||
expect(find.byIcon(Icons.group_add), findsNothing);
|
||||
expect(find.byIcon(Icons.person_remove_rounded), findsNothing);
|
||||
expect(find.byIcon(Icons.swap_vert_rounded), findsNothing);
|
||||
expect(find.byIcon(Icons.link), findsNothing);
|
||||
expect(find.byIcon(Icons.settings), findsNothing);
|
||||
expect(find.byIcon(Icons.delete), findsNothing);
|
||||
});
|
||||
|
||||
testWidgets('uses custom icon color when provided', (tester) async {
|
||||
const customColor = Colors.red;
|
||||
|
||||
await tester.pumpConsumerWidget(
|
||||
const DriftRemoteAlbumOption(
|
||||
iconColor: customColor,
|
||||
),
|
||||
);
|
||||
|
||||
final iconButton = tester.widget<IconButton>(find.byType(IconButton));
|
||||
final icon = iconButton.icon as Icon;
|
||||
|
||||
expect(icon.color, equals(customColor));
|
||||
});
|
||||
|
||||
testWidgets('uses default white color when iconColor is null',
|
||||
(tester) async {
|
||||
await tester.pumpConsumerWidget(
|
||||
const DriftRemoteAlbumOption(),
|
||||
);
|
||||
|
||||
final iconButton = tester.widget<IconButton>(find.byType(IconButton));
|
||||
final icon = iconButton.icon as Icon;
|
||||
|
||||
expect(icon.color, equals(Colors.white));
|
||||
});
|
||||
|
||||
testWidgets('applies icon shadows when provided', (tester) async {
|
||||
final shadows = [
|
||||
const Shadow(offset: Offset(0, 2), blurRadius: 5, color: Colors.black),
|
||||
];
|
||||
|
||||
await tester.pumpConsumerWidget(
|
||||
DriftRemoteAlbumOption(
|
||||
iconShadows: shadows,
|
||||
),
|
||||
);
|
||||
|
||||
final iconButton = tester.widget<IconButton>(find.byType(IconButton));
|
||||
final icon = iconButton.icon as Icon;
|
||||
|
||||
expect(icon.shadows, equals(shadows));
|
||||
});
|
||||
|
||||
group('owner vs non-owner scenarios', () {
|
||||
testWidgets('owner sees all management options', (tester) async {
|
||||
// Simulating owner scenario - all callbacks provided
|
||||
await tester.pumpConsumerWidget(
|
||||
DriftRemoteAlbumOption(
|
||||
onEditAlbum: () {},
|
||||
onAddPhotos: () {},
|
||||
onAddUsers: () {},
|
||||
onToggleAlbumOrder: () {},
|
||||
onCreateSharedLink: () {},
|
||||
onShowOptions: () {},
|
||||
onDeleteAlbum: () {},
|
||||
),
|
||||
);
|
||||
|
||||
await tester.tap(find.byIcon(Icons.more_vert_rounded));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
// Owner should see all management options
|
||||
expect(find.byIcon(Icons.edit), findsOneWidget);
|
||||
expect(find.byIcon(Icons.add_a_photo), findsOneWidget);
|
||||
expect(find.byIcon(Icons.group_add), findsOneWidget);
|
||||
expect(find.byIcon(Icons.swap_vert_rounded), findsOneWidget);
|
||||
expect(find.byIcon(Icons.link), findsOneWidget);
|
||||
expect(find.byIcon(Icons.delete), findsOneWidget);
|
||||
// Owner should NOT see leave album
|
||||
expect(find.byIcon(Icons.person_remove_rounded), findsNothing);
|
||||
});
|
||||
|
||||
testWidgets('non-owner with editor role sees limited options',
|
||||
(tester) async {
|
||||
// Simulating non-owner with editor role - can add photos, show options, leave
|
||||
await tester.pumpConsumerWidget(
|
||||
DriftRemoteAlbumOption(
|
||||
onAddPhotos: () {},
|
||||
onShowOptions: () {},
|
||||
onLeaveAlbum: () {},
|
||||
),
|
||||
);
|
||||
|
||||
await tester.tap(find.byIcon(Icons.more_vert_rounded));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
// Editor can add photos
|
||||
expect(find.byIcon(Icons.add_a_photo), findsOneWidget);
|
||||
// Can see options
|
||||
expect(find.byIcon(Icons.settings), findsOneWidget);
|
||||
// Can leave album
|
||||
expect(find.byIcon(Icons.person_remove_rounded), findsOneWidget);
|
||||
// Cannot see owner-only options
|
||||
expect(find.byIcon(Icons.edit), findsNothing);
|
||||
expect(find.byIcon(Icons.group_add), findsNothing);
|
||||
expect(find.byIcon(Icons.swap_vert_rounded), findsNothing);
|
||||
expect(find.byIcon(Icons.link), findsNothing);
|
||||
expect(find.byIcon(Icons.delete), findsNothing);
|
||||
});
|
||||
|
||||
testWidgets('non-owner viewer sees minimal options', (tester) async {
|
||||
// Simulating viewer - can only show options and leave
|
||||
await tester.pumpConsumerWidget(
|
||||
DriftRemoteAlbumOption(
|
||||
onShowOptions: () {},
|
||||
onLeaveAlbum: () {},
|
||||
),
|
||||
);
|
||||
|
||||
await tester.tap(find.byIcon(Icons.more_vert_rounded));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
// Can see options
|
||||
expect(find.byIcon(Icons.settings), findsOneWidget);
|
||||
// Can leave album
|
||||
expect(find.byIcon(Icons.person_remove_rounded), findsOneWidget);
|
||||
// Cannot see any other options
|
||||
expect(find.byIcon(Icons.edit), findsNothing);
|
||||
expect(find.byIcon(Icons.add_a_photo), findsNothing);
|
||||
expect(find.byIcon(Icons.group_add), findsNothing);
|
||||
expect(find.byIcon(Icons.swap_vert_rounded), findsNothing);
|
||||
expect(find.byIcon(Icons.link), findsNothing);
|
||||
expect(find.byIcon(Icons.delete), findsNothing);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
48
mobile/test/repository.mocks.dart
Normal file
48
mobile/test/repository.mocks.dart
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
import 'package:immich_mobile/infrastructure/repositories/exif.repository.dart';
|
||||
import 'package:immich_mobile/repositories/partner_api.repository.dart';
|
||||
import 'package:immich_mobile/repositories/album_media.repository.dart';
|
||||
import 'package:immich_mobile/repositories/album_api.repository.dart';
|
||||
import 'package:immich_mobile/repositories/partner.repository.dart';
|
||||
import 'package:immich_mobile/repositories/etag.repository.dart';
|
||||
import 'package:immich_mobile/repositories/local_files_manager.repository.dart';
|
||||
import 'package:immich_mobile/repositories/file_media.repository.dart';
|
||||
import 'package:immich_mobile/repositories/backup.repository.dart';
|
||||
import 'package:immich_mobile/repositories/auth.repository.dart';
|
||||
import 'package:immich_mobile/repositories/auth_api.repository.dart';
|
||||
import 'package:immich_mobile/repositories/asset.repository.dart';
|
||||
import 'package:immich_mobile/repositories/asset_media.repository.dart';
|
||||
import 'package:immich_mobile/repositories/album.repository.dart';
|
||||
import 'package:immich_mobile/repositories/asset_api.repository.dart';
|
||||
import 'package:mocktail/mocktail.dart';
|
||||
|
||||
class MockAlbumRepository extends Mock implements AlbumRepository {}
|
||||
|
||||
class MockAssetRepository extends Mock implements AssetRepository {}
|
||||
|
||||
class MockBackupRepository extends Mock implements BackupAlbumRepository {}
|
||||
|
||||
class MockExifInfoRepository extends Mock implements IsarExifRepository {}
|
||||
|
||||
class MockETagRepository extends Mock implements ETagRepository {}
|
||||
|
||||
class MockAlbumMediaRepository extends Mock implements AlbumMediaRepository {}
|
||||
|
||||
class MockBackupAlbumRepository extends Mock implements BackupAlbumRepository {}
|
||||
|
||||
class MockAssetApiRepository extends Mock implements AssetApiRepository {}
|
||||
|
||||
class MockAssetMediaRepository extends Mock implements AssetMediaRepository {}
|
||||
|
||||
class MockFileMediaRepository extends Mock implements FileMediaRepository {}
|
||||
|
||||
class MockAlbumApiRepository extends Mock implements AlbumApiRepository {}
|
||||
|
||||
class MockAuthApiRepository extends Mock implements AuthApiRepository {}
|
||||
|
||||
class MockAuthRepository extends Mock implements AuthRepository {}
|
||||
|
||||
class MockPartnerRepository extends Mock implements PartnerRepository {}
|
||||
|
||||
class MockPartnerApiRepository extends Mock implements PartnerApiRepository {}
|
||||
|
||||
class MockLocalFilesManagerRepository extends Mock implements LocalFilesManagerRepository {}
|
||||
31
mobile/test/service.mocks.dart
Normal file
31
mobile/test/service.mocks.dart
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
import 'package:immich_mobile/services/album.service.dart';
|
||||
import 'package:immich_mobile/services/api.service.dart';
|
||||
import 'package:immich_mobile/services/app_settings.service.dart';
|
||||
import 'package:immich_mobile/services/background.service.dart';
|
||||
import 'package:immich_mobile/services/backup.service.dart';
|
||||
import 'package:immich_mobile/services/entity.service.dart';
|
||||
import 'package:immich_mobile/services/hash.service.dart';
|
||||
import 'package:immich_mobile/services/network.service.dart';
|
||||
import 'package:immich_mobile/services/sync.service.dart';
|
||||
import 'package:mocktail/mocktail.dart';
|
||||
import 'package:openapi/api.dart';
|
||||
|
||||
class MockApiService extends Mock implements ApiService {}
|
||||
|
||||
class MockAlbumService extends Mock implements AlbumService {}
|
||||
|
||||
class MockBackupService extends Mock implements BackupService {}
|
||||
|
||||
class MockSyncService extends Mock implements SyncService {}
|
||||
|
||||
class MockHashService extends Mock implements HashService {}
|
||||
|
||||
class MockEntityService extends Mock implements EntityService {}
|
||||
|
||||
class MockNetworkService extends Mock implements NetworkService {}
|
||||
|
||||
class MockSearchApi extends Mock implements SearchApi {}
|
||||
|
||||
class MockAppSettingService extends Mock implements AppSettingsService {}
|
||||
|
||||
class MockBackgroundService extends Mock implements BackgroundService {}
|
||||
118
mobile/test/services/action.service_test.dart
Normal file
118
mobile/test/services/action.service_test.dart
Normal file
|
|
@ -0,0 +1,118 @@
|
|||
import 'package:drift/drift.dart' as drift;
|
||||
import 'package:drift/native.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:immich_mobile/domain/models/store.model.dart';
|
||||
import 'package:immich_mobile/domain/services/store.service.dart';
|
||||
import 'package:immich_mobile/entities/store.entity.dart';
|
||||
import 'package:immich_mobile/infrastructure/repositories/db.repository.dart';
|
||||
import 'package:immich_mobile/infrastructure/repositories/store.repository.dart';
|
||||
import 'package:immich_mobile/repositories/download.repository.dart';
|
||||
import 'package:immich_mobile/services/action.service.dart';
|
||||
import 'package:mocktail/mocktail.dart';
|
||||
|
||||
import '../infrastructure/repository.mock.dart';
|
||||
import '../repository.mocks.dart';
|
||||
|
||||
class MockDownloadRepository extends Mock implements DownloadRepository {}
|
||||
|
||||
void main() {
|
||||
late ActionService sut;
|
||||
|
||||
late MockAssetApiRepository assetApiRepository;
|
||||
late MockRemoteAssetRepository remoteAssetRepository;
|
||||
late MockDriftLocalAssetRepository localAssetRepository;
|
||||
late MockDriftAlbumApiRepository albumApiRepository;
|
||||
late MockRemoteAlbumRepository remoteAlbumRepository;
|
||||
late MockTrashedLocalAssetRepository trashedLocalAssetRepository;
|
||||
late MockAssetMediaRepository assetMediaRepository;
|
||||
late MockDownloadRepository downloadRepository;
|
||||
|
||||
late Drift db;
|
||||
|
||||
setUpAll(() async {
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
debugDefaultTargetPlatformOverride = TargetPlatform.android;
|
||||
|
||||
db = Drift(drift.DatabaseConnection(NativeDatabase.memory(), closeStreamsSynchronously: true));
|
||||
await StoreService.init(storeRepository: DriftStoreRepository(db));
|
||||
});
|
||||
|
||||
tearDownAll(() async {
|
||||
debugDefaultTargetPlatformOverride = null;
|
||||
await Store.clear();
|
||||
await db.close();
|
||||
});
|
||||
|
||||
setUp(() {
|
||||
assetApiRepository = MockAssetApiRepository();
|
||||
remoteAssetRepository = MockRemoteAssetRepository();
|
||||
localAssetRepository = MockDriftLocalAssetRepository();
|
||||
albumApiRepository = MockDriftAlbumApiRepository();
|
||||
remoteAlbumRepository = MockRemoteAlbumRepository();
|
||||
trashedLocalAssetRepository = MockTrashedLocalAssetRepository();
|
||||
assetMediaRepository = MockAssetMediaRepository();
|
||||
downloadRepository = MockDownloadRepository();
|
||||
|
||||
sut = ActionService(
|
||||
assetApiRepository,
|
||||
remoteAssetRepository,
|
||||
localAssetRepository,
|
||||
albumApiRepository,
|
||||
remoteAlbumRepository,
|
||||
trashedLocalAssetRepository,
|
||||
assetMediaRepository,
|
||||
downloadRepository,
|
||||
);
|
||||
});
|
||||
|
||||
tearDown(() async {
|
||||
await Store.clear();
|
||||
});
|
||||
|
||||
group('ActionService.deleteLocal', () {
|
||||
test('routes deleted ids to trashed repository when Android trash handling is enabled', () async {
|
||||
await Store.put(StoreKey.manageLocalMediaAndroid, true);
|
||||
const ids = ['a', 'b'];
|
||||
|
||||
when(() => assetMediaRepository.deleteAll(ids)).thenAnswer((_) async => ids);
|
||||
when(() => trashedLocalAssetRepository.applyTrashedAssets(ids)).thenAnswer((_) async {});
|
||||
|
||||
final result = await sut.deleteLocal(ids);
|
||||
|
||||
expect(result, ids.length);
|
||||
verify(() => assetMediaRepository.deleteAll(ids)).called(1);
|
||||
verify(() => trashedLocalAssetRepository.applyTrashedAssets(ids)).called(1);
|
||||
verifyNever(() => localAssetRepository.delete(any()));
|
||||
});
|
||||
|
||||
test('deletes locally when Android trash handling is disabled', () async {
|
||||
await Store.put(StoreKey.manageLocalMediaAndroid, false);
|
||||
const ids = ['c'];
|
||||
|
||||
when(() => assetMediaRepository.deleteAll(ids)).thenAnswer((_) async => ids);
|
||||
when(() => localAssetRepository.delete(ids)).thenAnswer((_) async {});
|
||||
|
||||
final result = await sut.deleteLocal(ids);
|
||||
|
||||
expect(result, ids.length);
|
||||
verify(() => assetMediaRepository.deleteAll(ids)).called(1);
|
||||
verify(() => localAssetRepository.delete(ids)).called(1);
|
||||
verifyNever(() => trashedLocalAssetRepository.applyTrashedAssets(any()));
|
||||
});
|
||||
|
||||
test('short-circuits when nothing was deleted', () async {
|
||||
await Store.put(StoreKey.manageLocalMediaAndroid, true);
|
||||
const ids = ['x'];
|
||||
|
||||
when(() => assetMediaRepository.deleteAll(ids)).thenAnswer((_) async => <String>[]);
|
||||
|
||||
final result = await sut.deleteLocal(ids);
|
||||
|
||||
expect(result, 0);
|
||||
verify(() => assetMediaRepository.deleteAll(ids)).called(1);
|
||||
verifyNever(() => trashedLocalAssetRepository.applyTrashedAssets(any()));
|
||||
verifyNever(() => localAssetRepository.delete(any()));
|
||||
});
|
||||
});
|
||||
}
|
||||
177
mobile/test/services/album.service_test.dart
Normal file
177
mobile/test/services/album.service_test.dart
Normal file
|
|
@ -0,0 +1,177 @@
|
|||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:immich_mobile/entities/backup_album.entity.dart';
|
||||
import 'package:immich_mobile/services/album.service.dart';
|
||||
import 'package:mocktail/mocktail.dart';
|
||||
|
||||
import '../domain/service.mock.dart';
|
||||
import '../fixtures/album.stub.dart';
|
||||
import '../fixtures/asset.stub.dart';
|
||||
import '../fixtures/user.stub.dart';
|
||||
import '../repository.mocks.dart';
|
||||
import '../service.mocks.dart';
|
||||
|
||||
void main() {
|
||||
late AlbumService sut;
|
||||
late MockUserService userService;
|
||||
late MockSyncService syncService;
|
||||
late MockEntityService entityService;
|
||||
late MockAlbumRepository albumRepository;
|
||||
late MockAssetRepository assetRepository;
|
||||
late MockBackupRepository backupRepository;
|
||||
late MockAlbumMediaRepository albumMediaRepository;
|
||||
late MockAlbumApiRepository albumApiRepository;
|
||||
|
||||
setUp(() {
|
||||
userService = MockUserService();
|
||||
syncService = MockSyncService();
|
||||
entityService = MockEntityService();
|
||||
albumRepository = MockAlbumRepository();
|
||||
assetRepository = MockAssetRepository();
|
||||
backupRepository = MockBackupRepository();
|
||||
albumMediaRepository = MockAlbumMediaRepository();
|
||||
albumApiRepository = MockAlbumApiRepository();
|
||||
|
||||
when(() => userService.getMyUser()).thenReturn(UserStub.user1);
|
||||
|
||||
when(
|
||||
() => albumRepository.transaction<void>(any()),
|
||||
).thenAnswer((call) => (call.positionalArguments.first as Function).call());
|
||||
when(
|
||||
() => assetRepository.transaction<Null>(any()),
|
||||
).thenAnswer((call) => (call.positionalArguments.first as Function).call());
|
||||
|
||||
sut = AlbumService(
|
||||
syncService,
|
||||
userService,
|
||||
entityService,
|
||||
albumRepository,
|
||||
assetRepository,
|
||||
backupRepository,
|
||||
albumMediaRepository,
|
||||
albumApiRepository,
|
||||
);
|
||||
});
|
||||
|
||||
group('refreshDeviceAlbums', () {
|
||||
test('empty selection with one album in db', () async {
|
||||
when(() => backupRepository.getIdsBySelection(BackupSelection.exclude)).thenAnswer((_) async => []);
|
||||
when(() => backupRepository.getIdsBySelection(BackupSelection.select)).thenAnswer((_) async => []);
|
||||
when(() => albumMediaRepository.getAll()).thenAnswer((_) async => []);
|
||||
when(() => albumRepository.count(local: true)).thenAnswer((_) async => 1);
|
||||
when(() => syncService.removeAllLocalAlbumsAndAssets()).thenAnswer((_) async => true);
|
||||
final result = await sut.refreshDeviceAlbums();
|
||||
expect(result, false);
|
||||
verify(() => syncService.removeAllLocalAlbumsAndAssets());
|
||||
});
|
||||
|
||||
test('one selected albums, two on device', () async {
|
||||
when(() => backupRepository.getIdsBySelection(BackupSelection.exclude)).thenAnswer((_) async => []);
|
||||
when(
|
||||
() => backupRepository.getIdsBySelection(BackupSelection.select),
|
||||
).thenAnswer((_) async => [AlbumStub.oneAsset.localId!]);
|
||||
when(() => albumMediaRepository.getAll()).thenAnswer((_) async => [AlbumStub.oneAsset, AlbumStub.twoAsset]);
|
||||
when(() => syncService.syncLocalAlbumAssetsToDb(any(), any())).thenAnswer((_) async => true);
|
||||
final result = await sut.refreshDeviceAlbums();
|
||||
expect(result, true);
|
||||
verify(() => syncService.syncLocalAlbumAssetsToDb([AlbumStub.oneAsset], null)).called(1);
|
||||
verifyNoMoreInteractions(syncService);
|
||||
});
|
||||
});
|
||||
|
||||
group('refreshRemoteAlbums', () {
|
||||
test('is working', () async {
|
||||
when(() => syncService.getUsersFromServer()).thenAnswer((_) async => []);
|
||||
when(() => syncService.syncUsersFromServer(any())).thenAnswer((_) async => true);
|
||||
when(() => albumApiRepository.getAll(shared: true)).thenAnswer((_) async => [AlbumStub.sharedWithUser]);
|
||||
|
||||
when(
|
||||
() => albumApiRepository.getAll(shared: null),
|
||||
).thenAnswer((_) async => [AlbumStub.oneAsset, AlbumStub.twoAsset]);
|
||||
|
||||
when(
|
||||
() => syncService.syncRemoteAlbumsToDb([AlbumStub.twoAsset, AlbumStub.oneAsset, AlbumStub.sharedWithUser]),
|
||||
).thenAnswer((_) async => true);
|
||||
final result = await sut.refreshRemoteAlbums();
|
||||
expect(result, true);
|
||||
verify(() => syncService.getUsersFromServer()).called(1);
|
||||
verify(() => syncService.syncUsersFromServer([])).called(1);
|
||||
verify(() => albumApiRepository.getAll(shared: true)).called(1);
|
||||
verify(() => albumApiRepository.getAll(shared: null)).called(1);
|
||||
verify(
|
||||
() => syncService.syncRemoteAlbumsToDb([AlbumStub.twoAsset, AlbumStub.oneAsset, AlbumStub.sharedWithUser]),
|
||||
).called(1);
|
||||
verifyNoMoreInteractions(userService);
|
||||
verifyNoMoreInteractions(albumApiRepository);
|
||||
verifyNoMoreInteractions(syncService);
|
||||
});
|
||||
});
|
||||
|
||||
group('createAlbum', () {
|
||||
test('shared with assets', () async {
|
||||
when(
|
||||
() => albumApiRepository.create(
|
||||
"name",
|
||||
assetIds: any(named: "assetIds"),
|
||||
sharedUserIds: any(named: "sharedUserIds"),
|
||||
),
|
||||
).thenAnswer((_) async => AlbumStub.oneAsset);
|
||||
|
||||
when(
|
||||
() => entityService.fillAlbumWithDatabaseEntities(AlbumStub.oneAsset),
|
||||
).thenAnswer((_) async => AlbumStub.oneAsset);
|
||||
|
||||
when(() => albumRepository.create(AlbumStub.oneAsset)).thenAnswer((_) async => AlbumStub.twoAsset);
|
||||
|
||||
final result = await sut.createAlbum("name", [AssetStub.image1], [UserStub.user1]);
|
||||
expect(result, AlbumStub.twoAsset);
|
||||
verify(
|
||||
() => albumApiRepository.create(
|
||||
"name",
|
||||
assetIds: [AssetStub.image1.remoteId!],
|
||||
sharedUserIds: [UserStub.user1.id],
|
||||
),
|
||||
).called(1);
|
||||
verify(() => entityService.fillAlbumWithDatabaseEntities(AlbumStub.oneAsset)).called(1);
|
||||
});
|
||||
});
|
||||
|
||||
group('addAdditionalAssetToAlbum', () {
|
||||
test('one added, one duplicate', () async {
|
||||
when(
|
||||
() => albumApiRepository.addAssets(AlbumStub.oneAsset.remoteId!, any()),
|
||||
).thenAnswer((_) async => (added: [AssetStub.image2.remoteId!], duplicates: [AssetStub.image1.remoteId!]));
|
||||
when(() => albumRepository.get(AlbumStub.oneAsset.id)).thenAnswer((_) async => AlbumStub.oneAsset);
|
||||
when(() => albumRepository.addAssets(AlbumStub.oneAsset, [AssetStub.image2])).thenAnswer((_) async {});
|
||||
when(() => albumRepository.removeAssets(AlbumStub.oneAsset, [])).thenAnswer((_) async {});
|
||||
when(() => albumRepository.recalculateMetadata(AlbumStub.oneAsset)).thenAnswer((_) async => AlbumStub.oneAsset);
|
||||
when(() => albumRepository.update(AlbumStub.oneAsset)).thenAnswer((_) async => AlbumStub.oneAsset);
|
||||
|
||||
final result = await sut.addAssets(AlbumStub.oneAsset, [AssetStub.image1, AssetStub.image2]);
|
||||
|
||||
expect(result != null, true);
|
||||
expect(result!.alreadyInAlbum, [AssetStub.image1.remoteId!]);
|
||||
expect(result.successfullyAdded, 1);
|
||||
});
|
||||
});
|
||||
|
||||
group('addAdditionalUserToAlbum', () {
|
||||
test('one added', () async {
|
||||
when(
|
||||
() => albumApiRepository.addUsers(AlbumStub.emptyAlbum.remoteId!, any()),
|
||||
).thenAnswer((_) async => AlbumStub.sharedWithUser);
|
||||
|
||||
when(
|
||||
() => albumRepository.addUsers(
|
||||
AlbumStub.emptyAlbum,
|
||||
AlbumStub.emptyAlbum.sharedUsers.map((u) => u.toDto()).toList(),
|
||||
),
|
||||
).thenAnswer((_) async => AlbumStub.emptyAlbum);
|
||||
|
||||
when(() => albumRepository.update(AlbumStub.emptyAlbum)).thenAnswer((_) async => AlbumStub.emptyAlbum);
|
||||
|
||||
final result = await sut.addUsers(AlbumStub.emptyAlbum, [UserStub.user2.id]);
|
||||
|
||||
expect(result, true);
|
||||
});
|
||||
});
|
||||
}
|
||||
103
mobile/test/services/asset.service_test.dart
Normal file
103
mobile/test/services/asset.service_test.dart
Normal file
|
|
@ -0,0 +1,103 @@
|
|||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:immich_mobile/entities/asset.entity.dart';
|
||||
import 'package:immich_mobile/services/asset.service.dart';
|
||||
import 'package:maplibre_gl/maplibre_gl.dart';
|
||||
import 'package:mocktail/mocktail.dart';
|
||||
import 'package:openapi/api.dart';
|
||||
|
||||
import '../api.mocks.dart';
|
||||
import '../domain/service.mock.dart';
|
||||
import '../fixtures/asset.stub.dart';
|
||||
import '../infrastructure/repository.mock.dart';
|
||||
import '../repository.mocks.dart';
|
||||
import '../service.mocks.dart';
|
||||
|
||||
class FakeAssetBulkUpdateDto extends Fake implements AssetBulkUpdateDto {}
|
||||
|
||||
void main() {
|
||||
late AssetService sut;
|
||||
|
||||
late MockAssetRepository assetRepository;
|
||||
late MockAssetApiRepository assetApiRepository;
|
||||
late MockExifInfoRepository exifInfoRepository;
|
||||
late MockETagRepository eTagRepository;
|
||||
late MockBackupAlbumRepository backupAlbumRepository;
|
||||
late MockIsarUserRepository userRepository;
|
||||
late MockAssetMediaRepository assetMediaRepository;
|
||||
late MockApiService apiService;
|
||||
|
||||
late MockSyncService syncService;
|
||||
late MockAlbumService albumService;
|
||||
late MockBackupService backupService;
|
||||
late MockUserService userService;
|
||||
|
||||
setUp(() {
|
||||
assetRepository = MockAssetRepository();
|
||||
assetApiRepository = MockAssetApiRepository();
|
||||
exifInfoRepository = MockExifInfoRepository();
|
||||
userRepository = MockIsarUserRepository();
|
||||
eTagRepository = MockETagRepository();
|
||||
backupAlbumRepository = MockBackupAlbumRepository();
|
||||
apiService = MockApiService();
|
||||
assetMediaRepository = MockAssetMediaRepository();
|
||||
|
||||
syncService = MockSyncService();
|
||||
userService = MockUserService();
|
||||
albumService = MockAlbumService();
|
||||
backupService = MockBackupService();
|
||||
|
||||
sut = AssetService(
|
||||
assetApiRepository,
|
||||
assetRepository,
|
||||
exifInfoRepository,
|
||||
userRepository,
|
||||
eTagRepository,
|
||||
backupAlbumRepository,
|
||||
apiService,
|
||||
syncService,
|
||||
backupService,
|
||||
albumService,
|
||||
userService,
|
||||
assetMediaRepository,
|
||||
);
|
||||
|
||||
registerFallbackValue(FakeAssetBulkUpdateDto());
|
||||
});
|
||||
|
||||
group("Edit ExifInfo", () {
|
||||
late AssetsApi assetsApi;
|
||||
setUp(() {
|
||||
assetsApi = MockAssetsApi();
|
||||
when(() => apiService.assetsApi).thenReturn(assetsApi);
|
||||
when(() => assetsApi.updateAssets(any())).thenAnswer((_) async => Future.value());
|
||||
});
|
||||
|
||||
test("asset is updated with DateTime", () async {
|
||||
final assets = [AssetStub.image1, AssetStub.image2];
|
||||
final dateTime = DateTime.utc(2025, 6, 4, 2, 57);
|
||||
await sut.changeDateTime(assets, dateTime.toIso8601String());
|
||||
|
||||
verify(() => assetsApi.updateAssets(any())).called(1);
|
||||
final upsertExifCallback = verify(() => syncService.upsertAssetsWithExif(captureAny()));
|
||||
upsertExifCallback.called(1);
|
||||
final receivedAssets = upsertExifCallback.captured.firstOrNull as List<Object>? ?? [];
|
||||
final receivedDatetime = receivedAssets.cast<Asset>().map((a) => a.exifInfo?.dateTimeOriginal ?? DateTime(0));
|
||||
expect(receivedDatetime.every((d) => d == dateTime), isTrue);
|
||||
});
|
||||
|
||||
test("asset is updated with LatLng", () async {
|
||||
final assets = [AssetStub.image1, AssetStub.image2];
|
||||
final latLng = const LatLng(37.7749, -122.4194);
|
||||
await sut.changeLocation(assets, latLng);
|
||||
|
||||
verify(() => assetsApi.updateAssets(any())).called(1);
|
||||
final upsertExifCallback = verify(() => syncService.upsertAssetsWithExif(captureAny()));
|
||||
upsertExifCallback.called(1);
|
||||
final receivedAssets = upsertExifCallback.captured.firstOrNull as List<Object>? ?? [];
|
||||
final receivedCoords = receivedAssets.cast<Asset>().map(
|
||||
(a) => LatLng(a.exifInfo?.latitude ?? 0, a.exifInfo?.longitude ?? 0),
|
||||
);
|
||||
expect(receivedCoords.every((l) => l == latLng), isTrue);
|
||||
});
|
||||
});
|
||||
}
|
||||
285
mobile/test/services/auth.service_test.dart
Normal file
285
mobile/test/services/auth.service_test.dart
Normal file
|
|
@ -0,0 +1,285 @@
|
|||
import 'package:flutter/widgets.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:immich_mobile/domain/services/store.service.dart';
|
||||
import 'package:immich_mobile/infrastructure/repositories/store.repository.dart';
|
||||
import 'package:immich_mobile/models/auth/auxilary_endpoint.model.dart';
|
||||
import 'package:immich_mobile/services/app_settings.service.dart';
|
||||
import 'package:immich_mobile/services/auth.service.dart';
|
||||
import 'package:isar/isar.dart';
|
||||
import 'package:mocktail/mocktail.dart';
|
||||
import 'package:openapi/api.dart';
|
||||
|
||||
import '../domain/service.mock.dart';
|
||||
import '../repository.mocks.dart';
|
||||
import '../service.mocks.dart';
|
||||
import '../test_utils.dart';
|
||||
|
||||
void main() {
|
||||
late AuthService sut;
|
||||
late MockAuthApiRepository authApiRepository;
|
||||
late MockAuthRepository authRepository;
|
||||
late MockApiService apiService;
|
||||
late MockNetworkService networkService;
|
||||
late MockBackgroundSyncManager backgroundSyncManager;
|
||||
late MockAppSettingService appSettingsService;
|
||||
late Isar db;
|
||||
|
||||
setUp(() async {
|
||||
authApiRepository = MockAuthApiRepository();
|
||||
authRepository = MockAuthRepository();
|
||||
apiService = MockApiService();
|
||||
networkService = MockNetworkService();
|
||||
backgroundSyncManager = MockBackgroundSyncManager();
|
||||
appSettingsService = MockAppSettingService();
|
||||
|
||||
sut = AuthService(
|
||||
authApiRepository,
|
||||
authRepository,
|
||||
apiService,
|
||||
networkService,
|
||||
backgroundSyncManager,
|
||||
appSettingsService,
|
||||
);
|
||||
|
||||
registerFallbackValue(Uri());
|
||||
});
|
||||
|
||||
setUpAll(() async {
|
||||
db = await TestUtils.initIsar();
|
||||
db.writeTxnSync(() => db.clearSync());
|
||||
await StoreService.init(storeRepository: IsarStoreRepository(db));
|
||||
});
|
||||
|
||||
group('validateServerUrl', () {
|
||||
setUpAll(() async {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
final db = await TestUtils.initIsar();
|
||||
db.writeTxnSync(() => db.clearSync());
|
||||
await StoreService.init(storeRepository: IsarStoreRepository(db));
|
||||
});
|
||||
|
||||
test('Should resolve HTTP endpoint', () async {
|
||||
const testUrl = 'http://ip:2283';
|
||||
const resolvedUrl = 'http://ip:2283/api';
|
||||
|
||||
when(() => apiService.resolveAndSetEndpoint(testUrl)).thenAnswer((_) async => resolvedUrl);
|
||||
when(() => apiService.setDeviceInfoHeader()).thenAnswer((_) async => {});
|
||||
|
||||
final result = await sut.validateServerUrl(testUrl);
|
||||
|
||||
expect(result, resolvedUrl);
|
||||
|
||||
verify(() => apiService.resolveAndSetEndpoint(testUrl)).called(1);
|
||||
verify(() => apiService.setDeviceInfoHeader()).called(1);
|
||||
});
|
||||
|
||||
test('Should resolve HTTPS endpoint', () async {
|
||||
const testUrl = 'https://immich.domain.com';
|
||||
const resolvedUrl = 'https://immich.domain.com/api';
|
||||
|
||||
when(() => apiService.resolveAndSetEndpoint(testUrl)).thenAnswer((_) async => resolvedUrl);
|
||||
when(() => apiService.setDeviceInfoHeader()).thenAnswer((_) async => {});
|
||||
|
||||
final result = await sut.validateServerUrl(testUrl);
|
||||
|
||||
expect(result, resolvedUrl);
|
||||
|
||||
verify(() => apiService.resolveAndSetEndpoint(testUrl)).called(1);
|
||||
verify(() => apiService.setDeviceInfoHeader()).called(1);
|
||||
});
|
||||
|
||||
test('Should throw error on invalid URL', () async {
|
||||
const testUrl = 'invalid-url';
|
||||
|
||||
when(() => apiService.resolveAndSetEndpoint(testUrl)).thenThrow(Exception('Invalid URL'));
|
||||
|
||||
expect(() async => await sut.validateServerUrl(testUrl), throwsA(isA<Exception>()));
|
||||
|
||||
verify(() => apiService.resolveAndSetEndpoint(testUrl)).called(1);
|
||||
verifyNever(() => apiService.setDeviceInfoHeader());
|
||||
});
|
||||
|
||||
test('Should throw error on unreachable server', () async {
|
||||
const testUrl = 'https://unreachable.server';
|
||||
|
||||
when(() => apiService.resolveAndSetEndpoint(testUrl)).thenThrow(Exception('Server is not reachable'));
|
||||
|
||||
expect(() async => await sut.validateServerUrl(testUrl), throwsA(isA<Exception>()));
|
||||
|
||||
verify(() => apiService.resolveAndSetEndpoint(testUrl)).called(1);
|
||||
verifyNever(() => apiService.setDeviceInfoHeader());
|
||||
});
|
||||
});
|
||||
|
||||
group('logout', () {
|
||||
test('Should logout user', () async {
|
||||
when(() => authApiRepository.logout()).thenAnswer((_) async => {});
|
||||
when(() => backgroundSyncManager.cancel()).thenAnswer((_) async => {});
|
||||
when(() => authRepository.clearLocalData()).thenAnswer((_) => Future.value(null));
|
||||
when(
|
||||
() => appSettingsService.setSetting(AppSettingsEnum.enableBackup, false),
|
||||
).thenAnswer((_) => Future.value(null));
|
||||
await sut.logout();
|
||||
|
||||
verify(() => authApiRepository.logout()).called(1);
|
||||
verify(() => backgroundSyncManager.cancel()).called(1);
|
||||
verify(() => authRepository.clearLocalData()).called(1);
|
||||
});
|
||||
|
||||
test('Should clear local data even on server error', () async {
|
||||
when(() => authApiRepository.logout()).thenThrow(Exception('Server error'));
|
||||
when(() => backgroundSyncManager.cancel()).thenAnswer((_) async => {});
|
||||
when(() => authRepository.clearLocalData()).thenAnswer((_) => Future.value(null));
|
||||
when(
|
||||
() => appSettingsService.setSetting(AppSettingsEnum.enableBackup, false),
|
||||
).thenAnswer((_) => Future.value(null));
|
||||
await sut.logout();
|
||||
|
||||
verify(() => authApiRepository.logout()).called(1);
|
||||
verify(() => backgroundSyncManager.cancel()).called(1);
|
||||
verify(() => authRepository.clearLocalData()).called(1);
|
||||
});
|
||||
});
|
||||
|
||||
group('setOpenApiServiceEndpoint', () {
|
||||
setUp(() {
|
||||
when(() => networkService.getWifiName()).thenAnswer((_) async => 'TestWifi');
|
||||
});
|
||||
|
||||
test('Should return null if auto endpoint switching is disabled', () async {
|
||||
when(() => authRepository.getEndpointSwitchingFeature()).thenReturn((false));
|
||||
|
||||
final result = await sut.setOpenApiServiceEndpoint();
|
||||
|
||||
expect(result, isNull);
|
||||
verify(() => authRepository.getEndpointSwitchingFeature()).called(1);
|
||||
verifyNever(() => networkService.getWifiName());
|
||||
});
|
||||
|
||||
test('Should set local connection if wifi name matches', () async {
|
||||
when(() => authRepository.getEndpointSwitchingFeature()).thenReturn(true);
|
||||
when(() => authRepository.getPreferredWifiName()).thenReturn('TestWifi');
|
||||
when(() => authRepository.getLocalEndpoint()).thenReturn('http://local.endpoint');
|
||||
when(
|
||||
() => apiService.resolveAndSetEndpoint('http://local.endpoint'),
|
||||
).thenAnswer((_) async => 'http://local.endpoint');
|
||||
|
||||
final result = await sut.setOpenApiServiceEndpoint();
|
||||
|
||||
expect(result, 'http://local.endpoint');
|
||||
verify(() => authRepository.getEndpointSwitchingFeature()).called(1);
|
||||
verify(() => networkService.getWifiName()).called(1);
|
||||
verify(() => authRepository.getPreferredWifiName()).called(1);
|
||||
verify(() => authRepository.getLocalEndpoint()).called(1);
|
||||
verify(() => apiService.resolveAndSetEndpoint('http://local.endpoint')).called(1);
|
||||
});
|
||||
|
||||
test('Should set external endpoint if wifi name not matching', () async {
|
||||
when(() => authRepository.getEndpointSwitchingFeature()).thenReturn(true);
|
||||
when(() => authRepository.getPreferredWifiName()).thenReturn('DifferentWifi');
|
||||
when(
|
||||
() => authRepository.getExternalEndpointList(),
|
||||
).thenReturn([const AuxilaryEndpoint(url: 'https://external.endpoint', status: AuxCheckStatus.valid)]);
|
||||
when(
|
||||
() => apiService.resolveAndSetEndpoint('https://external.endpoint'),
|
||||
).thenAnswer((_) async => 'https://external.endpoint/api');
|
||||
|
||||
final result = await sut.setOpenApiServiceEndpoint();
|
||||
|
||||
expect(result, 'https://external.endpoint/api');
|
||||
verify(() => authRepository.getEndpointSwitchingFeature()).called(1);
|
||||
verify(() => networkService.getWifiName()).called(1);
|
||||
verify(() => authRepository.getPreferredWifiName()).called(1);
|
||||
verify(() => authRepository.getExternalEndpointList()).called(1);
|
||||
verify(() => apiService.resolveAndSetEndpoint('https://external.endpoint')).called(1);
|
||||
});
|
||||
|
||||
test('Should set second external endpoint if the first throw any error', () async {
|
||||
when(() => authRepository.getEndpointSwitchingFeature()).thenReturn(true);
|
||||
when(() => authRepository.getPreferredWifiName()).thenReturn('DifferentWifi');
|
||||
when(() => authRepository.getExternalEndpointList()).thenReturn([
|
||||
const AuxilaryEndpoint(url: 'https://external.endpoint', status: AuxCheckStatus.valid),
|
||||
const AuxilaryEndpoint(url: 'https://external.endpoint2', status: AuxCheckStatus.valid),
|
||||
]);
|
||||
|
||||
when(
|
||||
() => apiService.resolveAndSetEndpoint('https://external.endpoint'),
|
||||
).thenThrow(Exception('Invalid endpoint'));
|
||||
when(
|
||||
() => apiService.resolveAndSetEndpoint('https://external.endpoint2'),
|
||||
).thenAnswer((_) async => 'https://external.endpoint2/api');
|
||||
|
||||
final result = await sut.setOpenApiServiceEndpoint();
|
||||
|
||||
expect(result, 'https://external.endpoint2/api');
|
||||
verify(() => authRepository.getEndpointSwitchingFeature()).called(1);
|
||||
verify(() => networkService.getWifiName()).called(1);
|
||||
verify(() => authRepository.getPreferredWifiName()).called(1);
|
||||
verify(() => authRepository.getExternalEndpointList()).called(1);
|
||||
verify(() => apiService.resolveAndSetEndpoint('https://external.endpoint2')).called(1);
|
||||
});
|
||||
|
||||
test('Should set second external endpoint if the first throw ApiException', () async {
|
||||
when(() => authRepository.getEndpointSwitchingFeature()).thenReturn(true);
|
||||
when(() => authRepository.getPreferredWifiName()).thenReturn('DifferentWifi');
|
||||
when(() => authRepository.getExternalEndpointList()).thenReturn([
|
||||
const AuxilaryEndpoint(url: 'https://external.endpoint', status: AuxCheckStatus.valid),
|
||||
const AuxilaryEndpoint(url: 'https://external.endpoint2', status: AuxCheckStatus.valid),
|
||||
]);
|
||||
|
||||
when(
|
||||
() => apiService.resolveAndSetEndpoint('https://external.endpoint'),
|
||||
).thenThrow(ApiException(503, 'Invalid endpoint'));
|
||||
when(
|
||||
() => apiService.resolveAndSetEndpoint('https://external.endpoint2'),
|
||||
).thenAnswer((_) async => 'https://external.endpoint2/api');
|
||||
|
||||
final result = await sut.setOpenApiServiceEndpoint();
|
||||
|
||||
expect(result, 'https://external.endpoint2/api');
|
||||
verify(() => authRepository.getEndpointSwitchingFeature()).called(1);
|
||||
verify(() => networkService.getWifiName()).called(1);
|
||||
verify(() => authRepository.getPreferredWifiName()).called(1);
|
||||
verify(() => authRepository.getExternalEndpointList()).called(1);
|
||||
verify(() => apiService.resolveAndSetEndpoint('https://external.endpoint2')).called(1);
|
||||
});
|
||||
|
||||
test('Should handle error when setting local connection', () async {
|
||||
when(() => authRepository.getEndpointSwitchingFeature()).thenReturn(true);
|
||||
when(() => authRepository.getPreferredWifiName()).thenReturn('TestWifi');
|
||||
when(() => authRepository.getLocalEndpoint()).thenReturn('http://local.endpoint');
|
||||
when(
|
||||
() => apiService.resolveAndSetEndpoint('http://local.endpoint'),
|
||||
).thenThrow(Exception('Local endpoint error'));
|
||||
|
||||
final result = await sut.setOpenApiServiceEndpoint();
|
||||
|
||||
expect(result, isNull);
|
||||
verify(() => authRepository.getEndpointSwitchingFeature()).called(1);
|
||||
verify(() => networkService.getWifiName()).called(1);
|
||||
verify(() => authRepository.getPreferredWifiName()).called(1);
|
||||
verify(() => authRepository.getLocalEndpoint()).called(1);
|
||||
verify(() => apiService.resolveAndSetEndpoint('http://local.endpoint')).called(1);
|
||||
});
|
||||
|
||||
test('Should handle error when setting external connection', () async {
|
||||
when(() => authRepository.getEndpointSwitchingFeature()).thenReturn(true);
|
||||
when(() => authRepository.getPreferredWifiName()).thenReturn('DifferentWifi');
|
||||
when(
|
||||
() => authRepository.getExternalEndpointList(),
|
||||
).thenReturn([const AuxilaryEndpoint(url: 'https://external.endpoint', status: AuxCheckStatus.valid)]);
|
||||
when(
|
||||
() => apiService.resolveAndSetEndpoint('https://external.endpoint'),
|
||||
).thenThrow(Exception('External endpoint error'));
|
||||
|
||||
final result = await sut.setOpenApiServiceEndpoint();
|
||||
|
||||
expect(result, isNull);
|
||||
verify(() => authRepository.getEndpointSwitchingFeature()).called(1);
|
||||
verify(() => networkService.getWifiName()).called(1);
|
||||
verify(() => authRepository.getPreferredWifiName()).called(1);
|
||||
verify(() => authRepository.getExternalEndpointList()).called(1);
|
||||
verify(() => apiService.resolveAndSetEndpoint('https://external.endpoint')).called(1);
|
||||
});
|
||||
});
|
||||
}
|
||||
351
mobile/test/services/background_upload.service_test.dart
Normal file
351
mobile/test/services/background_upload.service_test.dart
Normal file
|
|
@ -0,0 +1,351 @@
|
|||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:drift/drift.dart' hide isNull, isNotNull;
|
||||
import 'package:drift/native.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:immich_mobile/domain/models/asset/base_asset.model.dart';
|
||||
import 'package:immich_mobile/domain/models/store.model.dart';
|
||||
import 'package:immich_mobile/domain/services/store.service.dart';
|
||||
import 'package:immich_mobile/entities/store.entity.dart';
|
||||
import 'package:immich_mobile/infrastructure/repositories/db.repository.dart';
|
||||
import 'package:immich_mobile/infrastructure/repositories/store.repository.dart';
|
||||
import 'package:immich_mobile/services/app_settings.service.dart';
|
||||
import 'package:immich_mobile/services/background_upload.service.dart';
|
||||
import 'package:mocktail/mocktail.dart';
|
||||
|
||||
import '../domain/service.mock.dart';
|
||||
import '../fixtures/asset.stub.dart';
|
||||
import '../infrastructure/repository.mock.dart';
|
||||
import '../mocks/asset_entity.mock.dart';
|
||||
import '../repository.mocks.dart';
|
||||
|
||||
void main() {
|
||||
late BackgroundUploadService sut;
|
||||
late MockUploadRepository mockUploadRepository;
|
||||
late MockStorageRepository mockStorageRepository;
|
||||
late MockDriftLocalAssetRepository mockLocalAssetRepository;
|
||||
late MockDriftBackupRepository mockBackupRepository;
|
||||
late MockAppSettingsService mockAppSettingsService;
|
||||
late MockAssetMediaRepository mockAssetMediaRepository;
|
||||
late Drift db;
|
||||
|
||||
setUpAll(() async {
|
||||
registerFallbackValue(AppSettingsEnum.useCellularForUploadPhotos);
|
||||
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger.setMockMethodCallHandler(
|
||||
const MethodChannel('plugins.flutter.io/path_provider'),
|
||||
(MethodCall methodCall) async => 'test',
|
||||
);
|
||||
db = Drift(DatabaseConnection(NativeDatabase.memory(), closeStreamsSynchronously: true));
|
||||
await StoreService.init(storeRepository: DriftStoreRepository(db));
|
||||
|
||||
await Store.put(StoreKey.serverEndpoint, 'http://test-server.com');
|
||||
await Store.put(StoreKey.deviceId, 'test-device-id');
|
||||
});
|
||||
|
||||
setUp(() {
|
||||
mockUploadRepository = MockUploadRepository();
|
||||
mockStorageRepository = MockStorageRepository();
|
||||
mockLocalAssetRepository = MockDriftLocalAssetRepository();
|
||||
mockBackupRepository = MockDriftBackupRepository();
|
||||
mockAppSettingsService = MockAppSettingsService();
|
||||
mockAssetMediaRepository = MockAssetMediaRepository();
|
||||
|
||||
when(() => mockAppSettingsService.getSetting(AppSettingsEnum.useCellularForUploadVideos)).thenReturn(false);
|
||||
when(() => mockAppSettingsService.getSetting(AppSettingsEnum.useCellularForUploadPhotos)).thenReturn(false);
|
||||
|
||||
sut = BackgroundUploadService(
|
||||
mockUploadRepository,
|
||||
mockStorageRepository,
|
||||
mockLocalAssetRepository,
|
||||
mockBackupRepository,
|
||||
mockAppSettingsService,
|
||||
mockAssetMediaRepository,
|
||||
);
|
||||
|
||||
mockUploadRepository.onUploadStatus = (_) {};
|
||||
mockUploadRepository.onTaskProgress = (_) {};
|
||||
});
|
||||
|
||||
tearDown(() {
|
||||
sut.dispose();
|
||||
});
|
||||
|
||||
group('getUploadTask', () {
|
||||
test('should call getOriginalFilename from AssetMediaRepository for regular photo', () async {
|
||||
final asset = LocalAssetStub.image1;
|
||||
final mockEntity = MockAssetEntity();
|
||||
final mockFile = File('/path/to/file.jpg');
|
||||
|
||||
when(() => mockEntity.isLivePhoto).thenReturn(false);
|
||||
when(() => mockStorageRepository.getAssetEntityForAsset(asset)).thenAnswer((_) async => mockEntity);
|
||||
when(() => mockStorageRepository.getFileForAsset(asset.id)).thenAnswer((_) async => mockFile);
|
||||
when(() => mockAssetMediaRepository.getOriginalFilename(asset.id)).thenAnswer((_) async => 'OriginalPhoto.jpg');
|
||||
|
||||
final task = await sut.getUploadTask(asset);
|
||||
|
||||
expect(task, isNotNull);
|
||||
expect(task!.fields['filename'], equals('OriginalPhoto.jpg'));
|
||||
verify(() => mockAssetMediaRepository.getOriginalFilename(asset.id)).called(1);
|
||||
});
|
||||
|
||||
test('should call getOriginalFilename when original filename is null', () async {
|
||||
final asset = LocalAssetStub.image2;
|
||||
final mockEntity = MockAssetEntity();
|
||||
final mockFile = File('/path/to/file.jpg');
|
||||
|
||||
when(() => mockEntity.isLivePhoto).thenReturn(false);
|
||||
when(() => mockStorageRepository.getAssetEntityForAsset(asset)).thenAnswer((_) async => mockEntity);
|
||||
when(() => mockStorageRepository.getFileForAsset(asset.id)).thenAnswer((_) async => mockFile);
|
||||
when(() => mockAssetMediaRepository.getOriginalFilename(asset.id)).thenAnswer((_) async => null);
|
||||
|
||||
final task = await sut.getUploadTask(asset);
|
||||
|
||||
expect(task, isNotNull);
|
||||
expect(task!.fields['filename'], equals(asset.name));
|
||||
verify(() => mockAssetMediaRepository.getOriginalFilename(asset.id)).called(1);
|
||||
});
|
||||
|
||||
test('should call getOriginalFilename for live photo', () async {
|
||||
final asset = LocalAssetStub.image1;
|
||||
final mockEntity = MockAssetEntity();
|
||||
final mockFile = File('/path/to/file.mov');
|
||||
|
||||
when(() => mockEntity.isLivePhoto).thenReturn(true);
|
||||
when(() => mockStorageRepository.getAssetEntityForAsset(asset)).thenAnswer((_) async => mockEntity);
|
||||
when(() => mockStorageRepository.getMotionFileForAsset(asset)).thenAnswer((_) async => mockFile);
|
||||
when(
|
||||
() => mockAssetMediaRepository.getOriginalFilename(asset.id),
|
||||
).thenAnswer((_) async => 'OriginalLivePhoto.HEIC');
|
||||
|
||||
final task = await sut.getUploadTask(asset);
|
||||
expect(task, isNotNull);
|
||||
// For live photos, extension should be changed to match the video file
|
||||
expect(task!.fields['filename'], equals('OriginalLivePhoto.mov'));
|
||||
verify(() => mockAssetMediaRepository.getOriginalFilename(asset.id)).called(1);
|
||||
});
|
||||
});
|
||||
|
||||
group('getLivePhotoUploadTask', () {
|
||||
test('should call getOriginalFilename for live photo upload task', () async {
|
||||
final asset = LocalAssetStub.image1;
|
||||
final mockEntity = MockAssetEntity();
|
||||
final mockFile = File('/path/to/livephoto.heic');
|
||||
|
||||
when(() => mockEntity.isLivePhoto).thenReturn(true);
|
||||
when(() => mockStorageRepository.getAssetEntityForAsset(asset)).thenAnswer((_) async => mockEntity);
|
||||
when(() => mockStorageRepository.getFileForAsset(asset.id)).thenAnswer((_) async => mockFile);
|
||||
when(
|
||||
() => mockAssetMediaRepository.getOriginalFilename(asset.id),
|
||||
).thenAnswer((_) async => 'OriginalLivePhoto.HEIC');
|
||||
|
||||
final task = await sut.getLivePhotoUploadTask(asset, 'video-id-123');
|
||||
|
||||
expect(task, isNotNull);
|
||||
expect(task!.fields['filename'], equals('OriginalLivePhoto.HEIC'));
|
||||
expect(task.fields['livePhotoVideoId'], equals('video-id-123'));
|
||||
verify(() => mockAssetMediaRepository.getOriginalFilename(asset.id)).called(1);
|
||||
});
|
||||
|
||||
test('should call getOriginalFilename when original filename is null', () async {
|
||||
final asset = LocalAssetStub.image2;
|
||||
final mockEntity = MockAssetEntity();
|
||||
final mockFile = File('/path/to/fallback.heic');
|
||||
|
||||
when(() => mockEntity.isLivePhoto).thenReturn(true);
|
||||
when(() => mockStorageRepository.getAssetEntityForAsset(asset)).thenAnswer((_) async => mockEntity);
|
||||
when(() => mockStorageRepository.getFileForAsset(asset.id)).thenAnswer((_) async => mockFile);
|
||||
when(() => mockAssetMediaRepository.getOriginalFilename(asset.id)).thenAnswer((_) async => null);
|
||||
|
||||
final task = await sut.getLivePhotoUploadTask(asset, 'video-id-456');
|
||||
expect(task, isNotNull);
|
||||
// Should fall back to asset.name when original filename is null
|
||||
expect(task!.fields['filename'], equals(asset.name));
|
||||
verify(() => mockAssetMediaRepository.getOriginalFilename(asset.id)).called(1);
|
||||
});
|
||||
});
|
||||
|
||||
group('Server Info - cloudId and eTag metadata', () {
|
||||
test('should include cloudId and eTag metadata on iOS when server version is 2.4+', () async {
|
||||
debugDefaultTargetPlatformOverride = TargetPlatform.iOS;
|
||||
addTearDown(() => debugDefaultTargetPlatformOverride = null);
|
||||
|
||||
final sutWithV24 = BackgroundUploadService(
|
||||
mockUploadRepository,
|
||||
mockStorageRepository,
|
||||
mockLocalAssetRepository,
|
||||
mockBackupRepository,
|
||||
mockAppSettingsService,
|
||||
mockAssetMediaRepository,
|
||||
);
|
||||
addTearDown(() => sutWithV24.dispose());
|
||||
|
||||
final assetWithCloudId = LocalAsset(
|
||||
id: 'test-asset-id',
|
||||
name: 'test.jpg',
|
||||
type: AssetType.image,
|
||||
createdAt: DateTime(2025, 1, 1),
|
||||
updatedAt: DateTime(2025, 1, 2),
|
||||
cloudId: 'cloud-id-123',
|
||||
latitude: 37.7749,
|
||||
longitude: -122.4194,
|
||||
adjustmentTime: DateTime(2026, 1, 2),
|
||||
isEdited: false,
|
||||
);
|
||||
|
||||
final mockEntity = MockAssetEntity();
|
||||
final mockFile = File('/path/to/test.jpg');
|
||||
|
||||
when(() => mockEntity.isLivePhoto).thenReturn(false);
|
||||
when(() => mockStorageRepository.getAssetEntityForAsset(assetWithCloudId)).thenAnswer((_) async => mockEntity);
|
||||
when(() => mockStorageRepository.getFileForAsset(assetWithCloudId.id)).thenAnswer((_) async => mockFile);
|
||||
when(() => mockAssetMediaRepository.getOriginalFilename(assetWithCloudId.id)).thenAnswer((_) async => 'test.jpg');
|
||||
|
||||
final task = await sutWithV24.getUploadTask(assetWithCloudId);
|
||||
|
||||
expect(task, isNotNull);
|
||||
expect(task!.fields.containsKey('metadata'), isTrue);
|
||||
|
||||
final metadata = jsonDecode(task.fields['metadata']!) as List;
|
||||
expect(metadata, hasLength(1));
|
||||
expect(metadata[0]['key'], equals('mobile-app'));
|
||||
expect(metadata[0]['value']['iCloudId'], equals('cloud-id-123'));
|
||||
expect(metadata[0]['value']['createdAt'], isNotNull);
|
||||
expect(metadata[0]['value']['adjustmentTime'], isNotNull);
|
||||
expect(metadata[0]['value']['latitude'], isNotNull);
|
||||
expect(metadata[0]['value']['longitude'], isNotNull);
|
||||
});
|
||||
|
||||
test('should NOT include metadata on Android regardless of server version', () async {
|
||||
debugDefaultTargetPlatformOverride = TargetPlatform.android;
|
||||
addTearDown(() => debugDefaultTargetPlatformOverride = null);
|
||||
|
||||
final sutAndroid = BackgroundUploadService(
|
||||
mockUploadRepository,
|
||||
mockStorageRepository,
|
||||
mockLocalAssetRepository,
|
||||
mockBackupRepository,
|
||||
mockAppSettingsService,
|
||||
mockAssetMediaRepository,
|
||||
);
|
||||
addTearDown(() => sutAndroid.dispose());
|
||||
|
||||
final assetWithCloudId = LocalAsset(
|
||||
id: 'test-asset-id',
|
||||
name: 'test.jpg',
|
||||
type: AssetType.image,
|
||||
createdAt: DateTime(2025, 1, 1),
|
||||
updatedAt: DateTime(2025, 1, 2),
|
||||
cloudId: 'cloud-id-123',
|
||||
latitude: 37.7749,
|
||||
longitude: -122.4194,
|
||||
isEdited: false,
|
||||
);
|
||||
|
||||
final mockEntity = MockAssetEntity();
|
||||
final mockFile = File('/path/to/test.jpg');
|
||||
|
||||
when(() => mockEntity.isLivePhoto).thenReturn(false);
|
||||
when(() => mockStorageRepository.getAssetEntityForAsset(assetWithCloudId)).thenAnswer((_) async => mockEntity);
|
||||
when(() => mockStorageRepository.getFileForAsset(assetWithCloudId.id)).thenAnswer((_) async => mockFile);
|
||||
when(() => mockAssetMediaRepository.getOriginalFilename(assetWithCloudId.id)).thenAnswer((_) async => 'test.jpg');
|
||||
|
||||
final task = await sutAndroid.getUploadTask(assetWithCloudId);
|
||||
|
||||
expect(task, isNotNull);
|
||||
expect(task!.fields.containsKey('metadata'), isFalse);
|
||||
});
|
||||
|
||||
test('should NOT include metadata when cloudId is null even on iOS with server 2.4+', () async {
|
||||
debugDefaultTargetPlatformOverride = TargetPlatform.iOS;
|
||||
addTearDown(() => debugDefaultTargetPlatformOverride = null);
|
||||
|
||||
final sutWithV24 = BackgroundUploadService(
|
||||
mockUploadRepository,
|
||||
mockStorageRepository,
|
||||
mockLocalAssetRepository,
|
||||
mockBackupRepository,
|
||||
mockAppSettingsService,
|
||||
mockAssetMediaRepository,
|
||||
);
|
||||
addTearDown(() => sutWithV24.dispose());
|
||||
|
||||
final assetWithoutCloudId = LocalAsset(
|
||||
id: 'test-asset-id',
|
||||
name: 'test.jpg',
|
||||
type: AssetType.image,
|
||||
createdAt: DateTime(2025, 1, 1),
|
||||
updatedAt: DateTime(2025, 1, 2),
|
||||
cloudId: null, // No cloudId
|
||||
isEdited: false,
|
||||
);
|
||||
|
||||
final mockEntity = MockAssetEntity();
|
||||
final mockFile = File('/path/to/test.jpg');
|
||||
|
||||
when(() => mockEntity.isLivePhoto).thenReturn(false);
|
||||
when(() => mockStorageRepository.getAssetEntityForAsset(assetWithoutCloudId)).thenAnswer((_) async => mockEntity);
|
||||
when(() => mockStorageRepository.getFileForAsset(assetWithoutCloudId.id)).thenAnswer((_) async => mockFile);
|
||||
when(
|
||||
() => mockAssetMediaRepository.getOriginalFilename(assetWithoutCloudId.id),
|
||||
).thenAnswer((_) async => 'test.jpg');
|
||||
|
||||
final task = await sutWithV24.getUploadTask(assetWithoutCloudId);
|
||||
|
||||
expect(task, isNotNull);
|
||||
expect(task!.fields.containsKey('metadata'), isFalse);
|
||||
});
|
||||
|
||||
test('should include metadata for live photos with cloudId on iOS 2.4+', () async {
|
||||
debugDefaultTargetPlatformOverride = TargetPlatform.iOS;
|
||||
addTearDown(() => debugDefaultTargetPlatformOverride = null);
|
||||
|
||||
final sutWithV24 = BackgroundUploadService(
|
||||
mockUploadRepository,
|
||||
mockStorageRepository,
|
||||
mockLocalAssetRepository,
|
||||
mockBackupRepository,
|
||||
mockAppSettingsService,
|
||||
mockAssetMediaRepository,
|
||||
);
|
||||
addTearDown(() => sutWithV24.dispose());
|
||||
|
||||
final assetWithCloudId = LocalAsset(
|
||||
id: 'test-livephoto-id',
|
||||
name: 'livephoto.heic',
|
||||
type: AssetType.image,
|
||||
createdAt: DateTime(2025, 1, 1),
|
||||
updatedAt: DateTime(2025, 1, 2),
|
||||
cloudId: 'cloud-id-livephoto',
|
||||
latitude: 37.7749,
|
||||
longitude: -122.4194,
|
||||
isEdited: false,
|
||||
);
|
||||
|
||||
final mockEntity = MockAssetEntity();
|
||||
final mockFile = File('/path/to/livephoto.heic');
|
||||
|
||||
when(() => mockEntity.isLivePhoto).thenReturn(true);
|
||||
when(() => mockStorageRepository.getAssetEntityForAsset(assetWithCloudId)).thenAnswer((_) async => mockEntity);
|
||||
when(() => mockStorageRepository.getFileForAsset(assetWithCloudId.id)).thenAnswer((_) async => mockFile);
|
||||
when(
|
||||
() => mockAssetMediaRepository.getOriginalFilename(assetWithCloudId.id),
|
||||
).thenAnswer((_) async => 'livephoto.heic');
|
||||
|
||||
final task = await sutWithV24.getLivePhotoUploadTask(assetWithCloudId, 'video-123');
|
||||
|
||||
expect(task, isNotNull);
|
||||
expect(task!.fields.containsKey('metadata'), isTrue);
|
||||
expect(task.fields['livePhotoVideoId'], equals('video-123'));
|
||||
|
||||
final metadata = jsonDecode(task.fields['metadata']!) as List;
|
||||
expect(metadata, hasLength(1));
|
||||
expect(metadata[0]['key'], equals('mobile-app'));
|
||||
expect(metadata[0]['value']['iCloudId'], equals('cloud-id-livephoto'));
|
||||
});
|
||||
});
|
||||
}
|
||||
76
mobile/test/services/entity.service_test.dart
Normal file
76
mobile/test/services/entity.service_test.dart
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:immich_mobile/entities/album.entity.dart';
|
||||
import 'package:immich_mobile/infrastructure/entities/user.entity.dart';
|
||||
import 'package:immich_mobile/services/entity.service.dart';
|
||||
import 'package:mocktail/mocktail.dart';
|
||||
|
||||
import '../fixtures/asset.stub.dart';
|
||||
import '../fixtures/user.stub.dart';
|
||||
import '../infrastructure/repository.mock.dart';
|
||||
import '../repository.mocks.dart';
|
||||
|
||||
void main() {
|
||||
late EntityService sut;
|
||||
late MockAssetRepository assetRepository;
|
||||
late MockIsarUserRepository userRepository;
|
||||
|
||||
setUp(() {
|
||||
assetRepository = MockAssetRepository();
|
||||
userRepository = MockIsarUserRepository();
|
||||
sut = EntityService(assetRepository, userRepository);
|
||||
});
|
||||
|
||||
group('fillAlbumWithDatabaseEntities', () {
|
||||
test('remote album with owner, thumbnail, sharedUsers and assets', () async {
|
||||
final Album album =
|
||||
Album(
|
||||
name: "album-with-two-assets-and-two-users",
|
||||
localId: "album-with-two-assets-and-two-users-local",
|
||||
remoteId: "album-with-two-assets-and-two-users-remote",
|
||||
createdAt: DateTime(2001),
|
||||
modifiedAt: DateTime(2010),
|
||||
shared: true,
|
||||
activityEnabled: true,
|
||||
startDate: DateTime(2019),
|
||||
endDate: DateTime(2020),
|
||||
)
|
||||
..remoteThumbnailAssetId = AssetStub.image1.remoteId
|
||||
..assets.addAll([AssetStub.image1, AssetStub.image1])
|
||||
..owner.value = User.fromDto(UserStub.user1)
|
||||
..sharedUsers.addAll([User.fromDto(UserStub.admin), User.fromDto(UserStub.admin)]);
|
||||
|
||||
when(() => userRepository.getByUserId(any())).thenAnswer((_) async => UserStub.admin);
|
||||
when(() => userRepository.getByUserId(any())).thenAnswer((_) async => UserStub.admin);
|
||||
|
||||
when(() => assetRepository.getByRemoteId(AssetStub.image1.remoteId!)).thenAnswer((_) async => AssetStub.image1);
|
||||
|
||||
when(() => userRepository.getByUserIds(any())).thenAnswer((_) async => [UserStub.user1, UserStub.user2]);
|
||||
|
||||
when(() => assetRepository.getAllByRemoteId(any())).thenAnswer((_) async => [AssetStub.image1, AssetStub.image2]);
|
||||
|
||||
await sut.fillAlbumWithDatabaseEntities(album);
|
||||
expect(album.owner.value?.toDto(), UserStub.admin);
|
||||
expect(album.thumbnail.value, AssetStub.image1);
|
||||
expect(album.remoteUsers.map((u) => u.toDto()).toSet(), {UserStub.user1, UserStub.user2});
|
||||
expect(album.remoteAssets.toSet(), {AssetStub.image1, AssetStub.image2});
|
||||
});
|
||||
|
||||
test('remote album without any info', () async {
|
||||
makeEmptyAlbum() => Album(
|
||||
name: "album-without-info",
|
||||
localId: "album-without-info-local",
|
||||
remoteId: "album-without-info-remote",
|
||||
createdAt: DateTime(2001),
|
||||
modifiedAt: DateTime(2010),
|
||||
shared: false,
|
||||
activityEnabled: false,
|
||||
);
|
||||
|
||||
final album = makeEmptyAlbum();
|
||||
await sut.fillAlbumWithDatabaseEntities(album);
|
||||
verifyNoMoreInteractions(assetRepository);
|
||||
verifyNoMoreInteractions(userRepository);
|
||||
expect(album, makeEmptyAlbum());
|
||||
});
|
||||
});
|
||||
}
|
||||
349
mobile/test/services/hash_service_test.dart
Normal file
349
mobile/test/services/hash_service_test.dart
Normal file
|
|
@ -0,0 +1,349 @@
|
|||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
import 'dart:math';
|
||||
|
||||
import 'package:collection/collection.dart';
|
||||
import 'package:file/memory.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:immich_mobile/domain/models/device_asset.model.dart';
|
||||
import 'package:immich_mobile/entities/asset.entity.dart';
|
||||
import 'package:immich_mobile/infrastructure/repositories/device_asset.repository.dart';
|
||||
import 'package:immich_mobile/services/background.service.dart';
|
||||
import 'package:immich_mobile/services/hash.service.dart';
|
||||
import 'package:mocktail/mocktail.dart';
|
||||
|
||||
import '../fixtures/asset.stub.dart';
|
||||
import '../infrastructure/repository.mock.dart';
|
||||
import '../service.mocks.dart';
|
||||
import '../mocks/asset_entity.mock.dart';
|
||||
|
||||
class MockAsset extends Mock implements Asset {}
|
||||
|
||||
void main() {
|
||||
late HashService sut;
|
||||
late BackgroundService mockBackgroundService;
|
||||
late IsarDeviceAssetRepository mockDeviceAssetRepository;
|
||||
|
||||
setUp(() {
|
||||
mockBackgroundService = MockBackgroundService();
|
||||
mockDeviceAssetRepository = MockDeviceAssetRepository();
|
||||
|
||||
sut = HashService(deviceAssetRepository: mockDeviceAssetRepository, backgroundService: mockBackgroundService);
|
||||
|
||||
when(() => mockDeviceAssetRepository.transaction<Null>(any())).thenAnswer((_) async {
|
||||
final capturedCallback = verify(() => mockDeviceAssetRepository.transaction<Null>(captureAny())).captured;
|
||||
// Invoke the transaction callback
|
||||
await (capturedCallback.firstOrNull as Future<Null> Function()?)?.call();
|
||||
});
|
||||
when(() => mockDeviceAssetRepository.updateAll(any())).thenAnswer((_) async => true);
|
||||
when(() => mockDeviceAssetRepository.deleteIds(any())).thenAnswer((_) async => true);
|
||||
});
|
||||
|
||||
group("HashService: No DeviceAsset entry", () {
|
||||
test("hash successfully", () async {
|
||||
final (mockAsset, file, deviceAsset, hash) = await _createAssetMock(AssetStub.image1);
|
||||
|
||||
when(() => mockBackgroundService.digestFiles([file.path])).thenAnswer((_) async => [hash]);
|
||||
// No DB entries for this asset
|
||||
when(() => mockDeviceAssetRepository.getByIds([AssetStub.image1.localId!])).thenAnswer((_) async => []);
|
||||
|
||||
final result = await sut.hashAssets([mockAsset]);
|
||||
|
||||
// Verify we stored the new hash in DB
|
||||
when(() => mockDeviceAssetRepository.transaction<Null>(any())).thenAnswer((_) async {
|
||||
final capturedCallback = verify(() => mockDeviceAssetRepository.transaction<Null>(captureAny())).captured;
|
||||
// Invoke the transaction callback
|
||||
await (capturedCallback.firstOrNull as Future<Null> Function()?)?.call();
|
||||
verify(
|
||||
() => mockDeviceAssetRepository.updateAll([
|
||||
deviceAsset.copyWith(modifiedTime: AssetStub.image1.fileModifiedAt),
|
||||
]),
|
||||
).called(1);
|
||||
verify(() => mockDeviceAssetRepository.deleteIds([])).called(1);
|
||||
});
|
||||
expect(result, [AssetStub.image1.copyWith(checksum: base64.encode(hash))]);
|
||||
});
|
||||
});
|
||||
|
||||
group("HashService: Has DeviceAsset entry", () {
|
||||
test("when the asset is not modified", () async {
|
||||
final hash = utf8.encode("image1-hash");
|
||||
|
||||
when(() => mockDeviceAssetRepository.getByIds([AssetStub.image1.localId!])).thenAnswer(
|
||||
(_) async => [
|
||||
DeviceAsset(assetId: AssetStub.image1.localId!, hash: hash, modifiedTime: AssetStub.image1.fileModifiedAt),
|
||||
],
|
||||
);
|
||||
final result = await sut.hashAssets([AssetStub.image1]);
|
||||
|
||||
verifyNever(() => mockBackgroundService.digestFiles(any()));
|
||||
verifyNever(() => mockBackgroundService.digestFile(any()));
|
||||
verifyNever(() => mockDeviceAssetRepository.updateAll(any()));
|
||||
verifyNever(() => mockDeviceAssetRepository.deleteIds(any()));
|
||||
|
||||
expect(result, [AssetStub.image1.copyWith(checksum: base64.encode(hash))]);
|
||||
});
|
||||
|
||||
test("hashed successful when asset is modified", () async {
|
||||
final (mockAsset, file, deviceAsset, hash) = await _createAssetMock(AssetStub.image1);
|
||||
|
||||
when(() => mockBackgroundService.digestFiles([file.path])).thenAnswer((_) async => [hash]);
|
||||
when(
|
||||
() => mockDeviceAssetRepository.getByIds([AssetStub.image1.localId!]),
|
||||
).thenAnswer((_) async => [deviceAsset]);
|
||||
|
||||
final result = await sut.hashAssets([mockAsset]);
|
||||
|
||||
when(() => mockDeviceAssetRepository.transaction<Null>(any())).thenAnswer((_) async {
|
||||
final capturedCallback = verify(() => mockDeviceAssetRepository.transaction<Null>(captureAny())).captured;
|
||||
// Invoke the transaction callback
|
||||
await (capturedCallback.firstOrNull as Future<Null> Function()?)?.call();
|
||||
verify(
|
||||
() => mockDeviceAssetRepository.updateAll([
|
||||
deviceAsset.copyWith(modifiedTime: AssetStub.image1.fileModifiedAt),
|
||||
]),
|
||||
).called(1);
|
||||
verify(() => mockDeviceAssetRepository.deleteIds([])).called(1);
|
||||
});
|
||||
|
||||
verify(() => mockBackgroundService.digestFiles([file.path])).called(1);
|
||||
|
||||
expect(result, [AssetStub.image1.copyWith(checksum: base64.encode(hash))]);
|
||||
});
|
||||
});
|
||||
|
||||
group("HashService: Cleanup", () {
|
||||
late Asset mockAsset;
|
||||
late Uint8List hash;
|
||||
late DeviceAsset deviceAsset;
|
||||
late File file;
|
||||
|
||||
setUp(() async {
|
||||
(mockAsset, file, deviceAsset, hash) = await _createAssetMock(AssetStub.image1);
|
||||
|
||||
when(() => mockBackgroundService.digestFiles([file.path])).thenAnswer((_) async => [hash]);
|
||||
when(
|
||||
() => mockDeviceAssetRepository.getByIds([AssetStub.image1.localId!]),
|
||||
).thenAnswer((_) async => [deviceAsset]);
|
||||
});
|
||||
|
||||
test("cleanups DeviceAsset when local file cannot be obtained", () async {
|
||||
when(() => mockAsset.local).thenThrow(Exception("File not found"));
|
||||
final result = await sut.hashAssets([mockAsset]);
|
||||
|
||||
verifyNever(() => mockBackgroundService.digestFiles(any()));
|
||||
verifyNever(() => mockBackgroundService.digestFile(any()));
|
||||
verifyNever(() => mockDeviceAssetRepository.updateAll(any()));
|
||||
verify(() => mockDeviceAssetRepository.deleteIds([AssetStub.image1.localId!])).called(1);
|
||||
|
||||
expect(result, isEmpty);
|
||||
});
|
||||
|
||||
test("cleanups DeviceAsset when hashing failed", () async {
|
||||
when(() => mockDeviceAssetRepository.transaction<Null>(any())).thenAnswer((_) async {
|
||||
final capturedCallback = verify(() => mockDeviceAssetRepository.transaction<Null>(captureAny())).captured;
|
||||
// Invoke the transaction callback
|
||||
await (capturedCallback.firstOrNull as Future<Null> Function()?)?.call();
|
||||
|
||||
// Verify the callback inside the transaction because, doing it outside results
|
||||
// in a small delay before the callback is invoked, resulting in other LOCs getting executed
|
||||
// resulting in an incorrect state
|
||||
//
|
||||
// i.e, consider the following piece of code
|
||||
// await _deviceAssetRepository.transaction(() async {
|
||||
// await _deviceAssetRepository.updateAll(toBeAdded);
|
||||
// await _deviceAssetRepository.deleteIds(toBeDeleted);
|
||||
// });
|
||||
// toBeDeleted.clear();
|
||||
// since the transaction method is mocked, the callback is not invoked until it is captured
|
||||
// and executed manually in the next event loop. However, the toBeDeleted.clear() is executed
|
||||
// immediately once the transaction stub is executed, resulting in the deleteIds method being
|
||||
// called with an empty list.
|
||||
//
|
||||
// To avoid this, we capture the callback and execute it within the transaction stub itself
|
||||
// and verify the results inside the transaction stub
|
||||
verify(() => mockDeviceAssetRepository.updateAll([])).called(1);
|
||||
verify(() => mockDeviceAssetRepository.deleteIds([AssetStub.image1.localId!])).called(1);
|
||||
});
|
||||
|
||||
when(() => mockBackgroundService.digestFiles([file.path])).thenAnswer(
|
||||
// Invalid hash, length != 20
|
||||
(_) async => [Uint8List.fromList(hash.slice(2).toList())],
|
||||
);
|
||||
|
||||
final result = await sut.hashAssets([mockAsset]);
|
||||
|
||||
verify(() => mockBackgroundService.digestFiles([file.path])).called(1);
|
||||
expect(result, isEmpty);
|
||||
});
|
||||
});
|
||||
|
||||
group("HashService: Batch processing", () {
|
||||
test("processes assets in batches when size limit is reached", () async {
|
||||
// Setup multiple assets with large file sizes
|
||||
final (mock1, mock2, mock3) = await (
|
||||
_createAssetMock(AssetStub.image1),
|
||||
_createAssetMock(AssetStub.image2),
|
||||
_createAssetMock(AssetStub.image3),
|
||||
).wait;
|
||||
|
||||
final (asset1, file1, deviceAsset1, hash1) = mock1;
|
||||
final (asset2, file2, deviceAsset2, hash2) = mock2;
|
||||
final (asset3, file3, deviceAsset3, hash3) = mock3;
|
||||
|
||||
when(() => mockDeviceAssetRepository.getByIds(any())).thenAnswer((_) async => []);
|
||||
|
||||
// Setup for multiple batch processing calls
|
||||
when(() => mockBackgroundService.digestFiles([file1.path, file2.path])).thenAnswer((_) async => [hash1, hash2]);
|
||||
when(() => mockBackgroundService.digestFiles([file3.path])).thenAnswer((_) async => [hash3]);
|
||||
|
||||
final size = await file1.length() + await file2.length();
|
||||
|
||||
sut = HashService(
|
||||
deviceAssetRepository: mockDeviceAssetRepository,
|
||||
backgroundService: mockBackgroundService,
|
||||
batchSizeLimit: size,
|
||||
);
|
||||
final result = await sut.hashAssets([asset1, asset2, asset3]);
|
||||
|
||||
// Verify multiple batch process calls
|
||||
verify(() => mockBackgroundService.digestFiles([file1.path, file2.path])).called(1);
|
||||
verify(() => mockBackgroundService.digestFiles([file3.path])).called(1);
|
||||
|
||||
expect(result, [
|
||||
AssetStub.image1.copyWith(checksum: base64.encode(hash1)),
|
||||
AssetStub.image2.copyWith(checksum: base64.encode(hash2)),
|
||||
AssetStub.image3.copyWith(checksum: base64.encode(hash3)),
|
||||
]);
|
||||
});
|
||||
|
||||
test("processes assets in batches when file limit is reached", () async {
|
||||
// Setup multiple assets with large file sizes
|
||||
final (mock1, mock2, mock3) = await (
|
||||
_createAssetMock(AssetStub.image1),
|
||||
_createAssetMock(AssetStub.image2),
|
||||
_createAssetMock(AssetStub.image3),
|
||||
).wait;
|
||||
|
||||
final (asset1, file1, deviceAsset1, hash1) = mock1;
|
||||
final (asset2, file2, deviceAsset2, hash2) = mock2;
|
||||
final (asset3, file3, deviceAsset3, hash3) = mock3;
|
||||
|
||||
when(() => mockDeviceAssetRepository.getByIds(any())).thenAnswer((_) async => []);
|
||||
|
||||
when(() => mockBackgroundService.digestFiles([file1.path])).thenAnswer((_) async => [hash1]);
|
||||
when(() => mockBackgroundService.digestFiles([file2.path])).thenAnswer((_) async => [hash2]);
|
||||
when(() => mockBackgroundService.digestFiles([file3.path])).thenAnswer((_) async => [hash3]);
|
||||
|
||||
sut = HashService(
|
||||
deviceAssetRepository: mockDeviceAssetRepository,
|
||||
backgroundService: mockBackgroundService,
|
||||
batchFileLimit: 1,
|
||||
);
|
||||
final result = await sut.hashAssets([asset1, asset2, asset3]);
|
||||
|
||||
// Verify multiple batch process calls
|
||||
verify(() => mockBackgroundService.digestFiles([file1.path])).called(1);
|
||||
verify(() => mockBackgroundService.digestFiles([file2.path])).called(1);
|
||||
verify(() => mockBackgroundService.digestFiles([file3.path])).called(1);
|
||||
|
||||
expect(result, [
|
||||
AssetStub.image1.copyWith(checksum: base64.encode(hash1)),
|
||||
AssetStub.image2.copyWith(checksum: base64.encode(hash2)),
|
||||
AssetStub.image3.copyWith(checksum: base64.encode(hash3)),
|
||||
]);
|
||||
});
|
||||
|
||||
test("HashService: Sort & Process different states", () async {
|
||||
final (asset1, file1, deviceAsset1, hash1) = await _createAssetMock(AssetStub.image1); // Will need rehashing
|
||||
final (asset2, file2, deviceAsset2, hash2) = await _createAssetMock(AssetStub.image2); // Will have matching hash
|
||||
final (asset3, file3, deviceAsset3, hash3) = await _createAssetMock(AssetStub.image3); // No DB entry
|
||||
final asset4 = AssetStub.image3.copyWith(localId: "image4"); // Cannot be hashed
|
||||
|
||||
when(() => mockBackgroundService.digestFiles([file1.path, file3.path])).thenAnswer((_) async => [hash1, hash3]);
|
||||
// DB entries are not sorted and a dummy entry added
|
||||
when(
|
||||
() => mockDeviceAssetRepository.getByIds([
|
||||
AssetStub.image1.localId!,
|
||||
AssetStub.image2.localId!,
|
||||
AssetStub.image3.localId!,
|
||||
asset4.localId!,
|
||||
]),
|
||||
).thenAnswer(
|
||||
(_) async => [
|
||||
// Same timestamp to reuse deviceAsset
|
||||
deviceAsset2.copyWith(modifiedTime: asset2.fileModifiedAt),
|
||||
deviceAsset1,
|
||||
deviceAsset3.copyWith(assetId: asset4.localId!),
|
||||
],
|
||||
);
|
||||
|
||||
final result = await sut.hashAssets([asset1, asset2, asset3, asset4]);
|
||||
|
||||
// Verify correct processing of all assets
|
||||
verify(() => mockBackgroundService.digestFiles([file1.path, file3.path])).called(1);
|
||||
expect(result.length, 3);
|
||||
expect(result, [
|
||||
AssetStub.image2.copyWith(checksum: base64.encode(hash2)),
|
||||
AssetStub.image1.copyWith(checksum: base64.encode(hash1)),
|
||||
AssetStub.image3.copyWith(checksum: base64.encode(hash3)),
|
||||
]);
|
||||
});
|
||||
|
||||
group("HashService: Edge cases", () {
|
||||
test("handles empty list of assets", () async {
|
||||
when(() => mockDeviceAssetRepository.getByIds(any())).thenAnswer((_) async => []);
|
||||
|
||||
final result = await sut.hashAssets([]);
|
||||
|
||||
verifyNever(() => mockBackgroundService.digestFiles(any()));
|
||||
verifyNever(() => mockDeviceAssetRepository.updateAll(any()));
|
||||
verifyNever(() => mockDeviceAssetRepository.deleteIds(any()));
|
||||
|
||||
expect(result, isEmpty);
|
||||
});
|
||||
|
||||
test("handles all file access failures", () async {
|
||||
// No DB entries
|
||||
when(
|
||||
() => mockDeviceAssetRepository.getByIds([AssetStub.image1.localId!, AssetStub.image2.localId!]),
|
||||
).thenAnswer((_) async => []);
|
||||
|
||||
final result = await sut.hashAssets([AssetStub.image1, AssetStub.image2]);
|
||||
|
||||
verifyNever(() => mockBackgroundService.digestFiles(any()));
|
||||
verifyNever(() => mockDeviceAssetRepository.updateAll(any()));
|
||||
expect(result, isEmpty);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Future<(Asset, File, DeviceAsset, Uint8List)> _createAssetMock(Asset asset) async {
|
||||
final random = Random();
|
||||
final hash = Uint8List.fromList(List.generate(20, (i) => random.nextInt(255)));
|
||||
final mockAsset = MockAsset();
|
||||
final mockAssetEntity = MockAssetEntity();
|
||||
final fs = MemoryFileSystem();
|
||||
final deviceAsset = DeviceAsset(
|
||||
assetId: asset.localId!,
|
||||
hash: Uint8List.fromList(hash),
|
||||
modifiedTime: DateTime.now(),
|
||||
);
|
||||
final tmp = await fs.systemTempDirectory.createTemp();
|
||||
final file = tmp.childFile("${asset.fileName}-path");
|
||||
await file.writeAsString("${asset.fileName}-content");
|
||||
|
||||
when(() => mockAsset.localId).thenReturn(asset.localId);
|
||||
when(() => mockAsset.fileName).thenReturn(asset.fileName);
|
||||
when(() => mockAsset.fileCreatedAt).thenReturn(asset.fileCreatedAt);
|
||||
when(() => mockAsset.fileModifiedAt).thenReturn(asset.fileModifiedAt);
|
||||
when(
|
||||
() => mockAsset.copyWith(checksum: any(named: "checksum")),
|
||||
).thenReturn(asset.copyWith(checksum: base64.encode(hash)));
|
||||
when(() => mockAsset.local).thenAnswer((_) => mockAssetEntity);
|
||||
when(() => mockAssetEntity.originFile).thenAnswer((_) async => file);
|
||||
|
||||
return (mockAsset, file, deviceAsset, hash);
|
||||
}
|
||||
161
mobile/test/test_utils.dart
Normal file
161
mobile/test/test_utils.dart
Normal file
|
|
@ -0,0 +1,161 @@
|
|||
import 'dart:async';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:easy_localization/easy_localization.dart';
|
||||
import 'package:fake_async/fake_async.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
import 'package:immich_mobile/domain/models/asset/base_asset.model.dart' as domain;
|
||||
import 'package:immich_mobile/entities/album.entity.dart';
|
||||
import 'package:immich_mobile/entities/android_device_asset.entity.dart';
|
||||
import 'package:immich_mobile/entities/asset.entity.dart';
|
||||
import 'package:immich_mobile/entities/backup_album.entity.dart';
|
||||
import 'package:immich_mobile/entities/duplicated_asset.entity.dart';
|
||||
import 'package:immich_mobile/entities/etag.entity.dart';
|
||||
import 'package:immich_mobile/entities/ios_device_asset.entity.dart';
|
||||
import 'package:immich_mobile/infrastructure/entities/device_asset.entity.dart';
|
||||
import 'package:immich_mobile/infrastructure/entities/exif.entity.dart';
|
||||
import 'package:immich_mobile/infrastructure/entities/store.entity.dart';
|
||||
import 'package:immich_mobile/infrastructure/entities/user.entity.dart';
|
||||
import 'package:isar/isar.dart';
|
||||
import 'package:mocktail/mocktail.dart';
|
||||
|
||||
import 'mock_http_override.dart';
|
||||
|
||||
// Listener Mock to test when a provider notifies its listeners
|
||||
class ListenerMock<T> extends Mock {
|
||||
void call(T? previous, T next);
|
||||
}
|
||||
|
||||
abstract final class TestUtils {
|
||||
const TestUtils._();
|
||||
|
||||
/// Downloads Isar binaries (if required) and initializes a new Isar db
|
||||
static Future<Isar> initIsar() async {
|
||||
await Isar.initializeIsarCore(download: true);
|
||||
|
||||
final instance = Isar.getInstance();
|
||||
if (instance != null) {
|
||||
return instance;
|
||||
}
|
||||
|
||||
final db = await Isar.open(
|
||||
[
|
||||
StoreValueSchema,
|
||||
ExifInfoSchema,
|
||||
AssetSchema,
|
||||
AlbumSchema,
|
||||
UserSchema,
|
||||
BackupAlbumSchema,
|
||||
DuplicatedAssetSchema,
|
||||
ETagSchema,
|
||||
AndroidDeviceAssetSchema,
|
||||
IOSDeviceAssetSchema,
|
||||
DeviceAssetEntitySchema,
|
||||
],
|
||||
directory: "test/",
|
||||
maxSizeMiB: 1024,
|
||||
inspector: false,
|
||||
);
|
||||
|
||||
// Clear and close db on test end
|
||||
addTearDown(() async {
|
||||
await db.writeTxn(() async => await db.clear());
|
||||
await db.close();
|
||||
});
|
||||
return db;
|
||||
}
|
||||
|
||||
/// Creates a new ProviderContainer to test Riverpod providers
|
||||
static ProviderContainer createContainer({
|
||||
ProviderContainer? parent,
|
||||
List<Override> overrides = const [],
|
||||
List<ProviderObserver>? observers,
|
||||
}) {
|
||||
final container = ProviderContainer(parent: parent, overrides: overrides, observers: observers);
|
||||
|
||||
// Dispose on test end
|
||||
addTearDown(container.dispose);
|
||||
|
||||
return container;
|
||||
}
|
||||
|
||||
static void init() {
|
||||
// Turn off easy localization logging
|
||||
EasyLocalization.logger.enableBuildModes = [];
|
||||
WidgetController.hitTestWarningShouldBeFatal = true;
|
||||
HttpOverrides.global = MockHttpOverrides();
|
||||
}
|
||||
|
||||
// Workaround till the following issue is resolved
|
||||
// https://github.com/dart-lang/test/issues/2307
|
||||
static T fakeAsync<T>(Future<T> Function(FakeAsync _) callback, {DateTime? initialTime}) {
|
||||
late final T result;
|
||||
Object? error;
|
||||
StackTrace? stack;
|
||||
FakeAsync(initialTime: initialTime).run((FakeAsync async) {
|
||||
bool shouldPump = true;
|
||||
unawaited(
|
||||
callback(async)
|
||||
.then<void>(
|
||||
(value) => result = value,
|
||||
onError: (e, s) {
|
||||
error = e;
|
||||
stack = s;
|
||||
},
|
||||
)
|
||||
.whenComplete(() => shouldPump = false),
|
||||
);
|
||||
|
||||
while (shouldPump) {
|
||||
async.flushMicrotasks();
|
||||
}
|
||||
});
|
||||
|
||||
if (error != null) {
|
||||
Error.throwWithStackTrace(error!, stack!);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
static domain.RemoteAsset createRemoteAsset({required String id, int? width, int? height, String? ownerId}) {
|
||||
return domain.RemoteAsset(
|
||||
id: id,
|
||||
checksum: 'checksum1',
|
||||
ownerId: ownerId ?? 'owner1',
|
||||
name: 'test.jpg',
|
||||
type: domain.AssetType.image,
|
||||
createdAt: DateTime(2024, 1, 1),
|
||||
updatedAt: DateTime(2024, 1, 1),
|
||||
durationInSeconds: 0,
|
||||
isFavorite: false,
|
||||
width: width,
|
||||
height: height,
|
||||
isEdited: false,
|
||||
);
|
||||
}
|
||||
|
||||
static domain.LocalAsset createLocalAsset({
|
||||
required String id,
|
||||
String? remoteId,
|
||||
int? width,
|
||||
int? height,
|
||||
int orientation = 0,
|
||||
}) {
|
||||
return domain.LocalAsset(
|
||||
id: id,
|
||||
remoteId: remoteId,
|
||||
checksum: 'checksum1',
|
||||
name: 'test.jpg',
|
||||
type: domain.AssetType.image,
|
||||
createdAt: DateTime(2024, 1, 1),
|
||||
updatedAt: DateTime(2024, 1, 1),
|
||||
durationInSeconds: 0,
|
||||
isFavorite: false,
|
||||
width: width,
|
||||
height: height,
|
||||
orientation: orientation,
|
||||
isEdited: false,
|
||||
);
|
||||
}
|
||||
}
|
||||
62
mobile/test/test_utils/medium_factory.dart
Normal file
62
mobile/test/test_utils/medium_factory.dart
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
import 'dart:math';
|
||||
|
||||
import 'package:immich_mobile/domain/models/album/local_album.model.dart';
|
||||
import 'package:immich_mobile/domain/models/asset/base_asset.model.dart';
|
||||
import 'package:immich_mobile/infrastructure/repositories/db.repository.dart';
|
||||
import 'package:immich_mobile/infrastructure/repositories/local_album.repository.dart';
|
||||
|
||||
class MediumFactory {
|
||||
final Drift _db;
|
||||
|
||||
const MediumFactory(Drift db) : _db = db;
|
||||
|
||||
LocalAsset localAsset({
|
||||
String? id,
|
||||
String? name,
|
||||
AssetType? type,
|
||||
DateTime? createdAt,
|
||||
DateTime? updatedAt,
|
||||
String? checksum,
|
||||
}) {
|
||||
final random = Random();
|
||||
|
||||
return LocalAsset(
|
||||
id: id ?? '${random.nextInt(1000000)}',
|
||||
name: name ?? 'Asset ${random.nextInt(1000000)}',
|
||||
checksum: checksum ?? '${random.nextInt(1000000)}',
|
||||
type: type ?? AssetType.image,
|
||||
createdAt: createdAt ?? DateTime.fromMillisecondsSinceEpoch(random.nextInt(1000000000)),
|
||||
updatedAt: updatedAt ?? DateTime.fromMillisecondsSinceEpoch(random.nextInt(1000000000)),
|
||||
isEdited: false,
|
||||
);
|
||||
}
|
||||
|
||||
LocalAlbum localAlbum({
|
||||
String? id,
|
||||
String? name,
|
||||
DateTime? updatedAt,
|
||||
int? assetCount,
|
||||
BackupSelection? backupSelection,
|
||||
bool? isIosSharedAlbum,
|
||||
}) {
|
||||
final random = Random();
|
||||
|
||||
return LocalAlbum(
|
||||
id: id ?? '${random.nextInt(1000000)}',
|
||||
name: name ?? 'Album ${random.nextInt(1000000)}',
|
||||
updatedAt: updatedAt ?? DateTime.fromMillisecondsSinceEpoch(random.nextInt(1000000000)),
|
||||
assetCount: assetCount ?? random.nextInt(100),
|
||||
backupSelection: backupSelection ?? BackupSelection.none,
|
||||
isIosSharedAlbum: isIosSharedAlbum ?? false,
|
||||
);
|
||||
}
|
||||
|
||||
T getRepository<T>() {
|
||||
switch (T) {
|
||||
case const (DriftLocalAlbumRepository):
|
||||
return DriftLocalAlbumRepository(_db) as T;
|
||||
default:
|
||||
throw Exception('Unknown repository: $T');
|
||||
}
|
||||
}
|
||||
}
|
||||
967
mobile/test/utils/action_button_utils_test.dart
Normal file
967
mobile/test/utils/action_button_utils_test.dart
Normal file
|
|
@ -0,0 +1,967 @@
|
|||
import 'package:flutter/widgets.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:immich_mobile/constants/enums.dart';
|
||||
import 'package:immich_mobile/domain/models/album/album.model.dart';
|
||||
import 'package:immich_mobile/domain/models/asset/base_asset.model.dart';
|
||||
import 'package:immich_mobile/utils/action_button.utils.dart';
|
||||
|
||||
LocalAsset createLocalAsset({
|
||||
String? remoteId,
|
||||
String name = 'test.jpg',
|
||||
String? checksum = 'test-checksum',
|
||||
AssetType type = AssetType.image,
|
||||
DateTime? createdAt,
|
||||
DateTime? updatedAt,
|
||||
bool isFavorite = false,
|
||||
}) {
|
||||
return LocalAsset(
|
||||
id: 'local-id',
|
||||
remoteId: remoteId,
|
||||
name: name,
|
||||
checksum: checksum,
|
||||
type: type,
|
||||
createdAt: createdAt ?? DateTime.now(),
|
||||
updatedAt: updatedAt ?? DateTime.now(),
|
||||
isFavorite: isFavorite,
|
||||
isEdited: false,
|
||||
);
|
||||
}
|
||||
|
||||
RemoteAsset createRemoteAsset({
|
||||
String? localId,
|
||||
String name = 'test.jpg',
|
||||
String checksum = 'test-checksum',
|
||||
AssetType type = AssetType.image,
|
||||
DateTime? createdAt,
|
||||
DateTime? updatedAt,
|
||||
bool isFavorite = false,
|
||||
}) {
|
||||
return RemoteAsset(
|
||||
id: 'remote-id',
|
||||
localId: localId,
|
||||
name: name,
|
||||
checksum: checksum,
|
||||
type: type,
|
||||
ownerId: 'owner-id',
|
||||
createdAt: createdAt ?? DateTime.now(),
|
||||
updatedAt: updatedAt ?? DateTime.now(),
|
||||
isFavorite: isFavorite,
|
||||
isEdited: false,
|
||||
);
|
||||
}
|
||||
|
||||
RemoteAlbum createRemoteAlbum({
|
||||
String id = 'test-album-id',
|
||||
String name = 'Test Album',
|
||||
bool isActivityEnabled = false,
|
||||
bool isShared = false,
|
||||
}) {
|
||||
return RemoteAlbum(
|
||||
id: id,
|
||||
name: name,
|
||||
ownerId: 'owner-id',
|
||||
description: 'Test Description',
|
||||
createdAt: DateTime.now(),
|
||||
updatedAt: DateTime.now(),
|
||||
isActivityEnabled: isActivityEnabled,
|
||||
isShared: isShared,
|
||||
order: AlbumAssetOrder.asc,
|
||||
assetCount: 0,
|
||||
ownerName: 'Test Owner',
|
||||
);
|
||||
}
|
||||
|
||||
void main() {
|
||||
group('ActionButtonContext', () {
|
||||
test('should create context with all required parameters', () {
|
||||
final asset = createLocalAsset();
|
||||
|
||||
final context = ActionButtonContext(
|
||||
asset: asset,
|
||||
isOwner: true,
|
||||
isArchived: false,
|
||||
isTrashEnabled: true,
|
||||
isInLockedView: false,
|
||||
currentAlbum: null,
|
||||
advancedTroubleshooting: false,
|
||||
isStacked: false,
|
||||
source: ActionSource.timeline,
|
||||
);
|
||||
|
||||
expect(context.asset, isA<BaseAsset>());
|
||||
expect(context.isOwner, isTrue);
|
||||
expect(context.isArchived, isFalse);
|
||||
expect(context.isTrashEnabled, isTrue);
|
||||
expect(context.isInLockedView, isFalse);
|
||||
expect(context.currentAlbum, isNull);
|
||||
expect(context.source, ActionSource.timeline);
|
||||
});
|
||||
});
|
||||
|
||||
group('ActionButtonType.shouldShow', () {
|
||||
late BaseAsset mergedAsset;
|
||||
|
||||
setUp(() {
|
||||
mergedAsset = createLocalAsset(remoteId: 'remote-id');
|
||||
});
|
||||
|
||||
group('share button', () {
|
||||
test('should show when not in locked view', () {
|
||||
final context = ActionButtonContext(
|
||||
asset: mergedAsset,
|
||||
isOwner: true,
|
||||
isArchived: false,
|
||||
isTrashEnabled: true,
|
||||
isInLockedView: false,
|
||||
currentAlbum: null,
|
||||
advancedTroubleshooting: false,
|
||||
isStacked: false,
|
||||
source: ActionSource.timeline,
|
||||
);
|
||||
|
||||
expect(ActionButtonType.share.shouldShow(context), isTrue);
|
||||
});
|
||||
|
||||
test('should show when in locked view', () {
|
||||
final context = ActionButtonContext(
|
||||
asset: mergedAsset,
|
||||
isOwner: true,
|
||||
isArchived: false,
|
||||
isTrashEnabled: true,
|
||||
isInLockedView: true,
|
||||
currentAlbum: null,
|
||||
advancedTroubleshooting: false,
|
||||
isStacked: false,
|
||||
source: ActionSource.timeline,
|
||||
);
|
||||
|
||||
expect(ActionButtonType.share.shouldShow(context), isTrue);
|
||||
});
|
||||
});
|
||||
|
||||
group('shareLink button', () {
|
||||
test('should show when not in locked view and asset has remote', () {
|
||||
final remoteAsset = createRemoteAsset();
|
||||
final context = ActionButtonContext(
|
||||
asset: remoteAsset,
|
||||
isOwner: true,
|
||||
isArchived: false,
|
||||
isTrashEnabled: true,
|
||||
isInLockedView: false,
|
||||
currentAlbum: null,
|
||||
advancedTroubleshooting: false,
|
||||
isStacked: false,
|
||||
source: ActionSource.timeline,
|
||||
);
|
||||
|
||||
expect(ActionButtonType.shareLink.shouldShow(context), isTrue);
|
||||
});
|
||||
|
||||
test('should not show when in locked view', () {
|
||||
final remoteAsset = createRemoteAsset();
|
||||
final context = ActionButtonContext(
|
||||
asset: remoteAsset,
|
||||
isOwner: true,
|
||||
isArchived: false,
|
||||
isTrashEnabled: true,
|
||||
isInLockedView: true,
|
||||
currentAlbum: null,
|
||||
advancedTroubleshooting: false,
|
||||
isStacked: false,
|
||||
source: ActionSource.timeline,
|
||||
);
|
||||
|
||||
expect(ActionButtonType.shareLink.shouldShow(context), isFalse);
|
||||
});
|
||||
|
||||
test('should not show when asset has no remote', () {
|
||||
final localAsset = createLocalAsset();
|
||||
final context = ActionButtonContext(
|
||||
asset: localAsset,
|
||||
isOwner: true,
|
||||
isArchived: false,
|
||||
isTrashEnabled: true,
|
||||
isInLockedView: false,
|
||||
currentAlbum: null,
|
||||
advancedTroubleshooting: false,
|
||||
isStacked: false,
|
||||
source: ActionSource.timeline,
|
||||
);
|
||||
|
||||
expect(ActionButtonType.shareLink.shouldShow(context), isFalse);
|
||||
});
|
||||
});
|
||||
|
||||
group('archive button', () {
|
||||
test('should show when owner, not locked, has remote, and not archived', () {
|
||||
final remoteAsset = createRemoteAsset();
|
||||
final context = ActionButtonContext(
|
||||
asset: remoteAsset,
|
||||
isOwner: true,
|
||||
isArchived: false,
|
||||
isTrashEnabled: true,
|
||||
isInLockedView: false,
|
||||
currentAlbum: null,
|
||||
advancedTroubleshooting: false,
|
||||
isStacked: false,
|
||||
source: ActionSource.timeline,
|
||||
);
|
||||
|
||||
expect(ActionButtonType.archive.shouldShow(context), isTrue);
|
||||
});
|
||||
|
||||
test('should not show when not owner', () {
|
||||
final remoteAsset = createRemoteAsset();
|
||||
final context = ActionButtonContext(
|
||||
asset: remoteAsset,
|
||||
isOwner: false,
|
||||
isArchived: false,
|
||||
isTrashEnabled: true,
|
||||
isInLockedView: false,
|
||||
currentAlbum: null,
|
||||
advancedTroubleshooting: false,
|
||||
isStacked: false,
|
||||
source: ActionSource.timeline,
|
||||
);
|
||||
|
||||
expect(ActionButtonType.archive.shouldShow(context), isFalse);
|
||||
});
|
||||
|
||||
test('should not show when in locked view', () {
|
||||
final remoteAsset = createRemoteAsset();
|
||||
final context = ActionButtonContext(
|
||||
asset: remoteAsset,
|
||||
isOwner: true,
|
||||
isArchived: false,
|
||||
isTrashEnabled: true,
|
||||
isInLockedView: true,
|
||||
currentAlbum: null,
|
||||
advancedTroubleshooting: false,
|
||||
isStacked: false,
|
||||
source: ActionSource.timeline,
|
||||
);
|
||||
|
||||
expect(ActionButtonType.archive.shouldShow(context), isFalse);
|
||||
});
|
||||
|
||||
test('should not show when asset has no remote', () {
|
||||
final localAsset = createLocalAsset();
|
||||
final context = ActionButtonContext(
|
||||
asset: localAsset,
|
||||
isOwner: true,
|
||||
isArchived: false,
|
||||
isTrashEnabled: true,
|
||||
isInLockedView: false,
|
||||
currentAlbum: null,
|
||||
advancedTroubleshooting: false,
|
||||
isStacked: false,
|
||||
source: ActionSource.timeline,
|
||||
);
|
||||
|
||||
expect(ActionButtonType.archive.shouldShow(context), isFalse);
|
||||
});
|
||||
|
||||
test('should not show when already archived', () {
|
||||
final remoteAsset = createRemoteAsset();
|
||||
final context = ActionButtonContext(
|
||||
asset: remoteAsset,
|
||||
isOwner: true,
|
||||
isArchived: true,
|
||||
isTrashEnabled: true,
|
||||
isInLockedView: false,
|
||||
currentAlbum: null,
|
||||
advancedTroubleshooting: false,
|
||||
isStacked: false,
|
||||
source: ActionSource.timeline,
|
||||
);
|
||||
|
||||
expect(ActionButtonType.archive.shouldShow(context), isFalse);
|
||||
});
|
||||
});
|
||||
|
||||
group('unarchive button', () {
|
||||
test('should show when owner, not locked, has remote, and is archived', () {
|
||||
final remoteAsset = createRemoteAsset();
|
||||
final context = ActionButtonContext(
|
||||
asset: remoteAsset,
|
||||
isOwner: true,
|
||||
isArchived: true,
|
||||
isTrashEnabled: true,
|
||||
isInLockedView: false,
|
||||
currentAlbum: null,
|
||||
advancedTroubleshooting: false,
|
||||
isStacked: false,
|
||||
source: ActionSource.timeline,
|
||||
);
|
||||
|
||||
expect(ActionButtonType.unarchive.shouldShow(context), isTrue);
|
||||
});
|
||||
|
||||
test('should not show when not archived', () {
|
||||
final remoteAsset = createRemoteAsset();
|
||||
final context = ActionButtonContext(
|
||||
asset: remoteAsset,
|
||||
isOwner: true,
|
||||
isArchived: false,
|
||||
isTrashEnabled: true,
|
||||
isInLockedView: false,
|
||||
currentAlbum: null,
|
||||
advancedTroubleshooting: false,
|
||||
isStacked: false,
|
||||
source: ActionSource.timeline,
|
||||
);
|
||||
|
||||
expect(ActionButtonType.unarchive.shouldShow(context), isFalse);
|
||||
});
|
||||
|
||||
test('should not show when not owner', () {
|
||||
final remoteAsset = createRemoteAsset();
|
||||
final context = ActionButtonContext(
|
||||
asset: remoteAsset,
|
||||
isOwner: false,
|
||||
isArchived: true,
|
||||
isTrashEnabled: true,
|
||||
isInLockedView: false,
|
||||
currentAlbum: null,
|
||||
advancedTroubleshooting: false,
|
||||
isStacked: false,
|
||||
source: ActionSource.timeline,
|
||||
);
|
||||
|
||||
expect(ActionButtonType.unarchive.shouldShow(context), isFalse);
|
||||
});
|
||||
});
|
||||
|
||||
group('download button', () {
|
||||
test('should show when not locked, has remote, and no local copy', () {
|
||||
final remoteAsset = createRemoteAsset();
|
||||
final context = ActionButtonContext(
|
||||
asset: remoteAsset,
|
||||
isOwner: true,
|
||||
isArchived: false,
|
||||
isTrashEnabled: true,
|
||||
isInLockedView: false,
|
||||
currentAlbum: null,
|
||||
advancedTroubleshooting: false,
|
||||
isStacked: false,
|
||||
source: ActionSource.timeline,
|
||||
);
|
||||
|
||||
expect(ActionButtonType.download.shouldShow(context), isTrue);
|
||||
});
|
||||
|
||||
test('should not show when has local copy', () {
|
||||
final mergedAsset = createLocalAsset(remoteId: 'remote-id');
|
||||
final context = ActionButtonContext(
|
||||
asset: mergedAsset,
|
||||
isOwner: true,
|
||||
isArchived: false,
|
||||
isTrashEnabled: true,
|
||||
isInLockedView: false,
|
||||
currentAlbum: null,
|
||||
advancedTroubleshooting: false,
|
||||
isStacked: false,
|
||||
source: ActionSource.timeline,
|
||||
);
|
||||
|
||||
expect(ActionButtonType.download.shouldShow(context), isFalse);
|
||||
});
|
||||
|
||||
test('should not show when in locked view', () {
|
||||
final remoteAsset = createRemoteAsset();
|
||||
final context = ActionButtonContext(
|
||||
asset: remoteAsset,
|
||||
isOwner: true,
|
||||
isArchived: false,
|
||||
isTrashEnabled: true,
|
||||
isInLockedView: true,
|
||||
currentAlbum: null,
|
||||
advancedTroubleshooting: false,
|
||||
isStacked: false,
|
||||
source: ActionSource.timeline,
|
||||
);
|
||||
|
||||
expect(ActionButtonType.download.shouldShow(context), isFalse);
|
||||
});
|
||||
});
|
||||
|
||||
group('similar photos button', () {
|
||||
test('should show when not locked and has remote', () {
|
||||
final remoteAsset = createRemoteAsset();
|
||||
final context = ActionButtonContext(
|
||||
asset: remoteAsset,
|
||||
isOwner: true,
|
||||
isArchived: false,
|
||||
isTrashEnabled: true,
|
||||
isInLockedView: false,
|
||||
isStacked: false,
|
||||
currentAlbum: null,
|
||||
advancedTroubleshooting: false,
|
||||
source: ActionSource.timeline,
|
||||
);
|
||||
|
||||
expect(ActionButtonType.similarPhotos.shouldShow(context), isTrue);
|
||||
});
|
||||
|
||||
test('should not show when in locked view', () {
|
||||
final remoteAsset = createRemoteAsset();
|
||||
final context = ActionButtonContext(
|
||||
asset: remoteAsset,
|
||||
isOwner: true,
|
||||
isArchived: false,
|
||||
isTrashEnabled: true,
|
||||
isInLockedView: true,
|
||||
currentAlbum: null,
|
||||
isStacked: false,
|
||||
advancedTroubleshooting: false,
|
||||
source: ActionSource.timeline,
|
||||
);
|
||||
|
||||
expect(ActionButtonType.similarPhotos.shouldShow(context), isFalse);
|
||||
});
|
||||
});
|
||||
|
||||
group('trash button', () {
|
||||
test('should show when owner, not locked, has remote, and trash enabled', () {
|
||||
final remoteAsset = createRemoteAsset();
|
||||
final context = ActionButtonContext(
|
||||
asset: remoteAsset,
|
||||
isOwner: true,
|
||||
isArchived: false,
|
||||
isTrashEnabled: true,
|
||||
isInLockedView: false,
|
||||
currentAlbum: null,
|
||||
advancedTroubleshooting: false,
|
||||
isStacked: false,
|
||||
source: ActionSource.timeline,
|
||||
);
|
||||
|
||||
expect(ActionButtonType.trash.shouldShow(context), isTrue);
|
||||
});
|
||||
|
||||
test('should not show when trash disabled', () {
|
||||
final remoteAsset = createRemoteAsset();
|
||||
final context = ActionButtonContext(
|
||||
asset: remoteAsset,
|
||||
isOwner: true,
|
||||
isArchived: false,
|
||||
isTrashEnabled: false,
|
||||
isInLockedView: false,
|
||||
currentAlbum: null,
|
||||
advancedTroubleshooting: false,
|
||||
isStacked: false,
|
||||
source: ActionSource.timeline,
|
||||
);
|
||||
|
||||
expect(ActionButtonType.trash.shouldShow(context), isFalse);
|
||||
});
|
||||
});
|
||||
|
||||
group('deletePermanent button', () {
|
||||
test('should show when owner, not locked, has remote, and trash disabled', () {
|
||||
final remoteAsset = createRemoteAsset();
|
||||
final context = ActionButtonContext(
|
||||
asset: remoteAsset,
|
||||
isOwner: true,
|
||||
isArchived: false,
|
||||
isTrashEnabled: false,
|
||||
isInLockedView: false,
|
||||
currentAlbum: null,
|
||||
advancedTroubleshooting: false,
|
||||
isStacked: false,
|
||||
source: ActionSource.timeline,
|
||||
);
|
||||
|
||||
expect(ActionButtonType.deletePermanent.shouldShow(context), isTrue);
|
||||
});
|
||||
|
||||
test('should not show when trash enabled', () {
|
||||
final remoteAsset = createRemoteAsset();
|
||||
final context = ActionButtonContext(
|
||||
asset: remoteAsset,
|
||||
isOwner: true,
|
||||
isArchived: false,
|
||||
isTrashEnabled: true,
|
||||
isInLockedView: false,
|
||||
currentAlbum: null,
|
||||
advancedTroubleshooting: false,
|
||||
isStacked: false,
|
||||
source: ActionSource.timeline,
|
||||
);
|
||||
|
||||
expect(ActionButtonType.deletePermanent.shouldShow(context), isFalse);
|
||||
});
|
||||
});
|
||||
|
||||
group('delete button', () {
|
||||
test('should show when owner, not locked, and has remote', () {
|
||||
final remoteAsset = createRemoteAsset();
|
||||
final context = ActionButtonContext(
|
||||
asset: remoteAsset,
|
||||
isOwner: true,
|
||||
isArchived: false,
|
||||
isTrashEnabled: true,
|
||||
isInLockedView: false,
|
||||
currentAlbum: null,
|
||||
advancedTroubleshooting: false,
|
||||
isStacked: false,
|
||||
source: ActionSource.timeline,
|
||||
);
|
||||
|
||||
expect(ActionButtonType.delete.shouldShow(context), isTrue);
|
||||
});
|
||||
});
|
||||
|
||||
group('moveToLockFolder button', () {
|
||||
test('should show when owner, not locked, and has remote', () {
|
||||
final remoteAsset = createRemoteAsset();
|
||||
final context = ActionButtonContext(
|
||||
asset: remoteAsset,
|
||||
isOwner: true,
|
||||
isArchived: false,
|
||||
isTrashEnabled: true,
|
||||
isInLockedView: false,
|
||||
currentAlbum: null,
|
||||
advancedTroubleshooting: false,
|
||||
isStacked: false,
|
||||
source: ActionSource.timeline,
|
||||
);
|
||||
|
||||
expect(ActionButtonType.moveToLockFolder.shouldShow(context), isTrue);
|
||||
});
|
||||
});
|
||||
|
||||
group('deleteLocal button', () {
|
||||
test('should show when not locked and asset is local only', () {
|
||||
final localAsset = createLocalAsset();
|
||||
final context = ActionButtonContext(
|
||||
asset: localAsset,
|
||||
isOwner: true,
|
||||
isArchived: false,
|
||||
isTrashEnabled: true,
|
||||
isInLockedView: false,
|
||||
currentAlbum: null,
|
||||
advancedTroubleshooting: false,
|
||||
isStacked: false,
|
||||
source: ActionSource.timeline,
|
||||
);
|
||||
|
||||
expect(ActionButtonType.deleteLocal.shouldShow(context), isTrue);
|
||||
});
|
||||
|
||||
test('should not show when asset is not local only', () {
|
||||
final remoteAsset = createRemoteAsset();
|
||||
final context = ActionButtonContext(
|
||||
asset: remoteAsset,
|
||||
isOwner: true,
|
||||
isArchived: false,
|
||||
isTrashEnabled: true,
|
||||
isInLockedView: false,
|
||||
currentAlbum: null,
|
||||
advancedTroubleshooting: false,
|
||||
isStacked: false,
|
||||
source: ActionSource.timeline,
|
||||
);
|
||||
|
||||
expect(ActionButtonType.deleteLocal.shouldShow(context), isFalse);
|
||||
});
|
||||
|
||||
test('should show when asset is merged', () {
|
||||
final context = ActionButtonContext(
|
||||
asset: mergedAsset,
|
||||
isOwner: true,
|
||||
isArchived: false,
|
||||
isTrashEnabled: true,
|
||||
isInLockedView: false,
|
||||
currentAlbum: null,
|
||||
advancedTroubleshooting: false,
|
||||
isStacked: false,
|
||||
source: ActionSource.timeline,
|
||||
);
|
||||
|
||||
expect(ActionButtonType.deleteLocal.shouldShow(context), isTrue);
|
||||
});
|
||||
});
|
||||
|
||||
group('upload button', () {
|
||||
test('should show when not locked and asset is local only', () {
|
||||
final localAsset = createLocalAsset();
|
||||
final context = ActionButtonContext(
|
||||
asset: localAsset,
|
||||
isOwner: true,
|
||||
isArchived: false,
|
||||
isTrashEnabled: true,
|
||||
isInLockedView: false,
|
||||
currentAlbum: null,
|
||||
advancedTroubleshooting: false,
|
||||
isStacked: false,
|
||||
source: ActionSource.timeline,
|
||||
);
|
||||
|
||||
expect(ActionButtonType.upload.shouldShow(context), isTrue);
|
||||
});
|
||||
});
|
||||
|
||||
group('removeFromAlbum button', () {
|
||||
test('should show when owner, not locked, and has current album', () {
|
||||
final album = createRemoteAlbum();
|
||||
final context = ActionButtonContext(
|
||||
asset: mergedAsset,
|
||||
isOwner: true,
|
||||
isArchived: false,
|
||||
isTrashEnabled: true,
|
||||
isInLockedView: false,
|
||||
currentAlbum: album,
|
||||
advancedTroubleshooting: false,
|
||||
isStacked: false,
|
||||
source: ActionSource.timeline,
|
||||
);
|
||||
|
||||
expect(ActionButtonType.removeFromAlbum.shouldShow(context), isTrue);
|
||||
});
|
||||
|
||||
test('should not show when no current album', () {
|
||||
final context = ActionButtonContext(
|
||||
asset: mergedAsset,
|
||||
isOwner: true,
|
||||
isArchived: false,
|
||||
isTrashEnabled: true,
|
||||
isInLockedView: false,
|
||||
currentAlbum: null,
|
||||
advancedTroubleshooting: false,
|
||||
isStacked: false,
|
||||
source: ActionSource.timeline,
|
||||
);
|
||||
|
||||
expect(ActionButtonType.removeFromAlbum.shouldShow(context), isFalse);
|
||||
});
|
||||
});
|
||||
|
||||
group('likeActivity button', () {
|
||||
test('should show when not locked, has album, activity enabled, and shared', () {
|
||||
final album = createRemoteAlbum(isActivityEnabled: true, isShared: true);
|
||||
final context = ActionButtonContext(
|
||||
asset: mergedAsset,
|
||||
isOwner: true,
|
||||
isArchived: false,
|
||||
isTrashEnabled: true,
|
||||
isInLockedView: false,
|
||||
currentAlbum: album,
|
||||
advancedTroubleshooting: false,
|
||||
isStacked: false,
|
||||
source: ActionSource.timeline,
|
||||
);
|
||||
|
||||
expect(ActionButtonType.likeActivity.shouldShow(context), isTrue);
|
||||
});
|
||||
|
||||
test('should not show when activity not enabled', () {
|
||||
final album = createRemoteAlbum(isActivityEnabled: false, isShared: true);
|
||||
final context = ActionButtonContext(
|
||||
asset: mergedAsset,
|
||||
isOwner: true,
|
||||
isArchived: false,
|
||||
isTrashEnabled: true,
|
||||
isInLockedView: false,
|
||||
currentAlbum: album,
|
||||
advancedTroubleshooting: false,
|
||||
isStacked: false,
|
||||
source: ActionSource.timeline,
|
||||
);
|
||||
|
||||
expect(ActionButtonType.likeActivity.shouldShow(context), isFalse);
|
||||
});
|
||||
|
||||
test('should not show when album not shared', () {
|
||||
final album = createRemoteAlbum(isActivityEnabled: true, isShared: false);
|
||||
final context = ActionButtonContext(
|
||||
asset: mergedAsset,
|
||||
isOwner: true,
|
||||
isArchived: false,
|
||||
isTrashEnabled: true,
|
||||
isInLockedView: false,
|
||||
currentAlbum: album,
|
||||
advancedTroubleshooting: false,
|
||||
isStacked: false,
|
||||
source: ActionSource.timeline,
|
||||
);
|
||||
|
||||
expect(ActionButtonType.likeActivity.shouldShow(context), isFalse);
|
||||
});
|
||||
|
||||
test('should not show when no album', () {
|
||||
final context = ActionButtonContext(
|
||||
asset: mergedAsset,
|
||||
isOwner: true,
|
||||
isArchived: false,
|
||||
isTrashEnabled: true,
|
||||
isInLockedView: false,
|
||||
currentAlbum: null,
|
||||
advancedTroubleshooting: false,
|
||||
isStacked: false,
|
||||
source: ActionSource.timeline,
|
||||
);
|
||||
|
||||
expect(ActionButtonType.likeActivity.shouldShow(context), isFalse);
|
||||
});
|
||||
});
|
||||
|
||||
group('advancedTroubleshooting button', () {
|
||||
test('should show when in advanced troubleshooting mode', () {
|
||||
final context = ActionButtonContext(
|
||||
asset: mergedAsset,
|
||||
isOwner: true,
|
||||
isArchived: false,
|
||||
isTrashEnabled: true,
|
||||
isInLockedView: false,
|
||||
currentAlbum: null,
|
||||
advancedTroubleshooting: true,
|
||||
isStacked: false,
|
||||
source: ActionSource.timeline,
|
||||
);
|
||||
|
||||
expect(ActionButtonType.advancedInfo.shouldShow(context), isTrue);
|
||||
});
|
||||
|
||||
test('should not show when not in advanced troubleshooting mode', () {
|
||||
final context = ActionButtonContext(
|
||||
asset: mergedAsset,
|
||||
isOwner: true,
|
||||
isArchived: false,
|
||||
isTrashEnabled: true,
|
||||
isInLockedView: false,
|
||||
currentAlbum: null,
|
||||
advancedTroubleshooting: false,
|
||||
isStacked: false,
|
||||
source: ActionSource.timeline,
|
||||
);
|
||||
|
||||
expect(ActionButtonType.advancedInfo.shouldShow(context), isFalse);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
group('unstack button', () {
|
||||
test('should show when owner, not locked, has remote, and is stacked', () {
|
||||
final remoteAsset = createRemoteAsset();
|
||||
final context = ActionButtonContext(
|
||||
asset: remoteAsset,
|
||||
isOwner: true,
|
||||
isArchived: false,
|
||||
isTrashEnabled: true,
|
||||
isInLockedView: false,
|
||||
currentAlbum: null,
|
||||
advancedTroubleshooting: false,
|
||||
isStacked: true,
|
||||
source: ActionSource.timeline,
|
||||
);
|
||||
|
||||
expect(ActionButtonType.unstack.shouldShow(context), isTrue);
|
||||
});
|
||||
|
||||
test('should not show when not stacked', () {
|
||||
final remoteAsset = createRemoteAsset();
|
||||
final context = ActionButtonContext(
|
||||
asset: remoteAsset,
|
||||
isOwner: true,
|
||||
isArchived: false,
|
||||
isTrashEnabled: true,
|
||||
isInLockedView: false,
|
||||
currentAlbum: null,
|
||||
advancedTroubleshooting: false,
|
||||
isStacked: false,
|
||||
source: ActionSource.timeline,
|
||||
);
|
||||
|
||||
expect(ActionButtonType.unstack.shouldShow(context), isFalse);
|
||||
});
|
||||
|
||||
test('should not show when not owner', () {
|
||||
final remoteAsset = createRemoteAsset();
|
||||
final context = ActionButtonContext(
|
||||
asset: remoteAsset,
|
||||
isOwner: false,
|
||||
isArchived: true,
|
||||
isTrashEnabled: true,
|
||||
isInLockedView: false,
|
||||
currentAlbum: null,
|
||||
advancedTroubleshooting: false,
|
||||
isStacked: false,
|
||||
source: ActionSource.timeline,
|
||||
);
|
||||
|
||||
expect(ActionButtonType.unstack.shouldShow(context), isFalse);
|
||||
});
|
||||
});
|
||||
|
||||
group('ActionButtonType.buildButton', () {
|
||||
late BaseAsset asset;
|
||||
late ActionButtonContext context;
|
||||
|
||||
setUp(() {
|
||||
asset = createLocalAsset(remoteId: 'remote-id');
|
||||
context = ActionButtonContext(
|
||||
asset: asset,
|
||||
isOwner: true,
|
||||
isArchived: false,
|
||||
isTrashEnabled: true,
|
||||
isInLockedView: false,
|
||||
currentAlbum: null,
|
||||
advancedTroubleshooting: false,
|
||||
isStacked: false,
|
||||
source: ActionSource.timeline,
|
||||
);
|
||||
});
|
||||
|
||||
test('should build correct widget for each button type', () {
|
||||
for (final buttonType in ActionButtonType.values) {
|
||||
var buttonContext = context;
|
||||
|
||||
if (buttonType == ActionButtonType.removeFromAlbum) {
|
||||
final album = createRemoteAlbum();
|
||||
final contextWithAlbum = ActionButtonContext(
|
||||
asset: asset,
|
||||
isOwner: true,
|
||||
isArchived: false,
|
||||
isTrashEnabled: true,
|
||||
isInLockedView: false,
|
||||
currentAlbum: album,
|
||||
advancedTroubleshooting: false,
|
||||
isStacked: false,
|
||||
source: ActionSource.timeline,
|
||||
);
|
||||
final widget = buttonType.buildButton(contextWithAlbum);
|
||||
expect(widget, isA<Widget>());
|
||||
} else if (buttonType == ActionButtonType.similarPhotos) {
|
||||
final contextWithAlbum = ActionButtonContext(
|
||||
asset: createRemoteAsset(),
|
||||
isOwner: true,
|
||||
isArchived: false,
|
||||
isTrashEnabled: true,
|
||||
isInLockedView: false,
|
||||
currentAlbum: null,
|
||||
advancedTroubleshooting: false,
|
||||
isStacked: false,
|
||||
source: ActionSource.timeline,
|
||||
);
|
||||
final widget = buttonType.buildButton(contextWithAlbum);
|
||||
expect(widget, isA<Widget>());
|
||||
} else if (buttonType == ActionButtonType.unstack) {
|
||||
final album = createRemoteAlbum();
|
||||
final contextWithAlbum = ActionButtonContext(
|
||||
asset: asset,
|
||||
isOwner: true,
|
||||
isArchived: false,
|
||||
isTrashEnabled: true,
|
||||
isInLockedView: false,
|
||||
currentAlbum: album,
|
||||
advancedTroubleshooting: false,
|
||||
isStacked: true,
|
||||
source: ActionSource.timeline,
|
||||
);
|
||||
final widget = buttonType.buildButton(contextWithAlbum);
|
||||
expect(widget, isA<Widget>());
|
||||
} else {
|
||||
final widget = buttonType.buildButton(buttonContext);
|
||||
expect(widget, isA<Widget>());
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
group('ActionButtonBuilder', () {
|
||||
test('should return buttons that should show', () {
|
||||
final remoteAsset = createRemoteAsset();
|
||||
final context = ActionButtonContext(
|
||||
asset: remoteAsset,
|
||||
isOwner: true,
|
||||
isArchived: false,
|
||||
isTrashEnabled: true,
|
||||
isInLockedView: false,
|
||||
currentAlbum: null,
|
||||
advancedTroubleshooting: false,
|
||||
isStacked: false,
|
||||
source: ActionSource.timeline,
|
||||
);
|
||||
|
||||
final widgets = ActionButtonBuilder.build(context);
|
||||
|
||||
expect(widgets, isNotEmpty);
|
||||
expect(widgets.length, greaterThan(0));
|
||||
});
|
||||
|
||||
test('should include album-specific buttons when album is present', () {
|
||||
final remoteAsset = createRemoteAsset();
|
||||
final album = createRemoteAlbum(isActivityEnabled: true, isShared: true);
|
||||
final context = ActionButtonContext(
|
||||
asset: remoteAsset,
|
||||
isOwner: true,
|
||||
isArchived: false,
|
||||
isTrashEnabled: true,
|
||||
isInLockedView: false,
|
||||
currentAlbum: album,
|
||||
advancedTroubleshooting: false,
|
||||
isStacked: false,
|
||||
source: ActionSource.timeline,
|
||||
);
|
||||
|
||||
final widgets = ActionButtonBuilder.build(context);
|
||||
|
||||
expect(widgets, isNotEmpty);
|
||||
});
|
||||
|
||||
test('should only include local buttons for local assets', () {
|
||||
final localAsset = createLocalAsset();
|
||||
final context = ActionButtonContext(
|
||||
asset: localAsset,
|
||||
isOwner: true,
|
||||
isArchived: false,
|
||||
isTrashEnabled: true,
|
||||
isInLockedView: false,
|
||||
currentAlbum: null,
|
||||
advancedTroubleshooting: false,
|
||||
isStacked: false,
|
||||
source: ActionSource.timeline,
|
||||
);
|
||||
|
||||
final widgets = ActionButtonBuilder.build(context);
|
||||
|
||||
expect(widgets, isNotEmpty);
|
||||
});
|
||||
|
||||
test('should respect archived state', () {
|
||||
final remoteAsset = createRemoteAsset();
|
||||
|
||||
final archivedContext = ActionButtonContext(
|
||||
asset: remoteAsset,
|
||||
isOwner: true,
|
||||
isArchived: true,
|
||||
isTrashEnabled: true,
|
||||
isInLockedView: false,
|
||||
currentAlbum: null,
|
||||
advancedTroubleshooting: false,
|
||||
isStacked: false,
|
||||
source: ActionSource.timeline,
|
||||
);
|
||||
|
||||
final archivedWidgets = ActionButtonBuilder.build(archivedContext);
|
||||
|
||||
final nonArchivedContext = ActionButtonContext(
|
||||
asset: remoteAsset,
|
||||
isOwner: true,
|
||||
isArchived: false,
|
||||
isTrashEnabled: true,
|
||||
isInLockedView: false,
|
||||
currentAlbum: null,
|
||||
advancedTroubleshooting: false,
|
||||
isStacked: false,
|
||||
source: ActionSource.timeline,
|
||||
);
|
||||
|
||||
final nonArchivedWidgets = ActionButtonBuilder.build(nonArchivedContext);
|
||||
|
||||
expect(archivedWidgets, isNotEmpty);
|
||||
expect(nonArchivedWidgets, isNotEmpty);
|
||||
});
|
||||
});
|
||||
}
|
||||
92
mobile/test/utils/semver_test.dart
Normal file
92
mobile/test/utils/semver_test.dart
Normal file
|
|
@ -0,0 +1,92 @@
|
|||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:immich_mobile/utils/semver.dart';
|
||||
|
||||
void main() {
|
||||
group('SemVer', () {
|
||||
test('Parses valid semantic version strings correctly', () {
|
||||
final version = SemVer.fromString('1.2.3');
|
||||
expect(version.major, 1);
|
||||
expect(version.minor, 2);
|
||||
expect(version.patch, 3);
|
||||
});
|
||||
|
||||
test('Throws FormatException for invalid version strings', () {
|
||||
expect(() => SemVer.fromString('1.2'), throwsFormatException);
|
||||
expect(() => SemVer.fromString('a.b.c'), throwsFormatException);
|
||||
expect(() => SemVer.fromString('1.2.3.4'), throwsFormatException);
|
||||
});
|
||||
|
||||
test('Compares equal versons correctly', () {
|
||||
final v1 = SemVer.fromString('1.2.3');
|
||||
final v2 = SemVer.fromString('1.2.3');
|
||||
expect(v1 == v2, isTrue);
|
||||
expect(v1 > v2, isFalse);
|
||||
expect(v1 < v2, isFalse);
|
||||
});
|
||||
|
||||
test('Compares major version correctly', () {
|
||||
final v1 = SemVer.fromString('2.0.0');
|
||||
final v2 = SemVer.fromString('1.9.9');
|
||||
expect(v1 == v2, isFalse);
|
||||
expect(v1 > v2, isTrue);
|
||||
expect(v1 < v2, isFalse);
|
||||
});
|
||||
|
||||
test('Compares minor version correctly', () {
|
||||
final v1 = SemVer.fromString('1.3.0');
|
||||
final v2 = SemVer.fromString('1.2.9');
|
||||
expect(v1 == v2, isFalse);
|
||||
expect(v1 > v2, isTrue);
|
||||
expect(v1 < v2, isFalse);
|
||||
});
|
||||
|
||||
test('Compares patch version correctly', () {
|
||||
final v1 = SemVer.fromString('1.2.4');
|
||||
final v2 = SemVer.fromString('1.2.3');
|
||||
expect(v1 == v2, isFalse);
|
||||
expect(v1 > v2, isTrue);
|
||||
expect(v1 < v2, isFalse);
|
||||
});
|
||||
|
||||
test('Gives correct major difference type', () {
|
||||
final v1 = SemVer.fromString('2.0.0');
|
||||
final v2 = SemVer.fromString('1.9.9');
|
||||
expect(v1.differenceType(v2), SemVerType.major);
|
||||
});
|
||||
|
||||
test('Gives correct minor difference type', () {
|
||||
final v1 = SemVer.fromString('1.3.0');
|
||||
final v2 = SemVer.fromString('1.2.9');
|
||||
expect(v1.differenceType(v2), SemVerType.minor);
|
||||
});
|
||||
|
||||
test('Gives correct patch difference type', () {
|
||||
final v1 = SemVer.fromString('1.2.4');
|
||||
final v2 = SemVer.fromString('1.2.3');
|
||||
expect(v1.differenceType(v2), SemVerType.patch);
|
||||
});
|
||||
|
||||
test('Gives null difference type for equal versions', () {
|
||||
final v1 = SemVer.fromString('1.2.3');
|
||||
final v2 = SemVer.fromString('1.2.3');
|
||||
expect(v1.differenceType(v2), isNull);
|
||||
});
|
||||
|
||||
test('toString returns correct format', () {
|
||||
final version = SemVer.fromString('1.2.3');
|
||||
expect(version.toString(), '1.2.3');
|
||||
});
|
||||
|
||||
test('Parses versions with leading v correctly', () {
|
||||
final version1 = SemVer.fromString('v1.2.3');
|
||||
expect(version1.major, 1);
|
||||
expect(version1.minor, 2);
|
||||
expect(version1.patch, 3);
|
||||
|
||||
final version2 = SemVer.fromString('V1.2.3');
|
||||
expect(version2.major, 1);
|
||||
expect(version2.minor, 2);
|
||||
expect(version2.patch, 3);
|
||||
});
|
||||
});
|
||||
}
|
||||
278
mobile/test/utils/timezone_test.dart
Normal file
278
mobile/test/utils/timezone_test.dart
Normal file
|
|
@ -0,0 +1,278 @@
|
|||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:immich_mobile/utils/timezone.dart';
|
||||
import 'package:timezone/data/latest.dart' as tz;
|
||||
|
||||
void main() {
|
||||
setUpAll(() {
|
||||
tz.initializeTimeZones();
|
||||
});
|
||||
|
||||
group('applyTimezoneOffset', () {
|
||||
group('with named timezone locations', () {
|
||||
test('should convert UTC to Asia/Hong_Kong (+08:00)', () {
|
||||
final utcTime = DateTime.utc(2024, 6, 15, 12, 0, 0);
|
||||
|
||||
final (adjustedTime, offset) = applyTimezoneOffset(
|
||||
dateTime: utcTime,
|
||||
timeZone: 'Asia/Hong_Kong',
|
||||
);
|
||||
|
||||
expect(adjustedTime.hour, 20); // 12:00 UTC + 8 hours = 20:00
|
||||
expect(offset, const Duration(hours: 8));
|
||||
});
|
||||
|
||||
test('should convert UTC to America/New_York (handles DST)', () {
|
||||
// Summer time (EDT = UTC-4)
|
||||
final summerUtc = DateTime.utc(2024, 6, 15, 12, 0, 0);
|
||||
final (summerTime, summerOffset) = applyTimezoneOffset(
|
||||
dateTime: summerUtc,
|
||||
timeZone: 'America/New_York',
|
||||
);
|
||||
|
||||
expect(summerTime.hour, 8); // 12:00 UTC - 4 hours = 08:00
|
||||
expect(summerOffset, const Duration(hours: -4));
|
||||
|
||||
// Winter time (EST = UTC-5)
|
||||
final winterUtc = DateTime.utc(2024, 1, 15, 12, 0, 0);
|
||||
final (winterTime, winterOffset) = applyTimezoneOffset(
|
||||
dateTime: winterUtc,
|
||||
timeZone: 'America/New_York',
|
||||
);
|
||||
|
||||
expect(winterTime.hour, 7); // 12:00 UTC - 5 hours = 07:00
|
||||
expect(winterOffset, const Duration(hours: -5));
|
||||
});
|
||||
|
||||
test('should convert UTC to Europe/London', () {
|
||||
// Winter (GMT = UTC+0)
|
||||
final winterUtc = DateTime.utc(2024, 1, 15, 12, 0, 0);
|
||||
final (winterTime, winterOffset) = applyTimezoneOffset(
|
||||
dateTime: winterUtc,
|
||||
timeZone: 'Europe/London',
|
||||
);
|
||||
|
||||
expect(winterTime.hour, 12);
|
||||
expect(winterOffset, Duration.zero);
|
||||
|
||||
// Summer (BST = UTC+1)
|
||||
final summerUtc = DateTime.utc(2024, 6, 15, 12, 0, 0);
|
||||
final (summerTime, summerOffset) = applyTimezoneOffset(
|
||||
dateTime: summerUtc,
|
||||
timeZone: 'Europe/London',
|
||||
);
|
||||
|
||||
expect(summerTime.hour, 13);
|
||||
expect(summerOffset, const Duration(hours: 1));
|
||||
});
|
||||
|
||||
test('should handle timezone with 30-minute offset (Asia/Kolkata)', () {
|
||||
final utcTime = DateTime.utc(2024, 6, 15, 12, 0, 0);
|
||||
|
||||
final (adjustedTime, offset) = applyTimezoneOffset(
|
||||
dateTime: utcTime,
|
||||
timeZone: 'Asia/Kolkata',
|
||||
);
|
||||
|
||||
expect(adjustedTime.hour, 17);
|
||||
expect(adjustedTime.minute, 30); // 12:00 UTC + 5:30 = 17:30
|
||||
expect(offset, const Duration(hours: 5, minutes: 30));
|
||||
});
|
||||
|
||||
test('should handle timezone with 45-minute offset (Asia/Kathmandu)', () {
|
||||
final utcTime = DateTime.utc(2024, 6, 15, 12, 0, 0);
|
||||
|
||||
final (adjustedTime, offset) = applyTimezoneOffset(
|
||||
dateTime: utcTime,
|
||||
timeZone: 'Asia/Kathmandu',
|
||||
);
|
||||
|
||||
expect(adjustedTime.hour, 17);
|
||||
expect(adjustedTime.minute, 45); // 12:00 UTC + 5:45 = 17:45
|
||||
expect(offset, const Duration(hours: 5, minutes: 45));
|
||||
});
|
||||
});
|
||||
|
||||
group('with UTC offset format', () {
|
||||
test('should handle UTC+08:00 format', () {
|
||||
final utcTime = DateTime.utc(2024, 6, 15, 12, 0, 0);
|
||||
|
||||
final (adjustedTime, offset) = applyTimezoneOffset(
|
||||
dateTime: utcTime,
|
||||
timeZone: 'UTC+08:00',
|
||||
);
|
||||
|
||||
expect(adjustedTime.hour, 20);
|
||||
expect(offset, const Duration(hours: 8));
|
||||
});
|
||||
|
||||
test('should handle UTC-05:00 format', () {
|
||||
final utcTime = DateTime.utc(2024, 6, 15, 12, 0, 0);
|
||||
|
||||
final (adjustedTime, offset) = applyTimezoneOffset(
|
||||
dateTime: utcTime,
|
||||
timeZone: 'UTC-05:00',
|
||||
);
|
||||
|
||||
expect(adjustedTime.hour, 7);
|
||||
expect(offset, const Duration(hours: -5));
|
||||
});
|
||||
|
||||
test('should handle UTC+8 format (without minutes)', () {
|
||||
final utcTime = DateTime.utc(2024, 6, 15, 12, 0, 0);
|
||||
|
||||
final (adjustedTime, offset) = applyTimezoneOffset(
|
||||
dateTime: utcTime,
|
||||
timeZone: 'UTC+8',
|
||||
);
|
||||
|
||||
expect(adjustedTime.hour, 20);
|
||||
expect(offset, const Duration(hours: 8));
|
||||
});
|
||||
|
||||
test('should handle UTC-5 format (without minutes)', () {
|
||||
final utcTime = DateTime.utc(2024, 6, 15, 12, 0, 0);
|
||||
|
||||
final (adjustedTime, offset) = applyTimezoneOffset(
|
||||
dateTime: utcTime,
|
||||
timeZone: 'UTC-5',
|
||||
);
|
||||
|
||||
expect(adjustedTime.hour, 7);
|
||||
expect(offset, const Duration(hours: -5));
|
||||
});
|
||||
|
||||
test('should handle plain UTC format', () {
|
||||
final utcTime = DateTime.utc(2024, 6, 15, 12, 0, 0);
|
||||
|
||||
final (adjustedTime, offset) = applyTimezoneOffset(
|
||||
dateTime: utcTime,
|
||||
timeZone: 'UTC',
|
||||
);
|
||||
|
||||
expect(adjustedTime.hour, 12);
|
||||
expect(offset, Duration.zero);
|
||||
});
|
||||
|
||||
test('should handle lowercase utc format', () {
|
||||
final utcTime = DateTime.utc(2024, 6, 15, 12, 0, 0);
|
||||
|
||||
final (adjustedTime, offset) = applyTimezoneOffset(
|
||||
dateTime: utcTime,
|
||||
timeZone: 'utc+08:00',
|
||||
);
|
||||
|
||||
expect(adjustedTime.hour, 20);
|
||||
expect(offset, const Duration(hours: 8));
|
||||
});
|
||||
|
||||
test('should handle UTC+05:30 format (with minutes)', () {
|
||||
final utcTime = DateTime.utc(2024, 6, 15, 12, 0, 0);
|
||||
|
||||
final (adjustedTime, offset) = applyTimezoneOffset(
|
||||
dateTime: utcTime,
|
||||
timeZone: 'UTC+05:30',
|
||||
);
|
||||
|
||||
expect(adjustedTime.hour, 17);
|
||||
expect(adjustedTime.minute, 30);
|
||||
expect(offset, const Duration(hours: 5, minutes: 30));
|
||||
});
|
||||
});
|
||||
|
||||
group('with null or invalid timezone', () {
|
||||
test('should return UTC time when timezone is null', () {
|
||||
final localTime = DateTime(2024, 6, 15, 12, 0, 0);
|
||||
|
||||
final (adjustedTime, offset) = applyTimezoneOffset(
|
||||
dateTime: localTime,
|
||||
timeZone: null,
|
||||
);
|
||||
|
||||
expect(adjustedTime.isUtc, true);
|
||||
expect(offset, adjustedTime.timeZoneOffset);
|
||||
});
|
||||
|
||||
test('should return UTC time when timezone is invalid', () {
|
||||
final utcTime = DateTime.utc(2024, 6, 15, 12, 0, 0);
|
||||
|
||||
final (adjustedTime, offset) = applyTimezoneOffset(
|
||||
dateTime: utcTime,
|
||||
timeZone: 'Invalid/Timezone',
|
||||
);
|
||||
|
||||
expect(adjustedTime.isUtc, true);
|
||||
expect(adjustedTime.hour, 12);
|
||||
expect(offset, adjustedTime.timeZoneOffset);
|
||||
});
|
||||
|
||||
test('should return UTC time when UTC offset format is malformed', () {
|
||||
final utcTime = DateTime.utc(2024, 6, 15, 12, 0, 0);
|
||||
|
||||
final (adjustedTime, offset) = applyTimezoneOffset(
|
||||
dateTime: utcTime,
|
||||
timeZone: 'UTC++08',
|
||||
);
|
||||
|
||||
expect(adjustedTime.isUtc, true);
|
||||
expect(adjustedTime.hour, 12);
|
||||
});
|
||||
});
|
||||
|
||||
group('edge cases', () {
|
||||
test('should handle date crossing midnight forward', () {
|
||||
final utcTime = DateTime.utc(2024, 6, 15, 20, 0, 0);
|
||||
|
||||
final (adjustedTime, offset) = applyTimezoneOffset(
|
||||
dateTime: utcTime,
|
||||
timeZone: 'Asia/Tokyo', // UTC+9
|
||||
);
|
||||
|
||||
expect(adjustedTime.day, 16); // Crosses to next day
|
||||
expect(adjustedTime.hour, 5); // 20:00 UTC + 9 = 05:00 next day
|
||||
expect(offset, const Duration(hours: 9));
|
||||
});
|
||||
|
||||
test('should handle date crossing midnight backward', () {
|
||||
final utcTime = DateTime.utc(2024, 6, 15, 3, 0, 0);
|
||||
|
||||
final (adjustedTime, offset) = applyTimezoneOffset(
|
||||
dateTime: utcTime,
|
||||
timeZone: 'America/Los_Angeles', // UTC-7 in summer
|
||||
);
|
||||
|
||||
expect(adjustedTime.day, 14); // Crosses to previous day
|
||||
expect(adjustedTime.hour, 20); // 03:00 UTC - 7 = 20:00 previous day
|
||||
expect(offset, const Duration(hours: -7));
|
||||
});
|
||||
|
||||
test('should handle year boundary crossing', () {
|
||||
final utcTime = DateTime.utc(2024, 1, 1, 2, 0, 0);
|
||||
|
||||
final (adjustedTime, offset) = applyTimezoneOffset(
|
||||
dateTime: utcTime,
|
||||
timeZone: 'America/New_York', // UTC-5 in winter
|
||||
);
|
||||
|
||||
expect(adjustedTime.year, 2023);
|
||||
expect(adjustedTime.month, 12);
|
||||
expect(adjustedTime.day, 31);
|
||||
expect(adjustedTime.hour, 21); // 02:00 UTC - 5 = 21:00 Dec 31
|
||||
});
|
||||
|
||||
test('should convert local time to UTC before applying timezone', () {
|
||||
// Create a local time (not UTC)
|
||||
final localTime = DateTime(2024, 6, 15, 12, 0, 0);
|
||||
|
||||
final (adjustedTime, _) = applyTimezoneOffset(
|
||||
dateTime: localTime,
|
||||
timeZone: 'Asia/Hong_Kong',
|
||||
);
|
||||
|
||||
// The function converts to UTC first, then applies timezone
|
||||
// So local 12:00 -> UTC (depends on local timezone) -> HK time
|
||||
// We can verify it's working by checking it's a TZDateTime
|
||||
expect(adjustedTime, isNotNull);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
27
mobile/test/widget_tester_extensions.dart
Normal file
27
mobile/test/widget_tester_extensions.dart
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
|
||||
extension PumpConsumerWidget on WidgetTester {
|
||||
/// Wraps the provided [widget] with Material app such that it becomes:
|
||||
///
|
||||
/// ProviderScope
|
||||
/// |-MaterialApp
|
||||
/// |-Material
|
||||
/// |-[widget]
|
||||
Future<void> pumpConsumerWidget(
|
||||
Widget widget, {
|
||||
Duration? duration,
|
||||
EnginePhase phase = EnginePhase.sendSemanticsUpdate,
|
||||
List<Override> overrides = const [],
|
||||
}) async {
|
||||
return pumpWidget(
|
||||
ProviderScope(
|
||||
overrides: overrides,
|
||||
child: MaterialApp(debugShowCheckedModeBanner: false, home: Material(child: widget)),
|
||||
),
|
||||
duration: duration,
|
||||
phase: phase,
|
||||
);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue