Files
minstrel/web/src/lib/components/LoudnessSettingsCard.test.ts
T
bvandeusenandClaude Opus 5.5 94a9c8cbe3 chore(deps): SvelteKit 3 with adapter-static 4 and TypeScript 6, full-tree npm audit (#5021)
Merges Renovate's kit 3 (PR #149) and adapter-static 4 (PR #148) bumps,
plus the migration they need. The mechanical part is `sv migrate
sveltekit-3`, run one task at a time and reviewed:

- svelte.config.js is gone. Its options move into sveltekit() in
  vite.config.ts, exported as kitOptions so vitest.config.ts runs the
  same kit setup, including the $test-utils alias the tests import.
- $lib becomes #lib through package.json "imports". There is no
  src/lib/index, so only the "#lib/*" entry is kept.
- tsconfig extends $app/tsconfig.
- Peer floors raised to kit 3's requirements: svelte ^5.57.1, vite
  ^8.0.12, svelte-check ^4.7.5.

By hand, from the codemod's list of non-automated tasks:

- goto's replaceState option is now replace; keepFocus becomes
  reset: false. For the search typeahead, reset: false also stops the
  scroll-to-top, which is wanted while typing.
- The test setup mocks drop pushState/replaceState and $app/paths
  base/assets, which kit 3 removed, and mock refreshAll in place of
  invalidateAll.
- The other flagged files only read page.url or goto internal routes,
  so they needed no change.

TypeScript goes to ^6, not the ^7 Renovate offers: kit 3 declares
typescript ^6 as a peer and svelte-check 4.7 accepts ^5 || ^6. Move to
7 once both accept it.

With Tailwind 4 and kit 3 in, `npm audit` on the whole tree reports 0,
so the web lane now audits every dependency rather than only what
ships to browsers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-08 10:50:39 -04:00

109 lines
4.3 KiB
TypeScript

import { afterEach, describe, expect, test, vi } from 'vitest';
import { render, screen, fireEvent, waitFor } from '@testing-library/svelte';
import type { LoudnessCoverage, LoudnessSettings } from '#lib/api/admin.js';
vi.mock('#lib/api/admin.js', () => ({
getLoudnessSettings: vi.fn(),
updateLoudnessSettings: vi.fn(),
getLoudnessCoverage: vi.fn()
}));
vi.mock('#lib/stores/toast.svelte.js', () => ({ pushToast: vi.fn() }));
import LoudnessSettingsCard from './LoudnessSettingsCard.svelte';
import { getLoudnessCoverage, getLoudnessSettings, updateLoudnessSettings } from '#lib/api/admin.js';
import { pushToast } from '#lib/stores/toast.svelte.js';
const base: LoudnessSettings = { enabled: true, backfill_concurrency: 2, leveled_cache_mb: 2048 };
const coverage: LoudnessCoverage = {
total: 1200,
measured: 900,
silent: 2,
unreadable: 3,
pending: 295,
enabled: true
};
afterEach(() => vi.clearAllMocks());
async function renderCard(
over: Partial<LoudnessSettings> = {},
cov: Partial<LoudnessCoverage> = {}
) {
vi.mocked(getLoudnessSettings).mockResolvedValue({ ...base, ...over });
vi.mocked(getLoudnessCoverage).mockResolvedValue({ ...coverage, ...cov });
const r = render(LoudnessSettingsCard);
await screen.findByRole('spinbutton', { name: /files analyzed at once/i });
return r;
}
const saveButton = () => screen.getByRole('button', { name: /save/i });
const concurrency = () => screen.getByRole('spinbutton', { name: /files analyzed at once/i });
describe('LoudnessSettingsCard', () => {
test('shows how far the analysis has got', async () => {
await renderCard();
const gauge = await screen.findByTestId('loudness-coverage');
expect(gauge.textContent).toMatch(/900 of 1,200 tracks measured/);
expect(gauge.textContent).toMatch(/295 pending/);
expect(gauge.textContent).toMatch(/2 silent/);
expect(gauge.textContent).toMatch(/3 unreadable/);
expect(gauge.textContent).not.toMatch(/paused/);
});
// With analysis off, pending never shrinks; the gauge must not read as progress.
test('says the work is paused when analysis is off', async () => {
await renderCard({ enabled: false }, { enabled: false });
const gauge = await screen.findByTestId('loudness-coverage');
expect(gauge.textContent).toMatch(/paused while analysis is off/);
});
test('save is disabled until something changes, then saves the form', async () => {
vi.mocked(updateLoudnessSettings).mockResolvedValue({ ...base, backfill_concurrency: 4 });
await renderCard();
expect(saveButton()).toHaveProperty('disabled', true);
await fireEvent.input(concurrency(), { target: { value: '4' } });
await waitFor(() => expect(saveButton()).toHaveProperty('disabled', false));
await fireEvent.click(saveButton());
await waitFor(() =>
expect(updateLoudnessSettings).toHaveBeenCalledWith({
enabled: true,
backfill_concurrency: 4,
leveled_cache_mb: 2048
})
);
await waitFor(() => expect(pushToast).toHaveBeenCalledWith('Loudness analysis settings saved.'));
// The gauge is read again after a save: switching analysis on or off changes it.
expect(getLoudnessCoverage).toHaveBeenCalledTimes(2);
});
test('an out-of-range value is named and cannot be saved', async () => {
await renderCard();
await fireEvent.input(concurrency(), { target: { value: '9' } });
await waitFor(() =>
expect(screen.getByTestId('settings-problems').textContent).toMatch(/1 to 8/)
);
expect(saveButton()).toHaveProperty('disabled', true);
});
test('the speaker cache size is bounded too', async () => {
await renderCard();
const cache = screen.getByRole('spinbutton', { name: /speaker cache/i });
await fireEvent.input(cache, { target: { value: '100' } });
await waitFor(() =>
expect(screen.getByTestId('settings-problems').textContent).toMatch(/256 to 65536/)
);
expect(saveButton()).toHaveProperty('disabled', true);
});
test('a failed load offers a retry', async () => {
vi.mocked(getLoudnessSettings).mockRejectedValue(new Error('boom'));
vi.mocked(getLoudnessCoverage).mockResolvedValue(coverage);
render(LoudnessSettingsCard);
await screen.findByText(/couldn't load loudness analysis settings/i);
expect(screen.getByRole('button', { name: /try again/i })).toBeTruthy();
});
});