test: freeze wechat surface contract

This commit is contained in:
kris
2026-03-27 01:49:01 +08:00
parent 785db90a7a
commit efcefd8a62
2 changed files with 180 additions and 0 deletions

View File

@@ -0,0 +1,75 @@
package com.hyzq.boss;
import java.util.Arrays;
import java.util.List;
public final class WechatSurfaceMapper {
private static final List<String> ROOT_ME_MENU_TITLES = Arrays.asList(
"账号与安全",
"AI 账号",
"设置",
"技能",
"关于"
);
private static final List<String> PROJECT_QUICK_ACTIONS = Arrays.asList(
"项目目标",
"版本记录"
);
private WechatSurfaceMapper() {
}
public static ConversationRow toConversationRow(
String title,
String preview,
String timeLabel,
int unreadCount
) {
return new ConversationRow(title, preview, timeLabel, unreadCount);
}
public static DeviceRow toDeviceRow(String title, boolean online, String account) {
return new DeviceRow(title, buildSubtitle(online, account));
}
public static String[] rootMeMenuTitles() {
return ROOT_ME_MENU_TITLES.toArray(new String[0]);
}
public static String[] projectQuickActions() {
return PROJECT_QUICK_ACTIONS.toArray(new String[0]);
}
private static String buildSubtitle(boolean online, String account) {
String status = online ? "在线" : "离线";
if (account == null || account.isEmpty()) {
return status;
}
return status + " · 账号 " + account;
}
public static final class ConversationRow {
public final String title;
public final String preview;
public final String timeLabel;
public final int unreadCount;
public ConversationRow(String title, String preview, String timeLabel, int unreadCount) {
this.title = title;
this.preview = preview;
this.timeLabel = timeLabel;
this.unreadCount = unreadCount;
}
}
public static final class DeviceRow {
public final String title;
public final String subtitle;
public DeviceRow(String title, String subtitle) {
this.title = title;
this.subtitle = subtitle;
}
}
}

View File

@@ -0,0 +1,105 @@
package com.hyzq.boss;
import org.junit.Test;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
public class WechatSurfaceMapperTest {
@Test
public void toConversationRow_keepsOnlyWechatFields() throws Exception {
Object row = invokeStatic(
"toConversationRow",
new Class<?>[]{String.class, String.class, String.class, int.class},
"项目 A",
"最近消息预览",
"10:24",
3
);
assertEquals("项目 A", readField(row, "title"));
assertEquals("最近消息预览", readField(row, "preview"));
assertEquals("10:24", readField(row, "timeLabel"));
assertEquals(3, readField(row, "unreadCount"));
assertEquals(
Arrays.asList("title", "preview", "timeLabel", "unreadCount"),
Arrays.asList(readDeclaredFieldNames(row.getClass()))
);
}
@Test
public void toDeviceRow_keepsOnlySimpleSubtitle() throws Exception {
Object row = invokeStatic(
"toDeviceRow",
new Class<?>[]{String.class, boolean.class, String.class},
"Mac Studio",
true,
"17600003315"
);
assertEquals("Mac Studio", readField(row, "title"));
assertEquals("在线 · 账号 17600003315", readField(row, "subtitle"));
assertEquals(
Arrays.asList("title", "subtitle"),
Arrays.asList(readDeclaredFieldNames(row.getClass()))
);
}
@Test
public void rootMeMenuTitles_matchApprovedSimpleMenu() throws Exception {
Object titles = invokeStatic("rootMeMenuTitles", new Class<?>[0]);
assertArrayEquals(
new String[]{"账号与安全", "AI 账号", "设置", "技能", "关于"},
toStringArray(titles)
);
}
@Test
public void projectQuickActions_keepOnlyGoalsAndVersions() throws Exception {
Object titles = invokeStatic("projectQuickActions", new Class<?>[0]);
assertArrayEquals(
new String[]{"项目目标", "版本记录"},
toStringArray(titles)
);
}
private static Object invokeStatic(String methodName, Class<?>[] parameterTypes, Object... args) throws Exception {
Class<?> mapperClass = Class.forName("com.hyzq.boss.WechatSurfaceMapper");
Method method = mapperClass.getDeclaredMethod(methodName, parameterTypes);
return method.invoke(null, args);
}
private static Object readField(Object target, String fieldName) throws Exception {
Field field = target.getClass().getField(fieldName);
return field.get(target);
}
private static String[] readDeclaredFieldNames(Class<?> type) {
Field[] fields = type.getDeclaredFields();
String[] names = new String[fields.length];
for (int i = 0; i < fields.length; i++) {
names[i] = fields[i].getName();
}
return names;
}
private static String[] toStringArray(Object value) {
if (value instanceof String[]) {
return (String[]) value;
}
if (value instanceof List) {
List<?> list = (List<?>) value;
String[] result = new String[list.size()];
for (int i = 0; i < list.size(); i++) {
result[i] = String.valueOf(list.get(i));
}
return result;
}
throw new IllegalStateException("Unexpected return type: " + value.getClass());
}
}