import { afterEach, describe, expect, it, vi } from 'vitest' import { fetchSourceAudit } from './sourceSettingsAudit' afterEach(() => vi.unstubAllGlobals()) describe('all-source operation audit', () => { it('requests paginated global history without a source selector', async () => { const page = { items: [ { id: '1', connection_id: 'removed', source_name: '已移除来源', action: 'archived', actor: 'tester', created_at: '2026-10-27T02:00:00Z', detail: '移除连接', }, ], total: 101, limit: 100, offset: 100, } const fetcher = vi.fn().mockResolvedValue({ ok: true, json: async () => ({ data: page }) }) vi.stubGlobal('fetch', fetcher) expect(await fetchSourceAudit(100)).toEqual(page) expect(fetcher.mock.calls[0][0]).toBe('/api/v1/source-connections/audit?limit=100&offset=100') }) it('does not disguise errors as empty history', async () => { vi.stubGlobal('fetch', vi.fn().mockResolvedValue({ ok: false })) await expect(fetchSourceAudit()).rejects.toThrow('无法读取') }) it('rejects malformed history', async () => { vi.stubGlobal( 'fetch', vi .fn() .mockResolvedValue({ ok: true, json: async () => ({ data: { items: [], total: '0', offset: 0, limit: 100 } }) }) ) await expect(fetchSourceAudit()).rejects.toThrow('格式无效') }) })