Files
boss/tests/inter-thread-collaboration-route.test.ts
2026-05-31 03:25:30 +08:00

146 lines
4.7 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 postRoute: (typeof import("../src/app/api/v1/projects/[projectId]/thread-collaboration/route"))["POST"];
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 = "";
async function setup() {
if (runtimeRoot) return;
runtimeRoot = await mkdtemp(path.join(os.tmpdir(), "boss-inter-thread-collaboration-"));
process.env.BOSS_RUNTIME_ROOT = runtimeRoot;
process.env.BOSS_STATE_FILE = path.join(runtimeRoot, "boss-state.json");
const [route, data, auth] = await Promise.all([
import("../src/app/api/v1/projects/[projectId]/thread-collaboration/route.ts"),
import("../src/lib/boss-data.ts"),
import("../src/lib/boss-auth.ts"),
]);
postRoute = route.POST;
createAuthSession = data.createAuthSession;
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 });
}
});
test.beforeEach(async () => {
await setup();
await rm(runtimeRoot, { recursive: true, force: true });
});
async function createAuthedRequest(projectId: string, body: unknown) {
const session = await createAuthSession({
account: "krisolo",
role: "highest_admin",
displayName: "Boss 超级管理员",
loginMethod: "password",
});
return new NextRequest(`http://127.0.0.1:3000/api/v1/projects/${projectId}/thread-collaboration`, {
method: "POST",
headers: {
"content-type": "application/json",
cookie: `${AUTH_SESSION_COOKIE}=${session.sessionToken}`,
},
body: JSON.stringify(body),
});
}
function buildThreadProject(input: {
id: string;
name: string;
threadId: string;
codexThreadRef: string;
codexFolderRef: string;
}) {
return {
id: input.id,
name: input.name,
pinned: false,
systemPinned: false,
deviceIds: ["mac-studio"],
preview: "",
updatedAt: "2026-05-31T10:00:00+08:00",
lastMessageAt: "2026-05-31T10:00:00+08:00",
isGroup: false,
threadMeta: {
projectId: input.id,
threadId: input.threadId,
threadDisplayName: input.name,
folderName: input.codexFolderRef.split("/").pop() || input.codexFolderRef,
codexFolderRef: input.codexFolderRef,
codexThreadRef: input.codexThreadRef,
updatedAt: "2026-05-31T10:00:00+08:00",
},
groupMembers: [],
createdByAgent: false,
collaborationMode: "development" as const,
approvalState: "not_required" as const,
unreadCount: 0,
riskLevel: "low" as const,
messages: [],
goals: [],
versions: [],
};
}
test("POST /thread-collaboration queues a source-to-target Codex app-server collaboration task", async () => {
const state = await readState();
const source = buildThreadProject({
id: "source-project",
name: "源线程",
threadId: "source-thread",
codexThreadRef: "codex-source-thread",
codexFolderRef: "/Users/kris/code/source",
});
const target = buildThreadProject({
id: "target-project",
name: "目标线程",
threadId: "target-thread",
codexThreadRef: "codex-target-thread",
codexFolderRef: "/Users/kris/code/target",
});
state.projects = [
...state.projects.filter((project) => project.id === "master-agent"),
source,
target,
];
await writeState(state);
const response = await postRoute(
await createAuthedRequest(source.id, {
targetProjectId: target.id,
body: "把源线程的最新方案同步给目标线程,并让目标线程继续实现。",
}),
{ params: Promise.resolve({ projectId: source.id }) },
);
const payload = await response.json();
assert.equal(response.status, 200);
assert.equal(payload.ok, true);
assert.equal(payload.task.intentCategory, "thread_collaboration");
assert.equal(payload.task.sourceProjectId, source.id);
assert.equal(payload.task.sourceCodexThreadRef, "codex-source-thread");
assert.equal(payload.task.targetProjectId, target.id);
assert.equal(payload.task.targetCodexThreadRef, "codex-target-thread");
assert.equal(payload.task.targetCodexFolderRef, "/Users/kris/code/target");
const persisted = (await readState()).masterAgentTasks.find(
(task) => task.taskId === payload.task.taskId,
);
assert.equal(persisted?.deviceId, "mac-studio");
assert.equal(persisted?.status, "queued");
});