summaryrefslogtreecommitdiff
path: root/frontend/src/lib/dashboardMetrics.test.ts
diff options
context:
space:
mode:
authorSomhairle H. Marisol <[email protected]>2026-09-18 09:50:37 +0800
committerSomhairle H. Marisol <[email protected]>2026-09-18 09:50:37 +0800
commit0b1ad75571b312dd60f9ae0099b47cc02ac54c78 (patch)
tree1fdcdaa6085f25f6c28ea5cdb071d01c0bc50515 /frontend/src/lib/dashboardMetrics.test.ts
parent18b2d336d058015341e93dd95fa6a8ac8e2ec67d (diff)
downloadstrategy-lab-0b1ad75571b312dd60f9ae0099b47cc02ac54c78.tar.gz
feat(frontend): 首页指标卡与项目行 sparkline(前端重设计第二轮之一)
[变更性质] 新增能力,不动后端。 [新增功能] dashboardMetrics 聚合(项目/数据集/实验/AI 用量统计 + 最近回测首末净值),实盘数据优先、缺数以占位符 '-' 兜底;ProjectMetricRow 渲染 4 张 metric-card;Sparkline 组件输出 up/down 方向线段与端点圆点。 [实现方案] 聚合逻辑与展示组件分离,sparkline 仅依赖 first/last 两个字段,无图表库依赖。 [影响范围] frontend/src/lib 与 components;配套 5 组单测(dashboardMetrics/sparkline/metricRow)。
Diffstat (limited to 'frontend/src/lib/dashboardMetrics.test.ts')
-rw-r--r--frontend/src/lib/dashboardMetrics.test.ts77
1 files changed, 77 insertions, 0 deletions
diff --git a/frontend/src/lib/dashboardMetrics.test.ts b/frontend/src/lib/dashboardMetrics.test.ts
new file mode 100644
index 0000000..2a25fda
--- /dev/null
+++ b/frontend/src/lib/dashboardMetrics.test.ts
@@ -0,0 +1,77 @@
+// @ts-nocheck
+import { describe, expect, it } from 'vitest';
+import { aggregateDashboard, sparkFirstLast } from './dashboardMetrics';
+
+// Leader round2 spec: 登录后默认页顶部一行指标卡——
+// 当前项目数 / 数据集总数 / 运行中-已完成回测数 / AI 用量。
+// 取不到的字段占位 "-"(不得编造数字)。行为合同先定义:
+
+describe('aggregateDashboard', () => {
+ const projects = [{ id: 'p1' }, { id: 'p2' }, { id: 'p3' }];
+ const datasets = [{ id: 'd1' }, { id: 'd2' }];
+ const runs = [
+ { id: 'r1', status: 'running', result: null },
+ { id: 'r2', status: 'queued', result: null },
+ { id: 'r3', status: 'succeeded', result: { metrics: {} } },
+ { id: 'r4', status: 'succeeded', result: { metrics: {} } },
+ { id: 'r5', status: 'failed', result: null }
+ ];
+
+ it('counts projects, datasets and running/done runs from plain item arrays', () => {
+ const x = aggregateDashboard({ projects, datasets, runs, usage: null });
+ expect(x.projectCount).toBe(3);
+ expect(x.datasetCount).toBe(2);
+ expect(x.runningRuns).toBe(2); // queued + running
+ expect(x.doneRuns).toBe(2); // succeeded
+ });
+
+ it('usage label uses real totals when present, "-" placeholder otherwise', () => {
+ const withUsage = aggregateDashboard({
+ projects, datasets, runs,
+ usage: { totals: { input_tokens: 1200, output_tokens: 340, requests: 7 } }
+ });
+ expect(withUsage.usageLabel).toBe('7 次');
+ expect(withUsage.usageTokens).toBe('(1.2k/340 tok)');
+
+ const withoutUsage = aggregateDashboard({ projects, datasets, runs, usage: null });
+ expect(withoutUsage.usageLabel).toBe('-');
+ expect(withoutUsage.usageTokens).toBe('');
+ });
+
+ it('is resilient to missing arrays (fresh account)', () => {
+ const x = aggregateDashboard({ projects: null, datasets: undefined, runs: null, usage: {} });
+ expect(x.projectCount).toBe(0);
+ expect(x.datasetCount).toBe(0);
+ expect(x.runningRuns).toBe(0);
+ expect(x.doneRuns).toBe(0);
+ });
+});
+
+describe('sparkFirstLast (per-project latest backtest equity endpoints)', () => {
+ it('picks the newest run with a result per project and returns first/last equity', () => {
+ const runs = [
+ { id: 'r-old', project_id: 'p1', created_at: '2026-01-01', result: { equity: [
+ { equity: 100 },
+ { equity: 130 } // old run: up
+ ] } },
+ { id: 'r-new', project_id: 'p1', created_at: '2026-02-01', result: { equity: [
+ { equity: 100 },
+ { equity: 90 } // newer run is down — must win over the old run
+ ] } },
+ { id: 'r-broken', project_id: 'p2', created_at: '2026-02-01', result: null },
+ { id: 'r-only', project_id: 'p3', created_at: '2026-02-03', result: { equity: [
+ { equity: 50 },
+ { equity: 50 },
+ { equity: 58 }
+ ] } }
+ ];
+ const m = sparkFirstLast(runs);
+ expect(m.get('p1')).toEqual({ first: 100, last: 90 });
+ expect(m.get('p3')).toEqual({ first: 50, last: 58 });
+ expect(m.has('p2')).toBe(false); // result=null → no sparkline, renders as empty
+ });
+
+ it('returns empty map for no runs', () => {
+ expect([...sparkFirstLast([]).keys()]).toEqual([]);
+ });
+});