Improve chat title generation: refine model selection, ensure title consistency, and add comprehensive tests
This commit is contained in:
@@ -138,6 +138,46 @@ test("CSP restricts scripts, styles, and fonts to local assets", async t => {
|
||||
assert.doesNotMatch(policy, /unsafe-inline|cdnjs\.cloudflare\.com|fonts\.googleapis\.com|fonts\.gstatic\.com/);
|
||||
});
|
||||
|
||||
test("first chat message receives a generated title after the reply is saved", async t => {
|
||||
const server = await startTestServer();
|
||||
t.after(() => server.close());
|
||||
const jar = new CookieJar();
|
||||
await request(server.baseUrl, "/register", {method: "POST", jar, body: {username: "title-owner", password: "password123"}});
|
||||
const chat = (await request(server.baseUrl, "/chats", {method: "POST", jar, body: {title: "New chat"}})).json.chat;
|
||||
const repo = server.app.locals.repositories;
|
||||
const message = "Plan a weekend trip to Lucerne";
|
||||
repo.insertChatMessage(chat.id, "user", message);
|
||||
repo.insertChatMessage(chat.id, "bot", "Let's plan it.");
|
||||
const usedModels = [];
|
||||
t.mock.method(server.app.locals.modelService, "generateReply", async model => {
|
||||
usedModels.push(model);
|
||||
return JSON.stringify({title: "Weekend in Lucerne"});
|
||||
});
|
||||
|
||||
const titled = await request(server.baseUrl, `/chats/${chat.id}/title`, {
|
||||
method: "POST", jar, body: {message, model: "chosen-chat-model"}
|
||||
});
|
||||
assert.equal(titled.status, 200);
|
||||
assert.equal(titled.json.title, "Weekend in Lucerne");
|
||||
assert.equal(repo.getChat(chat.id, "title-owner").title, "Weekend in Lucerne");
|
||||
assert.deepEqual(usedModels, ["chosen-chat-model"]);
|
||||
|
||||
const another = (await request(server.baseUrl, "/chats", {method: "POST", jar, body: {title: "New chat"}})).json.chat;
|
||||
repo.insertChatMessage(another.id, "user", message);
|
||||
const mismatch = await request(server.baseUrl, `/chats/${another.id}/title`, {
|
||||
method: "POST", jar, body: {message: "Unrelated prompt", model: "chosen-chat-model"}
|
||||
});
|
||||
assert.equal(mismatch.json.title, "New chat");
|
||||
assert.equal(usedModels.length, 1);
|
||||
|
||||
server.app.locals.modelService.generateReply.mock.mockImplementation(async () => { throw new Error("model unavailable"); });
|
||||
const fallback = await request(server.baseUrl, `/chats/${another.id}/title`, {
|
||||
method: "POST", jar, body: {message, model: "chosen-chat-model"}
|
||||
});
|
||||
assert.equal(fallback.status, 200);
|
||||
assert.equal(fallback.json.title, "Plan a weekend trip to Lucerne");
|
||||
});
|
||||
|
||||
test("personality API keeps prompts private, validates IDs, and enforces chat ownership", async t => {
|
||||
const server = await startTestServer();
|
||||
t.after(() => server.close());
|
||||
|
||||
Reference in New Issue
Block a user