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, ) }) test('uses AI prediction values when available', () => { const now = new Date('2026-05-11T00:00:00.000Z') const items = rows.map(toBatteryInfo) const snapshot = createDashboardSnapshot( items, now, new Map([ [ 'RING-A03', { mac: 'RING-A03', nowSoh: 60, monthSoh: 58, trmonthSoh: 52, riskScore: 40, riskLevel: 'high', status: '危险', modelName: 'XGBoost', cyclesUsed: 6, updatedAt: '2026-05-11T00:00:00.000Z', }, ], ]), ) const predicted = snapshot.devices.find((device) => device.id === 'RING-A03') expect(predicted?.soh).toBe(60) expect(predicted?.soh30d).toBe(58) expect(predicted?.soh90d).toBe(52) expect(predicted?.status).toBe('预警') expect(predicted?.firmware).toBe('XGBoost') }) })