nc-photos/test/test_util.dart

377 lines
10 KiB
Dart
Raw Normal View History

2021-12-26 20:11:31 +01:00
import 'package:collection/collection.dart';
2021-11-11 18:04:17 +01:00
import 'package:flutter/foundation.dart';
2021-12-02 07:04:11 +01:00
import 'package:idb_shim/idb.dart';
2021-11-11 18:04:17 +01:00
import 'package:logging/logging.dart';
import 'package:nc_photos/account.dart';
2021-12-02 07:04:11 +01:00
import 'package:nc_photos/app_db.dart';
2021-11-12 22:13:02 +01:00
import 'package:nc_photos/ci_string.dart';
2021-11-23 08:39:35 +01:00
import 'package:nc_photos/entity/album.dart';
import 'package:nc_photos/entity/album/cover_provider.dart';
import 'package:nc_photos/entity/album/item.dart';
import 'package:nc_photos/entity/album/provider.dart';
import 'package:nc_photos/entity/album/sort_provider.dart';
2021-11-11 18:04:17 +01:00
import 'package:nc_photos/entity/file.dart';
import 'package:nc_photos/entity/share.dart';
import 'package:nc_photos/entity/sharee.dart';
2021-11-23 08:39:35 +01:00
import 'package:nc_photos/iterable_extension.dart';
import 'package:nc_photos/type.dart';
2021-12-02 07:04:11 +01:00
2021-11-23 08:39:35 +01:00
class FilesBuilder {
FilesBuilder({
int initialFileId = 0,
}) : fileId = initialFileId;
List<File> build() {
return files.map((f) => f.copyWith()).toList();
}
2022-01-01 18:29:51 +01:00
void add(
2021-11-23 08:39:35 +01:00
String relativePath, {
2022-01-01 18:29:51 +01:00
int? contentLength,
String? contentType,
2021-11-23 08:39:35 +01:00
DateTime? lastModified,
2022-01-01 18:29:51 +01:00
bool isCollection = false,
2021-11-23 08:39:35 +01:00
bool hasPreview = true,
String ownerId = "admin",
}) {
2022-01-01 18:29:51 +01:00
files.add(File(
2021-11-23 08:39:35 +01:00
path: "remote.php/dav/files/$relativePath",
contentLength: contentLength,
2022-01-01 18:29:51 +01:00
contentType: contentType,
2021-11-23 08:39:35 +01:00
lastModified:
lastModified ?? DateTime.utc(2020, 1, 2, 3, 4, 5 + files.length),
2022-01-01 18:29:51 +01:00
isCollection: isCollection,
2021-11-23 08:39:35 +01:00
hasPreview: hasPreview,
fileId: fileId++,
2022-01-01 18:29:51 +01:00
ownerId: ownerId.toCi(),
2021-11-23 08:39:35 +01:00
));
}
2022-01-01 18:29:51 +01:00
void addGenericFile(
String relativePath,
String contentType, {
int contentLength = 1024,
DateTime? lastModified,
bool hasPreview = true,
String ownerId = "admin",
}) =>
add(
relativePath,
contentLength: contentLength,
contentType: contentType,
lastModified: lastModified,
hasPreview: hasPreview,
ownerId: ownerId,
);
void addJpeg(
String relativePath, {
int contentLength = 1024,
DateTime? lastModified,
bool hasPreview = true,
String ownerId = "admin",
}) =>
add(
relativePath,
contentLength: contentLength,
contentType: "image/jpeg",
lastModified: lastModified,
hasPreview: hasPreview,
ownerId: ownerId,
);
void addDir(
String relativePath, {
int contentLength = 1024,
DateTime? lastModified,
String ownerId = "admin",
}) =>
add(
relativePath,
lastModified: lastModified,
isCollection: true,
hasPreview: false,
ownerId: ownerId,
);
2021-11-23 08:39:35 +01:00
final files = <File>[];
int fileId;
}
/// Create an album for testing
class AlbumBuilder {
AlbumBuilder({
DateTime? lastUpdated,
2021-11-27 10:22:51 +01:00
String? name,
this.albumFilename = "test0.nc_album.json",
2021-11-23 08:39:35 +01:00
this.fileId = 0,
2021-11-27 10:22:51 +01:00
String? ownerId,
}) : lastUpdated = lastUpdated ?? DateTime.utc(2020, 1, 2, 3, 4, 5),
name = name ?? "test",
ownerId = ownerId ?? "admin";
factory AlbumBuilder.ofId({
required int albumId,
DateTime? lastUpdated,
String? name,
String? ownerId,
}) =>
AlbumBuilder(
lastUpdated: lastUpdated,
name: name,
albumFilename: "test$albumId.nc_album.json",
fileId: albumId,
ownerId: ownerId,
);
2021-11-23 08:39:35 +01:00
Album build() {
final latestFileItem = items
.whereType<AlbumFileItem>()
.stableSorted(
(a, b) => a.file.lastModified!.compareTo(b.file.lastModified!))
.reversed
.firstOrNull;
return Album(
lastUpdated: lastUpdated,
name: name,
provider: AlbumStaticProvider(
items: items,
latestItemTime: latestFileItem?.file.lastModified,
),
coverProvider: cover == null
? AlbumAutoCoverProvider(coverFile: latestFileItem?.file)
: AlbumManualCoverProvider(coverFile: cover!),
sortProvider: const AlbumNullSortProvider(),
shares: shares.isEmpty ? null : shares,
albumFile: buildAlbumFile(
path: buildAlbumFilePath(albumFilename, user: ownerId),
fileId: fileId,
ownerId: ownerId,
),
);
}
/// Add a file item
///
/// By default, the item will be added by admin and added at the same time as
/// the file's lastModified.
///
/// If [isCover] is true, the coverProvider of the album will become
/// [AlbumManualCoverProvider]
void addFileItem(
File file, {
String addedBy = "admin",
DateTime? addedAt,
bool isCover = false,
}) {
final fileItem = AlbumFileItem(
file: file,
addedBy: addedBy.toCi(),
addedAt: addedAt ?? file.lastModified!,
);
items.add(fileItem);
if (isCover) {
cover = file;
}
}
/// Add an album share
2021-11-24 09:36:16 +01:00
///
/// By default, the album will be shared at 2020-01-02 03:04:05
void addShare(
String userId, {
DateTime? sharedAt,
}) {
shares.add(buildAlbumShare(
userId: userId,
sharedAt: sharedAt,
));
2021-11-23 08:39:35 +01:00
}
2021-12-02 06:00:18 +01:00
static List<AlbumFileItem> fileItemsOf(Album album) =>
2021-11-23 08:39:35 +01:00
AlbumStaticProvider.of(album).items.whereType<AlbumFileItem>().toList();
final DateTime lastUpdated;
final String name;
final String albumFilename;
final int fileId;
final String ownerId;
final items = <AlbumItem>[];
File? cover;
final shares = <AlbumShare>[];
}
2021-11-11 18:04:17 +01:00
void initLog() {
Logger.root.level = Level.ALL;
Logger.root.onRecord.listen((record) {
2021-12-02 06:00:18 +01:00
String msg =
"[${record.loggerName}] ${record.level.name}: ${record.message}";
if (record.error != null) {
msg += " (throw: ${record.error.runtimeType} { ${record.error} })";
}
if (record.stackTrace != null) {
msg += "\nStack Trace:\n${record.stackTrace}";
}
2021-12-07 15:28:20 +01:00
int color;
if (record.level >= Level.SEVERE) {
color = 91;
} else if (record.level >= Level.WARNING) {
color = 33;
} else if (record.level >= Level.INFO) {
color = 34;
} else if (record.level >= Level.FINER) {
color = 32;
} else {
color = 90;
}
msg = "\x1B[${color}m$msg\x1B[0m";
2021-12-02 06:00:18 +01:00
debugPrint(msg);
2021-11-11 18:04:17 +01:00
});
}
Account buildAccount({
2021-12-05 13:02:22 +01:00
String id = "123456-000000",
2021-11-11 18:04:17 +01:00
String scheme = "http",
String address = "example.com",
String username = "admin",
String password = "pass",
List<String> roots = const [""],
}) =>
2021-12-05 13:02:22 +01:00
Account(id, scheme, address, username.toCi(), password, roots);
2021-11-11 18:04:17 +01:00
/// Build a mock [File] pointing to a album JSON file
///
/// Warning: not all fields are filled, but the most essential ones are
File buildAlbumFile({
required String path,
int contentLength = 1024,
DateTime? lastModified,
required int fileId,
String ownerId = "admin",
}) =>
File(
path: path,
contentLength: contentLength,
contentType: "application/json",
lastModified: lastModified ?? DateTime.utc(2020, 1, 2, 3, 4, 5),
isCollection: false,
hasPreview: false,
fileId: fileId,
2021-11-12 22:13:02 +01:00
ownerId: ownerId.toCi(),
2021-11-11 18:04:17 +01:00
);
String buildAlbumFilePath(
String filename, {
String user = "admin",
}) =>
"remote.php/dav/files/$user/.com.nkming.nc_photos/albums/$filename";
2021-11-23 08:39:35 +01:00
AlbumShare buildAlbumShare({
required String userId,
String? displayName,
2021-11-24 09:36:16 +01:00
DateTime? sharedAt,
2021-11-23 08:39:35 +01:00
}) =>
AlbumShare(
userId: userId.toCi(),
displayName: displayName ?? userId,
2021-11-24 09:36:16 +01:00
sharedAt: sharedAt ?? DateTime.utc(2020, 1, 2, 3, 4, 5),
2021-11-23 08:39:35 +01:00
);
2021-11-11 18:04:17 +01:00
/// Build a mock [File] pointing to a JPEG image file
///
/// Warning: not all fields are filled, but the most essential ones are
File buildJpegFile({
required String path,
int contentLength = 1024,
DateTime? lastModified,
bool hasPreview = true,
required int fileId,
String ownerId = "admin",
}) =>
File(
path: path,
contentLength: contentLength,
contentType: "image/jpeg",
lastModified: lastModified ?? DateTime.utc(2020, 1, 2, 3, 4, 5),
isCollection: false,
hasPreview: hasPreview,
fileId: fileId,
2021-11-12 22:13:02 +01:00
ownerId: ownerId.toCi(),
2021-11-11 18:04:17 +01:00
);
Share buildShare({
required String id,
DateTime? stime,
String uidOwner = "admin",
String? displaynameOwner,
required File file,
required String shareWith,
}) =>
Share(
id: id,
shareType: ShareType.user,
stime: stime ?? DateTime.utc(2020, 1, 2, 3, 4, 5),
2021-11-12 22:13:02 +01:00
uidOwner: uidOwner.toCi(),
2021-11-11 18:04:17 +01:00
displaynameOwner: displaynameOwner ?? uidOwner,
uidFileOwner: file.ownerId!,
2021-11-11 18:04:17 +01:00
path: file.strippedPath,
itemType: ShareItemType.file,
mimeType: file.contentType ?? "",
itemSource: file.fileId!,
2021-11-12 22:13:02 +01:00
shareWith: shareWith.toCi(),
2021-11-11 18:04:17 +01:00
shareWithDisplayName: shareWith,
);
Sharee buildSharee({
ShareeType type = ShareeType.user,
2021-11-23 08:39:35 +01:00
String? label,
2021-11-11 18:04:17 +01:00
int shareType = 0,
2021-11-12 22:13:02 +01:00
required CiString shareWith,
2021-11-11 18:04:17 +01:00
String? shareWithDisplayNameUnique,
}) =>
Sharee(
type: type,
2021-11-23 08:39:35 +01:00
label: label ?? shareWith.toString(),
2021-11-11 18:04:17 +01:00
shareType: shareType,
shareWith: shareWith,
);
2021-12-02 07:04:11 +01:00
Future<void> fillAppDb(
AppDb appDb, Account account, Iterable<File> files) async {
2021-12-02 07:04:11 +01:00
await appDb.use((db) async {
final transaction = db.transaction(AppDb.file2StoreName, idbModeReadWrite);
final file2Store = transaction.objectStore(AppDb.file2StoreName);
2021-12-02 07:04:11 +01:00
for (final f in files) {
await file2Store.put(AppDbFile2Entry.fromFile(account, f).toJson(),
AppDbFile2Entry.toPrimaryKeyForFile(account, f));
2021-12-02 07:04:11 +01:00
}
});
}
Future<void> fillAppDbDir(
AppDb appDb, Account account, File dir, List<File> children) async {
await appDb.use((db) async {
final transaction = db.transaction(AppDb.dirStoreName, idbModeReadWrite);
final dirStore = transaction.objectStore(AppDb.dirStoreName);
await dirStore.put(AppDbDirEntry.fromFiles(account, dir, children).toJson(),
AppDbDirEntry.toPrimaryKeyForDir(account, dir));
});
}
Future<List<T>> listAppDb<T>(
AppDb appDb, String storeName, T Function(JsonObj) transform) {
return appDb.use((db) async {
final transaction = db.transaction(storeName, idbModeReadOnly);
final store = transaction.objectStore(storeName);
return await store
.openCursor(autoAdvance: true)
.map((c) => c.value)
.cast<Map>()
.map((e) => transform(e.cast<String, dynamic>()))
.toList();
});
}