Initial upload to Gitea
This commit is contained in:
1 parent
78193254ad
commit
8478998cae
1485 files changed
+461025
-42348
No files matched your search
@@ -0,0 +1,130 @@
|
||||
"use strict";
|
||||
|
||||
const test = require("node:test");
|
||||
const assert = require("node:assert/strict");
|
||||
const { createDashboardAssistantHistory, deleteAccountAssistantHistory } = require("../services/dashboard-assistant-history");
|
||||
|
||||
function harness() {
|
||||
const users = new Map([[42, { id: 42, email: "same@example.test", suspended: 0 }]]);
|
||||
const chats = [{ id: 1, user_id: 42, user_email: "same@example.test", title: "private" }, { id: 2, user_id: null, user_email: "same@example.test", title: "legacy" }];
|
||||
const messages = [{ id: 1, chat_id: 1, role: "user", content: "old private message" }];
|
||||
const queries = [];
|
||||
let nextId = 3;
|
||||
const db = { query: async (sql, params = []) => {
|
||||
queries.push({ sql, params });
|
||||
if (/^(CREATE|ALTER)/.test(sql)) return [];
|
||||
if (sql.includes("FROM dashboard_assistant_chats c JOIN users")) return chats.filter((chat) => chat.user_id === params[0] && users.has(params[0]) && (params.length < 2 || chat.id === params[1]));
|
||||
if (sql.includes("FROM dashboard_assistant_messages m")) return messages.filter((message) => message.chat_id === params[0] && chats.some((chat) => chat.id === message.chat_id && chat.user_id === params[1]) && users.has(params[1]));
|
||||
throw new Error(`Unexpected query: ${sql}`);
|
||||
} };
|
||||
const connection = {
|
||||
beginTransaction: async () => {}, commit: async () => {}, rollback: async () => {}, release() {},
|
||||
execute: async (sql, params = []) => {
|
||||
queries.push({ sql, params });
|
||||
if (sql.startsWith("SELECT id, email, suspended FROM users")) return [[users.get(params[0])].filter(Boolean)];
|
||||
if (sql.startsWith("INSERT INTO dashboard_assistant_chats")) { const chat = { id: nextId++, user_id: params[0], user_email: params[1], title: params[2] }; chats.push(chat); return [{ insertId: chat.id }]; }
|
||||
if (sql.startsWith("SELECT id, title")) return [chats.filter((chat) => chat.id === params[0] && chat.user_id === params[1])];
|
||||
if (sql.startsWith("SELECT id FROM dashboard_assistant_chats WHERE id")) return [chats.filter((chat) => chat.id === params[0] && chat.user_id === params[1])];
|
||||
if (sql.startsWith("SELECT id FROM dashboard_assistant_chats WHERE user_id")) return [chats.filter((chat) => chat.user_id === params[0] || (chat.user_id == null && chat.user_email === params[1]))];
|
||||
if (sql.startsWith("DELETE FROM dashboard_assistant_messages")) {
|
||||
const ids = chats.filter((chat) => chat.user_id === params[0] || (chat.user_id == null && chat.user_email === params[1])).map((chat) => chat.id);
|
||||
for (let position = messages.length - 1; position >= 0; position -= 1) if (ids.includes(messages[position].chat_id)) messages.splice(position, 1);
|
||||
return [{ affectedRows: 1 }];
|
||||
}
|
||||
if (sql.startsWith("DELETE FROM dashboard_assistant_chats")) {
|
||||
for (let position = chats.length - 1; position >= 0; position -= 1) if (chats[position].user_id === params[0] || (chats[position].user_id == null && chats[position].user_email === params[1])) chats.splice(position, 1);
|
||||
return [{ affectedRows: 1 }];
|
||||
}
|
||||
if (sql.startsWith("INSERT INTO dashboard_assistant_messages")) { messages.push({ id: messages.length + 1, chat_id: params[0], role: params[1], content: params[2] }); return [{ affectedRows: 1 }]; }
|
||||
if (sql.startsWith("UPDATE dashboard_assistant_chats")) return [{ affectedRows: 1 }];
|
||||
throw new Error(`Unexpected execute: ${sql}`);
|
||||
}
|
||||
};
|
||||
db.pool = { getConnection: async () => connection };
|
||||
const history = createDashboardAssistantHistory({ db, findUserByEmail: async (email) => [...users.values()].find((user) => user.email === email) });
|
||||
return { users, chats, messages, queries, connection, history };
|
||||
}
|
||||
|
||||
test("a recreated email cannot read its predecessor's assigned or legacy conversations", async () => {
|
||||
const state = harness();
|
||||
state.users.delete(42);
|
||||
state.users.set(43, { id: 43, email: "same@example.test", suspended: 0 });
|
||||
await state.history.runForUser("same@example.test", async () => {
|
||||
assert.equal(await state.history.getChatById("same@example.test", 1), null);
|
||||
assert.deepEqual(await state.history.getChatMessages(1), []);
|
||||
const chat = await state.history.getPrimaryChat("same@example.test");
|
||||
assert.equal(chat.id, 3);
|
||||
}, 43);
|
||||
assert.equal(state.chats.find((chat) => chat.id === 3).user_id, 43);
|
||||
});
|
||||
|
||||
test("an in-flight request never switches to a replacement account after deletion", async () => {
|
||||
const state = harness();
|
||||
await state.history.runForUser("same@example.test", async () => {
|
||||
state.users.delete(42);
|
||||
state.users.set(43, { id: 43, email: "same@example.test", suspended: 0 });
|
||||
await assert.rejects(state.history.addMessage(1, { role: "assistant", content: "late private reply" }), { code: "ASSISTANT_ACCOUNT_CHANGED" });
|
||||
await assert.rejects(state.history.createFreshChat("same@example.test"), { code: "ASSISTANT_ACCOUNT_CHANGED" });
|
||||
}, 42);
|
||||
assert.equal(state.messages.length, 1);
|
||||
assert.equal(state.chats.length, 2);
|
||||
});
|
||||
|
||||
test("account deletion removes assigned history and unassigned legacy history without touching another owner", async () => {
|
||||
const state = harness();
|
||||
state.chats.push({ id: 9, user_id: 99, user_email: "other@example.test" });
|
||||
await deleteAccountAssistantHistory(state.connection, 42, "same@example.test");
|
||||
assert.deepEqual(state.chats.map((chat) => chat.id), [9]);
|
||||
assert.deepEqual(state.messages, []);
|
||||
});
|
||||
|
||||
test("an expected original account ID cannot resolve to its replacement", async () => {
|
||||
const state = harness();
|
||||
state.users.delete(42);
|
||||
state.users.set(43, { id: 43, email: "same@example.test", suspended: 0 });
|
||||
await assert.rejects(state.history.runForUser("same@example.test", () => state.history.getPrimaryChat("same@example.test"), 42), { code: "ASSISTANT_ACCOUNT_CHANGED" });
|
||||
assert.equal(state.queries.length, 0);
|
||||
});
|
||||
|
||||
test("a message committed before deletion is removed and later writes cannot recreate its history", async () => {
|
||||
const state = harness();
|
||||
await state.history.runForUser("same@example.test", () => state.history.addMessage(1, { role: "assistant", content: "reply before deletion" }), 42);
|
||||
assert.equal(state.messages.length, 2);
|
||||
await deleteAccountAssistantHistory(state.connection, 42, "same@example.test");
|
||||
state.users.delete(42);
|
||||
state.users.set(43, { id: 43, email: "same@example.test", suspended: 0 });
|
||||
assert.deepEqual(state.messages, []);
|
||||
await assert.rejects(state.history.runForUser("same@example.test", () => state.history.createFreshChat("same@example.test"), 42), { code: "ASSISTANT_ACCOUNT_CHANGED" });
|
||||
assert.deepEqual(state.chats, []);
|
||||
});
|
||||
|
||||
test("cleanup handles a legacy email-only schema but never hides real SQL errors", async () => {
|
||||
const statements = [];
|
||||
const connection = { execute: async (sql, params) => {
|
||||
if (sql.startsWith("SELECT") && sql.includes("user_id")) {
|
||||
const error = new Error("Unknown column 'user_id'");
|
||||
error.code = "ER_BAD_FIELD_ERROR";
|
||||
throw error;
|
||||
}
|
||||
statements.push({ sql, params });
|
||||
return [[]];
|
||||
} };
|
||||
await deleteAccountAssistantHistory(connection, 42, "same@example.test");
|
||||
assert.equal(statements.length, 3);
|
||||
assert(statements.every(({ sql, params }) => !sql.includes("user_id") && params[0] === "same@example.test"));
|
||||
await assert.rejects(deleteAccountAssistantHistory({ execute: async () => { throw new Error("database connection failed"); } }, 42, "same@example.test"), /database connection failed/);
|
||||
});
|
||||
|
||||
test("both panel and CLI deletion use the shared history cleanup inside their transaction", () => {
|
||||
const fs = require("node:fs");
|
||||
for (const [file, call] of [
|
||||
["../index.js", "await deleteAccountAssistantHistory(conn, userId, emailLower);"],
|
||||
["../scripts/create-user.js", "await deleteAccountAssistantHistory(conn, existing.id, email);"]
|
||||
]) {
|
||||
const source = fs.readFileSync(require.resolve(file), "utf8");
|
||||
const cleanupAt = source.indexOf(call);
|
||||
assert(cleanupAt > 0, file);
|
||||
assert(source.indexOf('DELETE FROM users WHERE id = ?', cleanupAt) > cleanupAt, file);
|
||||
assert(source.indexOf("await conn.commit()", cleanupAt) > cleanupAt, file);
|
||||
}
|
||||
});
|
||||
Reference in new issue
Block a user