import { afterEach, describe, expect, it, vi } from 'vitest' import { activateSourceSettingsRevision, createSourceSettingsRevision, fetchSourceConnectionList, fetchSourceConnections, fetchSourceScanDetail, fetchSourceSettingsRevisions, fetchSourceScans, parseSourceConnectionsResponse, parseSourceScanDetailResponse, parseSourceScansResponse, triggerSourceScan, } from './sourceConnections' afterEach(() => vi.unstubAllGlobals()) describe('parseSourceConnectionsResponse', () => { it('normalizes only the safe source connection projection', () => { const result = parseSourceConnectionsResponse({ data: [{ id: 'conn-safe', source_type: 'alist', name: 'AList只读源', enabled: true, schedule_configured: true, health_status: 'healthy', item_count: 42, updated_at: '2026-09-06T01:00:00Z', base_url: 'https://secret-origin.example', secret_ref: 'secret-name', last_scan: { id: 'scan-1', state: 'completed', observed_items: 42, unchanged_items: 40, added_items: 1, updated_items: 1, over_limit_items: 0, missing_items: 0, planned_uploads: 1, error_code: '', created_at: '2026-09-06T00:59:00Z', completed_at: '2026-09-06T01:00:00Z', snapshot_sha256: 'must-strip', previous_revision: 'must-strip', }, }], summary: { total: 1, enabled: 1, disabled: 0, healthy: 1, degraded: 0, unhealthy: 0, unknown: 0 }, }) expect(result.items).toEqual([{ id: 'conn-safe', source_type: 'alist', name: 'AList只读源', enabled: true, schedule_configured: true, health_status: 'healthy', item_count: 42, updated_at: '2026-09-06T01:00:00Z', last_scan: { id: 'scan-1', state: 'completed', observed_items: 42, unchanged_items: 40, added_items: 1, updated_items: 1, over_limit_items: 0, missing_items: 0, planned_uploads: 1, error_code: '', created_at: '2026-09-06T00:59:00Z', completed_at: '2026-09-06T01:00:00Z', }, }]) expect(result.summary.total).toBe(1) expect(result.items[0]).not.toHaveProperty('base_url') expect(result.items[0]).not.toHaveProperty('secret_ref') expect(result.items[0].last_scan).not.toHaveProperty('snapshot_sha256') }) }) describe('fetchSourceConnections', () => { it('uses the fixed read-only source connection endpoint', async () => { const fetchMock = vi.fn().mockResolvedValue({ ok: true, json: async () => ({ data: [], summary: { total: 0, enabled: 0, disabled: 0, healthy: 0, degraded: 0, unhealthy: 0, unknown: 0 }, }), }) vi.stubGlobal('fetch', fetchMock) await fetchSourceConnections() expect(fetchMock).toHaveBeenCalledWith( '/api/v1/source-connections', expect.objectContaining({ headers: { Accept: 'application/json' } }), ) }) }) describe('triggerSourceScan', () => { it('posts to the encoded connection scan endpoint and normalizes the response', async () => { const fetchMock = vi.fn().mockResolvedValue({ ok: true, json: async () => ({ id: 'scan-safe', source_connection_id: 'conn/a', source_type: 'pp', state: 'pending', created_at: '2026-09-06T01:00:00Z', snapshot_sha256: 'must-strip', }), }) vi.stubGlobal('fetch', fetchMock) const scan = await triggerSourceScan('conn/a') expect(fetchMock).toHaveBeenCalledWith('/api/v1/source-connections/conn%2Fa/scan', expect.objectContaining({ method: 'POST' })) expect(scan).toEqual({ id: 'scan-safe', source_connection_id: 'conn/a', source_type: 'pp', state: 'pending', created_at: '2026-09-06T01:00:00Z', }) }) }) describe('source scan history and details', () => { const scan = { id: 'scan-safe', state: 'completed', observed_items: 2, unchanged_items: 0, added_items: 1, updated_items: 0, over_limit_items: 0, missing_items: 1, planned_uploads: 1, error_code: '', created_at: '2026-09-06T06:00:00Z', completed_at: '2026-09-06T06:01:00Z', snapshot_sha256: 'must-be-dropped', stable_external_id: 'must-be-dropped', } it('strictly parses safe history and detail projections', () => { expect(parseSourceScansResponse({ data: [scan], total: 1, limit: 20, offset: 0 })).toEqual({ items: [{ id: 'scan-safe', state: 'completed', observed_items: 2, unchanged_items: 0, added_items: 1, updated_items: 0, over_limit_items: 0, missing_items: 1, planned_uploads: 1, error_code: '', created_at: '2026-09-06T06:00:00Z', completed_at: '2026-09-06T06:01:00Z', }], total: 1, limit: 20, offset: 0, }) expect(parseSourceScanDetailResponse({ data: { ...scan, changes: [{ id: 'chg-safe', source_item_id: 'si-safe', change_type: 'updated', observed_size_bytes: 42, created_at: '2026-09-06T06:01:00Z', previous_revision: 'must-be-dropped', observed_revision: 'must-be-dropped', }], }})).toEqual({ id: 'scan-safe', state: 'completed', observed_items: 2, unchanged_items: 0, added_items: 1, updated_items: 0, over_limit_items: 0, missing_items: 1, planned_uploads: 1, error_code: '', created_at: '2026-09-06T06:00:00Z', completed_at: '2026-09-06T06:01:00Z', changes: [{ id: 'chg-safe', source_item_id: 'si-safe', change_type: 'updated', observed_size_bytes: 42, created_at: '2026-09-06T06:01:00Z' }], }) }) it('requests connection-scoped history and details', async () => { const fetchMock = vi.fn() .mockResolvedValueOnce(new Response(JSON.stringify({ data: [scan], total: 1, limit: 20, offset: 0 }), { status: 200 })) .mockResolvedValueOnce(new Response(JSON.stringify({ data: { ...scan, changes: [] } }), { status: 200 })) vi.stubGlobal('fetch', fetchMock) await fetchSourceScans('conn / pp') await fetchSourceScanDetail('conn / pp', 'scan / one') expect(fetchMock.mock.calls[0][0]).toBe('/api/v1/source-connections/conn%20%2F%20pp/scans?limit=20&offset=0') expect(fetchMock.mock.calls[1][0]).toBe('/api/v1/source-connections/conn%20%2F%20pp/scans/scan%20%2F%20one') }) }) describe('V1 source settings client', () => { it('projects the connection list without sensitive fields', async () => { const fetchMock = vi.fn().mockResolvedValue(new Response(JSON.stringify({ data: [{ id: 'conn-1', source_type: 'alist', name: 'A', enabled: true, schedule_configured: true, health_status: 'healthy', item_count: 3, updated_at: '2026-09-12T01:00:00Z', next_refresh_at: '2026-09-12T09:00:00Z', base_url: 'https://strip.me', secret_ref: 'strip-too' }], }), { status: 200 })) vi.stubGlobal('fetch', fetchMock) const items = await fetchSourceConnectionList() expect(items).toEqual([{ id: 'conn-1', source_type: 'alist', name: 'A', enabled: true, schedule_configured: true, health_status: 'healthy', item_count: 3, updated_at: '2026-09-12T01:00:00Z', next_refresh_at: '2026-09-12T09:00:00Z' }]) expect(JSON.stringify(items)).not.toContain('strip') }) it('rejects malformed connection items', async () => { vi.stubGlobal('fetch', vi.fn().mockResolvedValue(new Response(JSON.stringify({ data: [{ id: 'x' }], }), { status: 200 }))) await expect(fetchSourceConnectionList()).rejects.toThrow('invalid source connection item') }) it('creates and activates revisions with intent + idempotency headers and surfaces operation', async () => { const fetchMock = vi.fn() .mockResolvedValueOnce(new Response(JSON.stringify({ data: { id: 'scr-2' }, operation: 'created' }), { status: 201 })) .mockResolvedValueOnce(new Response(JSON.stringify({ data: { connection_id: 'c', revision_id: 'scr-2' }, operation: 'replayed' }), { status: 200 })) vi.stubGlobal('fetch', fetchMock) expect(await createSourceSettingsRevision('conn/1', { source_type: 'alist', name: 'A', endpoint: '', credential_ref: '', enabled: true, schedule: { refresh_interval_seconds: 3600 } })).toEqual({ operation: 'created', id: 'scr-2' }) expect(await activateSourceSettingsRevision('conn/1', 'scr/2')).toEqual({ operation: 'replayed', id: null }) const [createCall, activateCall] = fetchMock.mock.calls expect(createCall[0]).toBe('/api/v1/source-connections/conn%2F1/settings/revisions') expect((createCall[1] as RequestInit).headers).toMatchObject({ 'X-Governance-Action': 'source-settings' }) expect(((createCall[1] as RequestInit).headers as Record)['Idempotency-Key']).toMatch(/[0-9a-f-]{36}/) expect(activateCall[0]).toBe('/api/v1/source-connections/conn%2F1/settings/revisions/scr%2F2/activate') expect(createCall[1]).not.toBe(activateCall[1]) }) it('surfaces server error details on failure', async () => { vi.stubGlobal('fetch', vi.fn().mockResolvedValue(new Response(JSON.stringify({ error: 'manual sources cannot be scheduled' }), { status: 400 }))) await expect(createSourceSettingsRevision('c', { source_type: 'manual', name: 'A', endpoint: '', credential_ref: '', enabled: true, schedule: { refresh_interval_seconds: 3600 } })).rejects.toThrow('保存来源设置草稿失败:manual sources cannot be scheduled') }) it('parses revision history strictly', async () => { const revision = { id: 'scr-1', revision_number: 1, state: 'active', name: 'A', endpoint: 'http://x', credential_ref: 'ref', enabled: true, refresh_interval_seconds: '3600', created_by: 'z', created_at: '2026-09-12T01:00:00Z', validated_at: null, activated_at: '2026-09-12T01:00:00Z', retired_at: null } vi.stubGlobal('fetch', vi.fn().mockResolvedValue(new Response(JSON.stringify({ data: [revision] }), { status: 200 }))) const history = await fetchSourceSettingsRevisions('c') expect(history[0].state).toBe('active') vi.stubGlobal('fetch', vi.fn().mockResolvedValue(new Response(JSON.stringify({ data: [{ ...revision, state: 'weird' }] }), { status: 200 }))) await expect(fetchSourceSettingsRevisions('c')).rejects.toThrow('invalid source settings revision') }) })