diff --git a/web/src/lib/components/ConfirmModal.svelte b/web/src/lib/components/ConfirmModal.svelte new file mode 100644 index 00000000..53346aa3 --- /dev/null +++ b/web/src/lib/components/ConfirmModal.svelte @@ -0,0 +1,58 @@ + + + +
{@render children()}
+
+ + +
+
diff --git a/web/src/lib/components/ConfirmModal.test.ts b/web/src/lib/components/ConfirmModal.test.ts new file mode 100644 index 00000000..06508f64 --- /dev/null +++ b/web/src/lib/components/ConfirmModal.test.ts @@ -0,0 +1,64 @@ +import { describe, expect, test, vi } from 'vitest'; +import { render, screen, fireEvent } from '@testing-library/svelte'; +import { createRawSnippet } from 'svelte'; +import ConfirmModal from './ConfirmModal.svelte'; +import { offendingSources } from '#lib/testing/sourceGuard.js'; + +const body = createRawSnippet(() => ({ render: () => `Its songs stay in your library.` })); + +// The children Snippet isn't inferable through testing-library's render. +// eslint-disable-next-line @typescript-eslint/no-explicit-any +type AnyProps = any; + +function show(extra: Record = {}) { + const onCancel = vi.fn(); + const onConfirm = vi.fn(); + render(ConfirmModal, { + props: { + title: 'Delete this playlist?', + open: true, + confirmLabel: 'Delete', + onCancel, + onConfirm, + children: body, + ...extra + } as AnyProps + }); + return { onCancel, onConfirm }; +} + +describe('ConfirmModal', () => { + test('says what goes, and acts only on the confirm button', async () => { + const { onCancel, onConfirm } = show(); + expect(screen.getByRole('dialog', { name: 'Delete this playlist?' })).toBeInTheDocument(); + expect(screen.getByText('Its songs stay in your library.')).toBeInTheDocument(); + await fireEvent.click(screen.getByRole('button', { name: 'Cancel' })); + expect(onCancel).toHaveBeenCalledOnce(); + expect(onConfirm).not.toHaveBeenCalled(); + await fireEvent.click(screen.getByRole('button', { name: 'Delete' })); + expect(onConfirm).toHaveBeenCalledOnce(); + }); + + test('the confirm button wears the destructive action colour, not the error colour', () => { + show(); + const confirm = screen.getByRole('button', { name: 'Delete' }); + expect(confirm.className).toContain('bg-action-destructive'); + }); + + test('while the act runs, both buttons are off and the confirm says so', () => { + show({ busy: true, busyLabel: 'Deleting…' }); + expect(screen.getByRole('button', { name: 'Deleting…' })).toBeDisabled(); + expect(screen.getByRole('button', { name: 'Cancel' })).toBeDisabled(); + }); +}); + +// Asking before an act that can't be undone goes through ConfirmModal, and an +// error goes to a toast. The browser's own confirm() and alert() boxes look +// like neither app and can't be styled or tested, so no page calls them. +const NATIVE = /(? { + test('no page calls the browser confirm() or alert()', () => { + expect(offendingSources([NATIVE])).toEqual([]); + }); +}); diff --git a/web/src/lib/components/RecommendationMetrics.svelte b/web/src/lib/components/RecommendationMetrics.svelte index b2980e1c..ae09f493 100644 --- a/web/src/lib/components/RecommendationMetrics.svelte +++ b/web/src/lib/components/RecommendationMetrics.svelte @@ -83,7 +83,10 @@ {#if $metrics.isPending}

Loading…

{:else if $metrics.isError} -

Couldn't load metrics.

