import 'package:aves/model/entry/extensions/metadata_edition.dart'; import 'package:aves/ref/metadata/xmp.dart'; import 'package:aves/utils/xmp_utils.dart'; import 'package:test/test.dart'; import 'package:xml/xml.dart'; void main() { const toolkit = 'test-toolkit'; String? _toExpect(String? xmpString) => xmpString != null ? XmlDocument.parse(xmpString).toXmlString( pretty: true, sortAttributes: (a, b) => a.name.qualified.compareTo(b.name.qualified), ) : null; List _getDescriptions(String xmpString) { final xmpDoc = XmlDocument.parse(xmpString); final root = xmpDoc.rootElement; final rdf = root.getElement(XmpElements.rdfRoot, namespace: XmpNamespaces.rdf); return rdf!.children.where((node) { return node is XmlElement && node.name.local == XmpElements.rdfDescription && node.name.namespaceUri == XmpNamespaces.rdf; }).toList(); } const inMultiDescriptionRatings = ''' 5 99 '''; const inRatingAttribute = ''' '''; const inRatingElement = ''' 5 '''; const inSubjects = ''' the king '''; const inSubjectsCreator = ''' c a b '''; const inMotionPhotoMicroVideo = ''' '''; const inMotionPhotoContainer = ''' '''; test('Get string', () async { expect(XMP.getString(_getDescriptions(inRatingAttribute), XmpElements.xmpRating, namespace: XmpNamespaces.xmp), '5'); expect(XMP.getString(_getDescriptions(inRatingElement), XmpElements.xmpRating, namespace: XmpNamespaces.xmp), '5'); expect(XMP.getString(_getDescriptions(inSubjects), XmpElements.xmpRating, namespace: XmpNamespaces.xmp), null); }); test('Set tags without existing XMP', () async { final modifyDate = DateTime.now(); final xmpDate = XMP.toXmpDate(modifyDate); expect( _toExpect(await XMP.edit( null, toolkit, (descriptions) => ExtraAvesEntryMetadataEdition.editTagsXmp(descriptions, {'one', 'two'}), modifyDate: modifyDate, )), _toExpect(''' one two ''')); }); test('Set tags to XMP with ratings (multiple descriptions)', () async { final modifyDate = DateTime.now(); final xmpDate = XMP.toXmpDate(modifyDate); expect( _toExpect(await XMP.edit( inMultiDescriptionRatings, toolkit, (descriptions) => ExtraAvesEntryMetadataEdition.editTagsXmp(descriptions, {'one', 'two'}), modifyDate: modifyDate, )), _toExpect(''' 5 one two 99 ''')); }); test('Set tags to XMP with subjects only', () async { final modifyDate = DateTime.now(); final xmpDate = XMP.toXmpDate(modifyDate); expect( _toExpect(await XMP.edit( inSubjects, toolkit, (descriptions) => ExtraAvesEntryMetadataEdition.editTagsXmp(descriptions, {'one', 'two'}), modifyDate: modifyDate, )), _toExpect(''' one two ''')); }); test('Remove tags from XMP with subjects only', () async { expect( _toExpect(await XMP.edit( inSubjects, toolkit, (descriptions) => ExtraAvesEntryMetadataEdition.editTagsXmp(descriptions, {}), )), _toExpect(null)); }); test('Remove tags from XMP with subjects and creator', () async { final modifyDate = DateTime.now(); final xmpDate = XMP.toXmpDate(modifyDate); expect( _toExpect(await XMP.edit( inSubjectsCreator, toolkit, (descriptions) => ExtraAvesEntryMetadataEdition.editTagsXmp(descriptions, {}), modifyDate: modifyDate, )), _toExpect(''' c ''')); }); test('Set rating without existing XMP', () async { final modifyDate = DateTime.now(); final xmpDate = XMP.toXmpDate(modifyDate); expect( _toExpect(await XMP.edit( null, toolkit, (descriptions) => ExtraAvesEntryMetadataEdition.editRatingXmp(descriptions, 3), modifyDate: modifyDate, )), _toExpect(''' ''')); }); test('Set rating to XMP with ratings (multiple descriptions)', () async { final modifyDate = DateTime.now(); final xmpDate = XMP.toXmpDate(modifyDate); expect( _toExpect(await XMP.edit( inMultiDescriptionRatings, toolkit, (descriptions) => ExtraAvesEntryMetadataEdition.editRatingXmp(descriptions, 3), modifyDate: modifyDate, )), _toExpect(''' ''')); }); test('Set rating to XMP with rating attribute', () async { final modifyDate = DateTime.now(); final xmpDate = XMP.toXmpDate(modifyDate); expect( _toExpect(await XMP.edit( inRatingAttribute, toolkit, (descriptions) => ExtraAvesEntryMetadataEdition.editRatingXmp(descriptions, 3), modifyDate: modifyDate, )), _toExpect(''' ''')); }); test('Set rating to XMP with rating element', () async { final modifyDate = DateTime.now(); final xmpDate = XMP.toXmpDate(modifyDate); expect( _toExpect(await XMP.edit( inRatingElement, toolkit, (descriptions) => ExtraAvesEntryMetadataEdition.editRatingXmp(descriptions, 3), modifyDate: modifyDate, )), _toExpect(''' ''')); }); test('Set rating to XMP with subjects only', () async { final modifyDate = DateTime.now(); final xmpDate = XMP.toXmpDate(modifyDate); expect( _toExpect(await XMP.edit( inSubjects, toolkit, (descriptions) => ExtraAvesEntryMetadataEdition.editRatingXmp(descriptions, 3), modifyDate: modifyDate, )), _toExpect(''' the king ''')); }); test('Remove rating from XMP with subjects only', () async { final modifyDate = DateTime.now(); expect( _toExpect(await XMP.edit( inSubjects, toolkit, (descriptions) => ExtraAvesEntryMetadataEdition.editRatingXmp(descriptions, null), modifyDate: modifyDate, )), _toExpect(inSubjects)); }); test('Remove trailer media info from XMP with micro video', () async { final modifyDate = DateTime.now(); expect( _toExpect(await XMP.edit( inMotionPhotoMicroVideo, toolkit, ExtraAvesEntryMetadataEdition.removeContainerXmp, modifyDate: modifyDate, )), _toExpect(null)); }); test('Remove trailer media info from XMP with container', () async { final modifyDate = DateTime.now(); expect( _toExpect(await XMP.edit( inMotionPhotoContainer, toolkit, ExtraAvesEntryMetadataEdition.removeContainerXmp, modifyDate: modifyDate, )), _toExpect(null)); }); test('Remove trailer media info from XMP with no related metadata', () async { final modifyDate = DateTime.now(); expect( _toExpect(await XMP.edit( inSubjects, toolkit, ExtraAvesEntryMetadataEdition.removeContainerXmp, modifyDate: modifyDate, )), _toExpect(inSubjects)); }); test('Remove rating from XMP with ratings (multiple descriptions)', () async { final modifyDate = DateTime.now(); expect( _toExpect(await XMP.edit( inMultiDescriptionRatings, toolkit, (descriptions) => ExtraAvesEntryMetadataEdition.editRatingXmp(descriptions, null), modifyDate: modifyDate, )), _toExpect(null)); }); }