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.

Custom-exercise media beside the sync fixes: a refused phone or browser names its waiting photos and videos next to Pair again or Sign in, and a browser tab still sends and keeps them although the catalogue prefetch runs only in the installed app (tests)

Duarte Santos34435e2a1f21
diff --git a/frontend/src/components/ServerSync.media.test.jsx b/frontend/src/components/ServerSync.media.test.jsx
index 27aa2d2cd..3ac3f337d 100644
--- a/frontend/src/components/ServerSync.media.test.jsx
+++ b/frontend/src/components/ServerSync.media.test.jsx
@@ -10,7 +10,7 @@ globalThis.IS_REACT_ACT_ENVIRONMENT = true
too. */
const mocks = vi.hoisted(() => ({
sync: { status: 'offline', lastError: { status: 0, code: 'network' } },
- syncNow: null, signOut: null, syncMedia: null
+ syncNow: null, signOut: null, syncMedia: null, MOBILE: false
}))
vi.mock('../store/useStore.js', () => {
const snap = () => ({ sync: mocks.sync, syncNow: mocks.syncNow, signOut: mocks.signOut, config: null })
@@ -25,6 +25,7 @@ vi.mock('../store/useUI.js', () => {
return { useUI }
})
vi.mock('../lib/media-sync.js', () => ({ syncMedia: (...a) => mocks.syncMedia(...a) }))
+vi.mock('../lib/mobile.js', () => ({ get MOBILE() { return mocks.MOBILE } }))
vi.mock('../lib/api.js', () => ({ passkeyLogin: vi.fn(), webauthnOK: () => true }))
vi.mock('../sheets.jsx', () => ({ askAddDeviceData: vi.fn() }))
vi.mock('../views/MobileOnboarding.jsx', () => ({ ConnectSheet: () => null }))
@@ -33,6 +34,8 @@ const { OwedSheet } = await import('./ServerSync.jsx')
let host, root
beforeEach(() => {
+ mocks.MOBILE = false
+ mocks.sync = { status: 'offline', lastError: { status: 0, code: 'network' } }
mocks.syncNow = vi.fn(async () => mocks.sync)
mocks.syncMedia = vi.fn(async () => {})
mocks.signOut = vi.fn(async () => ({ owed: true, count: 0, media: 1 }))
@@ -77,4 +80,43 @@ describe('OwedSheet with photos and videos waiting', () => {
act(() => { button('Export backup (JSON)').click() })
expect(json).toHaveBeenCalled()
})
+
+ // Where the phone-sync fixes and the media meet: a server that refuses the device is not
+ // something "Try again" can fix, for photos and videos no more than for changes.
+ it('refused phone with only photos or videos waiting: names them, and offers Pair again, not Try again', () => {
+ mocks.MOBILE = true
+ mocks.sync = { status: 'auth', auth: true, lastError: { status: 401, code: 'auth' } }
+ const zip = vi.fn()
+ act(() => root.render(<OwedSheet kind="disconnect" count={0} media={3} exportBackup={vi.fn()} exportBackupZip={zip} done={vi.fn()} close={vi.fn()} />))
+ expect(host.textContent).toContain('3 photos or videos have not reached your server yet.')
+ expect(host.textContent).not.toContain('Some changes on this device')
+ expect(host.textContent).toContain('The server refuses this phone')
+ expect(host.textContent).toContain('Pair again, or export a backup first.')
+ expect(host.textContent).not.toContain('Try again')
+ expect(button('Pair again')).toBeTruthy()
+ act(() => { button('Export with photos & videos (.zip)').click() })
+ expect(zip).toHaveBeenCalledTimes(1)
+ expect(button('Disconnect anyway')).toBeTruthy()
+ expect(mocks.syncMedia).not.toHaveBeenCalled()
+ })
+
+ it('refused browser with photos or videos waiting: Sign in again, and the backup that carries them', () => {
+ mocks.sync = { status: 'auth', auth: true, lastError: { status: 401, code: 'auth' } }
+ act(() => root.render(<OwedSheet kind="signout" count={null} media={1} exportBackup={vi.fn()} exportBackupZip={vi.fn()} done={vi.fn()} close={vi.fn()} />))
+ expect(host.textContent).toContain('Some changes on this device have not reached your server.')
+ expect(host.textContent).toContain('1 photos or videos have not reached your server yet.')
+ expect(host.textContent).toContain('Sign in again, or export a backup first.')
+ expect(host.textContent).not.toContain('Try again')
+ expect(button('Sign in with passkey')).toBeTruthy()
+ expect(button('Export with photos & videos (.zip)')).toBeTruthy()
+ })
+
+ it('a phone whose server is only out of reach still offers Try again for them', () => {
+ mocks.MOBILE = true
+ act(() => root.render(<OwedSheet kind="disconnect" count={0} media={2} exportBackup={vi.fn()} exportBackupZip={vi.fn()} done={vi.fn()} close={vi.fn()} />))
+ expect(host.textContent).toContain('2 photos or videos have not reached your server yet.')
+ expect(host.textContent).toContain('Try again, or export a backup first.')
+ expect(button('Try again')).toBeTruthy()
+ expect(button('Pair again')).toBeFalsy()
+ })
})
diff --git a/frontend/src/lib/media-sync.test.js b/frontend/src/lib/media-sync.test.js
index ae9d8ea8b..1a13efbae 100644
--- a/frontend/src/lib/media-sync.test.js
+++ b/frontend/src/lib/media-sync.test.js
@@ -1,10 +1,11 @@
// @vitest-environment happy-dom
-import { describe, it, expect, vi, beforeEach } from 'vitest'
+import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
import { createMediaSync } from './media-sync.js'
import { createMediaStore, memoryBackend } from './media-store.js'
import { sha256Hex } from './sha256.js'
import { _resetMediaOwed, getMediaStatus } from './media-owed.js'
import { jpeg, png } from './media-samples.test-util.js'
+import { installedApp, prefetchAllowed } from './media-prefetch.js'
const MB = 1024 * 1024
const hex = c => c.repeat(64)
@@ -363,3 +364,41 @@ describe('localMediaGc', () => {
expect(await media.has(hex('a'))).toBe(true)
})
})
+
+// lib/media-prefetch.js fetches the shipped catalogue's animations ahead only in the app installed
+// on the home screen (#281). That gate is the catalogue's alone: a custom exercise's own file has
+// no copy anywhere but this device and its server, so a browser tab still sends it, and still
+// makes the plan's files local, on any connection that is not metered.
+describe('in a browser tab, not the installed app', () => {
+ const tab = () => vi.stubGlobal('matchMedia', q => ({ media: q, matches: false, addEventListener() {}, removeEventListener() {} }))
+ const plain = store => createMediaSync({ store, media, api, apiUpload, apiBlob, toast, now: () => clock, locks: null, storage })
+ afterEach(() => vi.unstubAllGlobals())
+
+ it('a big file goes up without being forced', async () => {
+ tab()
+ expect(installedApp()).toBe(false)
+ expect(prefetchAllowed()).toBe(true)
+ const S = { customEx: [{ id: 'v', media: refOf(hex('a'), null, { kind: 'video', mime: 'video/mp4', size: 6 * MB }) }] }
+ await put(hex('a'), 6 * MB, 'video/mp4')
+ await plain(appStore({ S })).syncMedia()
+ expect(apiUpload).toHaveBeenCalledTimes(1)
+ expect(getMediaStatus().deferred || 0).toBe(0)
+ })
+
+ it('the plan\'s own photos are made local ahead', async () => {
+ tab()
+ const file = jpeg()
+ const h = await sha256Hex(file)
+ apiBlob.mockResolvedValue(new Blob([file]))
+ const S = {
+ routines: [{ id: 'r', ex: [{ id: 'c1' }] }],
+ customEx: [{ id: 'c1', custom: true, media: { kind: 'image', hash: h, mime: 'image/jpeg', size: file.length, width: 640, height: 480, at: 1 } }]
+ }
+ const stop = plain(appStore({ S })).startCustomMediaPrefetch({ delay: 0 })
+ try {
+ for (let i = 0; i < 50 && !(await media.has(h)); i++) await new Promise(r => setTimeout(r, 10))
+ expect(apiBlob).toHaveBeenCalledWith('/api/media/' + h, { expectSize: file.length })
+ expect(await media.get(h)).toMatchObject({ mime: 'image/jpeg', pending: false })
+ } finally { stop() }
+ })
+})