import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest' import { usePlayGameInWebview } from '../use-play-game-in-webview' vi.mock('#imports', () => ({ useDevice: () => ({ isMobile: true }), useRuntimeConfig: () => ({ public: { WEBVIEW_USER_AGENT: 'test-webview-ua' }, }), })) describe('usePlayGameInWebview', () => { let originalLocalStorage: Storage let localStorageMock: any beforeEach(() => { // Mock localStorage originalLocalStorage = global.localStorage localStorageMock = { getItem: vi.fn(), setItem: vi.fn(), removeItem: vi.fn(), clear: vi.fn(), } // @ts-ignore global.localStorage = localStorageMock }) afterEach(() => { // Restore localStorage // @ts-ignore global.localStorage = originalLocalStorage vi.restoreAllMocks() }) it('should return true if ua matches WEBVIEW_USER_AGENT', () => { localStorageMock.getItem.mockReturnValue('test-webview-ua') const { isPlayingInWebview } = usePlayGameInWebview() expect(isPlayingInWebview()).toBe(true) }) it('should return false if ua does not match WEBVIEW_USER_AGENT', () => { localStorageMock.getItem.mockReturnValue('some-other-ua') const { isPlayingInWebview } = usePlayGameInWebview() expect(isPlayingInWebview()).toBe(false) }) it('should return false if ua is not set', () => { localStorageMock.getItem.mockReturnValue(null) const { isPlayingInWebview } = usePlayGameInWebview() expect(isPlayingInWebview()).toBe(false) }) it('should return false and log error if localStorage throws', () => { localStorageMock.getItem.mockImplementation(() => { throw new Error('fail') }) const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {}) const { isPlayingInWebview } = usePlayGameInWebview() expect(isPlayingInWebview()).toBe(false) expect(errorSpy).toHaveBeenCalled() errorSpy.mockRestore() }) it('should return false if not mobile', async () => { vi.doMock('#imports', () => ({ useDevice: () => ({ isMobile: false }), useRuntimeConfig: () => ({ public: { WEBVIEW_USER_AGENT: 'test-webview-ua' }, }), })) vi.resetModules() const { usePlayGameInWebview: usePlayGameInWebviewRemock } = await import( '../use-play-game-in-webview' ) const { isPlayingInWebview } = usePlayGameInWebviewRemock() expect(isPlayingInWebview()).toBe(false) }) })