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));