refactor: keep imported project understanding sync automatic

This commit is contained in:
kris
2026-04-04 08:43:53 +08:00
parent 432cf97541
commit cf57b5058f
9 changed files with 25 additions and 502 deletions

View File

@@ -1,7 +1,6 @@
package com.hyzq.boss;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
import android.content.Context;
import android.content.Intent;
@@ -9,8 +8,6 @@ import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.Nullable;
import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.Test;
@@ -18,14 +15,13 @@ import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowToast;
import org.robolectric.util.ReflectionHelpers;
@RunWith(RobolectricTestRunner.class)
@Config(sdk = 34)
public class DeviceDetailActivityTest {
@Test
public void renderDeviceShowsSyncProjectUnderstandingEntry() {
public void renderDeviceDoesNotShowManualProjectUnderstandingEntry() {
TestDeviceDetailActivity activity = Robolectric
.buildActivity(
TestDeviceDetailActivity.class,
@@ -37,30 +33,8 @@ public class DeviceDetailActivityTest {
.get();
View content = activity.findViewById(R.id.screen_content);
assertTrue(viewTreeContainsText(content, "同步项目理解"));
assertTrue(viewTreeContainsText(content, "让主 Agent 主动询问这台设备上的活跃项目目标、进度和架构"));
}
@Test
public void tappingSyncProjectUnderstandingCallsApiAndShowsQueuedCount() {
TestDeviceDetailActivity activity = Robolectric
.buildActivity(
TestDeviceDetailActivity.class,
new Intent()
.putExtra(DeviceDetailActivity.EXTRA_DEVICE_ID, "device-1")
.putExtra(DeviceDetailActivity.EXTRA_DEVICE_NAME, "Mac Studio")
)
.setup()
.get();
View syncLabel = findViewWithText(activity.findViewById(R.id.screen_content), "同步项目理解");
syncLabel.getParent().getParent();
View clickable = findClickableAncestor(syncLabel);
clickable.performClick();
org.robolectric.Shadows.shadowOf(activity.getMainLooper()).idle();
assertEquals(1, activity.fakeClient.syncCalls);
assertEquals("主 Agent 已开始同步 2 个项目理解。", ShadowToast.getTextOfLatestToast());
assertFalse(viewTreeContainsText(content, "同步项目理解"));
assertFalse(viewTreeContainsText(content, "让主 Agent 主动询问这台设备上的活跃项目目标、进度和架构"));
}
private static boolean viewTreeContainsText(View root, String expectedText) {
@@ -82,47 +56,10 @@ public class DeviceDetailActivityTest {
return false;
}
@Nullable
private static View findViewWithText(View root, String expectedText) {
if (root instanceof TextView) {
CharSequence text = ((TextView) root).getText();
if (text != null && text.toString().contains(expectedText)) {
return root;
}
}
if (!(root instanceof ViewGroup)) {
return null;
}
ViewGroup group = (ViewGroup) root;
for (int index = 0; index < group.getChildCount(); index += 1) {
View match = findViewWithText(group.getChildAt(index), expectedText);
if (match != null) {
return match;
}
}
return null;
}
private static View findClickableAncestor(View view) {
View current = view;
while (current != null && !current.isClickable()) {
if (!(current.getParent() instanceof View)) {
break;
}
current = (View) current.getParent();
}
return current == null ? view : current;
}
public static class TestDeviceDetailActivity extends DeviceDetailActivity {
FakeBossApiClient fakeClient;
@Override
protected void reload() {
if (fakeClient == null) {
fakeClient = new FakeBossApiClient(this);
}
this.apiClient = fakeClient;
this.apiClient = new BossApiClient(getSharedPreferences("test-boss-api", Context.MODE_PRIVATE), "https://boss.hyzq.net");
try {
ReflectionHelpers.callInstanceMethod(
this,
@@ -150,29 +87,4 @@ public class DeviceDetailActivityTest {
.put("note", "测试设备")));
}
}
private static class FakeBossApiClient extends BossApiClient {
int syncCalls = 0;
FakeBossApiClient(DeviceDetailActivity activity) {
super(activity.getSharedPreferences("test-boss-api", Context.MODE_PRIVATE), "https://boss.hyzq.net");
}
@Override
public ApiResponse syncDeviceProjectUnderstanding(String deviceId) {
syncCalls += 1;
try {
return new ApiResponse(
200,
new JSONObject()
.put("ok", true)
.put("queuedTasks", new JSONArray()
.put(new JSONObject().put("projectId", "project-1"))
.put(new JSONObject().put("projectId", "project-2")))
);
} catch (Exception error) {
throw new RuntimeException(error);
}
}
}
}