From 49538e8ae3a6f69180e47434ae85f33fe86026da Mon Sep 17 00:00:00 2001 From: Andreas Schneider Date: Tue, 1 Dec 2026 19:09:48 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Stop=20stale=20running=20entries?= =?UTF-8?q?=20while=20offline?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit autoStopStale only ran through beforeSync, and syncNow skipped that while offline: an app left open overnight offline kept yesterday's entry running, and startEntry then threw "an entry is already running" for an entry the Today view does not show. A round now runs its preparation whatever the online state and only leaves out the requests, so the minute tick stops stale entries offline too, and startEntry stops them itself before it checks for a running entry. --- web/src/lib/app.svelte.ts | 1 + web/src/lib/app.test.ts | 10 ++++++++++ web/src/lib/db.svelte.ts | 5 +++-- web/src/lib/db.test.ts | 22 ++++++++++++++++++++++ 4 files changed, 36 insertions(+), 2 deletions(-) diff --git a/web/src/lib/app.svelte.ts b/web/src/lib/app.svelte.ts index 6cefc12..bbb946b 100644 --- a/web/src/lib/app.svelte.ts +++ b/web/src/lib/app.svelte.ts @@ -101,6 +101,7 @@ export function runningEntry(): Doc | undefined { } export async function startEntry(note = ''): Promise { + await autoStopStale(); if (runningEntry()) throw new Error('an entry is already running'); checkNote(note); const now = Date.now(); diff --git a/web/src/lib/app.test.ts b/web/src/lib/app.test.ts index 431cea0..294aa89 100644 --- a/web/src/lib/app.test.ts +++ b/web/src/lib/app.test.ts @@ -131,6 +131,16 @@ describe('entries', () => { expect(await autoStopStale()).toBe(0); }); + it('startEntry stops a stale running entry first, also offline', async () => { + vi.useFakeTimers({ toFake: ['Date'] }); + vi.setSystemTime(new Date('2026-06-16T10:00:00Z')); + await save('entries', null, { start_time: Date.parse('2026-06-15T17:00:00Z'), end_time: null, auto_stopped: false, note: 'yesterday', day_key: '2026-06-15' }); + expect(status.online).toBe(false); + const id = await startEntry('today'); + expect(entriesForDay('2026-06-15')[0]).toMatchObject({ end_time: Date.parse('2026-06-15T23:59:59.999Z'), auto_stopped: true }); + expect(runningEntry()).toMatchObject({ id, note: 'today', day_key: '2026-06-16' }); + }); + it('startEntry twice throws an entry is already running', async () => { await startEntry('first'); await expect(startEntry('second')).rejects.toThrow('an entry is already running'); diff --git a/web/src/lib/db.svelte.ts b/web/src/lib/db.svelte.ts index 68a3990..e6aa6a8 100644 --- a/web/src/lib/db.svelte.ts +++ b/web/src/lib/db.svelte.ts @@ -450,10 +450,11 @@ function remoteChange(): void { }, REMOTE_DEBOUNCE); } +/** Runs the preparation and a round; offline only the preparation runs, which keeps the stale entries stopped. */ export function syncNow(): Promise { if (syncing) return syncing; const m = sm; - if (!m || !status.online) return Promise.resolve(); + if (!m) return Promise.resolve(); if (!pb.authStore.isValid) { sessionExpired?.(); return Promise.resolve(); @@ -461,7 +462,7 @@ export function syncNow(): Promise { rejected = false; m.failures.length = 0; const run: Promise = runBeforeSync() - .then(() => m.syncAll()) + .then(() => (status.online && m === sm ? m.syncAll() : undefined)) .catch((err: Error) => { if (m !== sm) return; if (!pb.authStore.isValid || m.failures.some(unauthorized)) sessionExpired?.(); diff --git a/web/src/lib/db.test.ts b/web/src/lib/db.test.ts index d79d776..798380f 100644 --- a/web/src/lib/db.test.ts +++ b/web/src/lib/db.test.ts @@ -426,6 +426,28 @@ describe('merge after apply', () => { }); }); +describe('minute tick', () => { + it('runs the sync preparation also while offline', async () => { + await closeDb(); + vi.useFakeTimers({ toFake: ['setInterval', 'clearInterval'] }); + try { + await openDb(`user-${n}`); + const before = vi.fn(async () => undefined); + onBeforeSync(before); + expect(status.online).toBe(false); + const pulls = mocks.pulls.count; + vi.advanceTimersByTime(60_000); + await settled(); + await settled(); + expect(before).toHaveBeenCalledTimes(1); + expect(mocks.pulls.count).toBe(pulls); + } finally { + await closeDb(); + vi.useRealTimers(); + } + }); +}); + describe('realtime and offline', () => { const wait = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));