feat(client): 按页面保存首页布局位置与隐藏状态

家属端 pages 改为包含 slot、position、hidden 的对象数组,移除 hiddenPages。家属端需同步更新。老人端专用读取接口返回可见分页顺序。
This commit is contained in:
tongtong committed 2026-11-26 03:14:59 +08:00
1 parent 9a1ce783f2
commit d9b923afc5
10 files changed
+195 -102

No files matched your search

@@ -0,0 +1,89 @@
package com.youlai.boot.device;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.youlai.boot.device.model.DeviceHomeLayoutPage;
import com.youlai.boot.device.model.entity.SnDeviceHomeLayout;
import com.youlai.boot.device.model.vo.DeviceHomeLayoutVO;
import com.youlai.boot.device.service.impl.DeviceHomeLayoutServiceImpl;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.ArgumentMatchers;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
class DeviceHomeLayoutServiceTest {
private final DeviceHomeLayoutServiceImpl service = spy(new DeviceHomeLayoutServiceImpl());
private LambdaQueryChainWrapper<SnDeviceHomeLayout> query;
@BeforeEach
@SuppressWarnings("unchecked")
void setUp() {
query = mock(LambdaQueryChainWrapper.class);
when(query.eq(any(), eq("device-a"))).thenReturn(query);
when(query.orderByAsc(ArgumentMatchers.<SFunction<SnDeviceHomeLayout, ?>>any())).thenReturn(query);
doReturn(query).when(service).lambdaQuery();
}
@Test
void keepsHiddenPageInItsPositionAfterSavingAndLoading() {
when(query.list()).thenReturn(List.of());
doReturn(true).when(service).save(any(SnDeviceHomeLayout.class));
DeviceHomeLayoutPage home = new DeviceHomeLayoutPage("home", 0, false);
DeviceHomeLayoutPage contact = new DeviceHomeLayoutPage("contact", 1, true);
DeviceHomeLayoutPage settings = new DeviceHomeLayoutPage("settings", 2, false);
DeviceHomeLayoutVO saved = service.saveLayout("device-a", List.of(settings, contact, home), "contact");
ArgumentCaptor<SnDeviceHomeLayout> stored = ArgumentCaptor.forClass(SnDeviceHomeLayout.class);
verify(service).save(stored.capture());
when(query.list()).thenReturn(List.of(stored.getValue()));
assertEquals(List.of(home, contact, settings), saved.getPages());
DeviceHomeLayoutVO loaded = service.getBySn("device-a");
assertEquals(saved, loaded);
assertEquals("home", loaded.getDefaultPage());
assertEquals(List.of("home", "settings"), loaded.toDeviceView().pages());
}
@Test
void readsPreviouslySavedVisibleOrderAndAppendsMissingPagesAsHidden() {
SnDeviceHomeLayout stored = new SnDeviceHomeLayout();
stored.setPageOrder("settings,home");
stored.setDefaultPage("settings");
when(query.list()).thenReturn(List.of(stored));
DeviceHomeLayoutVO loaded = service.getBySn("device-a");
assertEquals(List.of(
new DeviceHomeLayoutPage("settings", 0, false),
new DeviceHomeLayoutPage("home", 1, false),
new DeviceHomeLayoutPage("contact", 2, true)
), loaded.getPages());
assertEquals("settings", loaded.getDefaultPage());
}
@Test
void rejectsDuplicatePositionsAndHiddenHome() {
DeviceHomeLayoutPage home = new DeviceHomeLayoutPage("home", 0, false);
DeviceHomeLayoutPage contact = new DeviceHomeLayoutPage("contact", 1, true);
DeviceHomeLayoutPage settings = new DeviceHomeLayoutPage("settings", 2, false);
assertThrows(IllegalArgumentException.class, () -> service.saveLayout(
"device-a", List.of(home, contact, new DeviceHomeLayoutPage("settings", 1, false)), "home"));
assertThrows(IllegalArgumentException.class, () -> service.saveLayout(
"device-a", List.of(new DeviceHomeLayoutPage("home", 0, true), contact, settings), "home"));
}
}