提交更新

This commit is contained in:
super
2026-06-08 18:31:55 +08:00
parent 98e0e54309
commit 967eedcad7
7 changed files with 394 additions and 98 deletions

View File

@@ -0,0 +1,129 @@
package com.nanri.aiimage.modules.pricetrack.service;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.nanri.aiimage.modules.pricetrack.mapper.PriceTrackLoopRunMapper;
import com.nanri.aiimage.modules.pricetrack.model.entity.PriceTrackLoopRunEntity;
import com.nanri.aiimage.modules.pricetrack.model.vo.PriceTrackMatchShopsVo;
import com.nanri.aiimage.modules.task.mapper.FileTaskMapper;
import com.nanri.aiimage.modules.task.model.entity.FileTaskEntity;
import com.nanri.aiimage.modules.ziniao.service.ZiniaoShopSwitchService;
import org.junit.jupiter.api.Test;
import java.util.List;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
class PriceTrackLoopRunServiceTest {
@Test
void syncLoopRunAfterChildTerminalUsesChildContextWhenLoopRowAlreadyDrifted() throws Exception {
ObjectMapper objectMapper = new ObjectMapper();
PriceTrackLoopRunMapper loopRunMapper = mock(PriceTrackLoopRunMapper.class);
FileTaskMapper fileTaskMapper = mock(FileTaskMapper.class);
ZiniaoShopSwitchService ziniaoShopSwitchService = mock(ZiniaoShopSwitchService.class);
PriceTrackLoopRunService service = new PriceTrackLoopRunService(
loopRunMapper,
fileTaskMapper,
objectMapper,
ziniaoShopSwitchService);
PriceTrackLoopRunEntity loop = new PriceTrackLoopRunEntity();
loop.setId(743L);
loop.setUserId(1L);
loop.setStatus("RUNNING");
loop.setExecutionMode("FINITE");
loop.setTargetRounds(2);
loop.setCurrentRound(2);
loop.setCurrentShopIndex(0);
loop.setActiveTaskId(13184L);
loop.setStopRequested(false);
loop.setShopsJson(objectMapper.writeValueAsString(List.of(shop("魏振峰"))));
FileTaskEntity task = new FileTaskEntity();
task.setId(13184L);
task.setUserId(1L);
task.setModuleType("PRICE_TRACK");
task.setStatus("SUCCESS");
task.setRequestJson(objectMapper.writeValueAsString(Map.of(
"loopRunId", 743,
"roundIndex", 1,
"shopIndex", 0
)));
when(loopRunMapper.selectList(any())).thenReturn(List.of(loop));
when(fileTaskMapper.selectById(13184L)).thenReturn(task);
service.syncLoopRunAfterChildTerminal(13184L);
assertEquals("RUNNING", loop.getStatus());
assertEquals(2, loop.getCurrentRound());
assertEquals(0, loop.getCurrentShopIndex());
assertNull(loop.getActiveTaskId());
assertNull(loop.getFinishedAt());
assertNull(loop.getErrorMessage());
verify(loopRunMapper).updateById(loop);
}
@Test
void syncLoopRunAfterChildTerminalIgnoresAlreadyAdvancedChildWithoutActiveTask() throws Exception {
ObjectMapper objectMapper = new ObjectMapper();
PriceTrackLoopRunMapper loopRunMapper = mock(PriceTrackLoopRunMapper.class);
FileTaskMapper fileTaskMapper = mock(FileTaskMapper.class);
ZiniaoShopSwitchService ziniaoShopSwitchService = mock(ZiniaoShopSwitchService.class);
PriceTrackLoopRunService service = new PriceTrackLoopRunService(
loopRunMapper,
fileTaskMapper,
objectMapper,
ziniaoShopSwitchService);
PriceTrackLoopRunEntity loop = new PriceTrackLoopRunEntity();
loop.setId(743L);
loop.setUserId(1L);
loop.setStatus("RUNNING");
loop.setExecutionMode("FINITE");
loop.setTargetRounds(2);
loop.setCurrentRound(2);
loop.setCurrentShopIndex(0);
loop.setActiveTaskId(null);
loop.setStopRequested(false);
loop.setShopsJson(objectMapper.writeValueAsString(List.of(shop("魏振峰"))));
FileTaskEntity task = new FileTaskEntity();
task.setId(13184L);
task.setUserId(1L);
task.setModuleType("PRICE_TRACK");
task.setStatus("SUCCESS");
task.setRequestJson(objectMapper.writeValueAsString(Map.of(
"loopRunId", 743,
"roundIndex", 1,
"shopIndex", 0
)));
when(loopRunMapper.selectList(any())).thenReturn(List.of(loop));
when(fileTaskMapper.selectById(13184L)).thenReturn(task);
service.syncLoopRunAfterChildTerminal(13184L);
assertEquals("RUNNING", loop.getStatus());
assertEquals(2, loop.getCurrentRound());
assertEquals(0, loop.getCurrentShopIndex());
assertNull(loop.getActiveTaskId());
assertFalse(Boolean.TRUE.equals(loop.getStopRequested()));
verify(loopRunMapper, never()).updateById(loop);
}
private PriceTrackMatchShopsVo.PriceTrackShopQueueItem shop(String name) {
PriceTrackMatchShopsVo.PriceTrackShopQueueItem item = new PriceTrackMatchShopsVo.PriceTrackShopQueueItem();
item.setShopName(name);
item.setMatched(true);
return item;
}
}