Files
jiang 4c47841483
Agent CI/CD / docker-image (push) Successful in 21s
Agent CI/CD / deploy-fallback-log (push) Has been skipped
优化标题生成功能
2026-05-22 14:20:27 +08:00

74 lines
2.3 KiB
TypeScript

import { describe, expect, it } from "bun:test";
import {
generateSessionTitle,
shouldGenerateSessionTitle,
} from "../../src/routes/chatSession.js";
import { type OpencodeRuntimeAdapter } from "../../src/runtime/opencode.js";
describe("shouldGenerateSessionTitle", () => {
it("allows auto-title generation for the first turn when the title was not edited", () => {
expect(
shouldGenerateSessionTitle({
recentTurnCount: 0,
isTitleManuallyEdited: false,
}),
).toBe(true);
});
it("blocks auto-title generation after the user edits the title manually", () => {
expect(
shouldGenerateSessionTitle({
recentTurnCount: 0,
isTitleManuallyEdited: true,
}),
).toBe(false);
});
it("only allows auto-title generation during the first two turns", () => {
expect(
shouldGenerateSessionTitle({
recentTurnCount: 1,
isTitleManuallyEdited: false,
}),
).toBe(true);
expect(
shouldGenerateSessionTitle({
recentTurnCount: 2,
isTitleManuallyEdited: false,
}),
).toBe(false);
});
});
describe("generateSessionTitle", () => {
it("uses the current user and assistant turn instead of reading wrapped runtime context", async () => {
let titlePrompt = "";
const runtime = {
createSession: async () => ({ id: "title-session" }),
prompt: async (_sessionId: string, prompt: string) => {
titlePrompt = prompt;
},
waitForSessionIdle: async () => undefined,
messages: async () => [
{
info: { role: "assistant" },
parts: [{ type: "text", text: "标题:泵站压力异常排查。" }],
},
],
abortSession: async () => undefined,
} as unknown as OpencodeRuntimeAdapter;
const title = await generateSessionTitle(runtime, {
sessionId: "chat-session",
latestUserMessage: "检查一下三号泵站最近压力波动的原因",
latestAssistantMessage: "三号泵站压力波动主要与夜间阀门开度变化有关。",
fallbackTitle: "新对话",
});
expect(title).toBe("泵站压力异常排查");
expect(titlePrompt).toContain("用户:检查一下三号泵站最近压力波动的原因");
expect(titlePrompt).toContain("助手:三号泵站压力波动主要与夜间阀门开度变化有关。");
});
});