From ed1b3244b8760ed70c8ed0e354d377ce0a5d64fe Mon Sep 17 00:00:00 2001 From: ozh Date: Tue, 8 Sep 2026 17:45:19 +0800 Subject: [PATCH] =?UTF-8?q?feat(home):=20=E9=A6=96=E9=A1=B5=E5=91=8A?= =?UTF-8?q?=E8=AD=A6=E6=8C=89=E7=8A=B6=E6=80=81=E5=88=86=E6=B5=81=E7=9A=84?= =?UTF-8?q?=E7=BA=AF=E5=87=BD=E6=95=B0=E4=B8=8E=E5=8D=95=E6=B5=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pages/index/splitAlerts.ts | 15 +++++++++ tests/unit/home/splitAlerts.spec.ts | 49 +++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 src/pages/index/splitAlerts.ts create mode 100644 tests/unit/home/splitAlerts.spec.ts diff --git a/src/pages/index/splitAlerts.ts b/src/pages/index/splitAlerts.ts new file mode 100644 index 0000000..39828c0 --- /dev/null +++ b/src/pages/index/splitAlerts.ts @@ -0,0 +1,15 @@ +import type { EventItem } from '@/api/event' + +// 首页告警分流:status=1 未处理(首页只展示最新一条),其余全部进消息列表 +export function splitAlerts(list: EventItem[]): { + unhandled: EventItem[] + messages: EventItem[] +} { + const unhandled: EventItem[] = [] + const messages: EventItem[] = [] + for (const item of list) { + if (item.status === 1) unhandled.push(item) + else messages.push(item) + } + return { unhandled, messages } +} diff --git a/tests/unit/home/splitAlerts.spec.ts b/tests/unit/home/splitAlerts.spec.ts new file mode 100644 index 0000000..1a8a4bb --- /dev/null +++ b/tests/unit/home/splitAlerts.spec.ts @@ -0,0 +1,49 @@ +import { describe, it, expect } from 'vitest' +import { splitAlerts } from '@/pages/index/splitAlerts' +import type { EventItem } from '@/api/event' + +const item = (over: Partial): EventItem => ({ + id: 1, + sceneName: '客厅', + deviceName: '体征监测仪', + status: 2, + time: '2026-09-07 21:22:12', + ...over +}) + +describe('splitAlerts', () => { + it('status=1 进入未处理组,其余进入消息组', () => { + const list = [ + item({ id: 1, status: 1 }), + item({ id: 2, status: 2 }), + item({ id: 3, status: 3 }) + ] + const { unhandled, messages } = splitAlerts(list) + expect(unhandled.map((i) => i.id)).toEqual([1]) + expect(messages.map((i) => i.id)).toEqual([2, 3]) + }) + + it('status 异常值(如 0、99)落入消息组不丢弃', () => { + const list = [item({ id: 1, status: 0 }), item({ id: 2, status: 99 })] + const { unhandled, messages } = splitAlerts(list) + expect(unhandled).toEqual([]) + expect(messages.map((i) => i.id)).toEqual([1, 2]) + }) + + it('空列表两组均为空', () => { + const { unhandled, messages } = splitAlerts([]) + expect(unhandled).toEqual([]) + expect(messages).toEqual([]) + }) + + it('两组均保持原有相对顺序', () => { + const list = [ + item({ id: 3, status: 2 }), + item({ id: 1, status: 1 }), + item({ id: 2, status: 2 }) + ] + const { unhandled, messages } = splitAlerts(list) + expect(unhandled.map((i) => i.id)).toEqual([1]) + expect(messages.map((i) => i.id)).toEqual([3, 2]) + }) +})