refactor(chat): remove frontend state saves

This commit is contained in:
2026-06-10 19:29:45 +08:00
parent 9c0a7a2864
commit e2a6bb0e7d
6 changed files with 112 additions and 255 deletions
@@ -24,7 +24,6 @@ jest.mock("@/lib/chatStream", () => ({
const listChatSessions = jest.fn();
const deleteChatSession = jest.fn();
const saveActiveChatState = jest.fn();
const updateChatSessionTitle = jest.fn();
jest.mock("../chatStorage", () => ({
@@ -42,7 +41,6 @@ jest.mock("../chatStorage", () => ({
messages: [],
sessionId: "session-loaded",
})),
saveActiveChatState: (...args: unknown[]) => saveActiveChatState(...args),
updateChatSessionTitle: (...args: unknown[]) => updateChatSessionTitle(...args),
}));
@@ -50,7 +48,6 @@ describe("useAgentChatSession", () => {
beforeEach(() => {
listChatSessions.mockReset();
deleteChatSession.mockReset();
saveActiveChatState.mockReset();
updateChatSessionTitle.mockReset();
jest.mocked(abortAgentChat).mockReset();
jest.mocked(forkAgentChat).mockReset();
@@ -65,7 +62,6 @@ describe("useAgentChatSession", () => {
jest.mocked(resumeAgentChatStream).mockImplementation(async () => undefined);
jest.mocked(streamAgentChat).mockImplementation(async () => undefined);
deleteChatSession.mockImplementation(async () => undefined);
saveActiveChatState.mockImplementation(async (state) => state.sessionId);
updateChatSessionTitle.mockImplementation(async () => undefined);
});
@@ -190,7 +186,7 @@ describe("useAgentChatSession lifecycle and resume", () => {
);
});
it("persists a new conversation only after the stream is done", async () => {
it("does not autosave full messages after the stream is done", async () => {
listChatSessions.mockResolvedValue([]);
let emitStreamEvent: ((event: StreamEvent) => void) | undefined;
jest.mocked(streamAgentChat).mockImplementationOnce(async ({ onEvent }) => {
@@ -220,8 +216,6 @@ describe("useAgentChatSession lifecycle and resume", () => {
jest.advanceTimersByTime(200);
});
expect(saveActiveChatState).not.toHaveBeenCalled();
act(() => {
emitStreamEvent?.({
type: "token",
@@ -234,8 +228,6 @@ describe("useAgentChatSession lifecycle and resume", () => {
jest.advanceTimersByTime(200);
});
expect(saveActiveChatState).not.toHaveBeenCalled();
act(() => {
emitStreamEvent?.({
type: "done",
@@ -247,14 +239,11 @@ describe("useAgentChatSession lifecycle and resume", () => {
jest.advanceTimersByTime(200);
});
await waitFor(() => expect(saveActiveChatState).toHaveBeenCalledTimes(1));
expect(saveActiveChatState.mock.calls[0][0]).toMatchObject({
sessionId: "chat-stream-1",
messages: [
expect.objectContaining({ role: "user", content: "第一条消息" }),
expect.objectContaining({ role: "assistant", content: "收到" }),
],
});
expect(result.current.messages).toEqual([
expect.objectContaining({ role: "user", content: "第一条消息" }),
expect.objectContaining({ role: "assistant", content: "收到" }),
]);
expect(result.current.activeSessionId).toBe("chat-stream-1");
} finally {
jest.useRealTimers();
}