import { beforeEach, describe, expect, it, vi } from 'vitest' import { AuthHistoryApi } from './AuthHistoryApi' import type { ApiClient } from '../../core/ApiClient' import type { AuthHistory } from './types' import type { Paginated } from '../../core/types' // ── Mock client factory ────────────────────────────────────────────── function createMockClient(): ApiClient { return { setAccessToken: vi.fn().mockReturnThis(), request: vi.fn(), get: vi.fn(), post: vi.fn(), put: vi.fn(), patch: vi.fn(), delete: vi.fn(), } } // ── Fixtures ───────────────────────────────────────────────────────── const entry: AuthHistory = { id: 'ah-1', event: 'auth', userId: 'user-1', accountId: 'acc-1', branch: 'main', originIP: '127.0.0.1', originUA: 'Mozilla/5.0', createdAt: '2024-01-01T00:00:00Z', updatedAt: '2024-01-01T00:00:00Z', } const paginated: Paginated = { data: [entry], total: 1, limit: 10, skip: 0, currentPage: 1, lastPage: 1, from: 1, to: 1, } // ── Tests ──────────────────────────────────────────────────────────── describe('AuthHistoryApi', () => { let client: ReturnType let api: AuthHistoryApi beforeEach(() => { client = createMockClient() api = new AuthHistoryApi(client) }) // ── getMany ────────────────────────────────────────────────────── describe('getMany', () => { it('calls client.get with /auth-history when no query is provided', async () => { vi.mocked(client.get).mockResolvedValue(paginated) const result = await api.getMany() expect(client.get).toHaveBeenCalledWith('/auth-history', undefined) expect(result).toEqual(paginated) }) it('serializes query parameters', async () => { vi.mocked(client.get).mockResolvedValue(paginated) await api.getMany({ page: 2, perPage: 5 }) const url = vi.mocked(client.get).mock.calls[0][0] as string expect(url).toContain('/auth-history?') expect(url).toContain('page=2') }) it('forwards custom headers', async () => { vi.mocked(client.get).mockResolvedValue(paginated) const headers = { 'X-Custom': 'h' } await api.getMany(undefined, headers) expect(client.get).toHaveBeenCalledWith('/auth-history', headers) }) it('propagates errors from client.get', async () => { vi.mocked(client.get).mockRejectedValue(new Error('Forbidden')) await expect(api.getMany()).rejects.toThrow('Forbidden') }) }) // ── getById ────────────────────────────────────────────────────── describe('getById', () => { it('calls client.get with /auth-history/:id', async () => { vi.mocked(client.get).mockResolvedValue(entry) const result = await api.getById('ah-1') expect(client.get).toHaveBeenCalledWith('/auth-history/ah-1', undefined) expect(result).toEqual(entry) }) it('forwards custom headers', async () => { vi.mocked(client.get).mockResolvedValue(entry) const headers = { 'X-Custom': 'h' } await api.getById('ah-1', headers) expect(client.get).toHaveBeenCalledWith('/auth-history/ah-1', headers) }) it('propagates errors from client.get', async () => { vi.mocked(client.get).mockRejectedValue(new Error('Not found')) await expect(api.getById('missing')).rejects.toThrow('Not found') }) }) })