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.

Tests: logWeighIn (watch weigh-in) and effectiveLang with the cached instance default

Duarte Santos1bf37c9bed95
diff --git a/frontend/src/lib/bodyweight.test.js b/frontend/src/lib/bodyweight.test.js
index 37a5526da..778052925 100644
--- a/frontend/src/lib/bodyweight.test.js
+++ b/frontend/src/lib/bodyweight.test.js
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest'
-import { weeklyWeights, weighInAt } from './bodyweight.js'
+import { weeklyWeights, weighInAt, logWeighIn } from './bodyweight.js'
// Discord 'Weight': every weigh-in, week by week, with the week's mean.
describe('weeklyWeights', () => {
@@ -56,3 +56,35 @@ describe('weighInAt', () => {
expect(weighInAt({ d: '2026-03-02', w: 84 })).toBe(at('2026-03-02T12:00:00'))
})
})
+
+// The Apple Watch's weigh-in goes through here: one per day, the later one a correction.
+describe('logWeighIn', () => {
+ const at = iso => new Date(iso).getTime()
+
+ it('adds a weigh-in on the day it was made, rounded to 0.1, and keeps the list in date order', () => {
+ const S = { bodyweight: [{ d: '2026-10-20', w: 80 }, { d: '2026-10-10', w: 82 }] }
+ expect(logWeighIn(S, '81.26', at('2026-10-15T07:00:00'))).toBe(true)
+ expect(S.bodyweight.map(b => [b.d, b.w])).toEqual([['2026-10-10', 82], ['2026-10-15', 81.3], ['2026-10-20', 80]])
+ expect(S.bodyweight[1].t).toBe(at('2026-10-15T07:00:00'))
+ })
+
+ it('replaces that day\'s weigh-in, dropping where the old one came from', () => {
+ const S = { bodyweight: [{ d: '2026-10-15', w: 82, t: 1, src: 'health' }] }
+ logWeighIn(S, 81, at('2026-10-15T21:00:00'))
+ expect(S.bodyweight).toEqual([{ d: '2026-10-15', w: 81, t: at('2026-10-15T21:00:00') }])
+ })
+
+ it('starts the list on a profile that has none', () => {
+ const S = {}
+ expect(logWeighIn(S, 70, at('2026-10-15T07:00:00'))).toBe(true)
+ expect(S.bodyweight).toHaveLength(1)
+ })
+
+ it('refuses a weight that is not a positive number, and changes nothing', () => {
+ for (const w of [0, -5, 'abc', null, undefined, NaN, 0.04]) {
+ const S = { bodyweight: [{ d: '2026-10-15', w: 82 }] }
+ expect(logWeighIn(S, w, at('2026-10-15T07:00:00')), String(w)).toBe(false)
+ expect(S.bodyweight).toEqual([{ d: '2026-10-15', w: 82 }])
+ }
+ })
+})
diff --git a/frontend/src/lib/default-lang.effective.test.js b/frontend/src/lib/default-lang.effective.test.js
new file mode 100644
index 000000000..47531a15c
--- /dev/null
+++ b/frontend/src/lib/default-lang.effective.test.js
@@ -0,0 +1,46 @@
+// @vitest-environment happy-dom
+// effectiveLang: the language a copy is shown in on this device, and the cached instance default
+// (rememberDefaultLang) that stands in for /api/config until it answers, so a boot does not flash
+// the browser's language first.
+import { beforeEach, describe, expect, it } from 'vitest'
+import { effectiveLang, rememberDefaultLang, browserLangs } from './default-lang.js'
+
+const auto = { lang: 'en', langAuto: true }
+
+beforeEach(() => localStorage.clear())
+
+describe('effectiveLang', () => {
+ it('keeps a language somebody picked, whatever the instance or browser say', () => {
+ expect(effectiveLang({ lang: 'de' }, { default_lang: 'fr' }, ['it'])).toBe('de')
+ expect(effectiveLang({ lang: 'en' }, { default_lang: 'fr' }, ['it'])).toBe('en')
+ })
+ it('falls back to English for a copy with no usable lang', () => {
+ expect(effectiveLang(null, null, ['it'])).toBe('en')
+ expect(effectiveLang({ lang: '' }, null, [])).toBe('en')
+ })
+ it('gives an unpicked copy the instance default, then the browser\'s language', () => {
+ expect(effectiveLang(auto, { default_lang: 'fr' }, ['it'])).toBe('fr')
+ expect(effectiveLang(auto, { default_lang: '' }, ['it-IT'])).toBe('it')
+ expect(effectiveLang(auto, { default_lang: '' }, ['nl'])).toBe('en')
+ })
+ it('before the config is in, uses the default this device saw last', () => {
+ rememberDefaultLang({ default_lang: 'fr' })
+ expect(effectiveLang(auto, null, ['it'])).toBe('fr')
+ })
+ it('reads a remembered "no default" as no default, not as unknown', () => {
+ rememberDefaultLang({})
+ expect(localStorage.getItem('gym_default_lang')).toBe('')
+ expect(effectiveLang(auto, null, ['it'])).toBe('it')
+ })
+ it('with nothing seen yet, shows the stored language unchanged until the config answers', () => {
+ expect(effectiveLang({ lang: 'pt', langAuto: true }, null, ['it'])).toBe('pt')
+ })
+})
+
+describe('browserLangs', () => {
+ it('lists navigator.languages without blanks', () => {
+ const langs = browserLangs()
+ expect(Array.isArray(langs)).toBe(true)
+ expect(langs.every(Boolean)).toBe(true)
+ })
+})