Source Code added

This commit is contained in:
Fr4nz D13trich 2026-02-02 15:06:40 +01:00
parent 800376eafd
commit 9efa9bc6dd
3912 changed files with 754770 additions and 2 deletions

View 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);
});
});
}

View 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"]),
);
});
});
}

View 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');
});
});
}