Música: the add form now passes the id the list is keyed by
Tests / test (push) Canceled after 0s
Publish Docker images / build (., ./web/Dockerfile, web) (push) Canceled after 0s
Publish Docker images / build (./api, ./api/Dockerfile, api) (push) Canceled after 0s
Tests / test (push) Canceled after 0s
Publish Docker images / build (., ./web/Dockerfile, web) (push) Canceled after 0s
Publish Docker images / build (./api, ./api/Dockerfile, api) (push) Canceled after 0s
"Playlist added" and an empty list, every time. The form built the item without an id and
the store's guard was `if (!item.id) return` — a silent drop. The toast came from the sheet,
which had no idea; the list simply stayed as it was, and nothing in the app said why.
Two fixes, because there were two mistakes. The form passes `parsed.id`, which is the
playlist's own identifier and what the list is keyed by (adding the same link twice is still
a no-op). And the guard stops being silent: without an id the URL becomes the key, because a
missing key is a reason to pick a worse key, never a reason to throw away what somebody just
pasted.
The reason nothing caught it is worth recording: the tests called the store's addPlaylist
directly, with an item that had an id, so the form — the only path a person can take — was
never exercised. There is now a test that goes through the sheet itself: renders the screen
with the real Modals host, opens the form, fills the link, saves, and checks both the store
and the card on screen. Checked against the bug: it fails with the id removed, with exactly
the "expected { …(7) } to match object { id: 'pl.abc123' … }" that the empty list was.
This commit is contained in:
1 parent
caed0cbaa0
commit
4b6813d0bd
3 files changed
+37
-4
No files matched your search
@@ -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) {
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
|
||||
@@ -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(<><Music /><Modals /></>) })
|
||||
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()) })
|
||||
|
||||
Reference in new issue
Block a user