修复会话记录可能存储两次的bug;更改会话行为,默认进入新对话
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
import {
|
||||
createEmptyChatSession,
|
||||
loadActiveChatState,
|
||||
saveActiveChatState,
|
||||
} from "./chatStorage";
|
||||
@@ -16,33 +15,22 @@ describe("chatStorage backend-only persistence", () => {
|
||||
apiFetch.mockReset();
|
||||
});
|
||||
|
||||
it("loads the active remote session when localStorage has an active id", async () => {
|
||||
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");
|
||||
|
||||
apiFetch.mockImplementation(async (url: string) => {
|
||||
if (url.endsWith("/api/v1/agent/chat/session/chat-active-1")) {
|
||||
return {
|
||||
ok: true,
|
||||
json: async () => ({
|
||||
id: "chat-active-1",
|
||||
title: "已存在会话",
|
||||
is_title_manually_edited: false,
|
||||
session_id: "chat-active-1",
|
||||
messages: [],
|
||||
branch_groups: [],
|
||||
}),
|
||||
} as Response;
|
||||
}
|
||||
throw new Error(`Unexpected request ${url}`);
|
||||
});
|
||||
|
||||
const loaded = await loadActiveChatState();
|
||||
|
||||
expect(loaded.storageSessionId).toBe("chat-active-1");
|
||||
expect(loaded.title).toBe("已存在会话");
|
||||
expect(loaded).toMatchObject({
|
||||
storageSessionId: undefined,
|
||||
title: undefined,
|
||||
messages: [],
|
||||
sessionId: undefined,
|
||||
branchGroups: [],
|
||||
});
|
||||
expect(apiFetch).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("loads the active remote session from the current project's storage key", async () => {
|
||||
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",
|
||||
@@ -52,27 +40,12 @@ describe("chatStorage backend-only persistence", () => {
|
||||
"chat-project-b",
|
||||
);
|
||||
|
||||
apiFetch.mockImplementation(async (url: string) => {
|
||||
if (url.endsWith("/api/v1/agent/chat/session/chat-project-b")) {
|
||||
return {
|
||||
ok: true,
|
||||
json: async () => ({
|
||||
id: "chat-project-b",
|
||||
title: "项目 B 会话",
|
||||
is_title_manually_edited: false,
|
||||
session_id: "chat-project-b",
|
||||
messages: [],
|
||||
branch_groups: [],
|
||||
}),
|
||||
} as Response;
|
||||
}
|
||||
throw new Error(`Unexpected request ${url}`);
|
||||
});
|
||||
|
||||
const loaded = await loadActiveChatState("project-b");
|
||||
|
||||
expect(loaded.storageSessionId).toBe("chat-project-b");
|
||||
expect(loaded.title).toBe("项目 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 () => {
|
||||
@@ -122,14 +95,7 @@ describe("chatStorage backend-only persistence", () => {
|
||||
expect(savedSessionId).toBe("chat-new-1");
|
||||
expect(
|
||||
window.localStorage.getItem("tjwater_agent_active_session_id_v2:project-a"),
|
||||
).toBe("chat-new-1");
|
||||
).toBeNull();
|
||||
});
|
||||
|
||||
it("does not persist a blank new session before there is chat content", async () => {
|
||||
const session = await createEmptyChatSession();
|
||||
|
||||
expect(session.storageSessionId).toBeUndefined();
|
||||
expect(session.title).toBe("新对话");
|
||||
expect(apiFetch).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user