import { describe, expect, it, vi } from 'vitest'; import { useMarketOverview } from './useMarketOverview'; const response = { source: 'tdx', status: 'fresh', data: null, snapshotId: '20260818-101500', asOf: '2026-08-18T10:15:00+08:00', warnings: [], } as const; describe('市场概览状态', () => { it('并行加载五个模块,一个模块失败不影响其他模块', async () => { const api = { getSummary: vi.fn().mockResolvedValue(response), getIndices: vi.fn().mockRejectedValue(new Error('failed')), getBreadth: vi.fn().mockResolvedValue(response), getSectors: vi.fn().mockResolvedValue(response), getFunds: vi.fn().mockResolvedValue(response), getSectorRankings: vi.fn().mockResolvedValue(response), getCapitalFlow: vi.fn().mockResolvedValue(response), getTurnoverTrend: vi.fn().mockResolvedValue(response), getShortTermEmotion: vi.fn().mockResolvedValue(response), getShortTermConclusion: vi.fn().mockResolvedValue(response), }; const state = useMarketOverview(api); await state.refresh(); expect(state.summary.value.status).toBe('ready'); expect(state.indices.value.status).toBe('error'); expect(state.breadth.value.status).toBe('ready'); expect(state.isLoading.value).toBe(false); expect(state.shortTermEmotion.value.status).toBe('ready'); expect(state.shortTermConclusion.value.status).toBe('ready'); expect(state.sectorRankings.value.status).toBe('ready'); expect(state.capitalFlow.value.status).toBe('ready'); expect(state.turnoverTrend.value.status).toBe('ready'); }); it('分析偏好写入本地存储并可恢复', () => { const first = useMarketOverview({} as never); first.setAnalysisHorizon('短线'); first.setAnalysisEngine('混合'); const second = useMarketOverview({} as never); expect(second.analysisHorizon.value).toBe('短线'); expect(second.analysisEngine.value).toBe('混合'); }); it('切换行业结构分类时重新请求对应数据', async () => { const api = { getSectorRankings: vi.fn().mockResolvedValue(response), getCapitalFlow: vi.fn().mockResolvedValue(response), }; const state = useMarketOverview(api as never); await state.refreshSectorRankings('concept'); await state.refreshCapitalFlow('board'); expect(api.getSectorRankings).toHaveBeenCalledWith({ category: 'concept', sort: 'rise', limit: 10, }); expect(api.getCapitalFlow).toHaveBeenCalledWith({ category: 'board', sort: 'inflow', limit: 10, }); expect(state.sectorCategory.value).toBe('concept'); expect(state.capitalFlowCategory.value).toBe('board'); }); });