Files
boss/android/app/src/test/java/com/hyzq/boss/ProjectDetailActivityRealtimeTest.java

342 lines
13 KiB
Java

package com.hyzq.boss;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.junit.Assert.assertTrue;
import android.content.Intent;
import android.os.Looper;
import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.Shadows;
import org.robolectric.annotation.Config;
import org.robolectric.util.ReflectionHelpers;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.function.BooleanSupplier;
@RunWith(RobolectricTestRunner.class)
@Config(sdk = 34)
public class ProjectDetailActivityRealtimeTest {
private static void flushRealtimeDebounce(ProjectDetailActivity activity) {
Shadows.shadowOf(activity.getMainLooper()).idleFor(400, TimeUnit.MILLISECONDS);
}
@Test
public void matchingProjectMessageEventTriggersReload() throws Exception {
Intent intent = new Intent()
.putExtra(ProjectDetailActivity.EXTRA_PROJECT_ID, "project-1")
.putExtra(ProjectDetailActivity.EXTRA_PROJECT_NAME, "北区试产线");
TestRealtimeProjectDetailActivity activity = Robolectric
.buildActivity(TestRealtimeProjectDetailActivity.class, intent)
.setup()
.resume()
.get();
ReflectionHelpers.callInstanceMethod(
activity,
"handleRealtimeEvent",
ReflectionHelpers.ClassParameter.from(
BossRealtimeEvent.class,
new BossRealtimeEvent("project.messages.updated", new JSONObject().put("projectId", "project-1"))
)
);
flushRealtimeDebounce(activity);
assertEquals(0, activity.reloadCount);
assertEquals(1, activity.loadCallCount);
assertEquals(1, activity.renderCount);
}
@Test
public void unrelatedProjectMessageEventDoesNotTriggerReload() throws Exception {
Intent intent = new Intent()
.putExtra(ProjectDetailActivity.EXTRA_PROJECT_ID, "project-1")
.putExtra(ProjectDetailActivity.EXTRA_PROJECT_NAME, "北区试产线");
TestRealtimeProjectDetailActivity activity = Robolectric
.buildActivity(TestRealtimeProjectDetailActivity.class, intent)
.setup()
.resume()
.get();
ReflectionHelpers.callInstanceMethod(
activity,
"handleRealtimeEvent",
ReflectionHelpers.ClassParameter.from(
BossRealtimeEvent.class,
new BossRealtimeEvent("project.messages.updated", new JSONObject().put("projectId", "project-2"))
)
);
flushRealtimeDebounce(activity);
assertEquals(0, activity.reloadCount);
}
@Test
public void masterAgentTaskEventDoesNotRefreshForDifferentProjectId() throws Exception {
Intent intent = new Intent()
.putExtra(ProjectDetailActivity.EXTRA_PROJECT_ID, "master-agent")
.putExtra(ProjectDetailActivity.EXTRA_PROJECT_NAME, "主 Agent");
TestRealtimeProjectDetailActivity activity = Robolectric
.buildActivity(TestRealtimeProjectDetailActivity.class, intent)
.setup()
.resume()
.get();
ReflectionHelpers.callInstanceMethod(
activity,
"handleRealtimeEvent",
ReflectionHelpers.ClassParameter.from(
BossRealtimeEvent.class,
new BossRealtimeEvent("master_agent.task.updated", new JSONObject().put("projectId", "project-2"))
)
);
flushRealtimeDebounce(activity);
assertEquals(0, activity.reloadCount);
}
@Test
public void distinctRealtimeEventsBackToBackStillReloadMatchingProject() throws Exception {
Intent intent = new Intent()
.putExtra(ProjectDetailActivity.EXTRA_PROJECT_ID, "project-1")
.putExtra(ProjectDetailActivity.EXTRA_PROJECT_NAME, "北区试产线");
TestRealtimeProjectDetailActivity activity = Robolectric
.buildActivity(TestRealtimeProjectDetailActivity.class, intent)
.setup()
.resume()
.get();
ReflectionHelpers.callInstanceMethod(
activity,
"handleRealtimeEvent",
ReflectionHelpers.ClassParameter.from(
BossRealtimeEvent.class,
new BossRealtimeEvent(
"conversation.updated",
new JSONObject().put("projectId", "project-1").put("at", "2026-04-05T10:00:00.000Z")
)
)
);
ReflectionHelpers.callInstanceMethod(
activity,
"handleRealtimeEvent",
ReflectionHelpers.ClassParameter.from(
BossRealtimeEvent.class,
new BossRealtimeEvent(
"project.messages.updated",
new JSONObject().put("projectId", "project-1").put("at", "2026-04-05T10:00:00.500Z")
)
)
);
flushRealtimeDebounce(activity);
assertEquals(1, activity.reloadCount);
assertEquals(1, activity.loadCallCount);
assertEquals(1, activity.renderCount);
}
@Test
public void matchingProjectContextRiskEventTriggersReload() throws Exception {
Intent intent = new Intent()
.putExtra(ProjectDetailActivity.EXTRA_PROJECT_ID, "project-1")
.putExtra(ProjectDetailActivity.EXTRA_PROJECT_NAME, "北区试产线");
TestRealtimeProjectDetailActivity activity = Robolectric
.buildActivity(TestRealtimeProjectDetailActivity.class, intent)
.setup()
.resume()
.get();
ReflectionHelpers.callInstanceMethod(
activity,
"handleRealtimeEvent",
ReflectionHelpers.ClassParameter.from(
BossRealtimeEvent.class,
new BossRealtimeEvent(
"project.context_risk.updated",
new JSONObject().put("projectId", "project-1").put("deviceId", "mac-studio")
)
)
);
flushRealtimeDebounce(activity);
assertEquals(1, activity.reloadCount);
assertEquals(1, activity.loadCallCount);
assertEquals(1, activity.renderCount);
}
@Test
public void duplicateRealtimeEventsWithDifferentAtAreDeduped() throws Exception {
Intent intent = new Intent()
.putExtra(ProjectDetailActivity.EXTRA_PROJECT_ID, "project-1")
.putExtra(ProjectDetailActivity.EXTRA_PROJECT_NAME, "北区试产线");
TestRealtimeProjectDetailActivity activity = Robolectric
.buildActivity(TestRealtimeProjectDetailActivity.class, intent)
.setup()
.resume()
.get();
ReflectionHelpers.callInstanceMethod(
activity,
"handleRealtimeEvent",
ReflectionHelpers.ClassParameter.from(
BossRealtimeEvent.class,
new BossRealtimeEvent(
"project.messages.updated",
new JSONObject().put("projectId", "project-1").put("at", "2026-04-05T10:00:00.000Z")
)
)
);
ReflectionHelpers.callInstanceMethod(
activity,
"handleRealtimeEvent",
ReflectionHelpers.ClassParameter.from(
BossRealtimeEvent.class,
new BossRealtimeEvent(
"project.messages.updated",
new JSONObject().put("projectId", "project-1").put("at", "2026-04-05T10:00:00.500Z")
)
)
);
flushRealtimeDebounce(activity);
assertEquals(0, activity.reloadCount);
assertEquals(1, activity.loadCallCount);
assertEquals(1, activity.renderCount);
}
@Test
public void burstRealtimeEventsWhileReloadingCoalesceIntoSingleFollowUpReload() throws Exception {
Intent intent = new Intent()
.putExtra(ProjectDetailActivity.EXTRA_PROJECT_ID, "project-1")
.putExtra(ProjectDetailActivity.EXTRA_PROJECT_NAME, "北区试产线");
TestRealtimeProjectDetailActivity activity = Robolectric
.buildActivity(TestRealtimeProjectDetailActivity.class, intent)
.setup()
.resume()
.get();
activity.blockFirstReload();
ReflectionHelpers.callInstanceMethod(
activity,
"handleRealtimeEvent",
ReflectionHelpers.ClassParameter.from(
BossRealtimeEvent.class,
new BossRealtimeEvent("project.messages.updated", new JSONObject().put("projectId", "project-1"))
)
);
flushRealtimeDebounce(activity);
assertTrue(activity.awaitFirstLoadStarted());
ReflectionHelpers.callInstanceMethod(
activity,
"handleRealtimeEvent",
ReflectionHelpers.ClassParameter.from(
BossRealtimeEvent.class,
new BossRealtimeEvent("conversation.updated", new JSONObject().put("projectId", "project-1"))
)
);
ReflectionHelpers.callInstanceMethod(
activity,
"handleRealtimeEvent",
ReflectionHelpers.ClassParameter.from(
BossRealtimeEvent.class,
new BossRealtimeEvent("master_agent.task.updated", new JSONObject().put("projectId", "project-1"))
)
);
flushRealtimeDebounce(activity);
assertEquals(1, activity.loadCallCount);
assertEquals(0, activity.renderCount);
activity.releaseFirstLoad();
waitFor(() -> activity.renderCount == 2 && activity.loadCallCount == 2);
assertEquals(2, activity.loadCallCount);
assertEquals(2, activity.renderCount);
}
private static void waitFor(BooleanSupplier condition) throws Exception {
long deadlineAt = System.currentTimeMillis() + 2_000L;
while (System.currentTimeMillis() < deadlineAt) {
Shadows.shadowOf(Looper.getMainLooper()).idle();
if (condition.getAsBoolean()) {
return;
}
Thread.sleep(20L);
}
fail("condition not met before timeout");
}
public static class TestRealtimeProjectDetailActivity extends ProjectDetailActivity {
int reloadCount;
volatile int loadCallCount;
volatile int renderCount;
private CountDownLatch firstLoadStarted;
private CountDownLatch releaseFirstLoad;
@Override
boolean shouldLoadOnCreate() {
return false;
}
void blockFirstReload() {
firstLoadStarted = new CountDownLatch(1);
releaseFirstLoad = new CountDownLatch(1);
}
boolean awaitFirstLoadStarted() throws InterruptedException {
return firstLoadStarted != null && firstLoadStarted.await(2, TimeUnit.SECONDS);
}
void releaseFirstLoad() {
if (releaseFirstLoad != null) {
releaseFirstLoad.countDown();
}
}
@Override
protected void reload() {
reloadCount += 1;
super.reload();
}
@Override
ProjectSnapshot loadProjectSnapshotForRefresh() throws Exception {
loadCallCount += 1;
if (loadCallCount == 1 && firstLoadStarted != null && releaseFirstLoad != null) {
firstLoadStarted.countDown();
releaseFirstLoad.await(2, TimeUnit.SECONDS);
}
return new ProjectSnapshot(
new JSONObject().put(
"project",
new JSONObject()
.put("name", "北区试产线")
.put("messages", new JSONArray())
),
null,
null
);
}
@Override
ProjectSnapshot loadProjectMessagesSnapshotForRefresh() throws Exception {
return loadProjectSnapshotForRefresh();
}
@Override
void renderLoadedProjectSnapshot(ProjectSnapshot snapshot) {
renderCount += 1;
setRefreshing(false);
}
}
}