11import type { MetadataProvider , ProviderOptions } from './base.ts' ;
2- import type { EntityId , HarmonyRelease , ReleaseOptions , ReleaseSpecifier } from '@/harmonizer/types.ts' ;
2+ import type {
3+ EntityId ,
4+ HarmonyEntityType ,
5+ HarmonyRelease ,
6+ ReleaseOptions ,
7+ ReleaseSpecifier ,
8+ } from '@/harmonizer/types.ts' ;
39import { downloadMode } from '@/utils/fetch_stub.ts' ;
410import { isDefined } from '@/utils/predicate.ts' ;
511
12+ import { assert } from 'std/assert/assert.ts' ;
613import { assertEquals } from 'std/assert/assert_equals.ts' ;
714import { filterValues } from '@std/collections/filter-values' ;
815import { describe , it } from '@std/testing/bdd' ;
16+ import { preferArray } from 'utils/array/scalar.js' ;
917
1018/** Specification which describes the expected behavior of a {@linkcode MetadataProvider}. */
1119export interface ProviderSpecification {
@@ -51,6 +59,19 @@ export interface EntityUrlTest {
5159
5260function describeEntityUrlExtraction ( provider : MetadataProvider , tests : EntityUrlTest [ ] ) {
5361 describe ( 'extraction of entity type and ID from an URL' , ( ) => {
62+ // Construct the inverse entity type mapping.
63+ const inverseEntityTypeMap : Record < string , HarmonyEntityType > = { } ;
64+ for ( const [ harmonyType , providerTypes ] of Object . entries ( provider . entityTypeMap ) ) {
65+ for ( const providerType of preferArray ( providerTypes ) ) {
66+ const mappedHarmonyType = inverseEntityTypeMap [ providerType ] ;
67+ assert (
68+ ! mappedHarmonyType ,
69+ `Entity type "${ providerType } " has an ambiguous mapping, it can be ${ mappedHarmonyType } or ${ harmonyType } ` ,
70+ ) ;
71+ inverseEntityTypeMap [ providerType ] = harmonyType as HarmonyEntityType ;
72+ }
73+ }
74+
5475 for ( const test of tests ) {
5576 it ( `${ test . id ? 'supports' : 'ignores' } ${ test . description ?? test . url } ` , ( ) => {
5677 let actualId = provider . extractEntityFromUrl ( test . url ) ;
@@ -62,6 +83,19 @@ function describeEntityUrlExtraction(provider: MetadataProvider, tests: EntityUr
6283 const serializedId = test . serializedId ?? test . id ?. id ;
6384 if ( serializedId && actualId ) {
6485 assertEquals ( provider . serializeProviderId ( actualId ) , serializedId , 'Serialized entity ID is wrong' ) ;
86+ const harmonyType = inverseEntityTypeMap [ actualId . type ] ;
87+ assert ( harmonyType , `Type "${ actualId . type } " is missing from the provider's entity type mapping` ) ;
88+ if ( harmonyType === 'release' ) {
89+ // ID deserialization is only needed for releases so far.
90+ // We should not yet force providers to implement it for other ambiguous entity types.
91+ const deserializedId = provider . parseProviderId ( serializedId , harmonyType ) ;
92+ assertEquals ( deserializedId . id , actualId . id , 'Failed to parse serialized provider ID' ) ;
93+ assertEquals (
94+ deserializedId . type ,
95+ actualId . type ,
96+ 'Failed to extract entity type from serialized provider ID' ,
97+ ) ;
98+ }
6599 }
66100 } ) ;
67101 }
0 commit comments