/
kreyndelHam
/
Konstructorium
Обзор
Документация
Войти
/
kreyndelHam
/
Konstructorium
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
1
CI/CD
Аналитика
Безопасность
dev
frontend/src/utils/authBootstrap.test.js
58 строк
2 KB
kreindel.s
Fix front
23 май 2026, 11:29
23 май 2026, 11:29
a529735
Код
Авторство
О чём код?
import { describe, expect, it, vi } from 'vitest' import { isTransientAuthProbeError, resolveAuthOnStartup } from './authBootstrap' describe('isTransientAuthProbeError', () => { it('treats missing response as transient', () => { expect(isTransientAuthProbeError(new Error('network'))).toBe(true) }) it('treats 5xx as transient', () => { expect(isTransientAuthProbeError({ response: { status: 503 } })).toBe(true) }) it('does not treat 401 as transient', () => { expect(isTransientAuthProbeError({ response: { status: 401 } })).toBe(false) }) }) describe('resolveAuthOnStartup', () => { it('returns authenticated user on successful profile probe', async () => { const getProfile = vi.fn().mockResolvedValue({ data: { id: 1, username: 'u' } }) const result = await resolveAuthOnStartup({ getProfile }) expect(result).toEqual({ status: 'authenticated', user: { id: 1, username: 'u' }, shouldClearAuth: false, }) }) it('clears auth on explicit 401 after retries', async () => { const getProfile = vi.fn().mockRejectedValue({ response: { status: 401 } }) const result = await resolveAuthOnStartup({ getProfile, warmCsrf: vi.fn().mockResolvedValue(undefined), maxAttempts: 2, retryDelayMs: 0, }) expect(getProfile).toHaveBeenCalledTimes(2) expect(result.status).toBe('unauthorized') expect(result.shouldClearAuth).toBe(true) }) it('keeps auth snapshot on network error when credentials exist', async () => { const getProfile = vi.fn().mockRejectedValue(new Error('Network Error')) const result = await resolveAuthOnStartup({ getProfile, hasStoredCredentials: () => true, maxAttempts: 1, }) expect(result.status).toBe('offline') expect(result.shouldClearAuth).toBe(false) }) })