import 'package:nc_photos/entity/exif.dart'; import 'package:nc_photos/entity/file.dart'; import 'package:test/test.dart'; void main() { group("compareFileDateTimeDescending", () { test("lastModified a>b", () { final a = File( path: "/remote.php/dav/files/admin/test1.jpg", lastModified: DateTime.utc(2021), ); final b = File( path: "/remote.php/dav/files/admin/test2.jpg", lastModified: DateTime.utc(2020), ); expect(compareFileDateTimeDescending(a, b), lessThan(0)); }); test("lastModified ab", () { final a = File( path: "/remote.php/dav/files/admin/test1.jpg", metadata: Metadata( exif: Exif({ "DateTimeOriginal": "2021:01:02 03:04:05", }), ), ); final b = File( path: "/remote.php/dav/files/admin/test2.jpg", metadata: Metadata( exif: Exif({ "DateTimeOriginal": "2020:01:02 03:04:05", }), ), ); expect(compareFileDateTimeDescending(a, b), lessThan(0)); }); test("exif a{ "version": Metadata.version, "lastUpdated": "2020-01-02T03:04:05.678901Z", }; expect(Metadata.fromJson(json), Metadata(lastUpdated: DateTime.utc(2020, 1, 2, 3, 4, 5, 678, 901))); }); test("fileEtag", () { final json = { "version": Metadata.version, "lastUpdated": "2020-01-02T03:04:05.678901Z", "fileEtag": "8a3e0799b6f0711c23cc2d93950eceb5", }; expect( Metadata.fromJson(json), Metadata( lastUpdated: DateTime.utc(2020, 1, 2, 3, 4, 5, 678, 901), fileEtag: "8a3e0799b6f0711c23cc2d93950eceb5", )); }); test("imageWidth", () { final json = { "version": Metadata.version, "lastUpdated": "2020-01-02T03:04:05.678901Z", "imageWidth": 1024, }; expect( Metadata.fromJson(json), Metadata( lastUpdated: DateTime.utc(2020, 1, 2, 3, 4, 5, 678, 901), imageWidth: 1024, )); }); test("imageHeight", () { final json = { "version": Metadata.version, "lastUpdated": "2020-01-02T03:04:05.678901Z", "imageHeight": 768, }; expect( Metadata.fromJson(json), Metadata( lastUpdated: DateTime.utc(2020, 1, 2, 3, 4, 5, 678, 901), imageHeight: 768, )); }); test("exif", () { final json = { "version": Metadata.version, "lastUpdated": "2020-01-02T03:04:05.678901Z", "exif": { "Make": "dummy", }, }; expect( Metadata.fromJson(json), Metadata( lastUpdated: DateTime.utc(2020, 1, 2, 3, 4, 5, 678, 901), exif: Exif({ "Make": "dummy", }), )); }); }); group("toJson", () { test("lastUpdated", () { final metadata = Metadata(lastUpdated: DateTime.utc(2020, 1, 2, 3, 4, 5, 678, 901)); expect(metadata.toJson(), { "version": Metadata.version, "lastUpdated": "2020-01-02T03:04:05.678901Z", }); }); test("fileEtag", () { final metadata = Metadata( lastUpdated: DateTime.utc(2020, 1, 2, 3, 4, 5, 678, 901), fileEtag: "8a3e0799b6f0711c23cc2d93950eceb5", ); expect(metadata.toJson(), { "version": Metadata.version, "lastUpdated": "2020-01-02T03:04:05.678901Z", "fileEtag": "8a3e0799b6f0711c23cc2d93950eceb5", }); }); test("imageWidth", () { final metadata = Metadata( lastUpdated: DateTime.utc(2020, 1, 2, 3, 4, 5, 678, 901), imageWidth: 1024, ); expect(metadata.toJson(), { "version": Metadata.version, "lastUpdated": "2020-01-02T03:04:05.678901Z", "imageWidth": 1024, }); }); test("imageHeight", () { final metadata = Metadata( lastUpdated: DateTime.utc(2020, 1, 2, 3, 4, 5, 678, 901), imageHeight: 768, ); expect(metadata.toJson(), { "version": Metadata.version, "lastUpdated": "2020-01-02T03:04:05.678901Z", "imageHeight": 768, }); }); test("exif", () { final metadata = Metadata( lastUpdated: DateTime.utc(2020, 1, 2, 3, 4, 5, 678, 901), exif: Exif({ "Make": "dummy", }), ); expect(metadata.toJson(), { "version": Metadata.version, "lastUpdated": "2020-01-02T03:04:05.678901Z", "exif": { "Make": "dummy", }, }); }); }); }); group("MetadataUpgraderV1", () { test("call webp", () { final json = { "version": 1, "lastUpdated": "2020-01-02T03:04:05.678901Z", }; expect(MetadataUpgraderV1(fileContentType: "image/webp")(json), null); }); test("call jpeg", () { final json = { "version": 1, "lastUpdated": "2020-01-02T03:04:05.678901Z", }; expect( MetadataUpgraderV1(fileContentType: "image/jpeg")(json), { "version": 1, "lastUpdated": "2020-01-02T03:04:05.678901Z", }); }); }); group("MetadataUpgraderV2", () { test("call rotated jpeg", () { final json = { "version": 2, "exif": { "Orientation": 5, }, "imageWidth": 1024, "imageHeight": 768, }; expect( MetadataUpgraderV2(fileContentType: "image/jpeg")(json), { "version": 2, "exif": { "Orientation": 5, }, "imageWidth": 768, "imageHeight": 1024, }); }); test("call non-rotated jpeg", () { final json = { "version": 2, "exif": { "Orientation": 1, }, "imageWidth": 1024, "imageHeight": 768, }; expect( MetadataUpgraderV2(fileContentType: "image/jpeg")(json), { "version": 2, "exif": { "Orientation": 1, }, "imageWidth": 1024, "imageHeight": 768, }); }); test("call webp", () { final json = { "version": 2, "exif": { "Orientation": 5, }, "imageWidth": 1024, "imageHeight": 768, }; expect( MetadataUpgraderV2(fileContentType: "image/webp")(json), { "version": 2, "exif": { "Orientation": 5, }, "imageWidth": 1024, "imageHeight": 768, }); }); }); group("File", () { group("fromJson", () { test("path", () { final json = { "path": "/remote.php/dav/files/admin/test.jpg", }; final file = File.fromJson(json); expect(file, File(path: "/remote.php/dav/files/admin/test.jpg")); }); test("contentLength", () { final json = { "path": "", "contentLength": 123, }; final file = File.fromJson(json); expect(file, File(path: "", contentLength: 123)); }); test("contentType", () { final json = { "path": "", "contentType": "image/jpeg", }; final file = File.fromJson(json); expect(file, File(path: "", contentType: "image/jpeg")); }); test("etag", () { final json = { "path": "", "etag": "8a3e0799b6f0711c23cc2d93950eceb5", }; final file = File.fromJson(json); expect(file, File(path: "", etag: "8a3e0799b6f0711c23cc2d93950eceb5")); }); test("lastModified", () { final json = { "path": "", "lastModified": "2020-01-02T03:04:05.678901Z", }; final file = File.fromJson(json); expect( file, File( path: "", lastModified: DateTime.utc(2020, 1, 2, 3, 4, 5, 678, 901), )); }); test("isCollection", () { final json = { "path": "", "isCollection": true, }; final file = File.fromJson(json); expect(file, File(path: "", isCollection: true)); }); test("usedBytes", () { final json = { "path": "", "usedBytes": 123456, }; final file = File.fromJson(json); expect(file, File(path: "", usedBytes: 123456)); }); test("hasPreview", () { final json = { "path": "", "hasPreview": true, }; final file = File.fromJson(json); expect(file, File(path: "", hasPreview: true)); }); test("metadata", () { final json = { "path": "", "metadata": { "version": Metadata.version, "lastUpdated": "2020-01-02T03:04:05.678901Z", }, }; final file = File.fromJson(json); expect( file, File( path: "", metadata: Metadata( lastUpdated: DateTime.utc(2020, 1, 2, 3, 4, 5, 678, 901), ), )); }); }); group("toJson", () { test("path", () { final file = File(path: "/remote.php/dav/files/admin/test.jpg"); expect(file.toJson(), { "path": "/remote.php/dav/files/admin/test.jpg", }); }); test("contentLength", () { final file = File( path: "/remote.php/dav/files/admin/test.jpg", contentLength: 123); expect(file.toJson(), { "path": "/remote.php/dav/files/admin/test.jpg", "contentLength": 123, }); }); test("contentType", () { final file = File( path: "/remote.php/dav/files/admin/test.jpg", contentType: "image/jpeg"); expect(file.toJson(), { "path": "/remote.php/dav/files/admin/test.jpg", "contentType": "image/jpeg", }); }); test("etag", () { final file = File( path: "/remote.php/dav/files/admin/test.jpg", etag: "8a3e0799b6f0711c23cc2d93950eceb5"); expect(file.toJson(), { "path": "/remote.php/dav/files/admin/test.jpg", "etag": "8a3e0799b6f0711c23cc2d93950eceb5", }); }); test("lastModified", () { final file = File( path: "/remote.php/dav/files/admin/test.jpg", lastModified: DateTime.utc(2020, 1, 2, 3, 4, 5, 678, 901)); expect(file.toJson(), { "path": "/remote.php/dav/files/admin/test.jpg", "lastModified": "2020-01-02T03:04:05.678901Z", }); }); test("isCollection", () { final file = File( path: "/remote.php/dav/files/admin/test.jpg", isCollection: true); expect(file.toJson(), { "path": "/remote.php/dav/files/admin/test.jpg", "isCollection": true, }); }); test("usedBytes", () { final file = File( path: "/remote.php/dav/files/admin/test.jpg", usedBytes: 123456); expect(file.toJson(), { "path": "/remote.php/dav/files/admin/test.jpg", "usedBytes": 123456, }); }); test("hasPreview", () { final file = File( path: "/remote.php/dav/files/admin/test.jpg", hasPreview: true); expect(file.toJson(), { "path": "/remote.php/dav/files/admin/test.jpg", "hasPreview": true, }); }); test("metadata", () { final file = File( path: "/remote.php/dav/files/admin/test.jpg", metadata: Metadata( lastUpdated: DateTime.utc(2020, 1, 2, 3, 4, 5, 678, 901), )); expect(file.toJson(), { "path": "/remote.php/dav/files/admin/test.jpg", "metadata": { "version": Metadata.version, "lastUpdated": "2020-01-02T03:04:05.678901Z", }, }); }); }); group("copyWith", () { final src = File( path: "/remote.php/dav/files/admin/test.jpg", contentLength: 123, contentType: "image/jpeg", etag: "8a3e0799b6f0711c23cc2d93950eceb5", lastModified: DateTime.utc(2020, 1, 2, 3, 4, 5, 678, 901), isCollection: true, usedBytes: 123456, hasPreview: true, metadata: null, ); test("path", () { final file = src.copyWith(path: "/remote.php/dav/files/admin/test.png"); expect( file, File( path: "/remote.php/dav/files/admin/test.png", contentLength: 123, contentType: "image/jpeg", etag: "8a3e0799b6f0711c23cc2d93950eceb5", lastModified: DateTime.utc(2020, 1, 2, 3, 4, 5, 678, 901), isCollection: true, usedBytes: 123456, hasPreview: true, )); }); test("contentLength", () { final file = src.copyWith(contentLength: 321); expect( file, File( path: "/remote.php/dav/files/admin/test.jpg", contentLength: 321, contentType: "image/jpeg", etag: "8a3e0799b6f0711c23cc2d93950eceb5", lastModified: DateTime.utc(2020, 1, 2, 3, 4, 5, 678, 901), isCollection: true, usedBytes: 123456, hasPreview: true, )); }); test("contentType", () { final file = src.copyWith(contentType: "image/png"); expect( file, File( path: "/remote.php/dav/files/admin/test.jpg", contentLength: 123, contentType: "image/png", etag: "8a3e0799b6f0711c23cc2d93950eceb5", lastModified: DateTime.utc(2020, 1, 2, 3, 4, 5, 678, 901), isCollection: true, usedBytes: 123456, hasPreview: true, )); }); test("etag", () { final file = src.copyWith(etag: "000"); expect( file, File( path: "/remote.php/dav/files/admin/test.jpg", contentLength: 123, contentType: "image/jpeg", etag: "000", lastModified: DateTime.utc(2020, 1, 2, 3, 4, 5, 678, 901), isCollection: true, usedBytes: 123456, hasPreview: true, )); }); test("lastModified", () { final now = DateTime.now(); final file = src.copyWith(lastModified: now); expect( file, File( path: "/remote.php/dav/files/admin/test.jpg", contentLength: 123, contentType: "image/jpeg", etag: "8a3e0799b6f0711c23cc2d93950eceb5", lastModified: now, isCollection: true, usedBytes: 123456, hasPreview: true, )); }); test("isCollection", () { final file = src.copyWith(isCollection: false); expect( file, File( path: "/remote.php/dav/files/admin/test.jpg", contentLength: 123, contentType: "image/jpeg", etag: "8a3e0799b6f0711c23cc2d93950eceb5", lastModified: DateTime.utc(2020, 1, 2, 3, 4, 5, 678, 901), isCollection: false, usedBytes: 123456, hasPreview: true, )); }); test("usedBytes", () { final file = src.copyWith(usedBytes: 999999); expect( file, File( path: "/remote.php/dav/files/admin/test.jpg", contentLength: 123, contentType: "image/jpeg", etag: "8a3e0799b6f0711c23cc2d93950eceb5", lastModified: DateTime.utc(2020, 1, 2, 3, 4, 5, 678, 901), isCollection: true, usedBytes: 999999, hasPreview: true, )); }); test("hasPreview", () { final file = src.copyWith(hasPreview: false); expect( file, File( path: "/remote.php/dav/files/admin/test.jpg", contentLength: 123, contentType: "image/jpeg", etag: "8a3e0799b6f0711c23cc2d93950eceb5", lastModified: DateTime.utc(2020, 1, 2, 3, 4, 5, 678, 901), isCollection: true, usedBytes: 123456, hasPreview: false, )); }); test("metadata", () { final metadata = Metadata(); final file = src.copyWith(metadata: metadata); expect( file, File( path: "/remote.php/dav/files/admin/test.jpg", contentLength: 123, contentType: "image/jpeg", etag: "8a3e0799b6f0711c23cc2d93950eceb5", lastModified: DateTime.utc(2020, 1, 2, 3, 4, 5, 678, 901), isCollection: true, usedBytes: 123456, hasPreview: true, metadata: metadata, )); }); }); test("withoutMetadata", () { final file = File( path: "/remote.php/dav/files/admin/test.jpg", contentLength: 123, contentType: "image/jpeg", etag: "8a3e0799b6f0711c23cc2d93950eceb5", lastModified: DateTime.utc(2020, 1, 2, 3, 4, 5, 678, 901), isCollection: true, usedBytes: 123456, hasPreview: true, metadata: Metadata(), ); expect( file.withoutMetadata(), File( path: "/remote.php/dav/files/admin/test.jpg", contentLength: 123, contentType: "image/jpeg", etag: "8a3e0799b6f0711c23cc2d93950eceb5", lastModified: DateTime.utc(2020, 1, 2, 3, 4, 5, 678, 901), isCollection: true, usedBytes: 123456, hasPreview: true, )); }); test("strippedPath", () { final file = File(path: "/remote.php/dav/files/admin/test.jpg"); expect(file.strippedPath, "admin/test.jpg"); }); }); }