Skip to content

Commit 91f27ed

Browse files
committed
Convert Stack Overflow example to unit test
1 parent 3503c29 commit 91f27ed

File tree

1 file changed

+57
-1
lines changed

1 file changed

+57
-1
lines changed

test/test-musicbrainz-api.ts

+57-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, 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,61 @@ 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+
// @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+
703758
});
704759

705760
describe('Search', () => {
@@ -1079,4 +1134,5 @@ describe.skip('Node specific API', function (){
10791134

10801135
});
10811136

1137+
10821138
});

0 commit comments

Comments
 (0)