import 'package:aves/model/entry_metadata_edition.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(XMP.rdfRoot, namespace: Namespaces.rdf); return rdf!.children.where((node) { return node is XmlElement && node.name.local == XMP.rdfDescription && node.name.namespaceUri == Namespaces.rdf; }).toList(); } const inMultiDescriptionRatings = ''' 5 99 '''; const inRatingAttribute = ''' '''; const inRatingElement = ''' 5 '''; const inSubjects = ''' the king '''; const inSubjectsCreator = ''' c a b '''; test('Get string', () async { expect(XMP.getString(_getDescriptions(inRatingAttribute), XMP.xmpRating, namespace: Namespaces.xmp), '5'); expect(XMP.getString(_getDescriptions(inRatingElement), XMP.xmpRating, namespace: Namespaces.xmp), '5'); expect(XMP.getString(_getDescriptions(inSubjects), XMP.xmpRating, namespace: Namespaces.xmp), null); }); test('Set tags without existing XMP', () async { final modifyDate = DateTime.now(); final xmpDate = XMP.toXmpDate(modifyDate); expect( _toExpect(await XMP.edit( null, () async => 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, () async => 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, () async => 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, () async => 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, () async => 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, () async => 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, () async => 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, () async => 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, () async => 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, () async => toolkit, (descriptions) => ExtraAvesEntryMetadataEdition.editRatingXmp(descriptions, 3), modifyDate: modifyDate, )), _toExpect(''' the king ''')); }); test('Remove rating from XMP with subjects only', () async { final modifyDate = DateTime.now(); final xmpDate = XMP.toXmpDate(modifyDate); expect( _toExpect(await XMP.edit( inSubjects, () async => toolkit, (descriptions) => ExtraAvesEntryMetadataEdition.editRatingXmp(descriptions, null), modifyDate: modifyDate, )), _toExpect(''' the king ''')); }); test('Remove rating from XMP with ratings (multiple descriptions)', () async { final modifyDate = DateTime.now(); expect( _toExpect(await XMP.edit( inMultiDescriptionRatings, () async => toolkit, (descriptions) => ExtraAvesEntryMetadataEdition.editRatingXmp(descriptions, null), modifyDate: modifyDate, )), _toExpect(null)); }); }