import { describe, it, expect, vi, beforeEach } from 'vitest' import { useFeeds } from '../composables/useFeeds' // router/index.js's route definitions lazily import(...) the view components, // which importing the real module would still be safe to do (the imports // themselves aren't eagerly evaluated) — but mocking it keeps this test from // depending on the app's actual routes and lets currentRoute/push be // controlled directly. vi.mock('../router', () => ({ default: { currentRoute: { value: { name: 'feeds' } }, push: vi.fn(), }, })) import router from '../router' import { handleUnauthorizedResponse } from '../httpAuthInterceptor' describe('handleUnauthorizedResponse', () => { const { feedFilter } = useFeeds() beforeEach(() => { localStorage.setItem('user-token', 'abc123') localStorage.setItem('user-id', '7') localStorage.setItem('feedFilter', JSON.stringify(['Feed A'])) feedFilter.value = ['Feed A'] router.currentRoute.value.name = 'feeds' router.push.mockClear() }) it('clears the session and the feed filter, and redirects to login, on a 401 from a non-login route', async () => { const error = { response: { status: 401 } } await expect(handleUnauthorizedResponse(error)).rejects.toBe(error) expect(localStorage.getItem('user-token')).toBeNull() expect(localStorage.getItem('user-id')).toBeNull() expect(localStorage.getItem('feedFilter')).toBeNull() expect(feedFilter.value).toBeNull() expect(router.push).toHaveBeenCalledWith({ name: 'login' }) }) it('still clears the session on a 401 but does not push again when already on the login route', async () => { router.currentRoute.value.name = 'login' const error = { response: { status: 401 } } await expect(handleUnauthorizedResponse(error)).rejects.toBe(error) expect(localStorage.getItem('user-token')).toBeNull() expect(router.push).not.toHaveBeenCalled() }) it('leaves the session and filter untouched, and still rejects, on a non-401 error response', async () => { const error = { response: { status: 500 } } await expect(handleUnauthorizedResponse(error)).rejects.toBe(error) expect(localStorage.getItem('user-token')).toBe('abc123') expect(localStorage.getItem('feedFilter')).toBe(JSON.stringify(['Feed A'])) expect(feedFilter.value).toEqual(['Feed A']) expect(router.push).not.toHaveBeenCalled() }) it('leaves the session untouched and still rejects on an error with no response (e.g. a network error)', async () => { const error = new Error('network') await expect(handleUnauthorizedResponse(error)).rejects.toBe(error) expect(localStorage.getItem('user-token')).toBe('abc123') expect(router.push).not.toHaveBeenCalled() }) })