Files
ai-safety-console-web/tests/unit/auth/session.test.ts
T
baozaotumao2025 1b90e552a5 first commit
2026-07-18 21:10:39 +08:00

31 lines
1.1 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from 'vitest'
import { session } from '../../../src/auth/session'
const credentials = {
access_token: 'access', token_type: 'bearer' as const, expires_at: 2,
refresh_token: 'refresh', refresh_expires_at: 3,
}
describe('session', () => {
beforeEach(() => localStorage.clear())
it('stores and replaces the complete credential set atomically', () => {
const listener = vi.fn()
const unsubscribe = session.subscribe(listener)
session.set(credentials)
expect(session.get()).toEqual(credentials)
expect(JSON.parse(localStorage.getItem('safety-session') || 'null')).toEqual(credentials)
expect(listener).toHaveBeenCalledOnce()
unsubscribe()
})
it('clears credentials and pending refresh state together', () => {
localStorage.setItem('safety-session', JSON.stringify(credentials))
localStorage.setItem('safety-refresh-pending', 'secret')
localStorage.setItem('safety-refresh-after', '2')
localStorage.setItem('safety-last-activity-at', '1')
session.clear()
expect(localStorage.length).toBe(0)
})
})