parent
38e01c04d0
commit
f462a3d966
@ -0,0 +1,265 @@
|
|||||||
|
import 'dart:io';
|
||||||
|
import 'package:dio/dio.dart';
|
||||||
|
import '../local/local_storage_service.dart';
|
||||||
|
import '../local/models/item.dart';
|
||||||
|
import 'models/immich_asset.dart';
|
||||||
|
import 'models/upload_response.dart';
|
||||||
|
|
||||||
|
/// Exception thrown when Immich API operations fail.
|
||||||
|
class ImmichException implements Exception {
|
||||||
|
/// Error message.
|
||||||
|
final String message;
|
||||||
|
|
||||||
|
/// HTTP status code if available.
|
||||||
|
final int? statusCode;
|
||||||
|
|
||||||
|
/// Creates an [ImmichException] with the provided message.
|
||||||
|
ImmichException(this.message, [this.statusCode]);
|
||||||
|
|
||||||
|
@override
|
||||||
|
String toString() {
|
||||||
|
if (statusCode != null) {
|
||||||
|
return 'ImmichException: $message (Status: $statusCode)';
|
||||||
|
}
|
||||||
|
return 'ImmichException: $message';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Service for interacting with Immich API.
|
||||||
|
///
|
||||||
|
/// This service provides:
|
||||||
|
/// - Upload images to Immich
|
||||||
|
/// - Fetch image lists from Immich
|
||||||
|
/// - Store image metadata locally after uploads
|
||||||
|
///
|
||||||
|
/// The service is modular and UI-independent, designed for offline-first behavior.
|
||||||
|
class ImmichService {
|
||||||
|
/// HTTP client for API requests.
|
||||||
|
final Dio _dio;
|
||||||
|
|
||||||
|
/// Local storage service for caching metadata.
|
||||||
|
final LocalStorageService _localStorage;
|
||||||
|
|
||||||
|
/// Immich API base URL.
|
||||||
|
final String _baseUrl;
|
||||||
|
|
||||||
|
/// Immich API key for authentication.
|
||||||
|
final String _apiKey;
|
||||||
|
|
||||||
|
/// Creates an [ImmichService] instance.
|
||||||
|
///
|
||||||
|
/// [baseUrl] - Immich server base URL (e.g., 'https://immich.example.com').
|
||||||
|
/// [apiKey] - Immich API key for authentication.
|
||||||
|
/// [localStorage] - Local storage service for caching metadata.
|
||||||
|
/// [dio] - Optional Dio instance for dependency injection (useful for testing).
|
||||||
|
ImmichService({
|
||||||
|
required String baseUrl,
|
||||||
|
required String apiKey,
|
||||||
|
required LocalStorageService localStorage,
|
||||||
|
Dio? dio,
|
||||||
|
}) : _baseUrl = baseUrl,
|
||||||
|
_apiKey = apiKey,
|
||||||
|
_localStorage = localStorage,
|
||||||
|
_dio = dio ?? Dio() {
|
||||||
|
_dio.options.baseUrl = baseUrl;
|
||||||
|
_dio.options.headers['x-api-key'] = apiKey;
|
||||||
|
_dio.options.headers['Content-Type'] = 'application/json';
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Uploads an image file to Immich.
|
||||||
|
///
|
||||||
|
/// [imageFile] - The image file to upload.
|
||||||
|
/// [albumId] - Optional album ID to add the image to.
|
||||||
|
///
|
||||||
|
/// Returns [UploadResponse] containing the uploaded asset ID.
|
||||||
|
///
|
||||||
|
/// Throws [ImmichException] if upload fails.
|
||||||
|
/// Automatically stores metadata in local storage upon successful upload.
|
||||||
|
Future<UploadResponse> uploadImage(
|
||||||
|
File imageFile, {
|
||||||
|
String? albumId,
|
||||||
|
}) async {
|
||||||
|
try {
|
||||||
|
if (!await imageFile.exists()) {
|
||||||
|
throw ImmichException('Image file does not exist: ${imageFile.path}');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Prepare form data for multipart upload
|
||||||
|
final formData = FormData.fromMap({
|
||||||
|
'assetData': await MultipartFile.fromFile(
|
||||||
|
imageFile.path,
|
||||||
|
filename: imageFile.path.split('/').last,
|
||||||
|
),
|
||||||
|
if (albumId != null) 'albumId': albumId,
|
||||||
|
});
|
||||||
|
|
||||||
|
// Upload to Immich
|
||||||
|
final response = await _dio.post(
|
||||||
|
'/api/asset/upload',
|
||||||
|
data: formData,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (response.statusCode != 200 && response.statusCode != 201) {
|
||||||
|
throw ImmichException(
|
||||||
|
'Upload failed: ${response.statusMessage}',
|
||||||
|
response.statusCode,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
final uploadResponse = UploadResponse.fromJson(response.data);
|
||||||
|
|
||||||
|
// Fetch full asset details to store complete metadata
|
||||||
|
final asset = await _getAssetById(uploadResponse.id);
|
||||||
|
|
||||||
|
// Store metadata in local storage
|
||||||
|
await _storeAssetMetadata(asset);
|
||||||
|
|
||||||
|
return uploadResponse;
|
||||||
|
} on DioException catch (e) {
|
||||||
|
throw ImmichException(
|
||||||
|
'Upload failed: ${e.message ?? 'Unknown error'}',
|
||||||
|
e.response?.statusCode,
|
||||||
|
);
|
||||||
|
} catch (e) {
|
||||||
|
throw ImmichException('Upload failed: $e');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Fetches a list of assets from Immich.
|
||||||
|
///
|
||||||
|
/// [limit] - Maximum number of assets to fetch (default: 100).
|
||||||
|
/// [skip] - Number of assets to skip (for pagination).
|
||||||
|
///
|
||||||
|
/// Returns a list of [ImmichAsset] instances.
|
||||||
|
///
|
||||||
|
/// Throws [ImmichException] if fetch fails.
|
||||||
|
/// Automatically stores fetched metadata in local storage.
|
||||||
|
Future<List<ImmichAsset>> fetchAssets({
|
||||||
|
int limit = 100,
|
||||||
|
int skip = 0,
|
||||||
|
}) async {
|
||||||
|
try {
|
||||||
|
final response = await _dio.get(
|
||||||
|
'/api/asset',
|
||||||
|
queryParameters: {
|
||||||
|
'limit': limit,
|
||||||
|
'skip': skip,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
if (response.statusCode != 200) {
|
||||||
|
throw ImmichException(
|
||||||
|
'Failed to fetch assets: ${response.statusMessage}',
|
||||||
|
response.statusCode,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
final List<dynamic> assetsJson = response.data;
|
||||||
|
final List<ImmichAsset> assets = assetsJson
|
||||||
|
.map((json) => ImmichAsset.fromJson(json as Map<String, dynamic>))
|
||||||
|
.toList();
|
||||||
|
|
||||||
|
// Store all fetched assets in local storage
|
||||||
|
for (final asset in assets) {
|
||||||
|
await _storeAssetMetadata(asset);
|
||||||
|
}
|
||||||
|
|
||||||
|
return assets;
|
||||||
|
} on DioException catch (e) {
|
||||||
|
throw ImmichException(
|
||||||
|
'Failed to fetch assets: ${e.message ?? 'Unknown error'}',
|
||||||
|
e.response?.statusCode,
|
||||||
|
);
|
||||||
|
} catch (e) {
|
||||||
|
throw ImmichException('Failed to fetch assets: $e');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Fetches a single asset by ID.
|
||||||
|
///
|
||||||
|
/// [assetId] - The unique identifier of the asset.
|
||||||
|
///
|
||||||
|
/// Returns [ImmichAsset] if found.
|
||||||
|
///
|
||||||
|
/// Throws [ImmichException] if fetch fails.
|
||||||
|
Future<ImmichAsset> _getAssetById(String assetId) async {
|
||||||
|
try {
|
||||||
|
final response = await _dio.get('/api/asset/$assetId');
|
||||||
|
|
||||||
|
if (response.statusCode != 200) {
|
||||||
|
throw ImmichException(
|
||||||
|
'Failed to fetch asset: ${response.statusMessage}',
|
||||||
|
response.statusCode,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return ImmichAsset.fromJson(response.data as Map<String, dynamic>);
|
||||||
|
} on DioException catch (e) {
|
||||||
|
throw ImmichException(
|
||||||
|
'Failed to fetch asset: ${e.message ?? 'Unknown error'}',
|
||||||
|
e.response?.statusCode,
|
||||||
|
);
|
||||||
|
} catch (e) {
|
||||||
|
throw ImmichException('Failed to fetch asset: $e');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Stores asset metadata in local storage.
|
||||||
|
///
|
||||||
|
/// [asset] - The asset to store.
|
||||||
|
Future<void> _storeAssetMetadata(ImmichAsset asset) async {
|
||||||
|
try {
|
||||||
|
final item = Item(
|
||||||
|
id: 'immich_${asset.id}',
|
||||||
|
data: {
|
||||||
|
'type': 'immich_asset',
|
||||||
|
'asset': asset.toJson(),
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
await _localStorage.insertItem(item);
|
||||||
|
} catch (e) {
|
||||||
|
// Log error but don't fail the upload/fetch operation
|
||||||
|
// Local storage is a cache, not critical for the operation
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Gets locally cached asset metadata.
|
||||||
|
///
|
||||||
|
/// [assetId] - The unique identifier of the asset.
|
||||||
|
///
|
||||||
|
/// Returns [ImmichAsset] if found in local storage, null otherwise.
|
||||||
|
Future<ImmichAsset?> getCachedAsset(String assetId) async {
|
||||||
|
try {
|
||||||
|
final item = await _localStorage.getItem('immich_$assetId');
|
||||||
|
if (item == null) return null;
|
||||||
|
|
||||||
|
final assetData = item.data['asset'] as Map<String, dynamic>;
|
||||||
|
return ImmichAsset.fromLocalJson(assetData);
|
||||||
|
} catch (e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Gets all locally cached assets.
|
||||||
|
///
|
||||||
|
/// Returns a list of [ImmichAsset] instances from local storage.
|
||||||
|
Future<List<ImmichAsset>> getCachedAssets() async {
|
||||||
|
try {
|
||||||
|
final items = await _localStorage.getAllItems();
|
||||||
|
final List<ImmichAsset> assets = [];
|
||||||
|
|
||||||
|
for (final item in items) {
|
||||||
|
if (item.data['type'] == 'immich_asset') {
|
||||||
|
final assetData = item.data['asset'] as Map<String, dynamic>;
|
||||||
|
assets.add(ImmichAsset.fromLocalJson(assetData));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return assets;
|
||||||
|
} catch (e) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@ -0,0 +1,79 @@
|
|||||||
|
/// Represents an asset (image) from Immich API.
|
||||||
|
class ImmichAsset {
|
||||||
|
/// Unique identifier for the asset.
|
||||||
|
final String id;
|
||||||
|
|
||||||
|
/// File name of the asset.
|
||||||
|
final String fileName;
|
||||||
|
|
||||||
|
/// Creation date/time of the asset.
|
||||||
|
final DateTime createdAt;
|
||||||
|
|
||||||
|
/// File size in bytes.
|
||||||
|
final int fileSize;
|
||||||
|
|
||||||
|
/// MIME type of the asset.
|
||||||
|
final String mimeType;
|
||||||
|
|
||||||
|
/// Width of the image in pixels.
|
||||||
|
final int? width;
|
||||||
|
|
||||||
|
/// Height of the image in pixels.
|
||||||
|
final int? height;
|
||||||
|
|
||||||
|
/// Creates an [ImmichAsset] instance.
|
||||||
|
ImmichAsset({
|
||||||
|
required this.id,
|
||||||
|
required this.fileName,
|
||||||
|
required this.createdAt,
|
||||||
|
required this.fileSize,
|
||||||
|
required this.mimeType,
|
||||||
|
this.width,
|
||||||
|
this.height,
|
||||||
|
});
|
||||||
|
|
||||||
|
/// Creates an [ImmichAsset] from Immich API JSON response.
|
||||||
|
factory ImmichAsset.fromJson(Map<String, dynamic> json) {
|
||||||
|
return ImmichAsset(
|
||||||
|
id: json['id'] as String,
|
||||||
|
fileName: json['originalFileName'] as String? ?? json['fileName'] as String? ?? 'unknown',
|
||||||
|
createdAt: DateTime.parse(json['createdAt'] as String),
|
||||||
|
fileSize: json['fileSizeByte'] as int? ?? json['fileSize'] as int? ?? 0,
|
||||||
|
mimeType: json['mimeType'] as String? ?? 'image/jpeg',
|
||||||
|
width: json['exifInfo']?['imageWidth'] as int?,
|
||||||
|
height: json['exifInfo']?['imageHeight'] as int?,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Converts [ImmichAsset] to JSON for local storage.
|
||||||
|
Map<String, dynamic> toJson() {
|
||||||
|
return {
|
||||||
|
'id': id,
|
||||||
|
'fileName': fileName,
|
||||||
|
'createdAt': createdAt.toIso8601String(),
|
||||||
|
'fileSize': fileSize,
|
||||||
|
'mimeType': mimeType,
|
||||||
|
'width': width,
|
||||||
|
'height': height,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Creates an [ImmichAsset] from local storage JSON.
|
||||||
|
factory ImmichAsset.fromLocalJson(Map<String, dynamic> json) {
|
||||||
|
return ImmichAsset(
|
||||||
|
id: json['id'] as String,
|
||||||
|
fileName: json['fileName'] as String,
|
||||||
|
createdAt: DateTime.parse(json['createdAt'] as String),
|
||||||
|
fileSize: json['fileSize'] as int,
|
||||||
|
mimeType: json['mimeType'] as String,
|
||||||
|
width: json['width'] as int?,
|
||||||
|
height: json['height'] as int?,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
String toString() {
|
||||||
|
return 'ImmichAsset(id: $id, fileName: $fileName, createdAt: $createdAt)';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@ -0,0 +1,28 @@
|
|||||||
|
/// Response from Immich upload API.
|
||||||
|
class UploadResponse {
|
||||||
|
/// The unique identifier of the uploaded asset.
|
||||||
|
final String id;
|
||||||
|
|
||||||
|
/// Whether the upload was a duplicate (already existed).
|
||||||
|
final bool duplicate;
|
||||||
|
|
||||||
|
/// Creates an [UploadResponse] instance.
|
||||||
|
UploadResponse({
|
||||||
|
required this.id,
|
||||||
|
required this.duplicate,
|
||||||
|
});
|
||||||
|
|
||||||
|
/// Creates an [UploadResponse] from Immich API JSON response.
|
||||||
|
factory UploadResponse.fromJson(Map<String, dynamic> json) {
|
||||||
|
return UploadResponse(
|
||||||
|
id: json['id'] as String,
|
||||||
|
duplicate: json['duplicate'] as bool? ?? false,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
String toString() {
|
||||||
|
return 'UploadResponse(id: $id, duplicate: $duplicate)';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@ -0,0 +1,447 @@
|
|||||||
|
import 'dart:io';
|
||||||
|
import 'dart:convert';
|
||||||
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
import 'package:dio/dio.dart';
|
||||||
|
import 'package:app_boilerplate/data/immich/immich_service.dart';
|
||||||
|
import 'package:app_boilerplate/data/immich/models/immich_asset.dart';
|
||||||
|
import 'package:app_boilerplate/data/immich/models/upload_response.dart';
|
||||||
|
import 'package:app_boilerplate/data/local/local_storage_service.dart';
|
||||||
|
import 'package:app_boilerplate/data/local/models/item.dart';
|
||||||
|
import 'package:path/path.dart' as path;
|
||||||
|
import 'package:sqflite_common_ffi/sqflite_ffi.dart';
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
// Initialize Flutter bindings and sqflite for testing
|
||||||
|
TestWidgetsFlutterBinding.ensureInitialized();
|
||||||
|
sqfliteFfiInit();
|
||||||
|
databaseFactory = databaseFactoryFfi;
|
||||||
|
|
||||||
|
late LocalStorageService localStorage;
|
||||||
|
late Directory testDir;
|
||||||
|
late String testDbPath;
|
||||||
|
late Directory testCacheDir;
|
||||||
|
|
||||||
|
setUp(() async {
|
||||||
|
// Create temporary directory for testing
|
||||||
|
testDir = await Directory.systemTemp.createTemp('immich_test_');
|
||||||
|
testDbPath = path.join(testDir.path, 'test_local_storage.db');
|
||||||
|
testCacheDir = Directory(path.join(testDir.path, 'image_cache'));
|
||||||
|
|
||||||
|
// Initialize local storage
|
||||||
|
localStorage = LocalStorageService(
|
||||||
|
testDbPath: testDbPath,
|
||||||
|
testCacheDir: testCacheDir,
|
||||||
|
);
|
||||||
|
await localStorage.initialize();
|
||||||
|
});
|
||||||
|
|
||||||
|
tearDown(() async {
|
||||||
|
await localStorage.close();
|
||||||
|
try {
|
||||||
|
if (await testDir.exists()) {
|
||||||
|
await testDir.delete(recursive: true);
|
||||||
|
}
|
||||||
|
} catch (_) {
|
||||||
|
// Ignore cleanup errors
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
group('ImmichService - Upload', () {
|
||||||
|
/// Tests successful image upload.
|
||||||
|
test('uploadImage - success', () async {
|
||||||
|
// Arrange
|
||||||
|
final testFile = File(path.join(testDir.path, 'test_image.jpg'));
|
||||||
|
await testFile.writeAsBytes([1, 2, 3, 4, 5]);
|
||||||
|
|
||||||
|
final mockDio = _createMockDio(
|
||||||
|
onPost: (path, data) => Response(
|
||||||
|
statusCode: 200,
|
||||||
|
data: {
|
||||||
|
'id': 'asset-123',
|
||||||
|
'duplicate': false,
|
||||||
|
},
|
||||||
|
requestOptions: RequestOptions(path: path),
|
||||||
|
),
|
||||||
|
onGet: (path) => Response(
|
||||||
|
statusCode: 200,
|
||||||
|
data: {
|
||||||
|
'id': 'asset-123',
|
||||||
|
'originalFileName': 'test_image.jpg',
|
||||||
|
'createdAt': DateTime.now().toIso8601String(),
|
||||||
|
'fileSizeByte': 5,
|
||||||
|
'mimeType': 'image/jpeg',
|
||||||
|
},
|
||||||
|
requestOptions: RequestOptions(path: path),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
final immichService = ImmichService(
|
||||||
|
baseUrl: 'https://immich.example.com',
|
||||||
|
apiKey: 'test-api-key',
|
||||||
|
localStorage: localStorage,
|
||||||
|
dio: mockDio,
|
||||||
|
);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
final response = await immichService.uploadImage(testFile);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
expect(response.id, equals('asset-123'));
|
||||||
|
expect(response.duplicate, isFalse);
|
||||||
|
|
||||||
|
// Verify metadata was stored locally
|
||||||
|
final cachedAsset = await immichService.getCachedAsset('asset-123');
|
||||||
|
expect(cachedAsset, isNotNull);
|
||||||
|
expect(cachedAsset!.id, equals('asset-123'));
|
||||||
|
});
|
||||||
|
|
||||||
|
/// Tests upload failure when file doesn't exist.
|
||||||
|
test('uploadImage - file not found', () async {
|
||||||
|
// Arrange
|
||||||
|
final nonExistentFile = File(path.join(testDir.path, 'non_existent.jpg'));
|
||||||
|
|
||||||
|
final mockDio = _createMockDio();
|
||||||
|
final immichService = ImmichService(
|
||||||
|
baseUrl: 'https://immich.example.com',
|
||||||
|
apiKey: 'test-api-key',
|
||||||
|
localStorage: localStorage,
|
||||||
|
dio: mockDio,
|
||||||
|
);
|
||||||
|
|
||||||
|
// Act & Assert
|
||||||
|
expect(
|
||||||
|
() => immichService.uploadImage(nonExistentFile),
|
||||||
|
throwsA(isA<ImmichException>()),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
/// Tests upload failure with network error.
|
||||||
|
test('uploadImage - network error', () async {
|
||||||
|
// Arrange
|
||||||
|
final testFile = File(path.join(testDir.path, 'test_image.jpg'));
|
||||||
|
await testFile.writeAsBytes([1, 2, 3]);
|
||||||
|
|
||||||
|
final mockDio = _createMockDio(
|
||||||
|
onPost: (path, data) => throw DioException(
|
||||||
|
requestOptions: RequestOptions(path: path),
|
||||||
|
error: 'Network error',
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
final immichService = ImmichService(
|
||||||
|
baseUrl: 'https://immich.example.com',
|
||||||
|
apiKey: 'test-api-key',
|
||||||
|
localStorage: localStorage,
|
||||||
|
dio: mockDio,
|
||||||
|
);
|
||||||
|
|
||||||
|
// Act & Assert
|
||||||
|
expect(
|
||||||
|
() => immichService.uploadImage(testFile),
|
||||||
|
throwsA(isA<ImmichException>()),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
/// Tests upload with duplicate detection.
|
||||||
|
test('uploadImage - duplicate asset', () async {
|
||||||
|
// Arrange
|
||||||
|
final testFile = File(path.join(testDir.path, 'test_image.jpg'));
|
||||||
|
await testFile.writeAsBytes([1, 2, 3]);
|
||||||
|
|
||||||
|
final mockDio = _createMockDio(
|
||||||
|
onPost: (path, data) => Response(
|
||||||
|
statusCode: 200,
|
||||||
|
data: {
|
||||||
|
'id': 'asset-456',
|
||||||
|
'duplicate': true,
|
||||||
|
},
|
||||||
|
requestOptions: RequestOptions(path: path),
|
||||||
|
),
|
||||||
|
onGet: (path) => Response(
|
||||||
|
statusCode: 200,
|
||||||
|
data: {
|
||||||
|
'id': 'asset-456',
|
||||||
|
'originalFileName': 'test_image.jpg',
|
||||||
|
'createdAt': DateTime.now().toIso8601String(),
|
||||||
|
'fileSizeByte': 3,
|
||||||
|
'mimeType': 'image/jpeg',
|
||||||
|
},
|
||||||
|
requestOptions: RequestOptions(path: path),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
final immichService = ImmichService(
|
||||||
|
baseUrl: 'https://immich.example.com',
|
||||||
|
apiKey: 'test-api-key',
|
||||||
|
localStorage: localStorage,
|
||||||
|
dio: mockDio,
|
||||||
|
);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
final response = await immichService.uploadImage(testFile);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
expect(response.duplicate, isTrue);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
group('ImmichService - Fetch Assets', () {
|
||||||
|
/// Tests successful asset fetch.
|
||||||
|
test('fetchAssets - success', () async {
|
||||||
|
// Arrange
|
||||||
|
final mockAssets = [
|
||||||
|
{
|
||||||
|
'id': 'asset-1',
|
||||||
|
'originalFileName': 'image1.jpg',
|
||||||
|
'createdAt': DateTime.now().toIso8601String(),
|
||||||
|
'fileSizeByte': 1000,
|
||||||
|
'mimeType': 'image/jpeg',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
'id': 'asset-2',
|
||||||
|
'originalFileName': 'image2.jpg',
|
||||||
|
'createdAt': DateTime.now().toIso8601String(),
|
||||||
|
'fileSizeByte': 2000,
|
||||||
|
'mimeType': 'image/png',
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
final mockDio = _createMockDio(
|
||||||
|
onGet: (path) => Response(
|
||||||
|
statusCode: 200,
|
||||||
|
data: mockAssets,
|
||||||
|
requestOptions: RequestOptions(path: path),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
final immichService = ImmichService(
|
||||||
|
baseUrl: 'https://immich.example.com',
|
||||||
|
apiKey: 'test-api-key',
|
||||||
|
localStorage: localStorage,
|
||||||
|
dio: mockDio,
|
||||||
|
);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
final assets = await immichService.fetchAssets(limit: 100);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
expect(assets.length, equals(2));
|
||||||
|
expect(assets[0].id, equals('asset-1'));
|
||||||
|
expect(assets[1].id, equals('asset-2'));
|
||||||
|
|
||||||
|
// Verify assets were cached locally
|
||||||
|
final cachedAssets = await immichService.getCachedAssets();
|
||||||
|
expect(cachedAssets.length, equals(2));
|
||||||
|
});
|
||||||
|
|
||||||
|
/// Tests fetch with pagination.
|
||||||
|
test('fetchAssets - pagination', () async {
|
||||||
|
// Arrange
|
||||||
|
final mockDio = _createMockDio(
|
||||||
|
onGet: (path) {
|
||||||
|
// Extract query parameters from request options
|
||||||
|
// The path might be just the endpoint, so we need to check request options
|
||||||
|
final uri = path.startsWith('http')
|
||||||
|
? Uri.parse(path)
|
||||||
|
: Uri.parse('https://immich.example.com$path');
|
||||||
|
final queryParams = uri.queryParameters;
|
||||||
|
final skip = int.parse(queryParams['skip'] ?? '0');
|
||||||
|
final limit = int.parse(queryParams['limit'] ?? '100');
|
||||||
|
|
||||||
|
return Response(
|
||||||
|
statusCode: 200,
|
||||||
|
data: List.generate(
|
||||||
|
limit,
|
||||||
|
(i) => {
|
||||||
|
'id': 'asset-${skip + i}',
|
||||||
|
'originalFileName': 'image${skip + i}.jpg',
|
||||||
|
'createdAt': DateTime.now().toIso8601String(),
|
||||||
|
'fileSizeByte': 1000,
|
||||||
|
'mimeType': 'image/jpeg',
|
||||||
|
},
|
||||||
|
),
|
||||||
|
requestOptions: RequestOptions(path: path),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
final immichService = ImmichService(
|
||||||
|
baseUrl: 'https://immich.example.com',
|
||||||
|
apiKey: 'test-api-key',
|
||||||
|
localStorage: localStorage,
|
||||||
|
dio: mockDio,
|
||||||
|
);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
final assets1 = await immichService.fetchAssets(limit: 10, skip: 0);
|
||||||
|
final assets2 = await immichService.fetchAssets(limit: 10, skip: 10);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
expect(assets1.length, equals(10));
|
||||||
|
expect(assets2.length, equals(10));
|
||||||
|
expect(assets1[0].id, equals('asset-0'));
|
||||||
|
expect(assets2[0].id, equals('asset-10'));
|
||||||
|
});
|
||||||
|
|
||||||
|
/// Tests fetch failure with server error.
|
||||||
|
test('fetchAssets - server error', () async {
|
||||||
|
// Arrange
|
||||||
|
final mockDio = _createMockDio(
|
||||||
|
onGet: (path) => Response(
|
||||||
|
statusCode: 500,
|
||||||
|
data: {'error': 'Internal server error'},
|
||||||
|
requestOptions: RequestOptions(path: path),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
final immichService = ImmichService(
|
||||||
|
baseUrl: 'https://immich.example.com',
|
||||||
|
apiKey: 'test-api-key',
|
||||||
|
localStorage: localStorage,
|
||||||
|
dio: mockDio,
|
||||||
|
);
|
||||||
|
|
||||||
|
// Act & Assert
|
||||||
|
expect(
|
||||||
|
() => immichService.fetchAssets(),
|
||||||
|
throwsA(isA<ImmichException>()),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
group('ImmichService - Cached Assets', () {
|
||||||
|
/// Tests getting cached asset by ID.
|
||||||
|
test('getCachedAsset - success', () async {
|
||||||
|
// Arrange - store asset in local storage
|
||||||
|
final asset = ImmichAsset(
|
||||||
|
id: 'asset-789',
|
||||||
|
fileName: 'cached_image.jpg',
|
||||||
|
createdAt: DateTime.now(),
|
||||||
|
fileSize: 5000,
|
||||||
|
mimeType: 'image/jpeg',
|
||||||
|
);
|
||||||
|
|
||||||
|
final item = Item(
|
||||||
|
id: 'immich_asset-789',
|
||||||
|
data: {
|
||||||
|
'type': 'immich_asset',
|
||||||
|
'asset': asset.toJson(),
|
||||||
|
},
|
||||||
|
);
|
||||||
|
await localStorage.insertItem(item);
|
||||||
|
|
||||||
|
final mockDio = _createMockDio();
|
||||||
|
final immichService = ImmichService(
|
||||||
|
baseUrl: 'https://immich.example.com',
|
||||||
|
apiKey: 'test-api-key',
|
||||||
|
localStorage: localStorage,
|
||||||
|
dio: mockDio,
|
||||||
|
);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
final cached = await immichService.getCachedAsset('asset-789');
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
expect(cached, isNotNull);
|
||||||
|
expect(cached!.id, equals('asset-789'));
|
||||||
|
expect(cached.fileName, equals('cached_image.jpg'));
|
||||||
|
});
|
||||||
|
|
||||||
|
/// Tests getting cached asset when not found.
|
||||||
|
test('getCachedAsset - not found', () async {
|
||||||
|
// Arrange
|
||||||
|
final mockDio = _createMockDio();
|
||||||
|
final immichService = ImmichService(
|
||||||
|
baseUrl: 'https://immich.example.com',
|
||||||
|
apiKey: 'test-api-key',
|
||||||
|
localStorage: localStorage,
|
||||||
|
dio: mockDio,
|
||||||
|
);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
final cached = await immichService.getCachedAsset('non-existent');
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
expect(cached, isNull);
|
||||||
|
});
|
||||||
|
|
||||||
|
/// Tests getting all cached assets.
|
||||||
|
test('getCachedAssets - returns all cached', () async {
|
||||||
|
// Arrange - store multiple assets
|
||||||
|
final assets = [
|
||||||
|
ImmichAsset(
|
||||||
|
id: 'asset-1',
|
||||||
|
fileName: 'image1.jpg',
|
||||||
|
createdAt: DateTime.now(),
|
||||||
|
fileSize: 1000,
|
||||||
|
mimeType: 'image/jpeg',
|
||||||
|
),
|
||||||
|
ImmichAsset(
|
||||||
|
id: 'asset-2',
|
||||||
|
fileName: 'image2.jpg',
|
||||||
|
createdAt: DateTime.now(),
|
||||||
|
fileSize: 2000,
|
||||||
|
mimeType: 'image/png',
|
||||||
|
),
|
||||||
|
];
|
||||||
|
|
||||||
|
for (final asset in assets) {
|
||||||
|
final item = Item(
|
||||||
|
id: 'immich_${asset.id}',
|
||||||
|
data: {
|
||||||
|
'type': 'immich_asset',
|
||||||
|
'asset': asset.toJson(),
|
||||||
|
},
|
||||||
|
);
|
||||||
|
await localStorage.insertItem(item);
|
||||||
|
}
|
||||||
|
|
||||||
|
final mockDio = _createMockDio();
|
||||||
|
final immichService = ImmichService(
|
||||||
|
baseUrl: 'https://immich.example.com',
|
||||||
|
apiKey: 'test-api-key',
|
||||||
|
localStorage: localStorage,
|
||||||
|
dio: mockDio,
|
||||||
|
);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
final cached = await immichService.getCachedAssets();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
expect(cached.length, equals(2));
|
||||||
|
expect(cached.map((a) => a.id).toList(), containsAll(['asset-1', 'asset-2']));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Helper to create a mock Dio instance for testing.
|
||||||
|
Dio _createMockDio({
|
||||||
|
Response Function(String path, dynamic data)? onPost,
|
||||||
|
Response Function(String path)? onGet,
|
||||||
|
}) {
|
||||||
|
final dio = Dio();
|
||||||
|
dio.options.baseUrl = 'https://immich.example.com';
|
||||||
|
|
||||||
|
dio.interceptors.add(InterceptorsWrapper(
|
||||||
|
onRequest: (options, handler) {
|
||||||
|
if (options.method == 'POST' && onPost != null) {
|
||||||
|
final response = onPost(options.path, options.data);
|
||||||
|
handler.resolve(response);
|
||||||
|
} else if (options.method == 'GET' && onGet != null) {
|
||||||
|
// Build full URL with query parameters for GET requests
|
||||||
|
final queryString = options.queryParameters.entries
|
||||||
|
.map((e) => '${e.key}=${e.value}')
|
||||||
|
.join('&');
|
||||||
|
final fullPath = queryString.isNotEmpty
|
||||||
|
? '${options.path}?$queryString'
|
||||||
|
: options.path;
|
||||||
|
final response = onGet(fullPath);
|
||||||
|
handler.resolve(response);
|
||||||
|
} else {
|
||||||
|
handler.next(options);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
));
|
||||||
|
|
||||||
|
return dio;
|
||||||
|
}
|
||||||
Loading…
Reference in new issue