diff --git a/packages/plex/src/client.sections-rows.test.ts b/packages/plex/src/client.sections-rows.test.ts new file mode 100644 index 0000000..1371f85 --- /dev/null +++ b/packages/plex/src/client.sections-rows.test.ts @@ -0,0 +1,21 @@ +import { afterEach, expect, it, vi } from 'vitest' +import { PlexClient } from './client.js' + +afterEach(() => { + vi.unstubAllGlobals() +}) + +it('skips a null entry in the library sections list instead of throwing a TypeError', async () => { + vi.stubGlobal('fetch', async (url: string) => + new Response( + JSON.stringify( + String(url).includes('/library/sections') + ? { MediaContainer: { Directory: [null, { type: 'artist', key: '1', title: 'Music' }] } } + : { MediaContainer: { machineIdentifier: 'm', version: '1' } }, + ), + ), + ) + const client = new PlexClient('http://plex.test:32400', 'tok') + const result = await client.testConnection() + expect(result.machineIdentifier).toBe('m') +}) diff --git a/packages/plex/src/client.ts b/packages/plex/src/client.ts index 23de31b..dd4b955 100644 --- a/packages/plex/src/client.ts +++ b/packages/plex/src/client.ts @@ -330,7 +330,7 @@ export class PlexClient { private async librarySections(): Promise { const payload = await this.request('/library/sections') - return (payload.MediaContainer?.Directory ?? []) + return plexRows(payload.MediaContainer?.Directory) .filter((section) => section.type === 'artist' && section.key !== undefined && section.title) .map((section) => ({ id: String(section.key),