Files
boss/tests/thread-status-route.test.ts
2026-04-04 11:39:06 +08:00

122 lines
4.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 getThreadStatusRoute: (typeof import("../src/app/api/v1/projects/[projectId]/thread-status/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 = "";
async function setup() {
if (runtimeRoot) return;
runtimeRoot = await mkdtemp(path.join(os.tmpdir(), "boss-thread-status-route-"));
process.env.BOSS_RUNTIME_ROOT = runtimeRoot;
process.env.BOSS_STATE_FILE = path.join(runtimeRoot, "boss-state.json");
const [routeModule, dataModule, authModule] = await Promise.all([
import("../src/app/api/v1/projects/[projectId]/thread-status/route.ts"),
import("../src/lib/boss-data.ts"),
import("../src/lib/boss-auth.ts"),
]);
getThreadStatusRoute = routeModule.GET;
createAuthSession = dataModule.createAuthSession;
readState = dataModule.readState;
writeState = dataModule.writeState;
AUTH_SESSION_COOKIE = authModule.AUTH_SESSION_COOKIE;
}
test.after(async () => {
if (runtimeRoot) {
await rm(runtimeRoot, { recursive: true, force: true });
}
});
async function createAuthedRequest(url: string) {
const session = await createAuthSession({
account: "17600003315",
role: "highest_admin",
displayName: "Boss 超级管理员",
loginMethod: "password",
});
return new NextRequest(url, {
method: "GET",
headers: {
cookie: `${AUTH_SESSION_COOKIE}=${session.sessionToken}`,
},
});
}
test("GET thread-status returns current document and recent progress events", async () => {
await setup();
const state = await readState();
const project = state.projects.find((item) => item.id === "master-agent");
assert.ok(project, "expected seeded master-agent project");
state.threadStatusDocuments = [
{
documentId: "doc-1",
projectId: "master-agent",
threadId: "thread-master-agent",
threadDisplayName: "主 Agent",
folderName: "Master",
deviceId: "mac-studio",
projectGoal: "完成线程状态同步",
currentPhase: "增量同步",
currentProgress: "已经记录最近进展事件",
technicalArchitecture: "Next.js API + Android 原生客户端",
currentBlockers: "",
recommendedNextStep: "继续补 Android 只读页",
keyFiles: ["src/app/api/v1/projects/[projectId]/thread-status/route.ts"],
keyCommands: ["npx --yes tsx --test tests/thread-status-route.test.ts"],
updatedAt: "2026-04-04T18:00:00+08:00",
sourceTaskId: "task-1",
sourceKind: "incremental_sync",
},
];
state.threadProgressEvents = Array.from({ length: 7 }, (_, index) => ({
eventId: `event-${index}`,
projectId: "master-agent",
threadId: "thread-master-agent",
threadDisplayName: "主 Agent",
deviceId: "mac-studio",
eventType: "progress_updated",
summary: `进展 ${index}`,
phase: "增量同步",
blockerDelta: "",
nextStepDelta: "",
createdAt: `2026-04-04T18:0${index}:00+08:00`,
sourceTaskId: `task-${index}`,
}));
await writeState(state);
const response = await getThreadStatusRoute(
await createAuthedRequest("http://127.0.0.1:3000/api/v1/projects/master-agent/thread-status"),
{ params: Promise.resolve({ projectId: "master-agent" }) },
);
assert.equal(response.status, 200);
const body = (await response.json()) as {
ok: boolean;
projectId: string;
threadStatusDocument: { documentId: string; threadDisplayName: string; projectGoal: string } | null;
recentProgressEvents: Array<{ eventId: string; summary: string }>;
};
assert.equal(body.ok, true);
assert.equal(body.projectId, "master-agent");
assert.equal(body.threadStatusDocument?.documentId, "doc-1");
assert.equal(body.threadStatusDocument?.threadDisplayName, "主 Agent");
assert.equal(body.threadStatusDocument?.projectGoal, "完成线程状态同步");
assert.equal(body.recentProgressEvents.length, 5);
assert.equal(body.recentProgressEvents[0]?.eventId, "event-6");
assert.equal(body.recentProgressEvents[4]?.eventId, "event-2");
});