Skip to content

Commit 089366d

Browse files
committed
Adds missing artist property to relation entity
Convert Stack Overflow example to unit test
1 parent d7ea75e commit 089366d

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

lib/musicbrainz.types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ export interface IUrlList extends ISearchResult {
226226
export type RelationDirection = 'backward' | 'forward';
227227

228228
export interface IRelation {
229+
artist?: IArtist;
229230
'attribute-ids':unknown[];
230231
direction: RelationDirection;
231232
'target-credit': string;

test/test-musicbrainz-api.ts

+55-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import {
2020
type ReleaseIncludes,
2121
LinkType,
2222
MusicBrainzApi, type RecordingIncludes, type IRecording,
23-
type IMusicBrainzConfig
23+
type IMusicBrainzConfig, type IArtist
2424
} from '../lib/entry-default.js';
2525
import { assert, expect } from 'chai';
2626
import type * as mb from '../lib/musicbrainz.types.js';
@@ -700,6 +700,59 @@ describe('MusicBrainz-api', function () {
700700
assert.isAtLeast(result.count, 1);
701701
});
702702

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+
});
703756
});
704757

705758
describe('Search', () => {
@@ -1101,4 +1154,5 @@ describe.skip('Node specific API', function (){
11011154

11021155
});
11031156

1157+
11041158
});

0 commit comments

Comments
 (0)