fix(chat): 删除会话时保留后台聊天记录
- 会话列表和操作排除软删除记录,保留全部历史消息供后台查看 - 新增 018 数据库迁移及管理员删除状态字段 - 补齐删除后访问边界回归并同步迁移文档
This commit is contained in:
@@ -273,3 +273,91 @@ test("后台可创建的长账号也能通过 C 端登录入口", async (t) => {
|
||||
await app.close();
|
||||
}
|
||||
});
|
||||
|
||||
test("前台删除保留消息和后台可读性,隐藏列表并拒绝旧会话操作", async (t) => {
|
||||
let deleted = false;
|
||||
const messages = [
|
||||
{ id: "m1", role: "user", content: "保留的聊天", status: "complete" },
|
||||
];
|
||||
const writes: string[] = [];
|
||||
const query = async (q: { sql: string }, args: unknown[]) => {
|
||||
const sql = q.sql;
|
||||
if (sql.startsWith("SELECT * FROM dh_conversations")) {
|
||||
assert.match(sql, /account=\? AND deleted_at IS NULL/);
|
||||
return [
|
||||
!deleted && args[1] === "user:7" ? [{ id: "chat", settings: {} }] : [],
|
||||
[],
|
||||
];
|
||||
}
|
||||
if (sql.startsWith("UPDATE dh_conversations SET deleted_at")) {
|
||||
assert.deepEqual(args, ["chat", "user:7"]);
|
||||
deleted = true;
|
||||
writes.push(sql);
|
||||
return [{ affectedRows: 1 }, []];
|
||||
}
|
||||
if (sql.startsWith("SELECT id,character_id AS characterId")) {
|
||||
assert.match(sql, /deleted_at IS NULL/);
|
||||
return [deleted ? [] : [{ id: "chat" }], []];
|
||||
}
|
||||
if (sql.includes("WHERE c.id=? AND c.account=?")) {
|
||||
assert.ok(!sql.includes("deleted_at IS NULL"));
|
||||
return [[{ id: "chat", title: "历史会话", characterName: "角色" }], []];
|
||||
}
|
||||
if (sql.includes("COUNT(*)")) return [[{ total: messages.length }], []];
|
||||
if (sql.startsWith("SELECT id,role,content")) return [messages, []];
|
||||
if (sql.startsWith("INSERT INTO dh_audits"))
|
||||
return [{ affectedRows: 1 }, []];
|
||||
throw Error("不应执行的 SQL:" + sql);
|
||||
};
|
||||
t.mock.method(pool, "query", query);
|
||||
t.mock.method(pool, "getConnection", async () => ({
|
||||
query,
|
||||
beginTransaction: async () => {},
|
||||
commit: async () => {},
|
||||
rollback: async () => {},
|
||||
release: () => {},
|
||||
}));
|
||||
t.mock.method(authService, "getCurrentUser", async () => ({
|
||||
accountType: "SUPER_ADMIN",
|
||||
}));
|
||||
const app = createApp();
|
||||
try {
|
||||
await app.ready();
|
||||
const front = { authorization: `Bearer ${app.jwt.sign(payload)}` };
|
||||
const admin = {
|
||||
authorization: `Bearer ${app.jwt.sign({ sub: "super_admin:1", accountType: "SUPER_ADMIN", scene: "MANAGEMENT" })}`,
|
||||
};
|
||||
const remove = await app.inject({
|
||||
method: "DELETE",
|
||||
url: "/api/digital/conversations/chat",
|
||||
headers: front,
|
||||
});
|
||||
assert.equal(remove.statusCode, 200, remove.body);
|
||||
assert.equal(writes.length, 1);
|
||||
const list = await app.inject({
|
||||
method: "GET",
|
||||
url: "/api/digital/conversations",
|
||||
headers: front,
|
||||
});
|
||||
assert.equal(list.statusCode, 200);
|
||||
assert.deepEqual(list.json().data, []);
|
||||
for (const method of ["GET", "DELETE", "PATCH"] as const) {
|
||||
const result = await app.inject({
|
||||
method,
|
||||
url: "/api/digital/conversations/chat",
|
||||
headers: front,
|
||||
...(method === "PATCH" ? { payload: { title: "改名" } } : {}),
|
||||
});
|
||||
assert.equal(result.statusCode, 404, result.body);
|
||||
}
|
||||
const history = await app.inject({
|
||||
method: "GET",
|
||||
url: "/api/digital-admin/users/7/conversations/chat/messages",
|
||||
headers: admin,
|
||||
});
|
||||
assert.equal(history.statusCode, 200, history.body);
|
||||
assert.equal(history.json().data.items[0].content, "保留的聊天");
|
||||
} finally {
|
||||
await app.close();
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user