Files
minstrel/web/src/lib/components/TrackRow.test.ts
T
bvandeusenandClaude Opus 5.5 5939248bef fix(metrics): web and Android tag plays with one source vocabulary, so a surface reads the same on either (#8075)
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>
2026-11-27 00:08:37 -05:00

142 lines
6.3 KiB
TypeScript

import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest';
import { render, screen, fireEvent } from '@testing-library/svelte';
import type { TrackRef } from '#lib/api/types.js';
import { emptyLikesMock } from '../../test-utils/mocks/likes';
import { emptyQuarantineMock } from '../../test-utils/mocks/quarantine';
import { makeTrack } from '#test-utils/fixtures/track.js';
vi.mock('#lib/player/store.svelte.js', () => ({
playQueue: vi.fn(),
enqueueTrack: vi.fn(),
playRadio: vi.fn(),
player: { current: undefined }
}));
vi.mock('#lib/api/likes.js', () => emptyLikesMock());
vi.mock('#lib/api/quarantine.js', () => emptyQuarantineMock());
import TrackRow from './TrackRow.svelte';
import { playQueue, enqueueTrack, playRadio } from '#lib/player/store.svelte.js';
import { selection, clearSelection } from '#lib/selection/store.svelte.js';
const tracks: TrackRef[] = [
makeTrack({ title: 'So Then', duration_sec: 545 }),
makeTrack({ id: 't2', title: 'Freddie Freewheeler', track_number: 2, duration_sec: 565, stream_url: '/api/tracks/t2/stream' })
];
beforeEach(() => clearSelection());
afterEach(() => vi.clearAllMocks());
describe('TrackRow', () => {
test('renders track number, title, formatted duration', () => {
render(TrackRow, { props: { tracks, index: 0 } });
expect(screen.getByText('1')).toBeInTheDocument();
expect(screen.getByText('So Then')).toBeInTheDocument();
expect(screen.getByText('9:05')).toBeInTheDocument();
});
test('track number falls back to dash when missing', () => {
const noNum = [{ ...tracks[0], track_number: undefined }];
render(TrackRow, { props: { tracks: noNum, index: 0 } });
expect(screen.getByText('—')).toBeInTheDocument();
});
test('clicking the row calls playQueue(tracks, index)', async () => {
render(TrackRow, { props: { tracks, index: 1 } });
await fireEvent.click(screen.getByRole('button', { name: 'Freddie Freewheeler' }));
expect(playQueue).toHaveBeenCalledWith(tracks, 1, { source: undefined });
});
test('Enter on the row activates play', async () => {
render(TrackRow, { props: { tracks, index: 0 } });
await fireEvent.keyDown(screen.getByRole('button', { name: 'So Then' }), { key: 'Enter' });
expect(playQueue).toHaveBeenCalledWith(tracks, 0, { source: undefined });
});
test('Space on the row activates play', async () => {
render(TrackRow, { props: { tracks, index: 0 } });
await fireEvent.keyDown(screen.getByRole('button', { name: 'So Then' }), { key: ' ' });
expect(playQueue).toHaveBeenCalledWith(tracks, 0, { source: undefined });
});
test('onPlay prop overrides default playQueue', async () => {
const onPlay = vi.fn();
render(TrackRow, { props: { tracks, index: 1, onPlay } });
await fireEvent.click(screen.getByRole('button', { name: 'Freddie Freewheeler' }));
expect(onPlay).toHaveBeenCalledWith(tracks, 1);
expect(playQueue).not.toHaveBeenCalled();
});
test('+ queue button calls enqueueTrack and does NOT trigger row play', async () => {
render(TrackRow, { props: { tracks, index: 0 } });
await fireEvent.click(screen.getByRole('button', { name: /add .* to queue/i }));
expect(enqueueTrack).toHaveBeenCalledWith(tracks[0]);
expect(playQueue).not.toHaveBeenCalled();
});
test('radio button calls playRadio with track id and does NOT trigger row play', async () => {
render(TrackRow, { props: { tracks, index: 0 } });
await fireEvent.click(screen.getByRole('button', { name: /play radio/i }));
expect(playRadio).toHaveBeenCalledWith('t1');
expect(playQueue).not.toHaveBeenCalled();
});
test('renders the TrackMenu kebab button', () => {
render(TrackRow, { props: { tracks, index: 0 } });
expect(
screen.getByRole('button', { name: /track actions for/i })
).toBeInTheDocument();
});
// A missing file stays in the list, greyed, with nothing to act on.
test('a missing file is marked, does not play, and offers no actions', async () => {
const withMissing = [{ ...tracks[0], unavailable: true }, tracks[1]];
render(TrackRow, { props: { tracks: withMissing, index: 0 } });
const row = screen.getByRole('button', { name: 'So Then' });
expect(row).toHaveAttribute('aria-disabled', 'true');
expect(screen.getByText(/file missing/i)).toBeInTheDocument();
await fireEvent.click(row);
expect(playQueue).not.toHaveBeenCalled();
expect(screen.queryByRole('button', { name: /add .* to queue/i })).not.toBeInTheDocument();
expect(screen.queryByRole('button', { name: /play radio/i })).not.toBeInTheDocument();
expect(screen.queryByRole('button', { name: /track actions for/i })).not.toBeInTheDocument();
});
describe('selection mode', () => {
test('clicking the checkbox toggles selection without playing', async () => {
render(TrackRow, { props: { tracks, index: 0 } });
const box = screen.getByRole('checkbox', { name: /select so then/i });
await fireEvent.click(box);
expect(selection.isSelected('t1')).toBe(true);
expect(playQueue).not.toHaveBeenCalled();
});
test('row click toggles selection (not play) once any row is selected', async () => {
render(TrackRow, { props: { tracks, index: 0 } });
const box = screen.getByRole('checkbox', { name: /select so then/i });
await fireEvent.click(box);
// Re-render at index 1 — selection mode is now active globally.
const { container } = render(TrackRow, { props: { tracks, index: 1 } });
const row = container.querySelector('[role="button"][aria-label="Freddie Freewheeler"]')!;
await fireEvent.click(row);
expect(selection.isSelected('t2')).toBe(true);
expect(playQueue).not.toHaveBeenCalled();
});
test('shift+click on the row extends the range from the anchor', async () => {
render(TrackRow, { props: { tracks, index: 0 } });
const box = screen.getByRole('checkbox', { name: /select so then/i });
await fireEvent.click(box);
const { container } = render(TrackRow, { props: { tracks, index: 1 } });
const row = container.querySelector('[role="button"][aria-label="Freddie Freewheeler"]')!;
await fireEvent.click(row, { shiftKey: true });
expect(selection.isSelected('t1')).toBe(true);
expect(selection.isSelected('t2')).toBe(true);
expect(selection.count).toBe(2);
});
});
});