Lighten Android chat realtime refreshes
This commit is contained in:
42
tests/android-chat-lightweight-realtime-refresh.test.ts
Normal file
42
tests/android-chat-lightweight-realtime-refresh.test.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { readFile } from "node:fs/promises";
|
||||
|
||||
async function readSource(path: string) {
|
||||
return readFile(new URL(path, import.meta.url), "utf8");
|
||||
}
|
||||
|
||||
test("BossApiClient exposes a lightweight project messages endpoint", async () => {
|
||||
const source = await readSource("../android/app/src/main/java/com/hyzq/boss/BossApiClient.java");
|
||||
|
||||
assert.match(
|
||||
source,
|
||||
/public ApiResponse getProjectMessages\(String projectId\) throws IOException, JSONException \{/,
|
||||
"expected Android client to expose a lightweight messages endpoint",
|
||||
);
|
||||
assert.match(
|
||||
source,
|
||||
/return requestWithRestore\("GET", "\/api\/v1\/projects\/" \+ encode\(projectId\) \+ "\/messages", null\);/,
|
||||
"expected lightweight message refreshes to reuse the dedicated messages route",
|
||||
);
|
||||
});
|
||||
|
||||
test("ProjectDetailActivity reserves full realtime reloads for non-message events", async () => {
|
||||
const source = await readSource("../android/app/src/main/java/com/hyzq/boss/ProjectDetailActivity.java");
|
||||
|
||||
assert.match(
|
||||
source,
|
||||
/private boolean realtimeReloadRequiresFullSnapshot;/,
|
||||
"expected chat page debounce state to remember whether a full snapshot is required",
|
||||
);
|
||||
assert.match(
|
||||
source,
|
||||
/runOnUiThread\(\(\) -> scheduleRealtimeReload\(!"project\.messages\.updated"\.equals\(event\.eventName\)\)\);/,
|
||||
"expected message-only realtime updates to avoid forcing a full snapshot",
|
||||
);
|
||||
assert.match(
|
||||
source,
|
||||
/void triggerRealtimeReload\(boolean requireFullSnapshot\) \{\s*if \(requireFullSnapshot\) \{\s*reload\(\);\s*return;\s*\}\s*reloadMessagesOnly\(\);\s*\}/s,
|
||||
"expected debounced realtime reloads to choose between full and lightweight refresh paths",
|
||||
);
|
||||
});
|
||||
57
tests/android-chat-realtime-status-indicator.test.ts
Normal file
57
tests/android-chat-realtime-status-indicator.test.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { readFile } from "node:fs/promises";
|
||||
|
||||
async function readSource(path: string) {
|
||||
return readFile(new URL(path, import.meta.url), "utf8");
|
||||
}
|
||||
|
||||
test("ProjectDetailActivity derives a subtitle suffix from realtime connection state", async () => {
|
||||
const source = await readSource("../android/app/src/main/java/com/hyzq/boss/ProjectDetailActivity.java");
|
||||
|
||||
assert.match(
|
||||
source,
|
||||
/private String realtimeStatusLabel\(\)/,
|
||||
"expected chat page to centralize realtime status wording",
|
||||
);
|
||||
assert.match(
|
||||
source,
|
||||
/return isRealtimeConnected\(\) \? "实时已连接" : "实时重连中";/,
|
||||
"expected chat page to distinguish healthy realtime from reconnecting state",
|
||||
);
|
||||
});
|
||||
|
||||
test("ProjectDetailActivity appends realtime status to the header subtitle", async () => {
|
||||
const source = await readSource("../android/app/src/main/java/com/hyzq/boss/ProjectDetailActivity.java");
|
||||
|
||||
assert.match(
|
||||
source,
|
||||
/updateProjectHeader\(title,\s*buildProjectSubtitle\([^)]*\)\);/,
|
||||
"expected project chat header to still flow through updateProjectHeader",
|
||||
);
|
||||
assert.match(
|
||||
source,
|
||||
/currentScreenSubtitle = withRealtimeStatus\(subtitle\);/,
|
||||
"expected current subtitle cache to include realtime status",
|
||||
);
|
||||
assert.match(
|
||||
source,
|
||||
/configureScreen\(title,\s*currentScreenSubtitle\);/,
|
||||
"expected rendered subtitle to include realtime status",
|
||||
);
|
||||
});
|
||||
|
||||
test("ProjectDetailActivity refreshes the subtitle when realtime connectivity changes", async () => {
|
||||
const source = await readSource("../android/app/src/main/java/com/hyzq/boss/ProjectDetailActivity.java");
|
||||
|
||||
assert.match(
|
||||
source,
|
||||
/private boolean lastKnownRealtimeConnected;/,
|
||||
"expected chat page to remember the last rendered realtime state",
|
||||
);
|
||||
assert.match(
|
||||
source,
|
||||
/syncRealtimeStatusIndicator\(\);/,
|
||||
"expected chat page to refresh subtitle state from lifecycle and realtime hooks",
|
||||
);
|
||||
});
|
||||
@@ -11,8 +11,8 @@ test("BossRealtimeClient tracks whether the SSE stream is currently connected",
|
||||
|
||||
assert.match(source, /private volatile boolean connected;/, "expected realtime client to cache connection state");
|
||||
assert.match(source, /boolean isConnected\(\)\s*\{\s*return connected;\s*\}/, "expected realtime client to expose connection state");
|
||||
assert.match(source, /connected = true;/, "expected realtime client to flip connected once the SSE stream is ready");
|
||||
assert.match(source, /connected = false;/, "expected realtime client to clear connected when the stream stops");
|
||||
assert.match(source, /setConnected\(true\);/, "expected realtime client to flip connected once the SSE stream is ready");
|
||||
assert.match(source, /setConnected\(false\);/, "expected realtime client to clear connected when the stream stops");
|
||||
});
|
||||
|
||||
test("MainActivity only performs conversation polling when realtime is unavailable", async () => {
|
||||
|
||||
@@ -25,8 +25,9 @@ test("ProjectDetailActivity debounces realtime chat reload bursts", async () =>
|
||||
|
||||
assert.match(source, /private static final long REALTIME_REFRESH_DEBOUNCE_MS = [\d_]+L;/);
|
||||
assert.match(source, /private boolean realtimeReloadScheduled(?: = false)?;/);
|
||||
assert.match(source, /private boolean realtimeReloadRequiresFullSnapshot;/);
|
||||
assert.match(source, /private final Runnable realtimeReloadRunnable = new Runnable\(\)/);
|
||||
assert.match(source, /scheduleRealtimeReload\(\)/);
|
||||
assert.match(source, /scheduleRealtimeReload\(boolean requireFullSnapshot\)/);
|
||||
assert.doesNotMatch(
|
||||
source,
|
||||
/runOnUiThread\(this::triggerRealtimeReload\)/,
|
||||
|
||||
170
tests/project-messages-route.test.ts
Normal file
170
tests/project-messages-route.test.ts
Normal file
@@ -0,0 +1,170 @@
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { mkdtemp, rm } from "node:fs/promises";
|
||||
import { NextRequest } from "next/server";
|
||||
|
||||
let runtimeRoot = "";
|
||||
let getMessagesRoute: (typeof import("../src/app/api/v1/projects/[projectId]/messages/route"))["GET"];
|
||||
let createAuthSession: (typeof import("../src/lib/boss-data"))["createAuthSession"];
|
||||
let readState: (typeof import("../src/lib/boss-data"))["readState"];
|
||||
let writeState: (typeof import("../src/lib/boss-data"))["writeState"];
|
||||
let AUTH_SESSION_COOKIE = "";
|
||||
let baseState: Awaited<ReturnType<typeof import("../src/lib/boss-data")["readState"]>>;
|
||||
|
||||
async function setup() {
|
||||
if (runtimeRoot) return;
|
||||
runtimeRoot = await mkdtemp(path.join(os.tmpdir(), "boss-project-messages-route-"));
|
||||
process.env.BOSS_RUNTIME_ROOT = runtimeRoot;
|
||||
process.env.BOSS_STATE_FILE = path.join(runtimeRoot, "boss-state.json");
|
||||
|
||||
const [messageModule, data, auth] = await Promise.all([
|
||||
import("../src/app/api/v1/projects/[projectId]/messages/route.ts"),
|
||||
import("../src/lib/boss-data.ts"),
|
||||
import("../src/lib/boss-auth.ts"),
|
||||
]);
|
||||
|
||||
getMessagesRoute = messageModule.GET;
|
||||
createAuthSession = data.createAuthSession;
|
||||
readState = data.readState;
|
||||
writeState = data.writeState;
|
||||
baseState = structuredClone(await readState());
|
||||
AUTH_SESSION_COOKIE = auth.AUTH_SESSION_COOKIE;
|
||||
}
|
||||
|
||||
test.after(async () => {
|
||||
if (runtimeRoot) {
|
||||
await rm(runtimeRoot, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test.beforeEach(async () => {
|
||||
await setup();
|
||||
await writeState(structuredClone(baseState));
|
||||
});
|
||||
|
||||
function buildSingleThreadProject(projectId: string) {
|
||||
return {
|
||||
id: projectId,
|
||||
name: "轻量消息线程",
|
||||
pinned: false,
|
||||
systemPinned: false,
|
||||
deviceIds: ["device-message-lite"],
|
||||
preview: "等待增量刷新。",
|
||||
updatedAt: "2026-04-10T16:20:00+08:00",
|
||||
lastMessageAt: "2026-04-10T16:20:00+08:00",
|
||||
isGroup: false,
|
||||
threadMeta: {
|
||||
projectId,
|
||||
threadId: "thread-message-lite",
|
||||
threadDisplayName: "轻量消息线程",
|
||||
folderName: "Boss",
|
||||
activityIconCount: 0,
|
||||
updatedAt: "2026-04-10T16:20:00+08:00",
|
||||
codexThreadRef: "thread-message-lite",
|
||||
codexFolderRef: "boss",
|
||||
},
|
||||
groupMembers: [],
|
||||
createdByAgent: true,
|
||||
collaborationMode: "development" as const,
|
||||
approvalState: "not_required" as const,
|
||||
unreadCount: 0,
|
||||
riskLevel: "low" as const,
|
||||
messages: [
|
||||
{
|
||||
id: "message-lite-1",
|
||||
sender: "assistant",
|
||||
senderLabel: "Codex",
|
||||
body: "新的消息已经到了。",
|
||||
kind: "text" as const,
|
||||
sentAt: "2026-04-10T16:20:00+08:00",
|
||||
},
|
||||
],
|
||||
goals: [],
|
||||
versions: [],
|
||||
};
|
||||
}
|
||||
|
||||
async function createAuthedRequest(projectId: string) {
|
||||
const session = await createAuthSession({
|
||||
account: "17600003315",
|
||||
role: "highest_admin",
|
||||
displayName: "Boss 超级管理员",
|
||||
loginMethod: "password",
|
||||
});
|
||||
|
||||
return new NextRequest(`http://127.0.0.1:3000/api/v1/projects/${projectId}/messages`, {
|
||||
method: "GET",
|
||||
headers: {
|
||||
cookie: `${AUTH_SESSION_COOKIE}=${session.sessionToken}`,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
test("GET /api/v1/projects/[projectId]/messages returns a lightweight chat payload", async () => {
|
||||
await setup();
|
||||
const state = await readState();
|
||||
const project = buildSingleThreadProject("message-lite");
|
||||
|
||||
await writeState({
|
||||
...state,
|
||||
devices: state.devices.concat({
|
||||
id: "device-message-lite",
|
||||
name: "Mac Studio",
|
||||
avatar: "M",
|
||||
account: "17600003315",
|
||||
source: "production",
|
||||
status: "online",
|
||||
projects: [project.id],
|
||||
quota5h: 0,
|
||||
quota7d: 0,
|
||||
lastSeenAt: "2026-04-10T16:20:00+08:00",
|
||||
note: "",
|
||||
}),
|
||||
projects: state.projects.concat(project),
|
||||
});
|
||||
|
||||
const response = await getMessagesRoute(
|
||||
await createAuthedRequest(project.id),
|
||||
{ params: Promise.resolve({ projectId: project.id }) },
|
||||
);
|
||||
|
||||
assert.equal(response.status, 200);
|
||||
assert.equal(response.headers.get("Cache-Control"), "private, no-store, max-age=0");
|
||||
|
||||
const payload = (await response.json()) as {
|
||||
ok: boolean;
|
||||
project: { id: string; messages: Array<{ id: string }> };
|
||||
devices: Array<{ id: string }>;
|
||||
activeThreadContexts?: unknown;
|
||||
recentAppLogs?: unknown;
|
||||
openFaults?: unknown;
|
||||
};
|
||||
|
||||
assert.equal(payload.ok, true);
|
||||
assert.equal(payload.project.id, project.id);
|
||||
assert.deepEqual(
|
||||
payload.project.messages.map((message) => message.id),
|
||||
["message-lite-1"],
|
||||
);
|
||||
assert.deepEqual(
|
||||
payload.devices.map((device) => device.id),
|
||||
["device-message-lite"],
|
||||
);
|
||||
assert.equal("activeThreadContexts" in payload, false);
|
||||
assert.equal("recentAppLogs" in payload, false);
|
||||
assert.equal("openFaults" in payload, false);
|
||||
});
|
||||
|
||||
test("GET /api/v1/projects/[projectId]/messages disables caching when unauthorized", async () => {
|
||||
await setup();
|
||||
|
||||
const response = await getMessagesRoute(
|
||||
new NextRequest("http://127.0.0.1:3000/api/v1/projects/message-lite/messages"),
|
||||
{ params: Promise.resolve({ projectId: "message-lite" }) },
|
||||
);
|
||||
|
||||
assert.equal(response.status, 401);
|
||||
assert.equal(response.headers.get("Cache-Control"), "private, no-store, max-age=0");
|
||||
});
|
||||
Reference in New Issue
Block a user