package com.battery.business.service; import static org.junit.jupiter.api.Assertions.*; import static org.mockito.Mockito.*; import java.time.Instant; import java.util.*; import com.battery.business.domain.*; import com.battery.business.mapper.TTestMapper; import org.h2.jdbcx.JdbcDataSource; import org.junit.jupiter.api.Test; import org.springframework.jdbc.core.JdbcTemplate; class DashboardServiceTest { @Test void aggregatesAllTestsAtHongKongMidnightAndKeepsDeviceFactsSeparate() { var database = new JdbcDataSource(); database.setURL("jdbc:h2:mem:dashboard_" + UUID.randomUUID() + ";MODE=MySQL;DB_CLOSE_DELAY=-1"); var jdbc = new JdbcTemplate(database); jdbc.execute("create table t_battery_group(group_id bigint, del_flag int)"); jdbc.execute("insert into t_battery_group values(1,0),(2,1)"); jdbc.execute("create table t_test_realtime_record(record_id bigint,test_id bigint,test_battery_id bigint,step_no int,cycle_no int)"); jdbc.execute("create table t_test_battery(test_battery_id bigint,battery_no varchar(20),del_flag int)"); jdbc.execute("create table t_test_channel_binding(channel_id bigint,test_id bigint,test_battery_id bigint)"); jdbc.execute("create table t_detection_channel(channel_id bigint,cabinet_id bigint,del_flag int)"); jdbc.execute("create table t_test(test_id bigint,test_status varchar(20),del_flag int)"); jdbc.execute("insert into t_test values(1,'RUNNING',0),(2,'COMPLETED',0)"); jdbc.execute("insert into t_test_battery values(1,'电池1',0),(2,'电池2',0)"); jdbc.execute("insert into t_detection_channel values(1,7,0)"); jdbc.execute("insert into t_test_channel_binding values(1,1,1),(1,2,2)"); var tests = mock(ITTestService.class); var cabinets = mock(ITDetectionCabinetService.class); var mapper = mock(TTestMapper.class); var rows = new ArrayList(); for (int index = 0; index < 1001; index++) { var test = new TTest(); test.setTestId((long) index + 1); test.setTestStatus("RUNNING"); test.setReceptionStatus("PAUSED"); test.setCreateTime(Date.from(Instant.parse(index == 0 ? "2026-08-31T15:59:59Z" : "2026-08-31T16:00:00Z"))); rows.add(test); } when(tests.selectTestList(any())).thenReturn(rows); var cabinet = new TDetectionCabinet(); cabinet.setCabinetId(7L); cabinet.setChannelCount(8); cabinet.setOnlineStatus("0"); when(cabinets.selectDetectionCabinetList(any())).thenReturn(List.of(cabinet)); var warning = new TTestWarningException(); warning.setExceptionId(3L); warning.setTestId(1L); warning.setExceptionType("DEVICE_OFFLINE"); when(mapper.selectWarningExceptions(null, "OPEN")).thenReturn(List.of(warning)); var result = new DashboardService(tests, cabinets, mapper, database).read(Instant.parse("2026-09-01T00:00:00Z")); assertEquals(1001, result.summary().testCount()); assertEquals(1, result.summary().batteryGroupCount()); assertEquals(1000, result.summary().todayCount()); assertEquals(1000, result.summary().monthCount()); assertEquals(List.of(1000L), result.trends().get("month").values()); assertEquals(1, result.trends().get("year").values().get(7)); assertEquals(1000, result.trends().get("year").values().get(8)); assertEquals("在线", result.cabinets().get(0).onlineStatus()); assertEquals(1, result.cabinets().get(0).usedChannels()); assertEquals(1, result.cabinets().get(0).testCount()); assertEquals("RUNNING", result.tests().get(0).status()); assertEquals("PAUSED", result.tests().get(0).receptionStatus()); assertEquals("设备离线", result.warnings().get(0).item()); } }