import { beforeEach, describe, expect, it, vi } from 'vitest' import { activity } from '../../../src/auth/activity' describe('user activity', () => { beforeEach(() => localStorage.clear()) it('records real input at most once every 30 seconds', () => { const checkRefresh = vi.fn() const now = vi.spyOn(Date, 'now').mockReturnValue(1_000_000) const stop = activity.start(checkRefresh) window.dispatchEvent(new MouseEvent('click')) expect(activity.get()).toBe(1_000_000) now.mockReturnValue(1_010_000) window.dispatchEvent(new KeyboardEvent('keydown')) expect(activity.get()).toBe(1_000_000) now.mockReturnValue(1_031_000) window.dispatchEvent(new TouchEvent('touchstart')) expect(activity.get()).toBe(1_031_000) expect(checkRefresh).toHaveBeenCalledTimes(3) stop() }) it('checks on foreground resume without treating visibility as activity', () => { const checkRefresh = vi.fn() const stop = activity.start(checkRefresh) document.dispatchEvent(new Event('visibilitychange')) expect(checkRefresh).toHaveBeenCalledOnce() expect(activity.get()).toBe(0) window.dispatchEvent(new PopStateEvent('popstate')) expect(activity.get()).toBeGreaterThan(0) stop() }) })