diff --git a/android/app/src/main/java/com/hyzq/boss/WechatSurfaceMapper.java b/android/app/src/main/java/com/hyzq/boss/WechatSurfaceMapper.java index 0ac3adf..da26239 100644 --- a/android/app/src/main/java/com/hyzq/boss/WechatSurfaceMapper.java +++ b/android/app/src/main/java/com/hyzq/boss/WechatSurfaceMapper.java @@ -1,5 +1,7 @@ package com.hyzq.boss; +import org.json.JSONObject; + import java.util.Arrays; import java.util.List; @@ -20,17 +22,22 @@ public final class WechatSurfaceMapper { private WechatSurfaceMapper() { } - public static ConversationRow toConversationRow( - String title, - String preview, - String timeLabel, - int unreadCount - ) { - return new ConversationRow(title, preview, timeLabel, unreadCount); + public static ConversationRow toConversationRow(JSONObject item) { + JSONObject source = item == null ? new JSONObject() : item; + return new ConversationRow( + source.optString("title", source.optString("projectTitle", "")), + source.optString("preview", ""), + source.optString("timeLabel", source.optString("latestReplyLabel", "")), + source.optInt("unreadCount", 0) + ); } - public static DeviceRow toDeviceRow(String title, boolean online, String account) { - return new DeviceRow(title, buildSubtitle(online, account)); + public static DeviceRow toDeviceRow(JSONObject item) { + JSONObject source = item == null ? new JSONObject() : item; + return new DeviceRow( + source.optString("title", source.optString("name", "")), + buildSubtitle(source) + ); } public static String[] rootMeMenuTitles() { @@ -41,12 +48,15 @@ public final class WechatSurfaceMapper { return PROJECT_QUICK_ACTIONS.toArray(new String[0]); } - private static String buildSubtitle(boolean online, String account) { - String status = online ? "在线" : "离线"; - if (account == null || account.isEmpty()) { + private static String buildSubtitle(JSONObject source) { + String status = source.optBoolean("online", "online".equals(source.optString("status", ""))) + ? "在线" + : "离线"; + String account = source.optString("account", ""); + if (account.isEmpty()) { return status; } - return status + " · 账号 " + account; + return status + " · " + account; } public static final class ConversationRow { diff --git a/android/app/src/test/java/com/hyzq/boss/WechatSurfaceMapperTest.java b/android/app/src/test/java/com/hyzq/boss/WechatSurfaceMapperTest.java index 0050699..70252c2 100644 --- a/android/app/src/test/java/com/hyzq/boss/WechatSurfaceMapperTest.java +++ b/android/app/src/test/java/com/hyzq/boss/WechatSurfaceMapperTest.java @@ -1,11 +1,7 @@ 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 org.json.JSONObject; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; @@ -13,93 +9,87 @@ 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 - ); + JSONObject item = new StubJSONObject() + .withString("projectTitle", "项目 A") + .withString("preview", "最近消息预览") + .withString("latestReplyLabel", "10:24") + .withInt("unreadCount", 3) + .withString("deviceNamesPreview", "Mac Studio") + .withBoolean("contextBudgetIndicator", true); - 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())) - ); + WechatSurfaceMapper.ConversationRow row = WechatSurfaceMapper.toConversationRow(item); + + assertEquals("项目 A", row.title); + assertEquals("最近消息预览", row.preview); + assertEquals("10:24", row.timeLabel); + assertEquals(3, row.unreadCount); } @Test public void toDeviceRow_keepsOnlySimpleSubtitle() throws Exception { - Object row = invokeStatic( - "toDeviceRow", - new Class[]{String.class, boolean.class, String.class}, - "Mac Studio", - true, - "17600003315" - ); + JSONObject item = new StubJSONObject() + .withString("name", "Mac Studio") + .withString("status", "online") + .withString("account", "17600003315") + .withInt("quota5h", 8) + .withInt("quota7d", 22); - assertEquals("Mac Studio", readField(row, "title")); - assertEquals("在线 · 账号 17600003315", readField(row, "subtitle")); - assertEquals( - Arrays.asList("title", "subtitle"), - Arrays.asList(readDeclaredFieldNames(row.getClass())) - ); + WechatSurfaceMapper.DeviceRow row = WechatSurfaceMapper.toDeviceRow(item); + + assertEquals("Mac Studio", row.title); + assertEquals("在线 · 17600003315", row.subtitle); } @Test public void rootMeMenuTitles_matchApprovedSimpleMenu() throws Exception { - Object titles = invokeStatic("rootMeMenuTitles", new Class[0]); assertArrayEquals( new String[]{"账号与安全", "AI 账号", "设置", "技能", "关于"}, - toStringArray(titles) + WechatSurfaceMapper.rootMeMenuTitles() ); } @Test public void projectQuickActions_keepOnlyGoalsAndVersions() throws Exception { - Object titles = invokeStatic("projectQuickActions", new Class[0]); assertArrayEquals( new String[]{"项目目标", "版本记录"}, - toStringArray(titles) + WechatSurfaceMapper.projectQuickActions() ); } - 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 final class StubJSONObject extends JSONObject { + private final java.util.Map values = new java.util.HashMap<>(); - private static Object readField(Object target, String fieldName) throws Exception { - Field field = target.getClass().getField(fieldName); - return field.get(target); - } + StubJSONObject withString(String key, String value) { + values.put(key, value); + return this; + } - 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(); + StubJSONObject withInt(String key, int value) { + values.put(key, value); + return this; } - return names; - } - private static String[] toStringArray(Object value) { - if (value instanceof String[]) { - return (String[]) value; + StubJSONObject withBoolean(String key, boolean value) { + values.put(key, value); + return this; } - 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; + + @Override + public String optString(String key, String defaultValue) { + Object value = values.get(key); + return value instanceof String ? (String) value : defaultValue; + } + + @Override + public int optInt(String key, int defaultValue) { + Object value = values.get(key); + return value instanceof Integer ? (Integer) value : defaultValue; + } + + @Override + public boolean optBoolean(String key, boolean defaultValue) { + Object value = values.get(key); + return value instanceof Boolean ? (Boolean) value : defaultValue; } - throw new IllegalStateException("Unexpected return type: " + value.getClass()); } }