@@ -20,7 +20,7 @@ import {
20
20
type ReleaseIncludes ,
21
21
LinkType ,
22
22
MusicBrainzApi , type RecordingIncludes , type IRecording ,
23
- type IMusicBrainzConfig
23
+ type IMusicBrainzConfig , IArtist
24
24
} from '../lib/entry-default.js' ;
25
25
import { assert , expect } from 'chai' ;
26
26
import type * as mb from '../lib/musicbrainz.types.js' ;
@@ -700,6 +700,61 @@ describe('MusicBrainz-api', function () {
700
700
assert . isAtLeast ( result . count , 1 ) ;
701
701
} ) ;
702
702
703
+ // Based on https://stackoverflow.com/a/79369493/28701779
704
+ it ( 'Find all artist relations of Queen - Made in Heaven' , async ( ) => {
705
+
706
+ async function findReleaseGroup ( artist : string , title : string ) {
707
+ const result = await mbApi . search ( 'release-group' , { query : `artist:"${ artist } " AND release:"${ title } "` } ) ;
708
+ // @ts -ignore
709
+ return result . count > 0 ? result [ "release-groups" ] [ 0 ] : undefined ;
710
+ }
711
+
712
+ /**
713
+ * Find all artists for provided MBID (MusicBrainz ID)
714
+ * @param mbidReleaseGroup MBID (MusicBrainz ID) Release-group
715
+ */
716
+ async function findAllRelatedArtist ( mbidReleaseGroup : string ) : Promise < MapIterator < IArtist > > {
717
+ const artists = [ ] ;
718
+ const relInfoGrp = await mbApi . lookup ( 'release-group' , mbidReleaseGroup , [ 'releases' ] ) ;
719
+ // @ts -ignore ToDo
720
+ let release = relInfoGrp . releases [ 0 ] ; // Pick the first (some) release from the release-group
721
+ release = await mbApi . lookup ( 'release' , release . id , [ 'artists' , 'recordings' ] ) ;
722
+
723
+ const artistRelations = new Map < string , IArtist > ( ) ; // Set to track unique relations
724
+
725
+ for ( const media of release . media ) {
726
+ for ( const track of media . tracks ) {
727
+ const recording = await mbApi . lookup ( 'recording' , track . recording . id , [ 'artists' , 'artist-rels' ] ) ;
728
+ // @ts -ignore ToDo
729
+ for ( const relation of recording . relations ) {
730
+ // @ts -ignore ToDo
731
+ const relationKey = `${ relation . type } /${ relation . artist . name } ` ; // Create a unique key
732
+ if ( ! artistRelations . has ( relationKey ) ) {
733
+ // @ts -ignore ToDo
734
+ artistRelations . set ( relationKey , relation . artist ) ; // Add the key to the set
735
+ }
736
+ }
737
+ }
738
+ }
739
+ return artistRelations . values ( ) ;
740
+ }
741
+
742
+ const releaseGroup = await findReleaseGroup ( 'Queen' , 'Made in Heaven' ) ;
743
+ assert . isDefined ( releaseGroup , 'Should be able to find the release-group for: Queen - Made in Heaven' ) ;
744
+ if ( releaseGroup ) {
745
+ const artists = Array . from ( await findAllRelatedArtist ( releaseGroup . id ) ) ;
746
+ const artistNames = artists . map ( artist => artist . name ) ;
747
+ assert . include ( artistNames , 'Queen' ) ;
748
+ assert . include ( artistNames , 'Josh Macrae' ) ;
749
+ assert . include ( artistNames , 'David Richards' ) ;
750
+ assert . include ( artistNames , 'Justin Shirley‐Smith' ) ;
751
+ assert . include ( artistNames , 'John Deacon' ) ;
752
+ assert . include ( artistNames , 'Brian May' ) ;
753
+ assert . include ( artistNames , 'Freddie Mercury' ) ;
754
+ assert . include ( artistNames , 'Roger Taylor' ) ;
755
+ }
756
+ } ) ;
757
+
703
758
} ) ;
704
759
705
760
describe ( 'Search' , ( ) => {
@@ -1079,4 +1134,5 @@ describe.skip('Node specific API', function (){
1079
1134
1080
1135
} ) ;
1081
1136
1137
+
1082
1138
} ) ;
0 commit comments