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.

Follow-up to #296: pausing, resuming or adding time in the app moves the rest notification with it, and the notification's own Pause, ±15 s and Skip keep the bar in the app in step, a rest already Ready included (#193, #204)

Duarte Santos372ec47e0531
diff --git a/frontend/android/app/src/main/java/ch/duartesantos/opengym/RestAlert.java b/frontend/android/app/src/main/java/ch/duartesantos/opengym/RestAlert.java
index 8e55285c7..8a34cc39a 100644
--- a/frontend/android/app/src/main/java/ch/duartesantos/opengym/RestAlert.java
+++ b/frontend/android/app/src/main/java/ch/duartesantos/opengym/RestAlert.java
@@ -31,6 +31,7 @@ public final class RestAlert {
static final String ACTION_PLUS = "ch.duartesantos.opengym.rest.PLUS";
static final String ACTION_SKIP = "ch.duartesantos.opengym.rest.SKIP";
static final String ACTION_ACCENT = "ch.duartesantos.opengym.rest.ACCENT";
+ static final String ACTION_HOLD = "ch.duartesantos.opengym.rest.HOLD";
static final String CHANNEL_ID = "rest-over";
private static volatile boolean toneSuppressed;
private static String lastAlertTitle = "Rest over";
diff --git a/frontend/android/app/src/main/java/ch/duartesantos/opengym/RestAlertPlugin.java b/frontend/android/app/src/main/java/ch/duartesantos/opengym/RestAlertPlugin.java
index be522a877..b1fa76619 100644
--- a/frontend/android/app/src/main/java/ch/duartesantos/opengym/RestAlertPlugin.java
+++ b/frontend/android/app/src/main/java/ch/duartesantos/opengym/RestAlertPlugin.java
@@ -86,6 +86,28 @@ public class RestAlertPlugin extends Plugin {
call.resolve();
}
+ /**
+ * The rest was paused in the app. The alarm for the old end is called off here, whether or
+ * not a countdown is on screen, and the countdown holds at the time the app shows. Resuming
+ * in the app schedules the new end, which restarts the clock.
+ */
+ @PluginMethod
+ public void hold(PluginCall call) {
+ Context ctx = getContext();
+ if (ctx == null) {
+ call.reject("no context");
+ return;
+ }
+ Context app = ctx.getApplicationContext();
+ RestAlert.cancelAlarmOnly(app, (int) number(call, "id", RestAlert.NOTIFICATION_ID));
+ Intent i = new Intent(app, RestTimerService.class);
+ i.setAction(RestAlert.ACTION_HOLD);
+ i.putExtra("leftMs", number(call, "leftMs", 0));
+ i.putExtra("totalMs", number(call, "totalMs", 0));
+ try { app.startService(i); } catch (Exception ignored) { /* no countdown on screen to hold */ }
+ call.resolve();
+ }
+
@PluginMethod
public void setAccent(PluginCall call) {
int color = (int) number(call, "accent", 0xFF30D158L);
diff --git a/frontend/android/app/src/main/java/ch/duartesantos/opengym/RestTimerService.java b/frontend/android/app/src/main/java/ch/duartesantos/opengym/RestTimerService.java
index f1c3ae950..08b583dbe 100644
--- a/frontend/android/app/src/main/java/ch/duartesantos/opengym/RestTimerService.java
+++ b/frontend/android/app/src/main/java/ch/duartesantos/opengym/RestTimerService.java
@@ -46,7 +46,8 @@ public class RestTimerService extends Service {
String action = intent == null ? null : intent.getAction();
if (RestAlert.ACTION_ACCENT.equals(action)) {
if (!running) {
- stopSelf();
+ // Only this start: a countdown asked for right behind it must still come up.
+ stopSelf(startId);
return START_NOT_STICKY;
}
accent = intent.getIntExtra("accent", accent);
@@ -55,6 +56,11 @@ public class RestTimerService extends Service {
show();
return START_NOT_STICKY;
}
+ if (RestAlert.ACTION_HOLD.equals(action)) {
+ if (running) hold(intent.getLongExtra("leftMs", 0), intent.getLongExtra("totalMs", 0));
+ else stopSelf(startId);
+ return START_NOT_STICKY;
+ }
if (RestAlert.ACTION_SKIP.equals(action)) {
finishSkip();
return START_NOT_STICKY;
@@ -120,6 +126,20 @@ public class RestTimerService extends Service {
emit("pause");
}
+ /**
+ * Paused in the app. The clock stops at the time the app holds, so the two read the same,
+ * and the button offers Resume. No event goes back: the app made this change. The plugin
+ * has already called off the alarm.
+ */
+ private void hold(long leftMs, long total) {
+ if (leftMs <= 0) return;
+ paused = true;
+ pausedLeft = leftMs;
+ if (total > 0) totalMs = total;
+ handler.removeCallbacks(tick);
+ show();
+ }
+
private void nudge(long deltaMs) {
long now = System.currentTimeMillis();
// endsAt may already be in the past when +15 lands on the last second.
diff --git a/frontend/src/lib/rest-alert.js b/frontend/src/lib/rest-alert.js
index f468e70e0..5b95f11ec 100644
--- a/frontend/src/lib/rest-alert.js
+++ b/frontend/src/lib/rest-alert.js
@@ -95,6 +95,18 @@ export function disarmRestAlert() {
})
}
+// The rest was paused in the app (#193): the notification stops its clock at the time held and
+// offers Resume, and the alarm for the old end is called off. Resuming arms it again for the new
+// end like any other rest; time added or taken while paused holds it again at the new figure.
+export function holdRestAlert(leftSec, totalSec) {
+ token++
+ if (!MOBILE) return
+ enqueue(async () => {
+ const p = await restPlugin()
+ if (p) await p.RestAlert.hold({ id: REST_ALERT_ID, leftMs: Math.max(1, leftSec) * 1000, totalMs: Math.max(1, totalSec) * 1000 })
+ })
+}
+
// The page is visible and about to play the in-app beep. Skip the native tone so
// the two don't both sound. The notification itself still posts.
let onNativeRest = null
diff --git a/frontend/src/lib/rest-alert.native.test.js b/frontend/src/lib/rest-alert.native.test.js
index 17d5a0050..9657e7078 100644
--- a/frontend/src/lib/rest-alert.native.test.js
+++ b/frontend/src/lib/rest-alert.native.test.js
@@ -57,6 +57,7 @@ describe('the rest alert in the iOS app', () => {
h.platform = 'ios'
const alert = await import('./rest-alert.js')
await expect(alert.armRestAlert(Date.now() + 90_000, { totalSec: 90 })).resolves.toBe(false)
+ alert.holdRestAlert(60, 90)
alert.setRestAccent('red')
alert.disarmRestAlert()
await settle()
@@ -71,12 +72,13 @@ describe('the rest alert in the Android app', () => {
h.platform = 'android'
const alert = await import('./rest-alert.js')
await expect(alert.armRestAlert(Date.now() + 90_000, { totalSec: 90 })).resolves.toBe(true)
+ alert.holdRestAlert(60, 90)
alert.setRestAccent('red')
alert.disarmRestAlert()
await settle()
expect(h.registerPlugin).toHaveBeenCalledTimes(1)
expect(h.calls).toContain('addListener')
- expect(h.calls.filter(c => c !== 'addListener')).toEqual(['schedule', 'setAccent', 'cancel'])
+ expect(h.calls.filter(c => c !== 'addListener')).toEqual(['schedule', 'hold', 'setAccent', 'cancel'])
expect(unhandled).toEqual([])
})
diff --git a/frontend/src/store/useUI.js b/frontend/src/store/useUI.js
index a63747680..48b8cacb7 100644
--- a/frontend/src/store/useUI.js
+++ b/frontend/src/store/useUI.js
@@ -5,7 +5,7 @@ import { api } from '../lib/api.js'
import { t } from '../lib/i18n.js'
import { deviceId } from '../lib/push.js'
import { MOBILE } from '../lib/mobile.js'
-import { armRestAlert, bindNativeRest, disarmRestAlert, hushRestTone } from '../lib/rest-alert.js'
+import { armRestAlert, bindNativeRest, disarmRestAlert, holdRestAlert, hushRestTone } from '../lib/rest-alert.js'
import { useStore } from './useStore.js'
// Fire-and-forget: lets the server push a "rest over" alert if this tab gets suspended
@@ -177,14 +177,15 @@ export const useUI = create((set, get) => ({
// Holding the rest where it is — a longer break than planned, a machine to wait for, a phone
// call (#193). Paused time does not count: the countdown stops, and so does everything that
// would announce its end — the server's push and the alert this tab shows are cancelled, not
- // left to fire at the old time. The timed hold is a different timer and is not touched.
+ // left to fire at the old time, and the Android notification stops its clock at the same time
+ // with its alarm called off. The timed hold is a different timer and is not touched.
pauseRest() {
const tm = get().timer
if (!tm || tm.ready || tm.paused) return
stopRestTicking()
cancelPushRestTimer()
- disarmRestAlert()
const left = Math.max(1, Math.round((tm.endsAt - Date.now()) / 1000))
+ holdRestAlert(left, tm.total)
set({ timer: { ...tm, left, paused: true } })
},
// Carrying on from where the pause held it: the end moves out by however long the pause was,
@@ -209,8 +210,9 @@ export const useUI = create((set, get) => ({
// taking off more than is left means "I'm ready now" — same as skipping, and it keeps a
// negative duration out of both the progress bar and the server-side push schedule
if (left <= 0) { get().stopRest(); return }
- // Paused, there is no end to move and nothing booked on the server: the time is simply held.
- if (tm.paused) { set({ timer: { ...tm, left, total: tm.total + sec } }); return }
+ // Paused, there is no end to move and nothing booked on the server: the time is simply held,
+ // and the notification holds the new figure.
+ if (tm.paused) { set({ timer: { ...tm, left, total: tm.total + sec } }); holdRestAlert(left, tm.total + sec); return }
const endsAt = tm.endsAt + sec * 1000
set({ timer: { ...tm, left, total: tm.total + sec, endsAt } })
bookRestEnd(endsAt, tm.total + sec)
@@ -222,13 +224,24 @@ export const useUI = create((set, get) => ({
if (!tm || !(tm.forIdx >= at)) return
set({ timer: { ...tm, forIdx: tm.forIdx + delta } })
},
- // The notification added time after this screen had already closed the rest.
- // Put the countdown back without tearing down the native timer that is still running.
- reviveRest(endsAt, total, left, paused) {
+ // Android: the rest notification's own Pause, −15 s and +15 s (#296) change the countdown there
+ // first, and this brings the bar in the app to the same place — a pause stops the ticking here
+ // too, a resume starts it again from the notification's end, and +15 s on a rest the app had
+ // already called Ready opens it again. Nothing is sent back: the notification already shows it.
+ followNativeRest({ endsAt, left, total, paused }) {
+ const tm = get().timer
+ const forIdx = tm?.forIdx
+ if (paused) {
+ stopRestTicking()
+ set({ timer: { left, total, endsAt, forIdx, paused: true } })
+ return
+ }
+ const ticking = !!timerInt && !!tm && !tm.paused && !tm.ready
+ set({ timer: { left, total, endsAt, forIdx } })
+ if (ticking) return
+ // As in resumeRest: a hide from while it was held or over is no catch-up of this countdown.
pageHiddenAt = document.hidden ? Date.now() : null
- set({ timer: { left, total, endsAt, forIdx: get().timer?.forIdx, ...(paused ? { paused: true } : {}) } })
- if (paused) stopRestTicking()
- else runRest(set, get)
+ runRest(set, get)
},
stopRest() {
stopRestTicking()
@@ -323,16 +336,13 @@ export const useUI = create((set, get) => ({
}))
// Buttons on the rest notification (pause, ±15s, skip) change the countdown in the
-// service first, then mirror that into the in-app timer. skip ends it.
+// service first, then mirror that into the in-app timer. skip ends it. Seconds round up, as the
+// notification's clock does, so a pause in the last half second still holds a second here.
bindNativeRest(ev => {
if (!ev) return
if (ev.type === 'skip') { useUI.getState().stopRest(); return }
- const tm = useUI.getState().timer
- const left = Math.max(0, Math.round((ev.leftMs || 0) / 1000))
- const total = Math.max(1, Math.round((ev.totalMs || 0) / 1000))
- if (!tm || tm.ready) {
- if (ev.type === 'adjust' && left > 0) useUI.getState().reviveRest(ev.endsAt, total, left, ev.paused)
- return
- }
- useUI.setState({ timer: { ...tm, left, total, endsAt: ev.endsAt || tm.endsAt, paused: !!ev.paused } })
+ const left = Math.ceil((ev.leftMs || 0) / 1000)
+ if (!(left > 0)) return
+ const total = Math.max(left, Math.round((ev.totalMs || 0) / 1000))
+ useUI.getState().followNativeRest({ endsAt: ev.endsAt, left, total, paused: !!ev.paused })
})
diff --git a/frontend/src/store/useUI.native-rest.test.js b/frontend/src/store/useUI.native-rest.test.js
index 8e623c637..e3c1a59bb 100644
--- a/frontend/src/store/useUI.native-rest.test.js
+++ b/frontend/src/store/useUI.native-rest.test.js
@@ -1,26 +1,38 @@
// @vitest-environment happy-dom
-// The locked-phone bug: the WebView countdown never reaches zero, so the only alert that
-// can fire is the one armed when the rest starts. Completing (or catching up) must not
-// cancel it. Skipping the rest must.
+// The Android rest notification (#296) and the bar in the app are one rest. The locked-phone bug
+// it fixes: the WebView countdown never reaches zero, so the only alert that can fire is the one
+// armed when the rest starts — the end must not cancel it, Skip and Dismiss must. And whatever
+// moves the rest on one side (a pause, a resume, ±15 s) must leave the other in the same place.
+// The native side is mocked at lib/rest-alert.js; MOBILE is on, so useUI takes the app's path.
import { describe, expect, it, beforeEach, afterEach, vi } from 'vitest'
-import { useUI } from './useUI.js'
-import { useStore } from './useStore.js'
-import { armRestAlert, disarmRestAlert } from '../lib/rest-alert.js'
-import { beep, vibrate } from '../lib/sound.js'
+const h = vi.hoisted(() => ({ native: null }))
+vi.mock('../lib/mobile.js', async importOriginal => ({ ...(await importOriginal()), MOBILE: true }))
vi.mock('../lib/rest-alert.js', () => ({
armRestAlert: vi.fn(() => Promise.resolve(true)),
+ holdRestAlert: vi.fn(),
disarmRestAlert: vi.fn(),
hushRestTone: vi.fn(),
- bindNativeRest: vi.fn(),
+ bindNativeRest: vi.fn(cb => { h.native = cb }),
}))
-vi.mock('../lib/sound.js', () => ({ beep: vi.fn(), vibrate: vi.fn() }))
+vi.mock('../lib/sound.js', () => ({ beep: vi.fn(), chime: vi.fn(), vibrate: vi.fn() }))
+vi.mock('../lib/api.js', () => ({ api: vi.fn(() => Promise.resolve({ ok: true })) }))
+
+import { useUI } from './useUI.js'
+import { useStore } from './useStore.js'
+import { api } from '../lib/api.js'
+import { armRestAlert, disarmRestAlert, holdRestAlert } from '../lib/rest-alert.js'
+import { beep, chime } from '../lib/sound.js'
const hide = hidden => {
Object.defineProperty(document, 'hidden', { configurable: true, get: () => hidden })
Object.defineProperty(document, 'visibilityState', { configurable: true, get: () => hidden ? 'hidden' : 'visible' })
document.dispatchEvent(new Event('visibilitychange'))
}
+const pushes = () => api.mock.calls.filter(([p]) => p === '/api/push/rest-timer').map(([, o]) => JSON.parse(o.body).seconds)
+// What the notification's buttons send (RestTimerService.emit).
+const fromNotification = (type, leftSec, totalSec, paused = false) =>
+ h.native({ type, leftMs: leftSec * 1000, totalMs: totalSec * 1000, endsAt: paused ? 0 : Date.now() + leftSec * 1000, paused })
let originalSettings
beforeEach(() => {
@@ -28,29 +40,42 @@ beforeEach(() => {
originalSettings = useStore.getState().S
useStore.setState({ S: { ...originalSettings, sound: true, timerFlash: false }, user: null })
useUI.setState({ timer: null, timerFlashId: 0 })
- armRestAlert.mockClear()
- disarmRestAlert.mockClear()
- beep.mockClear()
- vibrate.mockClear()
+ for (const f of [armRestAlert, holdRestAlert, disarmRestAlert, beep, chime, api]) f.mockClear()
hide(false)
})
afterEach(() => {
useUI.getState().stopRest()
- useStore.setState({ S: originalSettings })
+ useStore.setState({ S: originalSettings, user: null })
hide(false)
vi.useRealTimers()
})
-describe('native rest alert', () => {
- it('arms when the rest starts and rearms when time is added', () => {
+describe('the rest the app starts, pauses and ends', () => {
+ it('arms when the rest starts and rearms when time is added, with the whole rest', () => {
useUI.getState().startRest(90)
const first = armRestAlert.mock.calls.at(-1)[0]
useUI.getState().addRest(15)
const second = armRestAlert.mock.calls.at(-1)[0]
expect(second - first).toBe(15_000)
- expect(armRestAlert).toHaveBeenLastCalledWith(second, expect.objectContaining({
- title: 'Rest over', countdownTitle: 'Rest', totalSec: 105, sound: true,
- }))
+ expect(armRestAlert).toHaveBeenLastCalledWith(second, expect.objectContaining({ totalSec: 105, sound: true }))
+ })
+
+ it('holds the notification when paused in the app and books the new end on resume', () => {
+ useUI.getState().startRest(90)
+ vi.advanceTimersByTime(30_000)
+ const disarms = disarmRestAlert.mock.calls.length
+ useUI.getState().pauseRest()
+ expect(holdRestAlert).toHaveBeenLastCalledWith(60, 90)
+ expect(disarmRestAlert.mock.calls.length).toBe(disarms) // the notification stays, paused
+
+ useUI.getState().addRest(15) // time added while paused: held again at the new figure
+ expect(holdRestAlert).toHaveBeenLastCalledWith(75, 105)
+
+ vi.advanceTimersByTime(5 * 60_000)
+ const arms = armRestAlert.mock.calls.length
+ useUI.getState().resumeRest()
+ expect(armRestAlert.mock.calls.length).toBe(arms + 1)
+ expect(armRestAlert).toHaveBeenLastCalledWith(Date.now() + 75_000, expect.objectContaining({ totalSec: 105 }))
})
it('keeps the alarm when the countdown ends, including while the app is hidden', () => {
@@ -58,64 +83,128 @@ describe('native rest alert', () => {
const armed = disarmRestAlert.mock.calls.length
hide(true)
vi.setSystemTime(Date.now() + 91_000)
- hide(false) // reopening catches up; the alarm armed at the start must still be pending
+ hide(false) // reopening catches up; the alarm armed at the start must still be pending
+ expect(disarmRestAlert.mock.calls.length).toBe(armed)
+ expect(useUI.getState().timer).toMatchObject({ left: 0, ready: true })
+ expect(chime).not.toHaveBeenCalled()
+ })
+
+ it('still chimes in the app when the rest finishes on screen, and keeps the alarm', () => {
+ useStore.setState({ S: { ...useStore.getState().S, timerFlash: true } })
+ useUI.getState().startRest(1)
+ const armed = disarmRestAlert.mock.calls.length
+ vi.advanceTimersByTime(1000)
+ expect(chime).toHaveBeenCalledTimes(1)
+ expect(useUI.getState().timerFlashId).toBe(1)
expect(disarmRestAlert.mock.calls.length).toBe(armed)
- expect(useUI.getState().timer).toBe(null)
- expect(beep).not.toHaveBeenCalled()
})
- it('cancels the alarm when the rest is skipped', () => {
+ it('takes the notification down on Skip, and on Dismiss once it is Ready', () => {
useUI.getState().startRest(90)
const armed = disarmRestAlert.mock.calls.length
useUI.getState().stopRest()
expect(disarmRestAlert.mock.calls.length).toBe(armed + 1)
- })
- it('still plays the in-app beep when the rest finishes on screen', () => {
- useStore.setState({ S: { ...useStore.getState().S, timerFlash: true } })
useUI.getState().startRest(1)
vi.advanceTimersByTime(1000)
+ expect(useUI.getState().timer.ready).toBe(true)
+ const ready = disarmRestAlert.mock.calls.length
+ useUI.getState().stopRest()
+ expect(disarmRestAlert.mock.calls.length).toBe(ready + 1)
+ })
+
+ it('plays the last-seconds ticks', () => {
+ useUI.getState().startRest(5)
+ vi.advanceTimersByTime(3000)
expect(beep).toHaveBeenCalled()
- expect(useUI.getState().timerFlashId).toBe(1)
})
- it('stays open when time is added as the countdown hits zero', () => {
- useUI.getState().startRest(1)
- vi.advanceTimersByTime(500)
- const tm = useUI.getState().timer
- useUI.setState({ timer: { ...tm, endsAt: Date.now() + 15_000, left: 15, total: tm.total + 15 } })
+ it('still schedules an alert when sound is off', () => {
+ useStore.setState({ S: { ...useStore.getState().S, sound: false } })
+ useUI.getState().startRest(90)
+ expect(armRestAlert).toHaveBeenCalledWith(expect.any(Number), expect.objectContaining({ sound: false }))
+ })
+})
+
+describe('the server push in the Android app', () => {
+ it('is not booked while the alarm is', async () => {
+ useStore.setState({ user: { id: 'u1' } })
+ useUI.getState().startRest(90)
+ await vi.advanceTimersByTimeAsync(0)
+ expect(pushes()).toEqual([])
+ })
+
+ it('stands in when the alarm could not be scheduled, for that rest only', async () => {
+ useStore.setState({ user: { id: 'u1' } })
+ armRestAlert.mockResolvedValueOnce(false)
+ useUI.getState().startRest(90)
+ await vi.advanceTimersByTimeAsync(0)
+ expect(pushes()).toEqual([90])
+
+ api.mockClear()
+ armRestAlert.mockResolvedValueOnce(false)
+ useUI.getState().startRest(60)
+ useUI.getState().stopRest() // skipped before the schedule came back
+ await vi.advanceTimersByTimeAsync(0)
+ expect(pushes()).toEqual([])
+ })
+})
+
+describe('the notification’s own buttons', () => {
+ it('Pause holds the bar in the app, and Resume runs it out from the notification’s end', () => {
+ useUI.getState().startRest(90, 2)
+ vi.advanceTimersByTime(10_000)
+ fromNotification('pause', 80, 90, true)
+ expect(useUI.getState().timer).toMatchObject({ left: 80, total: 90, paused: true, forIdx: 2 })
+
+ vi.advanceTimersByTime(5 * 60_000) // held: nothing counts, nothing ends
+ expect(useUI.getState().timer).toMatchObject({ left: 80, paused: true })
+ expect(chime).not.toHaveBeenCalled()
+
+ fromNotification('pause', 80, 90)
+ expect(useUI.getState().timer.paused).toBeUndefined()
+ vi.advanceTimersByTime(79_000)
+ expect(useUI.getState().timer).toMatchObject({ left: 1 })
+ vi.advanceTimersByTime(1000)
+ expect(useUI.getState().timer).toMatchObject({ left: 0, ready: true, forIdx: 2 })
+ // The app made none of these changes, so it sends none of them back.
+ expect(holdRestAlert).not.toHaveBeenCalled()
+ expect(armRestAlert).toHaveBeenCalledTimes(1)
+ })
+
+ it('±15 s moves the end the app counts to', () => {
+ useUI.getState().startRest(60)
+ vi.advanceTimersByTime(20_000)
+ fromNotification('adjust', 55, 75)
+ expect(useUI.getState().timer).toMatchObject({ left: 55, total: 75 })
+ vi.advanceTimersByTime(54_000)
+ expect(useUI.getState().timer).toMatchObject({ left: 1 })
vi.advanceTimersByTime(1000)
- expect(useUI.getState().timer).not.toBe(null)
- expect(useUI.getState().timer.left).toBeGreaterThan(10)
+ expect(useUI.getState().timer.ready).toBe(true)
})
- it('reopens the rest after it closed if the notification added time', () => {
+ it('+15 s on a rest the app already called Ready opens it again', () => {
useUI.getState().startRest(1)
vi.advanceTimersByTime(1500)
- expect(useUI.getState().timer).toBe(null)
- useUI.getState().reviveRest(Date.now() + 15_000, 16, 15, false)
- expect(useUI.getState().timer.left).toBe(15)
+ expect(useUI.getState().timer.ready).toBe(true)
+ fromNotification('adjust', 15, 16)
+ expect(useUI.getState().timer).toMatchObject({ left: 15, total: 16 })
+ expect(useUI.getState().timer.ready).toBeUndefined()
vi.advanceTimersByTime(2000)
- expect(useUI.getState().timer.left).toBeLessThan(15)
+ expect(useUI.getState().timer.left).toBe(13)
})
- it('does not finish a rest that was paused', () => {
- useUI.getState().startRest(2)
- useUI.setState({ timer: { ...useUI.getState().timer, paused: true } })
- vi.advanceTimersByTime(5000)
- expect(useUI.getState().timer).not.toBe(null)
- expect(useUI.getState().timer.paused).toBe(true)
+ it('a pause in the last half second still holds a second, so the bar does not end under it', () => {
+ useUI.getState().startRest(10)
+ vi.advanceTimersByTime(9_600)
+ h.native({ type: 'pause', leftMs: 400, totalMs: 10_000, endsAt: 0, paused: true })
+ vi.advanceTimersByTime(5_000)
+ expect(useUI.getState().timer).toMatchObject({ left: 1, paused: true })
})
- it('plays the last-seconds ticks', () => {
- useUI.getState().startRest(5)
- vi.advanceTimersByTime(3000)
- expect(beep).toHaveBeenCalled()
- })
-
- it('still schedules an alert when sound is off', () => {
- useStore.setState({ S: { ...useStore.getState().S, sound: false } })
+ it('Skip ends the rest in the app', () => {
useUI.getState().startRest(90)
- expect(armRestAlert).toHaveBeenCalledWith(expect.any(Number), expect.objectContaining({ sound: false }))
+ h.native({ type: 'skip' })
+ expect(useUI.getState().timer).toBe(null)
})
})