The recommendation metrics judge each surface against a baseline of manual plays. The web tagged only system mixes, so its radio, album, artist and history plays all sat in the baseline; Android tagged nearly everything, and its search, Liked-list, library-shuffle and artist-top plays fell to Other. The surface deltas depended on which client someone listened on. - The web sends the vocabulary Android uses: radio:<seed>, album:<id>, artist:<id>, artist-top:<id>, home:most_played and history, kept in web/src/lib/player/playSource.ts. - The baseline is now named in one place (isManualSource): no source, a search pick, or a Liked-list pick. - library_shuffle gets its own row, the random baseline the mixes exist to beat; artist-top counts as an artist play. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
131 lines
4.9 KiB
TypeScript
131 lines
4.9 KiB
TypeScript
import { afterEach, describe, expect, test, vi } from 'vitest';
|
|
import { render, screen, fireEvent } from '@testing-library/svelte';
|
|
import type { AlbumRef, AlbumDetail, TrackRef } from '#lib/api/types.js';
|
|
import { FALLBACK_COVER } from '#lib/media/covers.js';
|
|
import { emptyLikesMock } from '../../test-utils/mocks/likes';
|
|
import { apiClientMock } from '../../test-utils/mocks/client';
|
|
import { makeTrack } from '#test-utils/fixtures/track.js';
|
|
|
|
vi.mock('#lib/api/client.js', () => apiClientMock());
|
|
vi.mock('#lib/player/store.svelte.js', () => ({
|
|
enqueueTracks: vi.fn(),
|
|
playQueue: vi.fn()
|
|
}));
|
|
|
|
vi.mock('#lib/api/likes.js', () => emptyLikesMock());
|
|
|
|
vi.mock('$app/navigation', () => ({ goto: vi.fn() }));
|
|
|
|
vi.mock('#lib/api/albums.js', async (orig) => {
|
|
const actual = (await orig()) as Record<string, unknown>;
|
|
return { ...actual, listAlbumTracks: vi.fn().mockResolvedValue([]) };
|
|
});
|
|
|
|
vi.mock('#lib/api/playlists.js', async (orig) => {
|
|
const { readable } = await import('svelte/store');
|
|
const actual = (await orig()) as Record<string, unknown>;
|
|
return {
|
|
...actual,
|
|
createPlaylistsQuery: () =>
|
|
readable({ data: { owned: [], public: [] }, isPending: false, isError: false }),
|
|
appendTracks: vi.fn().mockResolvedValue({ id: 'p1', tracks: [] }),
|
|
createPlaylist: vi.fn().mockResolvedValue({ id: 'p1', name: 'New' })
|
|
};
|
|
});
|
|
|
|
import AlbumCard from './AlbumCard.svelte';
|
|
import { api } from '#lib/api/client.js';
|
|
import { enqueueTracks, playQueue } from '#lib/player/store.svelte.js';
|
|
|
|
const album: AlbumRef = {
|
|
id: 'xyz',
|
|
title: 'Kind of Grey',
|
|
sort_title: 'Kind of Grey',
|
|
artist_id: 'm-davies',
|
|
artist_name: 'Marlo Davies',
|
|
year: 1959,
|
|
track_count: 5,
|
|
duration_sec: 2630,
|
|
cover_url: '/api/albums/xyz/cover',
|
|
cover_art_source: null,
|
|
};
|
|
|
|
afterEach(() => vi.clearAllMocks());
|
|
|
|
describe('AlbumCard', () => {
|
|
test('renders cover, title, artist inside a link to /albums/:id', () => {
|
|
const { container } = render(AlbumCard, { props: { album } });
|
|
const link = screen.getByRole('link');
|
|
expect(link).toHaveAttribute('href', '/albums/xyz');
|
|
expect(link).toHaveTextContent('Kind of Grey');
|
|
expect(link).toHaveTextContent('Marlo Davies');
|
|
// The year is intentionally omitted from the Home tile —
|
|
// dropped in the visual polish pass; the album detail page
|
|
// still shows it.
|
|
expect(link).not.toHaveTextContent('1959');
|
|
|
|
const img = container.querySelector('img') as HTMLImageElement;
|
|
expect(img.src).toContain('/api/albums/xyz/cover');
|
|
});
|
|
|
|
test('year is not rendered on the tile even when present', () => {
|
|
render(AlbumCard, { props: { album: { ...album, year: undefined } } });
|
|
expect(screen.queryByText(/1959/)).not.toBeInTheDocument();
|
|
});
|
|
|
|
test('broken cover falls back to FALLBACK_COVER asset', async () => {
|
|
const { container } = render(AlbumCard, { props: { album } });
|
|
const img = container.querySelector('img') as HTMLImageElement;
|
|
await fireEvent.error(img);
|
|
// jsdom resolves relative URLs against the test origin; assert containment
|
|
// rather than strict equality to keep the test agnostic of the host.
|
|
expect(img.src).toContain(FALLBACK_COVER);
|
|
});
|
|
|
|
test('+ queue button fetches album detail and calls enqueueTracks', async () => {
|
|
const tracks: TrackRef[] = [
|
|
makeTrack({
|
|
title: 'So Then',
|
|
album_id: 'xyz', album_title: 'Kind of Grey',
|
|
artist_id: 'm-davies', artist_name: 'Marlo Davies',
|
|
duration_sec: 545
|
|
})
|
|
];
|
|
const detail: AlbumDetail = { ...album, tracks, genres: [] };
|
|
(api.get as ReturnType<typeof vi.fn>).mockResolvedValueOnce(detail);
|
|
|
|
render(AlbumCard, { props: { album } });
|
|
await fireEvent.click(screen.getByRole('button', { name: /add to queue/i }));
|
|
|
|
expect(api.get).toHaveBeenCalledWith('/api/albums/xyz');
|
|
await Promise.resolve();
|
|
await Promise.resolve();
|
|
expect(enqueueTracks).toHaveBeenCalledWith(tracks);
|
|
});
|
|
|
|
test('play overlay click fetches album detail and calls playQueue at index 0', async () => {
|
|
const tracks: TrackRef[] = [
|
|
makeTrack({
|
|
title: 'So Then',
|
|
album_id: 'xyz', album_title: 'Kind of Grey',
|
|
artist_id: 'm-davies', artist_name: 'Marlo Davies',
|
|
duration_sec: 545
|
|
})
|
|
];
|
|
const detail: AlbumDetail = { ...album, tracks, genres: [] };
|
|
(api.get as ReturnType<typeof vi.fn>).mockResolvedValueOnce(detail);
|
|
|
|
render(AlbumCard, { props: { album } });
|
|
await fireEvent.click(screen.getByRole('button', { name: /play kind of grey/i }));
|
|
expect(api.get).toHaveBeenCalledWith('/api/albums/xyz');
|
|
await Promise.resolve();
|
|
await Promise.resolve();
|
|
expect(playQueue).toHaveBeenCalledWith(tracks, 0, { source: 'album:xyz' });
|
|
});
|
|
|
|
test('renders the bottom-right kebab menu trigger', () => {
|
|
render(AlbumCard, { props: { album } });
|
|
expect(screen.getByRole('button', { name: /Album actions for Kind of Grey/i })).toBeInTheDocument();
|
|
});
|
|
});
|