@@ -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 , type 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,59 @@ 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
+ return result . count > 0 ? result [ "release-groups" ] [ 0 ] : undefined ;
709
+ }
710
+
711
+ /**
712
+ * Find all artists for provided MBID (MusicBrainz ID)
713
+ * @param mbidReleaseGroup MBID (MusicBrainz ID) Release-group
714
+ */
715
+ async function findAllRelatedArtist ( mbidReleaseGroup : string ) : Promise < MapIterator < IArtist > > {
716
+ const relInfoGrp = await mbApi . lookup ( 'release-group' , mbidReleaseGroup , [ 'releases' ] ) ;
717
+ assert . exists ( relInfoGrp . releases , 'relInfoGrp.releases' ) ;
718
+ assert . isAtLeast ( relInfoGrp . releases . length , 1 , 'relInfoGrp.releases.length' ) ;
719
+ let release = relInfoGrp . releases [ 0 ] ; // Pick the first (some) release from the release-group
720
+ release = await mbApi . lookup ( 'release' , release . id , [ 'artists' , 'recordings' ] ) ;
721
+
722
+ const artistRelations = new Map < string , IArtist > ( ) ; // Set to track unique relations
723
+
724
+ for ( const media of release . media ) {
725
+ for ( const track of media . tracks ) {
726
+ const recording = await mbApi . lookup ( 'recording' , track . recording . id , [ 'artists' , 'artist-rels' ] ) ;
727
+ assert . exists ( recording . relations , 'recording.relations' ) ;
728
+ for ( const relation of recording . relations ) {
729
+ if ( relation . artist ) {
730
+ const relationKey = `${ relation . type } /${ relation . artist . name } ` ; // Create a unique key
731
+ if ( ! artistRelations . has ( relationKey ) ) {
732
+ artistRelations . set ( relationKey , relation . artist ) ; // Add the key to the set
733
+ }
734
+ }
735
+ }
736
+ }
737
+ }
738
+ return artistRelations . values ( ) ;
739
+ }
740
+
741
+ const releaseGroup = await findReleaseGroup ( 'Queen' , 'Made in Heaven' ) ;
742
+ assert . isDefined ( releaseGroup , 'Should be able to find the release-group for: Queen - Made in Heaven' ) ;
743
+ if ( releaseGroup ) {
744
+ const artists = Array . from ( await findAllRelatedArtist ( releaseGroup . id ) ) ;
745
+ const artistNames = artists . map ( artist => artist . name ) ;
746
+ assert . include ( artistNames , 'Queen' ) ;
747
+ assert . include ( artistNames , 'Josh Macrae' ) ;
748
+ assert . include ( artistNames , 'David Richards' ) ;
749
+ assert . include ( artistNames , 'Justin Shirley‐Smith' ) ;
750
+ assert . include ( artistNames , 'John Deacon' ) ;
751
+ assert . include ( artistNames , 'Brian May' ) ;
752
+ assert . include ( artistNames , 'Freddie Mercury' ) ;
753
+ assert . include ( artistNames , 'Roger Taylor' ) ;
754
+ }
755
+ } ) ;
703
756
} ) ;
704
757
705
758
describe ( 'Search' , ( ) => {
@@ -1101,4 +1154,5 @@ describe.skip('Node specific API', function (){
1101
1154
1102
1155
} ) ;
1103
1156
1157
+
1104
1158
} ) ;
0 commit comments