Files
minstrel/web/src/lib/components/NormalizationSetting.test.ts
T
bvandeusenandClaude Opus 5.5 af36b2f24a
release / govulncheck (push) Successful in 22s
release / go (push) Successful in 1m17s
release / web (push) Failing after 1m5s
release / android (push) Failing after 1m51s
release / Attach APK to the Release (tag releases only) (push) Canceled after 0s
release / Build + push container image (push) Canceled after 0s
release / Verify release artifacts (tag releases only) (push) Canceled after 0s
release / integration (push) Canceled after 4m12s
release / Build signed APK (releases and dev) (push) Canceled after 3m21s
feat: per-user volume leveling preference, synced across devices (M464 #4998)
Mode (off, auto, track, album), target (-18, -16, -14 LUFS) and boost
(within headroom, or fully with a limiter), stored per user on the server
so the web player, the Android app and casts apply the same one.

- Server: user_normalization_prefs (migration 0067), GET/PUT
  /api/me/normalization; a whole-body PUT, validated, last write wins.
- Web: Settings > Playback > Volume leveling. Saves at once, restores the
  old choice if the save fails, and caches the value for the player.
- Android: Settings card. The device keeps a copy for offline playback
  (Room v9 with an explicit migration, so the upgrade wipes nothing).
  Writes are offline-first: shown at once, PUT best effort, queued on
  failure (NORMALIZATION_SET, collapsed to the newest). A refresh never
  overwrites a change still queued.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-06 14:29:40 -04:00

66 lines
3.0 KiB
TypeScript

import { afterEach, describe, expect, test, vi } from 'vitest';
import { render, screen, fireEvent, waitFor } from '@testing-library/svelte';
vi.mock('$lib/api/normalization', async (importOriginal) => ({
...(await importOriginal<typeof import('$lib/api/normalization')>()),
getNormalization: vi.fn(),
putNormalization: vi.fn()
}));
vi.mock('$lib/stores/toast.svelte', () => ({ pushToast: vi.fn() }));
import NormalizationSetting from './NormalizationSetting.svelte';
import { getNormalization, putNormalization } from '$lib/api/normalization';
import { normalization } from '$lib/stores/normalization.svelte';
import { pushToast } from '$lib/stores/toast.svelte';
afterEach(() => {
vi.clearAllMocks();
localStorage.clear();
});
describe('NormalizationSetting', () => {
test('shows the server value and saves the whole preference on a change', async () => {
vi.mocked(getNormalization).mockResolvedValue({ mode: 'track', target_lufs: -16, boost: 'headroom' });
vi.mocked(putNormalization).mockImplementation(async (p) => p);
render(NormalizationSetting);
await waitFor(() => expect(screen.getByRole('radio', { name: 'Track' })).toBeChecked());
expect(screen.getByRole('radio', { name: '-16 LUFS' })).toBeChecked();
await fireEvent.click(screen.getByRole('radio', { name: 'Album' }));
await waitFor(() =>
expect(putNormalization).toHaveBeenCalledWith({ mode: 'album', target_lufs: -16, boost: 'headroom' })
);
await fireEvent.click(screen.getByRole('checkbox', { name: /boost quiet tracks/i }));
await waitFor(() =>
expect(putNormalization).toHaveBeenLastCalledWith({ mode: 'album', target_lufs: -16, boost: 'limiter' })
);
// The last value seen is cached for the player's first track of a session.
expect(JSON.parse(localStorage.getItem('minstrel:normalization') ?? 'null')).toEqual({
mode: 'album',
target_lufs: -16,
boost: 'limiter'
});
});
test('a failed save puts the previous choice back and says so', async () => {
vi.mocked(getNormalization).mockResolvedValue({ mode: 'auto', target_lufs: -18, boost: 'headroom' });
vi.mocked(putNormalization).mockRejectedValue(new Error('offline'));
render(NormalizationSetting);
await waitFor(() => expect(screen.getByRole('radio', { name: 'Auto' })).toBeChecked());
await fireEvent.click(screen.getByRole('radio', { name: 'Off' }));
await waitFor(() => expect(pushToast).toHaveBeenCalledWith('Could not save volume leveling.', 'error'));
expect(normalization.value.mode).toBe('auto');
expect(screen.getByRole('radio', { name: 'Auto' })).toBeChecked();
});
test('off hides the target and boost controls', async () => {
vi.mocked(getNormalization).mockResolvedValue({ mode: 'off', target_lufs: -18, boost: 'headroom' });
render(NormalizationSetting);
await waitFor(() => expect(screen.getByRole('radio', { name: 'Off' })).toBeChecked());
expect(screen.queryByRole('radio', { name: '-18 LUFS' })).toBeNull();
expect(screen.queryByRole('checkbox')).toBeNull();
});
});