refactor: use backend chat sessions

This commit is contained in:
2026-06-04 15:02:27 +08:00
parent 20ca410e0a
commit e60e1f6453
9 changed files with 91 additions and 178 deletions
+3 -31
View File
@@ -1,5 +1,5 @@
import {
loadActiveChatState,
createEmptyChatState,
saveActiveChatState,
} from "./chatStorage";
@@ -11,17 +11,13 @@ jest.mock("@/lib/apiFetch", () => ({
describe("chatStorage backend-only persistence", () => {
beforeEach(() => {
window.localStorage.clear();
apiFetch.mockReset();
});
it("starts from an empty conversation instead of restoring a stored active id", async () => {
window.localStorage.setItem("tjwater_agent_active_session_id_v2", "chat-active-1");
const loaded = await loadActiveChatState();
it("creates an empty initial conversation state without backend calls", () => {
const loaded = createEmptyChatState();
expect(loaded).toMatchObject({
storageSessionId: undefined,
title: undefined,
messages: [],
sessionId: undefined,
@@ -30,24 +26,6 @@ describe("chatStorage backend-only persistence", () => {
expect(apiFetch).not.toHaveBeenCalled();
});
it("starts from an empty conversation when a project has a stored active id", async () => {
window.localStorage.setItem(
"tjwater_agent_active_session_id_v2:project-a",
"chat-project-a",
);
window.localStorage.setItem(
"tjwater_agent_active_session_id_v2:project-b",
"chat-project-b",
);
const loaded = await loadActiveChatState("project-b");
expect(loaded.storageSessionId).toBeUndefined();
expect(loaded.title).toBeUndefined();
expect(loaded.messages).toEqual([]);
expect(apiFetch).not.toHaveBeenCalled();
});
it("creates a backend conversation when saving the first non-empty state", async () => {
apiFetch.mockImplementation(async (url: string, init?: RequestInit) => {
if (url.endsWith("/api/v1/agent/chat/session")) {
@@ -75,7 +53,6 @@ describe("chatStorage backend-only persistence", () => {
const savedSessionId = await saveActiveChatState(
{
storageSessionId: undefined,
title: "新对话",
isTitleManuallyEdited: false,
messages: [
@@ -89,13 +66,8 @@ describe("chatStorage backend-only persistence", () => {
sessionId: undefined,
branchGroups: [],
},
"project-a",
);
expect(savedSessionId).toBe("chat-new-1");
expect(
window.localStorage.getItem("tjwater_agent_active_session_id_v2:project-a"),
).toBeNull();
});
});