fix(client): work through the findings from the coverage pass

214 items came out of writing the tests. Real defects are corrected, dead code
is removed rather than covered, and the tests that pinned the old behaviour
move with them.

Also drops two agent coverage scratch files that slipped into the coverage PR
and adds the ignore rule that keeps them out.
This commit is contained in:
Maurice
2026-07-29 11:57:24 +02:00
committed by Maurice
parent a4bf815d24
commit 72a82b3c44
425 changed files with 3155 additions and 1968 deletions
@@ -3,7 +3,7 @@ import { http, HttpResponse } from 'msw';
import React from 'react';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { server } from '../../../tests/helpers/msw/server';
import { fireEvent, render, screen, waitFor } from '../../../tests/helpers/render';
import { act, fireEvent, render, screen, waitFor } from '../../../tests/helpers/render';
import { buildAdminToast } from '../../../tests/helpers/mobileAdmin';
import { resetAllStores } from '../../../tests/helpers/store';
import { useTranslation } from '../../i18n';
@@ -180,4 +180,31 @@ describe('AdminNotificationsPanel', () => {
expect(await screen.findByText('In-App')).toBeInTheDocument();
expect(screen.queryByText('Ntfy')).not.toBeInTheDocument();
});
it('FE-ADMMTX-012: a failing save keeps a toggle that succeeded before it', async () => {
servePreferences(matrix());
const bodies: Record<string, unknown>[] = [];
server.use(
http.put('/api/admin/notification-preferences', async ({ request }) => {
bodies.push((await request.json()) as Record<string, unknown>);
return bodies.length === 1 ? HttpResponse.json({ success: true }) : HttpResponse.json({}, { status: 500 });
})
);
const toast = renderPanel();
await screen.findByText('New version available');
const [inappToggle, emailToggle] = screen.getAllByRole('button');
// Both clicks land before React re-renders, so the second handler still sees the
// preferences of the first render.
await act(async () => {
fireEvent.click(inappToggle); // on -> off, succeeds
fireEvent.click(emailToggle); // off -> on, fails
});
await waitFor(() => expect(toast.error).toHaveBeenCalledWith('Error'));
expect(bodies[1]).toEqual({ version_available: { inapp: false, email: true } });
expect(emailToggle.className).toContain('bg-edge');
expect(inappToggle.className).toContain('bg-edge');
});
});
@@ -1,4 +1,4 @@
import React, { useEffect, useState } from 'react'
import React, { useEffect, useRef, useState } from 'react'
import { adminApi } from '../../api/client'
import { useToast } from '../../components/shared/Toast'
import { ADMIN_EVENT_LABEL_KEYS, ADMIN_CHANNEL_LABEL_KEYS } from './AdminPage.constants'
@@ -8,9 +8,20 @@ import { ADMIN_EVENT_LABEL_KEYS, ADMIN_CHANNEL_LABEL_KEYS } from './AdminPage.co
export default function AdminNotificationsPanel({ t, toast }: { t: (k: string) => string; toast: ReturnType<typeof useToast> }) {
const [matrix, setMatrix] = useState<any>(null)
const [saving, setSaving] = useState(false)
// Toggles fire faster than React re-renders, so the live preferences are mirrored in a
// ref. Reading state out of the render closure would let a second toggle undo the first.
const prefsRef = useRef<any>(null)
const writePrefs = (prefs: any) => {
prefsRef.current = prefs
setMatrix((m: any) => m ? { ...m, preferences: prefs } : m)
}
useEffect(() => {
adminApi.getNotificationPreferences().then((data: any) => setMatrix(data)).catch(() => {})
adminApi.getNotificationPreferences().then((data: any) => {
prefsRef.current = data.preferences
setMatrix(data)
}).catch(() => {})
}, [])
if (!matrix) return <p className="text-content-faint" style={{ fontSize: 'calc(12px * var(--fs-scale-body, 1))', fontStyle: 'italic', padding: 16 }}>Loading…</p>
@@ -24,14 +35,17 @@ export default function AdminNotificationsPanel({ t, toast }: { t: (k: string) =
})
const toggle = async (eventType: string, channel: string) => {
const current = matrix.preferences[eventType]?.[channel] ?? true
const updated = { ...matrix.preferences, [eventType]: { ...matrix.preferences[eventType], [channel]: !current } }
setMatrix((m: any) => m ? { ...m, preferences: updated } : m)
const before = prefsRef.current ?? matrix.preferences
const current = before[eventType]?.[channel] ?? true
const updated = { ...before, [eventType]: { ...before[eventType], [channel]: !current } }
writePrefs(updated)
setSaving(true)
try {
await adminApi.updateNotificationPreferences(updated)
} catch {
setMatrix((m: any) => m ? { ...m, preferences: matrix.preferences } : m)
// Revert this cell only — a toggle that already went through keeps its value.
const latest = prefsRef.current ?? updated
writePrefs({ ...latest, [eventType]: { ...latest[eventType], [channel]: current } })
toast.error(t('common.error'))
} finally {
setSaving(false)
@@ -244,8 +244,9 @@ export default function AdminNotificationsTab({ admin, t }: AdminNotificationsTa
</button>
<button
onClick={async () => {
// A masked value means the URL only lives on the server — send no url and let
// the server test the stored one instead of pre-saving the mask.
const url = smtpValues.admin_webhook_url === '••••••••' ? undefined : smtpValues.admin_webhook_url
if (!url && smtpValues.admin_webhook_url !== '••••••••') return
try {
if (url) await authApi.updateAppSettings({ admin_webhook_url: url }).catch(() => {})
const result = await notificationsApi.testWebhook(url)
+18
View File
@@ -579,6 +579,24 @@ describe('useAdmin', () => {
});
});
it('FE-ADMHOOK-036b: handleSaveUser does nothing without a user in the editor', async () => {
let called = false;
server.use(
http.put('/api/admin/users/:id', () => {
called = true;
return HttpResponse.json({ user: buildUser({}) });
})
);
const { result } = await mountAdmin();
await act(async () => {
await result.current.handleSaveUser();
});
expect(called).toBe(false);
expect(toastCalls).toEqual([]);
});
it('FE-ADMHOOK-036: handleSaveUser sends the trimmed fields and replaces the row', async () => {
let body: Record<string, unknown> | null = null;
server.use(
+1
View File
@@ -319,6 +319,7 @@ export function useAdmin() {
}
const handleSaveUser = async () => {
if (!editingUser) return
try {
const payload: { username?: string; email?: string; role: string; password?: string } = {
username: editForm.username.trim() || undefined,