diff --git a/frontend/src/store/useStore.js b/frontend/src/store/useStore.js index 09fbba1..2d4f07c 100644 --- a/frontend/src/store/useStore.js +++ b/frontend/src/store/useStore.js @@ -354,11 +354,16 @@ export const useStore = create((set, get) => { because the list is keyed by the playlist id: adding the same link twice is a no-op rather than two identical cards, and removing one never depends on its position. */ addPlaylist(item) { - if (!item || !item.id) return + if (!item || !item.url) return + // Falls back to the URL for the key rather than refusing the item: this guard used to be + // `if (!item.id) return`, and when the add form forgot to pass one the sheet said "playlist + // added", the list stayed empty, and nothing anywhere said why. A missing key is a reason to + // pick a worse key, never a reason to drop what somebody just saved. + const id = item.id || item.url get().update(s => { const list = (s.music && s.music.playlists) || [] - if (list.some(p => p.id === item.id)) return - s.music = { user: (s.music && s.music.user) || '', playlists: [...list, item] } + if (list.some(p => (p.id || p.url) === id)) return + s.music = { user: (s.music && s.music.user) || '', playlists: [...list, { ...item, id }] } }) }, removePlaylist(id) { diff --git a/frontend/src/views/Music.jsx b/frontend/src/views/Music.jsx index 8b085d5..1170fa1 100644 --- a/frontend/src/views/Music.jsx +++ b/frontend/src/views/Music.jsx @@ -64,7 +64,7 @@ function AddPlaylistSheet({ close }) { // playlist is saved either way under whatever name is in the field. let meta = null try { meta = (await api('/api/music/lookup', { method: 'POST', body: JSON.stringify({ url: parsed.url }) })).meta } catch { /* offline, or the route is older than the app */ } - add({ url: parsed.url, kind: parsed.kind, name: name.trim() || (meta && meta.title) || parsed.id, artwork: (meta && meta.artwork) || null, tracks: (meta && meta.tracks) || null, curator: (meta && meta.curator) || null }) + add({ id: parsed.id, url: parsed.url, kind: parsed.kind, name: name.trim() || (meta && meta.title) || parsed.id, artwork: (meta && meta.artwork) || null, tracks: (meta && meta.tracks) || null, curator: (meta && meta.curator) || null }) toast(t('Playlist added')) close() } diff --git a/frontend/src/views/Music.test.jsx b/frontend/src/views/Music.test.jsx index 2427709..e402785 100644 --- a/frontend/src/views/Music.test.jsx +++ b/frontend/src/views/Music.test.jsx @@ -15,6 +15,7 @@ const api = vi.fn() vi.mock('../lib/api.js', async importOriginal => ({ ...(await importOriginal()), api: (...a) => api(...a) })) const { default: Music } = await import('./Music.jsx') +const { default: Modals } = await import('../components/Modals.jsx') const item = (over = {}) => ({ id: 'pl.aaa', url: 'https://music.apple.com/us/playlist/leg-day/pl.aaa', kind: 'playlist', name: 'Leg Day', artwork: null, tracks: 42, curator: 'Ana', ...over }) @@ -64,6 +65,33 @@ describe('Música', () => { expect(host.textContent).toContain('Push Day') }) + /* The path that was broken, and the reason this file exists: the store was tested directly, so + the form was never exercised — and the form was passing an item without an id, which the store + dropped in silence. The toast fired, the list stayed empty, and nothing said why. */ + it('adds what the form was given, all the way through the sheet', async () => { + api.mockResolvedValue({ ok: true, meta: { title: 'Leg Day', artwork: 'https://is1-ssl.mzstatic.com/x/600x600bb.jpg', tracks: 42, curator: 'Ana' } }) + await act(async () => { root.render(<>) }) + act(() => { [...document.querySelectorAll('button')].find(b => b.getAttribute('aria-label') === 'Add a playlist').dispatchEvent(new MouseEvent('click', { bubbles: true })) }) + const fields = [...document.querySelectorAll('#modal-root input')] + expect(fields.length).toBe(2) // the link, then the optional name + act(() => { + const set = Object.getOwnPropertyDescriptor(window.HTMLInputElement.prototype, 'value').set + set.call(fields[0], 'https://music.apple.com/us/playlist/leg-day/pl.abc123?l=es') + fields[0].dispatchEvent(new Event('input', { bubbles: true })) + }) + await act(async () => {}) + act(() => { [...document.querySelectorAll('#modal-root button')].find(b => b.textContent.includes('Add')).dispatchEvent(new MouseEvent('click', { bubbles: true })) }) + await act(async () => {}) + + const saved = useStore.getState().S.music.playlists + expect(saved.length).toBe(1) + expect(saved[0]).toMatchObject({ id: 'pl.abc123', name: 'Leg Day', tracks: 42, curator: 'Ana' }) + // The tracking parameter is gone, and the card is on the screen behind the sheet. + expect(saved[0].url).toBe('https://music.apple.com/us/playlist/leg-day/pl.abc123') + expect(host.querySelectorAll('.mus-card').length).toBe(1) + expect(host.textContent).toContain('Leg Day') + }) + it('saving the same link twice does not duplicate the card', async () => { useStore.setState(s => ({ S: { ...s.S, music: { user: '', playlists: [] } } })) act(() => { useStore.getState().addPlaylist(item()) })