import { describe, expect, it, vi, afterEach } from 'vitest' import { configuredInterval, saveSourceRefresh } from './sourceRefreshPolicy' import type { SourceConnectionListItem, SourceSettingsRevision } from './sourceConnections' const source = { id: 'source-1', name: '测试来源', source_type: 'dingtalk', enabled: true, schedule_configured: true, } as SourceConnectionListItem const revision = { id: 'r1', state: 'active', refresh_interval_seconds: '21600' } as SourceSettingsRevision const response = (data: unknown) => ({ ok: true, json: async () => data }) afterEach(() => vi.unstubAllGlobals()) describe('source refresh policy', () => { it('uses the persisted interval, never the next-run countdown', () => { expect(configuredInterval(source, [revision])).toBe(21600) }) it('refuses to overwrite an unreadable configured policy', () => { expect(() => configuredInterval(source, [])).toThrow('未修改设置') }) it('preserves the interval when pausing', async () => { const fetch = vi .fn() .mockResolvedValueOnce( response({ data: [ { ...revision, revision_number: 1, name: '测试来源', endpoint: '', credential_ref: '', enabled: true, created_by: 'admin', created_at: '2026-10-26', validated_at: null, activated_at: null, retired_at: null, }, ], }) ) .mockResolvedValueOnce(response({ operation: 'created', data: { id: 'r2' } })) .mockResolvedValueOnce(response({ operation: 'created', data: {} })) vi.stubGlobal('fetch', fetch) await saveSourceRefresh(source, false) expect(JSON.parse(fetch.mock.calls[1][1].body)).toMatchObject({ enabled: false, schedule: { refresh_interval_seconds: 21600 }, }) }) })