-
Notifications
You must be signed in to change notification settings - Fork 54.3k
Expand file tree
/
Copy pathHomePage.test.tsx
More file actions
446 lines (389 loc) · 13.3 KB
/
Copy pathHomePage.test.tsx
File metadata and controls
446 lines (389 loc) · 13.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
import { fireEvent, render, screen, waitFor } from '@testing-library/react';
import { MemoryRouter } from 'react-router-dom';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { analysisApi, DuplicateTaskError } from '../../api/analysis';
import { historyApi } from '../../api/history';
import { systemConfigApi } from '../../api/systemConfig';
import { useStockPoolStore } from '../../stores';
import { getReportText, normalizeReportLanguage } from '../../utils/reportLanguage';
import HomePage from '../HomePage';
const navigateMock = vi.fn();
vi.mock('react-router-dom', async () => {
const actual = await vi.importActual<typeof import('react-router-dom')>('react-router-dom');
return {
...actual,
useNavigate: () => navigateMock,
};
});
vi.mock('../../api/history', () => ({
historyApi: {
getList: vi.fn(),
getDetail: vi.fn(),
deleteRecords: vi.fn(),
getNews: vi.fn().mockResolvedValue({ total: 0, items: [] }),
getMarkdown: vi.fn().mockResolvedValue('# report'),
},
}));
vi.mock('../../api/analysis', async () => {
const actual = await vi.importActual<typeof import('../../api/analysis')>('../../api/analysis');
return {
...actual,
analysisApi: {
analyzeAsync: vi.fn(),
triggerMarketReview: vi.fn(),
getStatus: vi.fn(),
},
};
});
vi.mock('../../api/systemConfig', () => ({
systemConfigApi: {
getSetupStatus: vi.fn(),
},
}));
vi.mock('../../hooks/useTaskStream', () => ({
useTaskStream: vi.fn(),
}));
const historyItem = {
id: 1,
queryId: 'q-1',
stockCode: '600519',
stockName: '贵州茅台',
sentimentScore: 82,
operationAdvice: '买入',
createdAt: '2026-03-18T08:00:00Z',
};
const historyReport = {
meta: {
id: 1,
queryId: 'q-1',
stockCode: '600519',
stockName: '贵州茅台',
reportType: 'detailed' as const,
reportLanguage: 'zh' as const,
createdAt: '2026-03-18T08:00:00Z',
},
summary: {
analysisSummary: '趋势维持强势',
operationAdvice: '继续观察买点',
trendPrediction: '短线震荡偏强',
sentimentScore: 78,
},
};
describe('HomePage', () => {
beforeEach(() => {
vi.clearAllMocks();
navigateMock.mockReset();
useStockPoolStore.getState().resetDashboardState();
vi.mocked(systemConfigApi.getSetupStatus).mockResolvedValue({
isComplete: true,
readyForSmoke: true,
requiredMissingKeys: [],
nextStepKey: null,
checks: [],
});
});
it('renders the dashboard workspace and auto-loads the first report', async () => {
vi.mocked(historyApi.getList).mockResolvedValue({
total: 1,
page: 1,
limit: 20,
items: [historyItem],
});
vi.mocked(historyApi.getDetail).mockResolvedValue(historyReport);
vi.mocked(analysisApi.analyzeAsync).mockResolvedValue({
taskId: 'task-1',
status: 'pending',
});
render(
<MemoryRouter>
<HomePage />
</MemoryRouter>,
);
const dashboard = await screen.findByTestId('home-dashboard');
expect(dashboard).toBeInTheDocument();
expect(dashboard.className).toContain('h-[calc(100vh-5rem)]');
expect(dashboard.className).toContain('lg:h-[calc(100vh-2rem)]');
expect(dashboard.firstElementChild?.className).toContain('min-h-0');
expect(dashboard.querySelector('.flex-1.flex.min-h-0.overflow-hidden')).toBeTruthy();
expect(screen.getByTestId('home-dashboard-scroll')).toBeInTheDocument();
expect(screen.getByPlaceholderText('输入股票代码或名称,如 600519、贵州茅台、AAPL')).toBeInTheDocument();
expect(await screen.findByText('趋势维持强势')).toBeInTheDocument();
expect(
screen.getByRole('button', {
name: getReportText(normalizeReportLanguage(historyReport.meta.reportLanguage)).fullReport,
}),
).toBeInTheDocument();
});
it('shows the empty report workspace when history is empty', async () => {
vi.mocked(historyApi.getList).mockResolvedValue({
total: 0,
page: 1,
limit: 20,
items: [],
});
render(
<MemoryRouter>
<HomePage />
</MemoryRouter>,
);
expect(await screen.findByText('开始分析')).toBeInTheDocument();
expect(screen.getByRole('heading', { name: '开始分析', level: 3 })).toBeInTheDocument();
expect(screen.getByText('输入股票代码进行分析,或从左侧选择历史报告查看。')).toBeInTheDocument();
expect(screen.getByText('暂无历史分析记录')).toBeInTheDocument();
});
it('surfaces duplicate task warnings from dashboard submission', async () => {
vi.mocked(historyApi.getList).mockResolvedValue({
total: 0,
page: 1,
limit: 20,
items: [],
});
vi.mocked(analysisApi.analyzeAsync).mockRejectedValue(
new DuplicateTaskError('600519', 'task-1', '股票 600519 正在分析中'),
);
render(
<MemoryRouter>
<HomePage />
</MemoryRouter>,
);
const input = await screen.findByPlaceholderText('输入股票代码或名称,如 600519、贵州茅台、AAPL');
fireEvent.change(input, { target: { value: '600519' } });
fireEvent.click(screen.getByRole('button', { name: '分析' }));
await waitFor(() => {
expect(screen.getByText(/股票 600519 正在分析中/)).toBeInTheDocument();
});
expect(screen.getByText(/股票 600519 正在分析中/).closest('[role="alert"]')).toBeInTheDocument();
});
it('submits market review from the home toolbar', async () => {
vi.mocked(historyApi.getList).mockResolvedValue({
total: 0,
page: 1,
limit: 20,
items: [],
});
vi.mocked(analysisApi.triggerMarketReview).mockResolvedValue({
status: 'accepted',
sendNotification: true,
message: '大盘复盘任务已提交',
taskId: 'task-1',
});
vi.mocked(analysisApi.getStatus).mockResolvedValue({
taskId: 'task-1',
status: 'completed',
marketReviewReport: '市场复盘报告示例文本',
});
render(
<MemoryRouter>
<HomePage />
</MemoryRouter>,
);
fireEvent.click(await screen.findByRole('button', { name: '大盘复盘' }));
await waitFor(() => {
expect(analysisApi.triggerMarketReview).toHaveBeenCalledWith({ sendNotification: true });
});
expect(await screen.findByText('大盘复盘已完成')).toBeInTheDocument();
expect(await screen.findByText('市场复盘报告示例文本')).toBeInTheDocument();
expect(analysisApi.getStatus).toHaveBeenCalledWith('task-1');
});
it('keeps market review results in the main dashboard scroll area', async () => {
vi.mocked(historyApi.getList).mockResolvedValue({
total: 0,
page: 1,
limit: 20,
items: [],
});
vi.mocked(analysisApi.triggerMarketReview).mockResolvedValue({
status: 'accepted',
sendNotification: true,
message: '大盘复盘任务已提交',
taskId: 'task-1',
});
vi.mocked(analysisApi.getStatus).mockResolvedValue({
taskId: 'task-1',
status: 'completed',
marketReviewReport: Array.from({ length: 30 }, (_, index) => `第 ${index + 1} 行复盘内容`).join('\n'),
});
render(
<MemoryRouter>
<HomePage />
</MemoryRouter>,
);
fireEvent.click(await screen.findByRole('button', { name: '大盘复盘' }));
const dashboardScroll = screen.getByTestId('home-dashboard-scroll');
const marketReviewReport = await screen.findByTestId('market-review-report');
expect(dashboardScroll).toContainElement(marketReviewReport);
expect(marketReviewReport.className).not.toContain('max-h-64');
expect(marketReviewReport.className).not.toContain('overflow-y-auto');
expect(await screen.findByText('开始分析')).toBeInTheDocument();
});
it('shows first-run setup gaps and links to settings', async () => {
vi.mocked(historyApi.getList).mockResolvedValue({
total: 0,
page: 1,
limit: 20,
items: [],
});
vi.mocked(systemConfigApi.getSetupStatus).mockResolvedValue({
isComplete: false,
readyForSmoke: false,
requiredMissingKeys: ['llm_primary', 'stock_list'],
nextStepKey: 'llm_primary',
checks: [
{
key: 'llm_primary',
title: 'LLM 主渠道',
category: 'ai_model',
required: true,
status: 'needs_action',
message: '缺少主模型配置',
},
{
key: 'stock_list',
title: '自选股',
category: 'base',
required: true,
status: 'needs_action',
message: '缺少自选股',
},
],
});
render(
<MemoryRouter>
<HomePage />
</MemoryRouter>,
);
expect(await screen.findByText('基础配置未完成')).toBeInTheDocument();
expect(screen.getByText(/LLM 主渠道、自选股/)).toBeInTheDocument();
fireEvent.click(screen.getByRole('button', { name: '去配置' }));
expect(navigateMock).toHaveBeenCalledWith('/settings');
});
it('navigates to chat with report context when asking a follow-up question', async () => {
vi.mocked(historyApi.getList).mockResolvedValue({
total: 1,
page: 1,
limit: 20,
items: [historyItem],
});
vi.mocked(historyApi.getDetail).mockResolvedValue(historyReport);
render(
<MemoryRouter>
<HomePage />
</MemoryRouter>,
);
const followUpButton = await screen.findByRole('button', { name: '追问 AI' });
fireEvent.click(followUpButton);
expect(navigateMock).toHaveBeenCalledWith(
'/chat?stock=600519&name=%E8%B4%B5%E5%B7%9E%E8%8C%85%E5%8F%B0&recordId=1',
);
});
it('confirms and deletes selected history from the dashboard state flow', async () => {
vi.mocked(historyApi.getList).mockResolvedValue({
total: 1,
page: 1,
limit: 20,
items: [historyItem],
});
vi.mocked(historyApi.getDetail).mockResolvedValue(historyReport);
vi.mocked(historyApi.deleteRecords).mockResolvedValue({ deleted: 1 });
useStockPoolStore.setState({
historyItems: [historyItem],
selectedHistoryIds: [1],
selectedReport: historyReport,
});
render(
<MemoryRouter>
<HomePage />
</MemoryRouter>,
);
fireEvent.click(await screen.findByRole('button', { name: '删除' }));
expect(
await screen.findByText('确认删除这条历史记录吗?删除后将不可恢复。'),
).toBeInTheDocument();
fireEvent.click(screen.getByRole('button', { name: '确认删除' }));
await waitFor(() => {
expect(historyApi.deleteRecords).toHaveBeenCalledWith([1]);
});
});
it('opens and closes the mobile history drawer without changing dashboard styles', async () => {
vi.mocked(historyApi.getList).mockResolvedValue({
total: 0,
page: 1,
limit: 20,
items: [],
});
const { container } = render(
<MemoryRouter>
<HomePage />
</MemoryRouter>,
);
const trigger = await screen.findByRole('button', { name: '历史记录' });
fireEvent.click(trigger);
expect(container.querySelector('.page-drawer-overlay')).toBeTruthy();
expect(container.querySelector('.dashboard-card')).toBeTruthy();
fireEvent.click(container.querySelector('.fixed.inset-0.z-40') as HTMLElement);
await waitFor(() => {
expect(container.querySelector('.page-drawer-overlay')).toBeFalsy();
});
});
it('renders active task panel content from dashboard state', async () => {
vi.mocked(historyApi.getList).mockResolvedValue({
total: 0,
page: 1,
limit: 20,
items: [],
});
useStockPoolStore.setState({
activeTasks: [
{
taskId: 'task-1',
stockCode: '600519',
stockName: '贵州茅台',
status: 'processing',
progress: 45,
message: '正在抓取最新行情',
reportType: 'detailed',
createdAt: '2026-03-18T08:00:00Z',
},
],
});
render(
<MemoryRouter>
<HomePage />
</MemoryRouter>,
);
expect(await screen.findByText('分析任务')).toBeInTheDocument();
expect(screen.getByText('正在抓取最新行情')).toBeInTheDocument();
});
it('triggers reanalyze for the current report even if the search input has other text', async () => {
vi.mocked(historyApi.getList).mockResolvedValue({
total: 1,
page: 1,
limit: 20,
items: [historyItem],
});
vi.mocked(historyApi.getDetail).mockResolvedValue(historyReport);
vi.mocked(analysisApi.analyzeAsync).mockResolvedValue({
taskId: 'task-re-1',
status: 'pending',
});
render(
<MemoryRouter>
<HomePage />
</MemoryRouter>,
);
// Wait for the report to load
await screen.findByText('趋势维持强势');
// Type something else in the search box
const input = screen.getByPlaceholderText('输入股票代码或名称,如 600519、贵州茅台、AAPL');
fireEvent.change(input, { target: { value: 'AAPL' } });
// Click "Reanalyze"
const reanalyzeButton = screen.getByRole('button', { name: '重新分析' });
fireEvent.click(reanalyzeButton);
// Verify that analyzeAsync is called with the report's stock code, not the search box text
expect(analysisApi.analyzeAsync).toHaveBeenCalledWith(expect.objectContaining({
stockCode: '600519',
originalQuery: '600519',
forceRefresh: true,
}));
});
});