From 993bcc6a14bf90e04d212441dd9aa095fee33de9 Mon Sep 17 00:00:00 2001 From: Bryan Van Deusen Date: Sun, 10 May 2026 15:48:04 -0400 Subject: [PATCH] fix(web/test): update playlist API tests for api.* call surface (#375) After e8eff1b migrated playlists.ts from raw apiFetch to the api.* wrapper, three test files mocked the wrong surface: - playlists.test.ts: GET case asserted init.method === 'GET' but api.get omits init.method entirely (fetch defaults to GET). Fall back to 'GET' when init.method is undefined. - playlists.refresh-discover.test.ts: re-mock ./client to expose `api: { post: vi.fn() }` instead of `apiFetch`; assertions check api.post call args. - playlists.refresh-foryou.test.ts: same. Co-Authored-By: Claude Opus 4.7 (1M context) --- web/src/lib/api/playlists.refresh-discover.test.ts | 13 +++++-------- web/src/lib/api/playlists.refresh-foryou.test.ts | 13 +++++-------- web/src/lib/api/playlists.test.ts | 3 ++- 3 files changed, 12 insertions(+), 17 deletions(-) diff --git a/web/src/lib/api/playlists.refresh-discover.test.ts b/web/src/lib/api/playlists.refresh-discover.test.ts index d8ec35a7..98b72e43 100644 --- a/web/src/lib/api/playlists.refresh-discover.test.ts +++ b/web/src/lib/api/playlists.refresh-discover.test.ts @@ -2,28 +2,25 @@ import { describe, it, expect, vi, beforeEach } from 'vitest'; import { refreshDiscover } from './playlists'; vi.mock('./client', () => ({ - apiFetch: vi.fn() + api: { post: vi.fn() } })); -import { apiFetch } from './client'; +import { api } from './client'; describe('refreshDiscover', () => { beforeEach(() => vi.clearAllMocks()); it('POSTs the correct path and returns the response', async () => { const sample = { playlist_id: 'abc-123', track_count: 100 }; - (apiFetch as unknown as ReturnType).mockResolvedValueOnce(sample); + (api.post as unknown as ReturnType).mockResolvedValueOnce(sample); const got = await refreshDiscover(); - expect(apiFetch).toHaveBeenCalledWith( - '/api/playlists/system/discover/refresh', - expect.objectContaining({ method: 'POST' }) - ); + expect(api.post).toHaveBeenCalledWith('/api/playlists/system/discover/refresh', {}); expect(got).toEqual(sample); }); it('handles empty-library response', async () => { const sample = { playlist_id: null, track_count: 0 }; - (apiFetch as unknown as ReturnType).mockResolvedValueOnce(sample); + (api.post as unknown as ReturnType).mockResolvedValueOnce(sample); const got = await refreshDiscover(); expect(got.playlist_id).toBe(null); expect(got.track_count).toBe(0); diff --git a/web/src/lib/api/playlists.refresh-foryou.test.ts b/web/src/lib/api/playlists.refresh-foryou.test.ts index b4602730..de715232 100644 --- a/web/src/lib/api/playlists.refresh-foryou.test.ts +++ b/web/src/lib/api/playlists.refresh-foryou.test.ts @@ -2,28 +2,25 @@ import { describe, it, expect, vi, beforeEach } from 'vitest'; import { refreshForYou } from './playlists'; vi.mock('./client', () => ({ - apiFetch: vi.fn() + api: { post: vi.fn() } })); -import { apiFetch } from './client'; +import { api } from './client'; describe('refreshForYou', () => { beforeEach(() => vi.clearAllMocks()); it('POSTs the correct path and returns the response', async () => { const sample = { playlist_id: 'pl-abc', track_count: 25, track_ids: ['t1', 't2'] }; - (apiFetch as unknown as ReturnType).mockResolvedValueOnce(sample); + (api.post as unknown as ReturnType).mockResolvedValueOnce(sample); const got = await refreshForYou(); - expect(apiFetch).toHaveBeenCalledWith( - '/api/playlists/system/for-you/refresh', - expect.objectContaining({ method: 'POST' }) - ); + expect(api.post).toHaveBeenCalledWith('/api/playlists/system/for-you/refresh', {}); expect(got).toEqual(sample); }); it('handles empty-library response', async () => { const sample = { playlist_id: null, track_count: 0, track_ids: [] }; - (apiFetch as unknown as ReturnType).mockResolvedValueOnce(sample); + (api.post as unknown as ReturnType).mockResolvedValueOnce(sample); const got = await refreshForYou(); expect(got.playlist_id).toBe(null); expect(got.track_ids).toEqual([]); diff --git a/web/src/lib/api/playlists.test.ts b/web/src/lib/api/playlists.test.ts index 4fe7f464..c2c017f6 100644 --- a/web/src/lib/api/playlists.test.ts +++ b/web/src/lib/api/playlists.test.ts @@ -29,7 +29,8 @@ describe('playlists API helper', () => { expect(r.public).toEqual([]); const call = spy.mock.calls[0]; expect(call[0]).toBe('/api/playlists?kind=user'); - expect((call[1] as RequestInit).method).toBe('GET'); + // GET is fetch's default method; api.get omits init.method entirely. + expect((call[1] as RequestInit | undefined)?.method ?? 'GET').toBe('GET'); }); test('listPlaylists passes kind=system through the query string', async () => {