Signed in, the server's profile is the truth: sign-in and pairing adopt it and ask once about workouts logged while signed out (Add them / Keep profile as is); the app polls the revision every 30 s and on every resume (GET /api/data/rev) and fetches only what moved; offline and unsynced states show a banner, retry on reconnect and confirm with a toast; pagehide flushes a pending change
This commit is contained in:
1 parent
28a068b8f0
commit
826432a3a2
30 files changed
+541
-37
No files matched your search
@@ -27,11 +27,11 @@ describe('Brazilian Portuguese locale', () => {
|
||||
.sort(byCodeUnit)
|
||||
const fingerprint = createHash('sha256').update(JSON.stringify(inherited)).digest('hex')
|
||||
|
||||
expect(Object.keys(PT_BR_OVERRIDES)).toHaveLength(635)
|
||||
expect(inherited).toHaveLength(639)
|
||||
expect(Object.keys(PT_BR_OVERRIDES)).toHaveLength(639)
|
||||
expect(inherited).toHaveLength(643)
|
||||
// If this fails, review the changed keys and wording before accepting a new hash. From
|
||||
// frontend/: node scripts/pt-br-inheritance-fingerprint.mjs --list
|
||||
expect(fingerprint, 'pt-PT inheritance changed; review the inherited pt-BR wording').toBe('39ca0cba5a4413823f571470c39739fd1708ec1e94a9eca4bbb6646a729a9397')
|
||||
expect(fingerprint, 'pt-PT inheritance changed; review the inherited pt-BR wording').toBe('f9f572ecbbdd79fbc62a1a7804224f219d23e07810121f0160e594b4518d09ac')
|
||||
})
|
||||
|
||||
test('does not leak European Portuguese UI terms', () => {
|
||||
|
||||
@@ -66,10 +66,13 @@ function mergeExWeights(n = {}, o = {}) {
|
||||
return out
|
||||
}
|
||||
|
||||
export function mergeStates(a, b) {
|
||||
// `prefer` names the side whose settings, plan and per-exercise config win regardless of `_ts`:
|
||||
// on sign-in the server's profile is the truth and the device only contributes the entries it
|
||||
// logged while signed out. Without it the newer copy decides, as for a conflict between devices.
|
||||
export function mergeStates(a, b, { prefer } = {}) {
|
||||
if (!a) return b ? clone(b) : b
|
||||
if (!b) return clone(a)
|
||||
const n = newerOf(a, b)
|
||||
const n = prefer === 'a' ? a : prefer === 'b' ? b : newerOf(a, b)
|
||||
const o = n === a ? b : a
|
||||
const out = clone(n)
|
||||
out.workouts = unionById(n.workouts, o.workouts, workoutKey).map(clone).sort(byDayStart)
|
||||
@@ -86,3 +89,17 @@ export function mergeStates(a, b) {
|
||||
delete out._rev
|
||||
return out
|
||||
}
|
||||
|
||||
// What `local` holds that `server` does not: the workouts and weigh-ins a device logged while it
|
||||
// was signed out, and the custom exercises they use. Sign-in asks about these before the server's
|
||||
// profile replaces the local copy; zero of each means there is nothing to ask about.
|
||||
export function localExtras(local, server) {
|
||||
const have = new Set(list(server?.workouts).map(workoutKey))
|
||||
const days = new Set(list(server?.bodyweight).map(e => e?.d))
|
||||
const ex = new Set(list(server?.customEx).map(e => e?.id))
|
||||
return {
|
||||
workouts: list(local?.workouts).filter(w => !have.has(workoutKey(w))).length,
|
||||
bodyweight: list(local?.bodyweight).filter(e => e && e.d != null && !days.has(e.d)).length,
|
||||
customEx: list(local?.customEx).filter(e => e && !ex.has(e.id)).length
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { mergeBodyweight, mergeStates, newerOf, unionById } from './sync-merge.js'
|
||||
import { localExtras, mergeBodyweight, mergeStates, newerOf, unionById } from './sync-merge.js'
|
||||
|
||||
const workout = (id, d = '2026-09-01', start = 1) => ({ id, d, start, entries: [] })
|
||||
const routine = (id, name = id) => ({ id, name, ex: [] })
|
||||
@@ -131,3 +131,23 @@ describe('mergeStates', () => {
|
||||
expect(m._ts).toBe(1002)
|
||||
})
|
||||
})
|
||||
|
||||
describe('sign-in adoption helpers', () => {
|
||||
const server = { _ts: 100, unit: 'lb', restSec: 60, workouts: [{ id: 'w1', d: '2026-09-01' }], bodyweight: [{ d: '2026-09-01', w: 80, t: 1 }], routines: [{ id: 'r1', name: 'A' }], week: { 1: ['r1'] } }
|
||||
const local = { _ts: 900, unit: 'kg', restSec: 90, workouts: [{ id: 'w9', d: '2026-09-11' }], bodyweight: [{ d: '2026-09-11', w: 81, t: 2 }, { d: '2026-09-01', w: 79, t: 9 }], routines: [{ id: 'rg', name: 'Guest' }], customEx: [{ id: 'c1', name: 'x' }], week: { 2: ['rg'] } }
|
||||
it('localExtras counts what the device has that the server does not', () => {
|
||||
expect(localExtras(local, server)).toEqual({ workouts: 1, bodyweight: 1, customEx: 1 })
|
||||
expect(localExtras(server, server)).toEqual({ workouts: 0, bodyweight: 0, customEx: 0 })
|
||||
expect(localExtras(null, server)).toEqual({ workouts: 0, bodyweight: 0, customEx: 0 })
|
||||
})
|
||||
it('mergeStates with prefer keeps the preferred side\'s settings and plan although the other is newer', () => {
|
||||
const m = mergeStates(server, local, { prefer: 'a' })
|
||||
expect(m.unit).toBe('lb'); expect(m.restSec).toBe(60); expect(m.week).toEqual({ 1: ['r1'] })
|
||||
expect(m.workouts.map(w => w.id)).toEqual(['w1', 'w9'])
|
||||
expect(m.routines.map(r => r.id).sort()).toEqual(['r1', 'rg'])
|
||||
expect(m.customEx.map(e => e.id)).toEqual(['c1'])
|
||||
// the weigh-in both sides have for the same day: the later `t` wins, as between devices
|
||||
expect(m.bodyweight.find(e => e.d === '2026-09-01').w).toBe(79)
|
||||
expect(mergeStates(server, local).unit).toBe('kg') // without prefer the newer copy decides
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user