+ {:else if $metrics.data && ($metrics.data.groups.length > 0 || $metrics.data.baseline)} {@const baseline = $metrics.data.baseline} {#if baseline} diff --git a/web/src/lib/testing/sourceGuard.ts b/web/src/lib/testing/sourceGuard.ts new file mode 100644 index 00000000..7550630a --- /dev/null +++ b/web/src/lib/testing/sourceGuard.ts @@ -0,0 +1,41 @@ +// Helpers for source guards: tests that read the web app's own source to +// assert a shape is absent (a hand-written count label, a native confirm()). +// Test-only; nothing in the app imports this. +import { readFileSync, readdirSync, statSync } from 'node:fs'; +import { dirname, join, relative } from 'node:path'; +import { fileURLToPath } from 'node:url'; + +/** web/src. */ +export const SRC = join(dirname(fileURLToPath(import.meta.url)), '../..'); + +/** + * Comments describe the forbidden shape; strip them before asserting its + * absence, or the prose explaining a rule trips the check that enforces it. + */ +export const stripComments = (s: string): string => + s.replace(//g, '').replace(/^\s*\/\/.*$/gm, ''); + +/** Every .svelte and .ts file under web/src, tests left out. */ +export function appSources(dir: string = SRC): string[] { + return readdirSync(dir).flatMap((name) => { + const path = join(dir, name); + if (statSync(path).isDirectory()) return appSources(path); + return /\.(svelte|ts)$/.test(name) && !/\.test\.ts$/.test(name) ? [path] : []; + }); +} + +/** + * The app files, relative to web/src, whose source (comments stripped) + * matches any of patterns. Throws if the walk finds almost nothing, so a + * wrong root can't pass as "no offenders". + */ +export function offendingSources(patterns: RegExp[]): string[] { + const all = appSources(); + if (all.length < 100) throw new Error(`source guard found only ${all.length} files under ${SRC}`); + return all + .filter((path) => { + const text = stripComments(readFileSync(path, 'utf8')); + return patterns.some((re) => re.test(text)); + }) + .map((path) => relative(SRC, path)); +} diff --git a/web/src/lib/utils/plural.test.ts b/web/src/lib/utils/plural.test.ts index b5e66901..cd24de4f 100644 --- a/web/src/lib/utils/plural.test.ts +++ b/web/src/lib/utils/plural.test.ts @@ -1,8 +1,6 @@ -import { readFileSync, readdirSync, statSync } from 'node:fs'; -import { dirname, join, relative } from 'node:path'; -import { fileURLToPath } from 'node:url'; import { describe, expect, test } from 'vitest'; import { plural } from './plural.js'; +import { offendingSources } from '#lib/testing/sourceGuard.js'; describe('plural', () => { test('says a count with the noun that agrees with it', () => { @@ -21,8 +19,6 @@ describe('plural', () => { // - the suffix trick: `minute${n === 1 ? '' : 's'}`. // A choice of words with no count in it ("it" / "they", "yesterday") is // not a count label and stays a ternary. -const SRC = join(dirname(fileURLToPath(import.meta.url)), '../..'); - const HAND_ROLLED = [ /=== 1 \? ['"`]1 /, /\{([\w.]+)\}\s*\{\1 === 1 \?/, @@ -30,23 +26,8 @@ const HAND_ROLLED = [ /=== 1 \? '' : 's'/ ]; -/** Comments describe the forbidden shape; strip them before asserting its absence. */ -const stripComments = (s: string) => s.replace(//g, '').replace(/^\s*\/\/.*$/gm, ''); - -function sources(dir: string): string[] { - return readdirSync(dir).flatMap((name) => { - const path = join(dir, name); - if (statSync(path).isDirectory()) return sources(path); - return /\.(svelte|ts)$/.test(name) && !/\.test\.ts$/.test(name) ? [path] : []; - }); -} - describe('count labels', () => { test('every count in the web app is said by plural()', () => { - const offenders = sources(SRC).flatMap((path) => { - const text = stripComments(readFileSync(path, 'utf8')); - return HAND_ROLLED.some((re) => re.test(text)) ? [relative(SRC, path)] : []; - }); - expect(offenders).toEqual([]); + expect(offendingSources(HAND_ROLLED)).toEqual([]); }); }); diff --git a/web/src/routes/admin/users/+page.svelte b/web/src/routes/admin/users/+page.svelte index 14ba270f..019271e2 100644 --- a/web/src/routes/admin/users/+page.svelte +++ b/web/src/routes/admin/users/+page.svelte @@ -22,6 +22,7 @@ import { errCode, errMessage } from '#lib/api/errors.js'; import { pushToast } from '#lib/stores/toast.svelte.js'; import Modal from '#lib/components/Modal.svelte'; + import ConfirmModal from '#lib/components/ConfirmModal.svelte'; import RowActionsMenu, { type RowAction } from '#lib/components/RowActionsMenu.svelte'; import { Shield, ShieldOff, KeyRound, Trash2, Copy, Bug, BugOff } from 'lucide-svelte'; @@ -483,31 +484,15 @@ - { confirmDeleteTarget = null; }} + confirmLabel="Delete" + busyLabel="Deleting…" + busy={saving} + onCancel={() => { confirmDeleteTarget = null; }} + onConfirm={onConfirmDelete} > -

- This permanently deletes the account and cascades through plays, likes, - sessions, and quarantine. The action cannot be undone. -

-
- - -
-
+ This permanently deletes the account and cascades through plays, likes, + sessions, and quarantine. The action cannot be undone. + diff --git a/web/src/routes/collection/missing/+page.svelte b/web/src/routes/collection/missing/+page.svelte index 42fa5853..89528e2e 100644 --- a/web/src/routes/collection/missing/+page.svelte +++ b/web/src/routes/collection/missing/+page.svelte @@ -3,7 +3,7 @@ import { plural } from '#lib/utils/plural.js'; import { pageTitle } from '#lib/branding.js'; import { Copy, Download, FolderX, Trash2 } from 'lucide-svelte'; - import Modal from '#lib/components/Modal.svelte'; + import ConfirmModal from '#lib/components/ConfirmModal.svelte'; import { createMissingFilesQuery, forgetMissingTracks, MISSING_FILES_CSV_URL } from '#lib/api/admin.js'; import { errMessage } from '#lib/api/errors.js'; import { pushToast } from '#lib/stores/toast.svelte.js'; @@ -243,27 +243,18 @@ {/if} - (forgetting = null)}> + (forgetting = null)} + onConfirm={() => confirmForget(forgetting!)} +> {#if forgetting} -

- Forget {forgetting.label}, with their likes, plays and playlist places. A copy added later - comes in as a new track. -

-
- - -
+ Forget {forgetting.label}, with their likes, plays and playlist places. A copy added later + comes in as a new track. {/if} -
+ diff --git a/web/src/routes/collection/reports/+page.svelte b/web/src/routes/collection/reports/+page.svelte index 3ce6093d..54463890 100644 --- a/web/src/routes/collection/reports/+page.svelte +++ b/web/src/routes/collection/reports/+page.svelte @@ -6,7 +6,7 @@ import { useQueryClient } from '@tanstack/svelte-query'; import RowActionsMenu, { type RowAction } from '#lib/components/RowActionsMenu.svelte'; import TabStrip from '#lib/components/TabStrip.svelte'; - import Modal from '#lib/components/Modal.svelte'; + import ConfirmModal from '#lib/components/ConfirmModal.svelte'; import { createAdminQuarantineQuery, createAdminPlaybackErrorsQuery, @@ -282,55 +282,37 @@ {/if} - (removeFileId = null)}> + (removeFileId = null)} + onConfirm={() => confirmRemoveFile(removeFileRow!)} +> {#if removeFileRow} -

- Delete the file of {removeFileRow.title} and keep the track. - It waits in Needs a file, with its likes and plays, and takes over a good copy when one is in the - library. Its reports are cleared. -

-
- - -
+ Delete the file of {removeFileRow.title} and keep the track. + It waits in Needs a file, with its likes and plays, and takes over a good copy when one is in the + library. Its reports are cleared. {/if} -
+ - (deleteFileId = null)}> + (deleteFileId = null)} + onConfirm={() => confirmDeleteFile(deleteFileRow!)} +> {#if deleteFileRow} -

- Remove {deleteFileRow.title} and its file for good, with its - likes, plays and playlist places. Its reports and playback errors go with it. To keep the track for a - good copy, remove the file instead. -

-
- - -
+ Remove {deleteFileRow.title} and its file for good, with its + likes, plays and playlist places. Its reports and playback errors go with it. To keep the track for a + good copy, remove the file instead. {/if} -
+