sonda/web/src/components/Cover.test.tsx
tgiachi 75a7898c68 feat(web): make Sonda feel like an app on a phone
A review of the phone experience (the issue) found a desktop page made
smaller. What changes:

feat:
- The player bar is one row on a phone (cover, title, play and next, a thin
  line of progress) and is not there while nothing plays. A tap or a swipe up
  on it opens the full-screen player.
- The Back gesture closes the full-screen player instead of leaving the page
  under it; a swipe down closes it too. The full queue is listed on a phone.
- The queue is kept in the browser: a reload, or a tab that Android throws
  away, comes back paused on the same song at the same second. It belongs to
  the person signed in and is forgotten on sign-out.
- Tabs have icons; the page starts at its top on a new route and back goes to
  where the list was; a swipe on the page no longer reloads it.

fix:
- The player now knows when the system pauses the audio (another app, a
  headset), so the button and the lock screen no longer say it plays.
- The lock screen has a working seek bar and the right play/pause state.
- The player bar was not hidden by max-sm:hidden: its grid rules were outside
  the Tailwind layers and won. They are in the components layer now.
- Song rows give the title the room on a phone (no number column).
- The album header is centred and the greeting is smaller on a phone; recent
  songs on Home show the artist.
- Thumbnails ask for the small cover (300 px) and not the 1000 px one.
- Pages use dvh, the viewport covers the notch, and the tabs keep clear of
  the gesture bar; the update notice no longer sits on the bar.
- No grey tap flash, no double-tap delay, no image menu on a long press.
2026-11-16 17:38:58 +01:00

125 lines
5.6 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { act, fireEvent, render, screen } from '@testing-library/react'
import { beforeEach, expect, test } from 'vitest'
import { sessionStore } from '@/auth/session'
import { mockFetch } from '@/test/mockFetch'
import ArtistPhoto from './ArtistPhoto'
import Cover from './Cover'
beforeEach(() => {
sessionStore.setState({ status: 'anonymous', user: null, accessToken: null, accessExpiresAt: 0, refreshToken: null })
})
function placeholder(container: HTMLElement) {
return container.querySelector('[data-placeholder]')
}
test('a cover asks for the album cover of the API', () => {
const { container } = render(<Cover albumId={12} size={64} />)
const img = container.querySelector('img')!
expect(img.getAttribute('src')).toBe('/api/v1/albums/12/cover?size=small')
expect(img).toHaveAttribute('loading', 'lazy')
expect(img).toHaveAttribute('alt', '')
expect(placeholder(container)).toBeNull()
})
test('the cover URL carries the access token and follows it when it changes', () => {
sessionStore.setState({ accessToken: 'tok1' })
const { container } = render(<Cover albumId={12} size={64} />)
expect(container.querySelector('img')!.getAttribute('src')).toBe('/api/v1/albums/12/cover?size=small&access_token=tok1')
act(() => sessionStore.setState({ accessToken: 'tok2' }))
expect(container.querySelector('img')!.getAttribute('src')).toBe('/api/v1/albums/12/cover?size=small&access_token=tok2')
})
test('a cover that fails to load is replaced by the placeholder', () => {
const { container } = render(<Cover albumId={12} size={64} />)
fireEvent.error(container.querySelector('img')!)
expect(container.querySelector('img')).toBeNull()
expect(placeholder(container)).not.toBeNull()
})
test('a failed cover is tried again once the token has changed', () => {
sessionStore.setState({ accessToken: 'old' })
const { container } = render(<Cover albumId={12} size={64} />)
fireEvent.error(container.querySelector('img')!)
expect(placeholder(container)).not.toBeNull()
act(() => sessionStore.setState({ accessToken: 'new' }))
expect(container.querySelector('img')).not.toBeNull()
})
test('a track with no album shows the placeholder and asks for nothing', () => {
const { calls } = mockFetch({})
const { container } = render(<Cover albumId={null} size={64} />)
expect(container.querySelector('img')).toBeNull()
expect(placeholder(container)).not.toBeNull()
expect(calls).toHaveLength(0)
})
test('the cover has the size it was given', () => {
const { container } = render(<Cover albumId={null} size={96} />)
const box = container.firstElementChild as HTMLElement
expect(box.style.width).toBe('96px')
expect(box.style.height).toBe('96px')
})
test('an artist photo shows the name as its text alternative', () => {
render(<ArtistPhoto artistId={5} name="Björk" size={64} />)
const img = screen.getByAltText('Björk')
expect(img.getAttribute('src')).toBe('/api/v1/artists/5/image')
})
test('an artist photo that fails to load shows the first letter of the name', () => {
const { container } = render(<ArtistPhoto artistId={5} name="Björk" size={64} />)
fireEvent.error(container.querySelector('img')!)
expect(container.querySelector('img')).toBeNull()
expect(screen.getByText('B')).toBeInTheDocument()
})
test('a very long or empty artist name does not break the photo', () => {
const long = 'x'.repeat(200)
expect(() => render(<ArtistPhoto artistId={1} name={long} size={64} />)).not.toThrow()
const { container } = render(<ArtistPhoto artistId={2} name="" size={64} />)
fireEvent.error(container.querySelector('img')!)
expect(screen.getByText('?')).toBeInTheDocument()
})
test('the cover of a friend’s album comes through this server, from the friend’s route', () => {
const { container } = render(<Cover albumId={12} friend={3} size={64} />)
expect(container.querySelector('img')!.getAttribute('src')).toBe('/api/v1/friends/3/albums/12/cover')
})
test('with no cover the rings of Sonda stand in, on a colour taken from the album', () => {
const { container } = render(<Cover albumId={null} size={64} />)
const ph = placeholder(container)!
expect(ph).not.toBeNull()
expect(ph.querySelectorAll('[data-ring]')).toHaveLength(3)
expect(container.firstElementChild).toHaveAttribute('data-tone')
})
test('an album keeps its colour, and albums do not all have the same one', () => {
const tone = (id: number, friend?: number) => {
const { container, unmount } = render(<Cover albumId={id} friend={friend} size={64} />)
fireEvent.error(container.querySelector('img')!)
const t = container.firstElementChild!.getAttribute('data-tone')
unmount()
return t
}
expect(tone(12)).toBe(tone(12))
expect(new Set([1, 2, 3, 4, 5, 6, 7, 8].map((id) => tone(id))).size).toBe(8)
// A friend's album 12 is not this server's album 12.
expect(tone(12, 3)).not.toBe(tone(12))
})
test('a cover that cannot be loaded falls back to the rings, and the picture hides them when it loads', () => {
const { container } = render(<Cover albumId={12} size={64} />)
expect(placeholder(container)).toBeNull()
fireEvent.error(container.querySelector('img')!)
expect(placeholder(container)).not.toBeNull()
expect(container.querySelector('img')).toBeNull()
})
test('a big cover asks for the full picture, a friend’s cover for the only one the friend has', () => {
const big = render(<Cover albumId={12} size={520} />)
expect(big.container.querySelector('img')!.getAttribute('src')).toBe('/api/v1/albums/12/cover')
const friend = render(<Cover albumId={12} friend={3} size={56} />)
expect(friend.container.querySelector('img')!.getAttribute('src')).toBe('/api/v1/friends/3/albums/12/cover')
})