Fix ABS metadata matching to respect Audible region

Updated Audiobookshelf metadata matching to use the user's configured Audible region instead of always defaulting to 'audible' (US). Introduced mapRegionToABSProvider to map region codes to the correct provider value, and added tests for all supported regions (US, CA, UK, AU, IN).
This commit is contained in:
kikootwo
2026-01-28 13:06:41 -05:00
parent 7a9f158320
commit 12c0305a4b
3 changed files with 113 additions and 3 deletions
+91 -2
View File
@@ -18,6 +18,7 @@ import {
const configServiceMock = vi.hoisted(() => ({
get: vi.fn(),
getAudibleRegion: vi.fn(),
}));
const fetchMock = vi.hoisted(() => vi.fn());
@@ -139,12 +140,13 @@ describe('Audiobookshelf API client', () => {
}));
});
it('includes ASIN overrides in metadata match requests', async () => {
it('includes ASIN overrides in metadata match requests with US region', async () => {
configServiceMock.get.mockImplementation(async (key: string) => {
if (key === 'audiobookshelf.server_url') return 'http://abs';
if (key === 'audiobookshelf.api_token') return 'token';
return null;
});
configServiceMock.getAudibleRegion.mockResolvedValue('us');
fetchMock.mockResolvedValue({
ok: true,
json: async () => ({}),
@@ -154,18 +156,105 @@ describe('Audiobookshelf API client', () => {
const body = JSON.parse(fetchMock.mock.calls[0][1].body);
expect(body).toEqual({
provider: 'audible',
provider: 'audible', // US uses 'audible'
asin: 'ASIN123',
overrideDefaults: true,
});
});
it('uses region-specific provider for Canada', async () => {
configServiceMock.get.mockImplementation(async (key: string) => {
if (key === 'audiobookshelf.server_url') return 'http://abs';
if (key === 'audiobookshelf.api_token') return 'token';
return null;
});
configServiceMock.getAudibleRegion.mockResolvedValue('ca');
fetchMock.mockResolvedValue({
ok: true,
json: async () => ({}),
});
await triggerABSItemMatch('item-1', 'ASIN123');
const body = JSON.parse(fetchMock.mock.calls[0][1].body);
expect(body).toEqual({
provider: 'audible.ca',
asin: 'ASIN123',
overrideDefaults: true,
});
});
it('uses region-specific provider for UK', async () => {
configServiceMock.get.mockImplementation(async (key: string) => {
if (key === 'audiobookshelf.server_url') return 'http://abs';
if (key === 'audiobookshelf.api_token') return 'token';
return null;
});
configServiceMock.getAudibleRegion.mockResolvedValue('uk');
fetchMock.mockResolvedValue({
ok: true,
json: async () => ({}),
});
await triggerABSItemMatch('item-1');
const body = JSON.parse(fetchMock.mock.calls[0][1].body);
expect(body).toEqual({
provider: 'audible.uk',
});
});
it('uses region-specific provider for Australia', async () => {
configServiceMock.get.mockImplementation(async (key: string) => {
if (key === 'audiobookshelf.server_url') return 'http://abs';
if (key === 'audiobookshelf.api_token') return 'token';
return null;
});
configServiceMock.getAudibleRegion.mockResolvedValue('au');
fetchMock.mockResolvedValue({
ok: true,
json: async () => ({}),
});
await triggerABSItemMatch('item-1', 'ASIN456');
const body = JSON.parse(fetchMock.mock.calls[0][1].body);
expect(body).toEqual({
provider: 'audible.au',
asin: 'ASIN456',
overrideDefaults: true,
});
});
it('uses region-specific provider for India', async () => {
configServiceMock.get.mockImplementation(async (key: string) => {
if (key === 'audiobookshelf.server_url') return 'http://abs';
if (key === 'audiobookshelf.api_token') return 'token';
return null;
});
configServiceMock.getAudibleRegion.mockResolvedValue('in');
fetchMock.mockResolvedValue({
ok: true,
json: async () => ({}),
});
await triggerABSItemMatch('item-1', 'ASIN789');
const body = JSON.parse(fetchMock.mock.calls[0][1].body);
expect(body).toEqual({
provider: 'audible.in',
asin: 'ASIN789',
overrideDefaults: true,
});
});
it('suppresses errors when metadata match fails', async () => {
configServiceMock.get.mockImplementation(async (key: string) => {
if (key === 'audiobookshelf.server_url') return 'http://abs';
if (key === 'audiobookshelf.api_token') return 'token';
return null;
});
configServiceMock.getAudibleRegion.mockResolvedValue('us');
fetchMock.mockResolvedValue({
ok: false,
status: 500,