import { describe, it, expect } from 'vitest'; import reducer, { setSession, clearSession, setStatus, selectIsAuthenticated, selectAccessToken, selectUserInfo, selectAuthStatus, } from './authSlice'; import type { UserInfo } from '../../api/auth'; const userInfo: UserInfo = { userId: 1, username: 'alice', userType: 'NORMAL', language: 'zh-CN', companyCode: 'HQ', }; describe('authSlice', () => { it('setSession writes accessToken and userInfo and status=success', () => { const next = reducer(undefined, setSession({ accessToken: 'jwt', userInfo })); expect(next.accessToken).toBe('jwt'); expect(next.userInfo).toEqual(userInfo); expect(next.status).toBe('success'); }); it('clearSession resets to null and status=idle', () => { const initial = reducer(undefined, setSession({ accessToken: 'jwt', userInfo })); const cleared = reducer(initial, clearSession()); expect(cleared.accessToken).toBeNull(); expect(cleared.userInfo).toBeNull(); expect(cleared.status).toBe('idle'); }); it('setStatus updates status', () => { const next = reducer(undefined, setStatus('submitting')); expect(next.status).toBe('submitting'); }); it('selectIsAuthenticated true when token present', () => { const state = { auth: reducer(undefined, setSession({ accessToken: 'jwt', userInfo })) }; expect(selectIsAuthenticated(state)).toBe(true); expect(selectAccessToken(state)).toBe('jwt'); expect(selectUserInfo(state)).toEqual(userInfo); expect(selectAuthStatus(state)).toBe('success'); }); it('selectIsAuthenticated false initially', () => { const state = { auth: reducer(undefined, { type: '@@INIT' } as any) }; expect(selectIsAuthenticated(state)).toBe(false); }); });