From 8094a5131b35caa45a365173e8bf8b2e4c1dc8d7 Mon Sep 17 00:00:00 2001 From: Duarte Santos Date: Sat, 19 Sep 2026 21:20:00 +0200 Subject: [PATCH] Admin: a profile with a null or shapeless workout entry opens instead of blanking the whole app (QA C16) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit UserDetail rendered w.name and setsDone(w) on every stored entry, and the sheet host sits outside the route's ErrorBoundary, so one null took the React tree down — #root empty until a full reload, and exactly that account could not be disabled from the dashboard. Entries without a usable session are skipped now; the API no longer accepts new ones, but a file written before still answers with them. --- frontend/src/views/Admin.jsx | 12 +++-- frontend/src/views/Admin.test.jsx | 84 +++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+), 3 deletions(-) create mode 100644 frontend/src/views/Admin.test.jsx diff --git a/frontend/src/views/Admin.jsx b/frontend/src/views/Admin.jsx index e52d1e9..8ca0409 100644 --- a/frontend/src/views/Admin.jsx +++ b/frontend/src/views/Admin.jsx @@ -36,6 +36,12 @@ function UserDetail({ id, onChanged, close }) { useEffect(() => { api('/api/admin/user?id=' + encodeURIComponent(id)).then(setD).catch(e => toast(e.message)) }, [id]) if (!d) return
Loading…
const u = d.user + // The document comes straight off the user's state file. PUT /api/data drops null and + // shapeless entries now, but a file written before it did still answers with them, and this + // sheet renders outside the route's ErrorBoundary: one throw here blanked the whole app and + // left exactly this account un-disableable. setsDone/workoutVolume walk entries and sets, so + // an entry that lacks either has nothing to show and is skipped rather than drawn. + const workouts = (d.workouts || []).filter(w => w && Array.isArray(w.entries) && w.entries.every(e => e && Array.isArray(e.sets))) const setDisabled = disabled => { api('/api/admin/user/disable', { method: 'POST', body: JSON.stringify({ id: u.id, disabled }) }) .then(() => { toast(disabled ? 'User disabled' : 'User enabled'); onChanged(); close() }) @@ -50,7 +56,7 @@ function UserDetail({ id, onChanged, close }) { joined {u.created ? fmtDate(u.created.slice(0, 10)) : '—'}
-
Workouts
{d.workouts.length}
+
Workouts
{workouts.length}
Weigh-ins
{d.bodyweight.length}
Routines
{d.routines.length}
Last sync
{rel(d.lastSync)}
@@ -63,8 +69,8 @@ function UserDetail({ id, onChanged, close }) {
{u.disabled ? 'Enabling lets them sign in and sync again.' : 'Disabling signs them out everywhere and blocks sign-in. Nothing is deleted.'}
}

Workout history

- {d.workouts.length ?
- {d.workouts.slice(0, 60).map(w =>
+ {workouts.length ?
+ {workouts.slice(0, 60).map(w =>
{w.name}
{fmtDate(w.d, true)} · {fmtDur((w.end || w.start) - w.start)} · {setsDone(w)} sets{w.prs?.length ? ' · ' + w.prs.length + ' PR' : ''}
{fmtVol(w.vol ?? workoutVolume(w), d.unit)} diff --git a/frontend/src/views/Admin.test.jsx b/frontend/src/views/Admin.test.jsx new file mode 100644 index 0000000..d3cdba6 --- /dev/null +++ b/frontend/src/views/Admin.test.jsx @@ -0,0 +1,84 @@ +// @vitest-environment happy-dom +// The admin drill-down (QA C16): a profile whose stored workouts hold a null or otherwise +// shapeless entry must still open, because the sheet host sits outside the route's +// ErrorBoundary — a throw here blanked the whole app and left the account un-disableable. +import React, { act } from 'react' +import { createRoot } from 'react-dom/client' +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' +import Admin from './Admin.jsx' + +globalThis.IS_REACT_ACT_ENVIRONMENT = true + +const mocks = vi.hoisted(() => ({ answers: {}, sheets: [] })) +vi.mock('../lib/api.js', () => ({ + api: path => { + const key = path.split('?')[0] + return key in mocks.answers ? Promise.resolve(mocks.answers[key]) : Promise.reject(new Error('not found')) + } +})) +vi.mock('../store/useStore.js', () => { + const snap = () => ({ user: { id: 'adm', name: 'Admin', admin: true }, S: {} }) + const useStore = selector => selector ? selector(snap()) : snap() + useStore.getState = snap + return { useStore } +}) +vi.mock('../store/useUI.js', () => { + const snap = () => ({ toast: () => {}, openSheet: render => mocks.sheets.push(render) }) + const useUI = selector => selector ? selector(snap()) : snap() + useUI.getState = snap + return { useUI } +}) +vi.mock('react-router-dom', () => ({ useNavigate: () => () => {} })) +vi.mock('../sheets.jsx', () => ({ confirmSheet: vi.fn() })) +vi.mock('./AdminCoach.jsx', () => ({ default: () => null })) + +const mounted = [] +function render(el) { + const host = document.createElement('div') + document.body.appendChild(host) + const root = createRoot(host) + mounted.push(root) + act(() => root.render(el)) + return host +} +const settle = () => act(() => new Promise(r => setTimeout(r, 0))) + +const good = { id: 'w1', name: 'Push day', d: '2026-09-10', start: 1000, end: 61000, entries: [{ sets: [{ done: true }, { done: false }] }] } + +beforeEach(() => { + document.body.innerHTML = '' + mocks.sheets.length = 0 + mocks.answers = { + '/api/admin/users': { users: [{ id: 'u1', name: 'Mallory', workouts: 2, lastSync: null, disabled: false }], invite_only: false }, + '/api/admin/invites': { invites: [] }, + '/api/admin/audit': { rows: [] }, + '/api/admin/user': { + user: { id: 'u1', name: 'Mallory', created: '2026-09-01T00:00:00Z', disabled: false, admin: false }, + unit: 'kg', lastSync: null, routines: [], bodyweight: [], + // What GET /api/admin/user hands over for a document written before PUT /api/data dropped + // such entries: a null, and a bare object without entries, next to a real session. + workouts: [null, good, {}] + } + } +}) +afterEach(() => { act(() => { mounted.splice(0).forEach(root => root.unmount()) }) }) + +describe('Admin user drill-down', () => { + it('opens a profile with malformed workout entries and still offers "Disable account"', async () => { + const page = render() + await settle() + const row = [...page.querySelectorAll('.item')].find(el => el.textContent.includes('Mallory')) + expect(row).toBeTruthy() + act(() => row.click()) + expect(mocks.sheets.length).toBe(1) + const sheet = render(mocks.sheets[0](() => {})) + await settle() + const buttons = [...sheet.querySelectorAll('button')].map(b => b.textContent) + expect(buttons).toContain('Disable account') + // The one real session is listed; the two shapeless entries are skipped, not drawn as blanks. + const rows = [...sheet.querySelectorAll('.list > div')] + expect(rows.length).toBe(1) + expect(rows[0].textContent).toContain('Push day') + expect(rows[0].textContent).toContain('1 sets') + }) +})