Skip to content

Open-source gym & body-weight tracker, now in the App Store (iPhone + Apple Watch) and as an Android APK. Plan routines, log workouts, see which muscles are trained, import from Strong/Hevy/FitNotes. Self-host it or use it on the phone alone.

added test for reordering qr.code-cards, last selected qr-code-card, edit card

Sebastian Gb7a42778700e
diff --git a/frontend/src/store/useStore.gymcards.test.jsx b/frontend/src/store/useStore.gymcards.test.jsx
index d3fcab994..ab301c7e8 100644
--- a/frontend/src/store/useStore.gymcards.test.jsx
+++ b/frontend/src/store/useStore.gymcards.test.jsx
@@ -68,6 +68,82 @@ describe('gymCards state', () => {
})
})
+// lastGymCardId remembers which card the check-in screen last settled on, so it reopens there.
+// It is plain profile data on the same update()/persist() path; these pin the contracts the view
+// relies on — a fresh default, add points it at the new card, edit-in-place touches only that
+// card, and removing the remembered card falls back to the first survivor (or null when none).
+describe('lastGymCardId', () => {
+ it('defaults to null on a fresh profile', () => {
+ expect(useStore.getState().S.lastGymCardId).toBeNull()
+ })
+
+ it('persists to localStorage like any other field', () => {
+ useStore.getState().update(s => { s.lastGymCardId = 'c9' }, false)
+ const saved = JSON.parse(localStorage.getItem('gym_state_v1'))
+ expect(saved.lastGymCardId).toBe('c9')
+ })
+
+ it('adding a card points lastGymCardId at the new card (view contract)', () => {
+ // Mirrors CardSheet's add branch: push a card and remember its id in one update.
+ useStore.getState().update(s => {
+ const id = 'new1'
+ s.gymCards.push({ id, label: 'FitZone', value: 'ABC', fmt: 'qrcode' })
+ s.lastGymCardId = id
+ }, false)
+ expect(useStore.getState().S.lastGymCardId).toBe('new1')
+ })
+
+ it('removing the remembered card falls back to the first survivor', () => {
+ useStore.getState().update(s => {
+ s.gymCards.push({ id: 'a', label: 'A', value: '1', fmt: 'qrcode' })
+ s.gymCards.push({ id: 'b', label: 'B', value: '2', fmt: 'qrcode' })
+ s.lastGymCardId = 'a'
+ }, false)
+ // Mirrors CardFace's remove: drop the card, then repoint a now-stale lastGymCardId.
+ useStore.getState().update(s => {
+ s.gymCards = s.gymCards.filter(c => c.id !== 'a')
+ if (s.lastGymCardId === 'a') s.lastGymCardId = s.gymCards[0]?.id || null
+ }, false)
+ expect(useStore.getState().S.lastGymCardId).toBe('b')
+ })
+
+ it('removing the last remaining remembered card resets to null', () => {
+ useStore.getState().update(s => {
+ s.gymCards.push({ id: 'only', label: 'Only', value: 'x', fmt: 'qrcode' })
+ s.lastGymCardId = 'only'
+ }, false)
+ useStore.getState().update(s => {
+ s.gymCards = s.gymCards.filter(c => c.id !== 'only')
+ if (s.lastGymCardId === 'only') s.lastGymCardId = s.gymCards[0]?.id || null
+ }, false)
+ expect(useStore.getState().S.gymCards).toEqual([])
+ expect(useStore.getState().S.lastGymCardId).toBeNull()
+ })
+})
+
+// Editing a card (rename / re-scan) updates that entry in place — same id, same slot, other cards
+// untouched. Mirrors CardSheet's edit branch, which finds the card by id and overwrites its label
+// and value only.
+describe('editing a card in place', () => {
+ it('updates label and value of the matching card, leaving order and siblings intact', () => {
+ useStore.getState().update(s => {
+ s.gymCards.push({ id: 'a', label: 'A', value: '1', fmt: 'qrcode' })
+ s.gymCards.push({ id: 'b', label: 'B', value: '2', fmt: 'qrcode' })
+ }, false)
+
+ useStore.getState().update(s => {
+ const c = s.gymCards.find(x => x.id === 'b')
+ c.label = 'B renamed'
+ c.value = 'rescanned'
+ }, false)
+
+ const cards = useStore.getState().S.gymCards
+ expect(cards.map(c => c.id)).toEqual(['a', 'b']) // order + count unchanged
+ expect(cards[0]).toMatchObject({ label: 'A', value: '1' }) // sibling untouched
+ expect(cards[1]).toMatchObject({ id: 'b', label: 'B renamed', value: 'rescanned', fmt: 'qrcode' })
+ })
+})
+
// The Settings switch. On by default; an older profile that predates the key must read as on
// too, and off only hides the feature — the cards themselves survive the round trip.
describe('checkIn switch', () => {
diff --git a/frontend/src/views/CheckIn.jsx b/frontend/src/views/CheckIn.jsx
index 6fb60f32d..b62f253bf 100644
--- a/frontend/src/views/CheckIn.jsx
+++ b/frontend/src/views/CheckIn.jsx
@@ -16,8 +16,22 @@ import { confirmSheet } from '../sheets.jsx'
// Long-press before a card lifts for reordering, and the finger travel that instead counts as a
// horizontal swipe of the rail. Mirrors the routine-editor drag (views/RoutineEdit.jsx) so the
// gesture feels the same across the app.
-const CI_LONG_PRESS_MS = 380
-const CI_DRAG_SLOP = 8
+export const CI_LONG_PRESS_MS = 380
+export const CI_DRAG_SLOP = 8
+
+// Move the card at `from` to sit at index `to`, returning a new array (the input is left alone).
+// This is the whole of the reorder once the drop target is known — kept as a pure function, apart
+// from the pointer maths that computes `to`, so the ordering contract can be tested without a DOM.
+// Out-of-range or no-op moves return an unchanged copy rather than throwing.
+export function moveGymCard(cards, from, to) {
+ const next = [...cards]
+ if (from < 0 || from >= next.length) return next
+ const target = Math.max(0, Math.min(to, next.length - 1))
+ if (target === from) return next
+ const [moved] = next.splice(from, 1)
+ next.splice(target, 0, moved)
+ return next
+}
// Gym check-in (reached from the Home "Check in" card; app and PWA alike). Shows each saved
// membership code as a QR the turnstile can read, swiped through horizontally, with a trailing
@@ -158,15 +172,8 @@ function useRailReorder(railRef, cards) {
if (from >= 0) {
const cardEls = [...rail.querySelectorAll('[data-ci-card]')]
const centres = cardEls.map(el => { const r = el.getBoundingClientRect(); return r.left + r.width / 2 })
- let to = centres.reduce((n, cx, i) => n + (i !== from && g.lastX > cx ? 1 : 0), 0)
- to = Math.min(to, list.length - 1)
- if (to !== from) {
- useStore.getState().update(s => {
- const arr = s.gymCards
- const [moved] = arr.splice(from, 1)
- arr.splice(to, 0, moved)
- })
- }
+ const to = centres.reduce((n, cx, i) => n + (i !== from && g.lastX > cx ? 1 : 0), 0)
+ if (to !== from) useStore.getState().update(s => { s.gymCards = moveGymCard(s.gymCards, from, to) })
}
setState({ dragging: false, dragId: null })
}
diff --git a/frontend/src/views/CheckIn.reorder.test.jsx b/frontend/src/views/CheckIn.reorder.test.jsx
new file mode 100644
index 000000000..6e3d97da8
--- /dev/null
+++ b/frontend/src/views/CheckIn.reorder.test.jsx
@@ -0,0 +1,58 @@
+// @vitest-environment happy-dom
+// @vitest-environment happy-dom
+
+// The rail's reorder maths. The drop handler in CheckIn.jsx turns a pointer position into a target
+// index; moveGymCard is the pure part that then rearranges the array. These tests cover that
+// contract — a move is a splice, the input is never mutated, and degenerate moves (in place, out of
+// range) are safe no-ops — the same shape reorderRoutineUnit is tested to.
+//
+// moveGymCard itself is pure, but importing CheckIn.jsx loads the store (which registers a
+// visibilitychange listener at module load) and the scanner/sheet modules, so the file runs under
+// happy-dom with those heavy imports stubbed. The function under test touches none of them.
+
+import { describe, expect, it, vi } from 'vitest'
+
+vi.mock('../lib/scan.js', () => ({ scanCode: vi.fn(), importCodeFromImage: vi.fn() }))
+vi.mock('../components/CameraScan.jsx', () => ({ default: () => null }))
+vi.mock('../sheets.jsx', () => ({ confirmSheet: vi.fn() }))
+
+import { moveGymCard } from './CheckIn.jsx'
+
+const cards = (...ids) => ids.map(id => ({ id, label: id.toUpperCase(), value: id, fmt: 'qrcode' }))
+const ids = list => list.map(c => c.id)
+
+describe('moveGymCard', () => {
+ it('moves a card forward to a later slot', () => {
+ expect(ids(moveGymCard(cards('a', 'b', 'c', 'd'), 0, 2))).toEqual(['b', 'c', 'a', 'd'])
+ })
+
+ it('moves a card backward to an earlier slot', () => {
+ expect(ids(moveGymCard(cards('a', 'b', 'c', 'd'), 3, 1))).toEqual(['a', 'd', 'b', 'c'])
+ })
+
+ it('is a permutation — no card is lost or duplicated', () => {
+ const before = cards('a', 'b', 'c', 'd', 'e')
+ const after = moveGymCard(before, 4, 0)
+ expect(ids(after).sort()).toEqual(['a', 'b', 'c', 'd', 'e'])
+ expect(after).toHaveLength(before.length)
+ })
+
+ it('leaves the array unchanged when dropped in place', () => {
+ expect(ids(moveGymCard(cards('a', 'b', 'c'), 1, 1))).toEqual(['a', 'b', 'c'])
+ })
+
+ it('clamps a target past the end to the last slot', () => {
+ expect(ids(moveGymCard(cards('a', 'b', 'c'), 0, 99))).toEqual(['b', 'c', 'a'])
+ })
+
+ it('ignores an out-of-range source without throwing', () => {
+ expect(ids(moveGymCard(cards('a', 'b'), 5, 0))).toEqual(['a', 'b'])
+ })
+
+ it('does not mutate the input array', () => {
+ const before = cards('a', 'b', 'c')
+ const snapshot = ids(before)
+ moveGymCard(before, 0, 2)
+ expect(ids(before)).toEqual(snapshot)
+ })
+})