feat: create dispatch plans from group messages
This commit is contained in:
178
tests/group-message-dispatch-plan.test.ts
Normal file
178
tests/group-message-dispatch-plan.test.ts
Normal file
@@ -0,0 +1,178 @@
|
||||
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 POST: (typeof import("../src/app/api/v1/projects/[projectId]/messages/route"))["POST"];
|
||||
let createAuthSession: (typeof import("../src/lib/boss-data"))["createAuthSession"];
|
||||
let createProjectGroupChat: (typeof import("../src/lib/boss-data"))["createProjectGroupChat"];
|
||||
let readState: (typeof import("../src/lib/boss-data"))["readState"];
|
||||
let writeState: (typeof import("../src/lib/boss-data"))["writeState"];
|
||||
let AUTH_SESSION_COOKIE: string;
|
||||
|
||||
async function setup() {
|
||||
if (runtimeRoot) {
|
||||
return;
|
||||
}
|
||||
|
||||
runtimeRoot = await mkdtemp(path.join(os.tmpdir(), "boss-task3-"));
|
||||
process.env.BOSS_RUNTIME_ROOT = runtimeRoot;
|
||||
process.env.BOSS_STATE_FILE = path.join(runtimeRoot, "boss-state.json");
|
||||
|
||||
const [{ POST: routePost }, 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"),
|
||||
]);
|
||||
|
||||
POST = routePost;
|
||||
createAuthSession = data.createAuthSession;
|
||||
createProjectGroupChat = data.createProjectGroupChat;
|
||||
readState = data.readState;
|
||||
writeState = data.writeState;
|
||||
AUTH_SESSION_COOKIE = auth.AUTH_SESSION_COOKIE;
|
||||
}
|
||||
|
||||
test.after(async () => {
|
||||
if (runtimeRoot) {
|
||||
await rm(runtimeRoot, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
async function createAuthedRequest(projectId: string, body: { body: string; kind?: 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: "POST",
|
||||
headers: {
|
||||
"content-type": "application/json",
|
||||
cookie: `${AUTH_SESSION_COOKIE}=${session.sessionToken}`,
|
||||
},
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
}
|
||||
|
||||
async function ensureTwoSingleThreadProjects() {
|
||||
const state = await readState();
|
||||
const singles = state.projects.filter((project) => project.id !== "master-agent" && !project.isGroup);
|
||||
if (singles.length >= 2) {
|
||||
return singles;
|
||||
}
|
||||
|
||||
assert.ok(singles[0], "expected at least one seeded single-thread project");
|
||||
const seed = singles[0];
|
||||
const clonedProject = {
|
||||
...seed,
|
||||
id: "boss-console-clone",
|
||||
name: "Boss 移动控制台副线程",
|
||||
deviceIds: ["win-gpu-01"],
|
||||
updatedAt: "2026-03-30T10:00:00+08:00",
|
||||
lastMessageAt: "2026-03-30T10:00:00+08:00",
|
||||
preview: "副线程等待主 Agent 汇总阻塞点。",
|
||||
threadMeta: {
|
||||
...seed.threadMeta,
|
||||
projectId: "boss-console-clone",
|
||||
threadId: "thread-boss-ui-clone",
|
||||
threadDisplayName: "南区试产线回归",
|
||||
folderName: "阻塞梳理",
|
||||
updatedAt: "2026-03-30T10:00:00+08:00",
|
||||
codexThreadRef: "thread-boss-ui-clone",
|
||||
codexFolderRef: "boss-console-clone",
|
||||
},
|
||||
groupMembers: [],
|
||||
messages: [
|
||||
{
|
||||
id: "msg-boss-console-clone",
|
||||
sender: "device" as const,
|
||||
senderLabel: "Win GPU / Codex",
|
||||
body: "这里还在等待视觉链路复核。",
|
||||
sentAt: "2026-03-30T10:00:00+08:00",
|
||||
kind: "text" as const,
|
||||
},
|
||||
],
|
||||
goals: [],
|
||||
versions: [],
|
||||
};
|
||||
|
||||
await writeState({
|
||||
...state,
|
||||
projects: [...state.projects, clonedProject],
|
||||
});
|
||||
|
||||
const nextState = await readState();
|
||||
return nextState.projects.filter((project) => project.id !== "master-agent" && !project.isGroup);
|
||||
}
|
||||
|
||||
test("POST /api/v1/projects/[projectId]/messages returns a dispatch plan for group text messages", async () => {
|
||||
await setup();
|
||||
const memberProjects = await ensureTwoSingleThreadProjects();
|
||||
assert.ok(memberProjects.length >= 2, "expected seeded single-thread projects");
|
||||
|
||||
const groupProject = await createProjectGroupChat({
|
||||
sourceProjectId: memberProjects[0].id,
|
||||
memberProjectIds: [memberProjects[1].id],
|
||||
createdBy: "17600003315",
|
||||
});
|
||||
|
||||
const response = await POST(await createAuthedRequest(groupProject.id, { body: "请大家汇总今天的阻塞点" }), {
|
||||
params: Promise.resolve({ projectId: groupProject.id }),
|
||||
});
|
||||
assert.equal(response.status, 200);
|
||||
|
||||
const payload = (await response.json()) as {
|
||||
ok: boolean;
|
||||
message: { id: string; body: string };
|
||||
dispatchPlan: null | {
|
||||
groupProjectId: string;
|
||||
requestMessageId: string;
|
||||
status: string;
|
||||
targets: Array<{ projectId: string }>;
|
||||
summary: string;
|
||||
};
|
||||
collaborationGate: { isGroup: boolean };
|
||||
};
|
||||
|
||||
assert.equal(payload.ok, true);
|
||||
assert.equal(payload.message.body, "请大家汇总今天的阻塞点");
|
||||
assert.ok(payload.dispatchPlan, "expected dispatch plan in response");
|
||||
assert.equal(payload.dispatchPlan?.groupProjectId, groupProject.id);
|
||||
assert.equal(payload.dispatchPlan?.requestMessageId, payload.message.id);
|
||||
assert.equal(payload.dispatchPlan?.status, "pending_user_confirmation");
|
||||
assert.equal(payload.dispatchPlan?.targets.length, groupProject.groupMembers.length);
|
||||
assert.match(payload.dispatchPlan?.summary ?? "", /阻塞点/);
|
||||
assert.equal(payload.collaborationGate.isGroup, true);
|
||||
});
|
||||
|
||||
test("POST /api/v1/projects/[projectId]/messages keeps dispatchPlan null for single-thread projects", async () => {
|
||||
await setup();
|
||||
const state = await readState();
|
||||
const singleProject = state.projects.find(
|
||||
(project) => project.id !== "master-agent" && !project.isGroup,
|
||||
);
|
||||
assert.ok(singleProject, "expected a seeded single-thread project");
|
||||
|
||||
const response = await POST(await createAuthedRequest(singleProject.id, { body: "单线程消息" }), {
|
||||
params: Promise.resolve({ projectId: singleProject.id }),
|
||||
});
|
||||
assert.equal(response.status, 200);
|
||||
|
||||
const payload = (await response.json()) as {
|
||||
ok: boolean;
|
||||
message: { body: string };
|
||||
dispatchPlan: null;
|
||||
collaborationGate: { isGroup: boolean };
|
||||
};
|
||||
|
||||
assert.equal(payload.ok, true);
|
||||
assert.equal(payload.message.body, "单线程消息");
|
||||
assert.equal(payload.dispatchPlan, null);
|
||||
assert.equal(payload.collaborationGate.isGroup, false);
|
||||
});
|
||||
Reference in New Issue
Block a user