feat(domain): 新增电池领域模型与聚合逻辑

This commit is contained in:
2026-05-11 20:51:24 +08:00
parent 393ff406a3
commit 8b6339f34b
2 changed files with 416 additions and 0 deletions
+62
View File
@@ -0,0 +1,62 @@
import { describe, expect, test } from 'bun:test'
import { createBatteriesResponse, createDashboardSnapshot, getDeviceStatus, toBatteryInfo } from './battery'
const rows = [
{
id: 1,
userId: 7,
mac: 'RING-A03',
devModel: '2401-A',
devName: 'RING-A03',
isLowPower: 'false',
powerStatus: 2,
power: 94,
createTime: new Date('2026-05-10T23:00:00.000Z'),
remark: 'v3.8.2',
},
{
id: 2,
userId: 7,
mac: 'RING-B11',
devModel: '2402-B',
devName: 'RING-B11',
isLowPower: 'true',
powerStatus: 1,
power: 84,
createTime: '2026-05-10 22:00:00',
remark: null,
},
]
describe('battery domain', () => {
test('preserves legacy SOH status thresholds', () => {
expect(getDeviceStatus(91)).toBe('健康')
expect(getDeviceStatus(90)).toBe('关注')
expect(getDeviceStatus(85)).toBe('预警')
})
test('builds batteries response counters from records', () => {
const now = new Date('2026-05-11T00:00:00.000Z')
const items = rows.map(toBatteryInfo)
const response = createBatteriesResponse(items, now)
expect(response.updatedAt).toBe('2026-05-11T00:00:00.000Z')
expect(response.total).toBe(items.length)
expect(response.lowPower).toBe(1)
expect(response.charging).toBe(1)
expect(response.items[0]?.createTime).toBe('2026-05-10T23:00:00.000Z')
})
test('creates old dashboard aggregate shape from deterministic records', () => {
const now = new Date('2026-05-11T00:00:00.000Z')
const snapshot = createDashboardSnapshot(rows.map(toBatteryInfo), now)
expect(snapshot.devices).toHaveLength(2)
expect(snapshot.soh.history).toHaveLength(12)
expect(snapshot.soh.forecast).toHaveLength(4)
expect(snapshot.summary.totalDevices).toBe(snapshot.devices.length)
expect(snapshot.summary.warningCount + snapshot.summary.watchCount + snapshot.summary.healthyCount).toBe(
snapshot.devices.length,
)
})
})