import { beforeEach, describe, expect, it, vi } from 'vitest' import { http } from '@/shared/api/http' import { getPlatformAuditLog, listPlatformAuditLogs } from './platformAudit' vi.mock('@/shared/api/http', () => ({ http: { get: vi.fn() }, unwrapApiResponse: (response: { data: T }) => response.data, })) describe('platform audit api', () => { beforeEach(() => vi.clearAllMocks()) it('queries the selected customer and preserves long ids as strings', async () => { vi.mocked(http.get).mockResolvedValue({ data: { records: [{ id: '2082813911786024961', accountId: '8', operatorTenantId: '0', userId: '9', resourceId: '8', eventId: 'evt', eventType: 'PLATFORM_TENANT_UPDATE', resourceType: 'TENANT', description: '编辑客户', username: 'admin', appCode: 'ADMIN', clientType: 'WEB', clientIp: '127.0.0.1', requestId: null, traceId: null, serviceName: 'iam-service', success: true, errorCode: null, errorMessage: null, occurredAt: '2026-08-13T01:00:00Z', createdAt: '2026-08-13T09:00:00' }], page: 1, size: 20, total: 1, pages: 1 } }) const result = await listPlatformAuditLogs({ accountId: '8', page: 1, size: 20 }) expect(http.get).toHaveBeenCalledWith('/platform/v1/audit/logs', { params: { accountId: '8', page: 1, size: 20 } }) expect(result.records[0].id).toBe('2082813911786024961') expect(result.records[0].clientIp).toBe('127.0.0.1') expect(result.total).toBe(1) expect(result.pages).toBe(1) }) it('preserves the server total when the current page has fewer records', async () => { vi.mocked(http.get).mockResolvedValue({ data: { records: [{ id: '1', accountId: '8', operatorTenantId: '0', userId: '9', resourceId: '8', eventId: 'evt', eventType: 'PLATFORM_TENANT_UPDATE', resourceType: 'TENANT', description: '编辑客户', username: 'admin', appCode: 'ADMIN', clientType: 'WEB', clientIp: '127.0.0.1', requestId: null, traceId: null, serviceName: 'iam-service', success: true, errorCode: null, errorMessage: null, occurredAt: '2026-08-13T01:00:00Z', createdAt: '2026-08-13T09:00:00' }], page: 1, size: 20, total: 13, pages: 1, } }) const result = await listPlatformAuditLogs({ accountId: '8', page: 1, size: 20 }) expect(result.records).toHaveLength(1) expect(result.total).toBe(13) expect(result.pages).toBe(1) }) it('maps audit filters and omits empty query values', async () => { vi.mocked(http.get).mockResolvedValue({ data: { records: [], page: 1, size: 20, total: 0, pages: 0 } }) await listPlatformAuditLogs({ accountId: '8', page: 1, size: 20, keyword: 'admin', success: false, occurredFrom: '2026-08-01T00:00:00.000Z', occurredTo: '2026-08-13T23:59:59.999Z', eventType: '', resourceType: undefined, }) expect(http.get).toHaveBeenCalledWith('/platform/v1/audit/logs', { params: { accountId: '8', page: 1, size: 20, keyword: 'admin', success: false, occurredFrom: '2026-08-01T00:00:00.000Z', occurredTo: '2026-08-13T23:59:59.999Z', } }) }) it('loads one audit detail', async () => { vi.mocked(http.get).mockResolvedValue({ data: { id: '1', accountId: '8', operatorTenantId: '0', userId: '9', resourceId: '8', eventId: 'evt', eventType: 'PLATFORM_TENANT_UPDATE', resourceType: 'TENANT', description: '编辑客户', username: 'admin', appCode: 'ADMIN', clientType: 'WEB', clientIp: '127.0.0.1', requestId: null, traceId: null, serviceName: 'iam-service', success: true, errorCode: null, errorMessage: null, occurredAt: '2026-08-13T01:00:00Z', createdAt: '2026-08-13T09:00:00', httpMethod: 'PUT', requestUri: '/tenants/8', durationMs: 12, beforeData: '{}', afterData: '{"name":"new"}' } }) const result = await getPlatformAuditLog('1') expect(http.get).toHaveBeenCalledWith('/platform/v1/audit/logs/1') expect(result.afterData).toBe('{"name":"new"}') }) })