261 lines
8.3 KiB
TypeScript
261 lines
8.3 KiB
TypeScript
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 getProjectRoute: (typeof import("../src/app/api/v1/projects/[projectId]/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-detail-route-"));
|
|
process.env.BOSS_RUNTIME_ROOT = runtimeRoot;
|
|
process.env.BOSS_STATE_FILE = path.join(runtimeRoot, "boss-state.json");
|
|
|
|
const [projectRouteModule, data, auth] = await Promise.all([
|
|
import("../src/app/api/v1/projects/[projectId]/route.ts"),
|
|
import("../src/lib/boss-data.ts"),
|
|
import("../src/lib/boss-auth.ts"),
|
|
]);
|
|
|
|
getProjectRoute = projectRouteModule.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-project-detail"],
|
|
preview: "等待详情刷新。",
|
|
updatedAt: "2026-04-14T14:00:00+08:00",
|
|
lastMessageAt: "2026-04-14T14:00:00+08:00",
|
|
isGroup: false,
|
|
threadMeta: {
|
|
projectId,
|
|
threadId: "thread-project-detail",
|
|
threadDisplayName: "项目详情线程",
|
|
folderName: "Boss",
|
|
activityIconCount: 0,
|
|
updatedAt: "2026-04-14T14:00:00+08:00",
|
|
codexThreadRef: "thread-project-detail",
|
|
codexFolderRef: "boss",
|
|
},
|
|
groupMembers: [],
|
|
createdByAgent: true,
|
|
collaborationMode: "development" as const,
|
|
approvalState: "not_required" as const,
|
|
unreadCount: 0,
|
|
riskLevel: "low" as const,
|
|
messages: [
|
|
{
|
|
id: "project-detail-message-1",
|
|
sender: "assistant",
|
|
senderLabel: "Codex",
|
|
body: "项目详情页需要展示任务状态。",
|
|
kind: "text" as const,
|
|
sentAt: "2026-04-14T14:00: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}`, {
|
|
method: "GET",
|
|
headers: {
|
|
cookie: `${AUTH_SESSION_COOKIE}=${session.sessionToken}`,
|
|
},
|
|
});
|
|
}
|
|
|
|
test("GET /api/v1/projects/[projectId] includes lightweight conversation tasks and execution warnings", async () => {
|
|
await setup();
|
|
const state = await readState();
|
|
const project = buildSingleThreadProject("project-detail");
|
|
|
|
await writeState({
|
|
...state,
|
|
devices: state.devices.concat({
|
|
id: "device-project-detail",
|
|
name: "Mac Studio",
|
|
avatar: "M",
|
|
account: "17600003315",
|
|
source: "production",
|
|
status: "online",
|
|
projects: [project.id],
|
|
quota5h: 0,
|
|
quota7d: 0,
|
|
lastSeenAt: "2026-04-14T14:00:00+08:00",
|
|
note: "",
|
|
}),
|
|
projects: state.projects.concat(project),
|
|
masterAgentTasks: state.masterAgentTasks.concat(
|
|
{
|
|
taskId: "task-project-detail-1",
|
|
projectId: project.id,
|
|
taskType: "conversation_reply",
|
|
requestMessageId: "project-detail-message-1",
|
|
requestText: "项目详情页需要展示任务状态。",
|
|
executionPrompt: "请继续回复。",
|
|
requestedBy: "Boss 超级管理员",
|
|
requestedByAccount: "17600003315",
|
|
deviceId: "master-agent-hermes",
|
|
accountId: "hermes-runtime",
|
|
accountLabel: "Hermes Runtime",
|
|
targetProjectId: project.id,
|
|
targetThreadId: "thread-project-detail",
|
|
targetThreadDisplayName: "项目详情线程",
|
|
status: "completed",
|
|
requestedAt: "2026-04-14T14:00:01+08:00",
|
|
completedAt: "2026-04-14T14:00:05+08:00",
|
|
replyBody: "Hermes 已完成回复。",
|
|
requestId: "req-project-detail-1",
|
|
sessionId: "session-project-detail-1",
|
|
},
|
|
{
|
|
taskId: "task-project-detail-hidden",
|
|
projectId: project.id,
|
|
taskType: "conversation_reply",
|
|
requestMessageId: "missing-message-id",
|
|
requestText: "不应暴露",
|
|
executionPrompt: "内部同步",
|
|
requestedBy: "Boss 超级管理员",
|
|
requestedByAccount: "17600003315",
|
|
deviceId: "master-agent-hermes",
|
|
accountId: "hermes-runtime",
|
|
accountLabel: "Hermes Runtime",
|
|
targetProjectId: project.id,
|
|
targetThreadId: "thread-project-detail",
|
|
targetThreadDisplayName: "项目详情线程",
|
|
status: "completed",
|
|
requestedAt: "2026-04-14T14:00:02+08:00",
|
|
completedAt: "2026-04-14T14:00:06+08:00",
|
|
replyBody: "内部同步回复。",
|
|
sessionId: "session-project-detail-hidden",
|
|
},
|
|
),
|
|
threadExecutionWarnings: state.threadExecutionWarnings.concat(
|
|
{
|
|
warningId: "warning-project-detail-1",
|
|
taskId: "task-project-detail-1",
|
|
requestMessageId: "project-detail-message-1",
|
|
projectId: project.id,
|
|
targetProjectId: project.id,
|
|
targetThreadId: "thread-project-detail",
|
|
sessionId: "session-project-detail-1",
|
|
requestId: "req-project-detail-1",
|
|
title: "上下文接近上限",
|
|
summary: "建议尽快压缩当前线程上下文。",
|
|
createdAt: "2026-04-14T14:00:06+08:00",
|
|
},
|
|
{
|
|
warningId: "warning-project-detail-other",
|
|
taskId: "task-other",
|
|
requestMessageId: "other-message",
|
|
projectId: "other-project",
|
|
targetProjectId: "other-project",
|
|
targetThreadId: "thread-other",
|
|
sessionId: "session-other",
|
|
requestId: "req-other",
|
|
title: "其他线程提醒",
|
|
summary: "不应进入当前项目详情。",
|
|
createdAt: "2026-04-14T14:00:07+08:00",
|
|
},
|
|
),
|
|
});
|
|
|
|
const response = await getProjectRoute(
|
|
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;
|
|
conversationTasks: Array<{
|
|
taskId: string;
|
|
requestMessageId: string;
|
|
status: string;
|
|
requestId?: string;
|
|
sessionId?: string;
|
|
targetProjectId?: string;
|
|
targetThreadId?: string;
|
|
}>;
|
|
executionWarnings: Array<{
|
|
warningId: string;
|
|
taskId: string;
|
|
requestMessageId: string;
|
|
requestId?: string;
|
|
sessionId?: string;
|
|
targetProjectId?: string;
|
|
targetThreadId?: string;
|
|
title: string;
|
|
summary: string;
|
|
createdAt: string;
|
|
}>;
|
|
};
|
|
|
|
assert.equal(payload.ok, true);
|
|
assert.deepEqual(payload.conversationTasks, [
|
|
{
|
|
taskId: "task-project-detail-1",
|
|
requestMessageId: "project-detail-message-1",
|
|
status: "completed",
|
|
requestId: "req-project-detail-1",
|
|
sessionId: "session-project-detail-1",
|
|
targetProjectId: project.id,
|
|
targetThreadId: "thread-project-detail",
|
|
},
|
|
]);
|
|
assert.deepEqual(payload.executionWarnings, [
|
|
{
|
|
warningId: "warning-project-detail-1",
|
|
taskId: "task-project-detail-1",
|
|
requestMessageId: "project-detail-message-1",
|
|
requestId: "req-project-detail-1",
|
|
sessionId: "session-project-detail-1",
|
|
targetProjectId: project.id,
|
|
targetThreadId: "thread-project-detail",
|
|
title: "上下文接近上限",
|
|
summary: "建议尽快压缩当前线程上下文。",
|
|
createdAt: "2026-04-14T14:00:06+08:00",
|
|
},
|
|
]);
|
|
});
|