修复会话记录可能存储两次的bug;更改会话行为,默认进入新对话
This commit is contained in:
@@ -4,6 +4,7 @@ import { act, renderHook, waitFor } from "@testing-library/react";
|
||||
|
||||
import { useAgentChatSession } from "./useAgentChatSession";
|
||||
import { streamAgentChat } from "@/lib/chatStream";
|
||||
import type { StreamEvent } from "@/lib/chatStream";
|
||||
|
||||
jest.mock("@/lib/chatStream", () => ({
|
||||
abortAgentChat: jest.fn(async () => undefined),
|
||||
@@ -13,6 +14,7 @@ jest.mock("@/lib/chatStream", () => ({
|
||||
|
||||
const loadActiveChatState = jest.fn();
|
||||
const listChatSessions = jest.fn();
|
||||
const saveActiveChatState = jest.fn();
|
||||
const updateChatSessionTitle = jest.fn();
|
||||
|
||||
jest.mock("../chatStorage", () => ({
|
||||
@@ -27,7 +29,7 @@ jest.mock("../chatStorage", () => ({
|
||||
sessionId: undefined,
|
||||
branchGroups: [],
|
||||
})),
|
||||
saveActiveChatState: jest.fn(async (state) => state.storageSessionId),
|
||||
saveActiveChatState: (...args: unknown[]) => saveActiveChatState(...args),
|
||||
updateChatSessionTitle: (...args: unknown[]) => updateChatSessionTitle(...args),
|
||||
}));
|
||||
|
||||
@@ -35,8 +37,10 @@ describe("useAgentChatSession", () => {
|
||||
beforeEach(() => {
|
||||
loadActiveChatState.mockReset();
|
||||
listChatSessions.mockReset();
|
||||
saveActiveChatState.mockReset();
|
||||
updateChatSessionTitle.mockReset();
|
||||
jest.mocked(streamAgentChat).mockReset();
|
||||
saveActiveChatState.mockImplementation(async (state) => state.storageSessionId);
|
||||
|
||||
loadActiveChatState.mockResolvedValue({
|
||||
storageSessionId: undefined,
|
||||
@@ -105,6 +109,59 @@ describe("useAgentChatSession", () => {
|
||||
]);
|
||||
});
|
||||
|
||||
it("waits for the stream session id before persisting a new streaming conversation", async () => {
|
||||
listChatSessions.mockResolvedValue([]);
|
||||
let emitStreamEvent: ((event: StreamEvent) => void) | undefined;
|
||||
jest.mocked(streamAgentChat).mockImplementationOnce(async ({ onEvent }) => {
|
||||
emitStreamEvent = onEvent;
|
||||
await new Promise<void>(() => undefined);
|
||||
});
|
||||
|
||||
const { result } = renderHook(() =>
|
||||
useAgentChatSession({
|
||||
projectId: "project-1",
|
||||
onToolCall: jest.fn(),
|
||||
}),
|
||||
);
|
||||
|
||||
await waitFor(() => expect(result.current.isHydrating).toBe(false));
|
||||
|
||||
jest.useFakeTimers();
|
||||
try {
|
||||
await act(async () => {
|
||||
void result.current.sendPrompt("第一条消息");
|
||||
await Promise.resolve();
|
||||
});
|
||||
|
||||
expect(result.current.isStreaming).toBe(true);
|
||||
|
||||
await act(async () => {
|
||||
jest.advanceTimersByTime(200);
|
||||
});
|
||||
|
||||
expect(saveActiveChatState).not.toHaveBeenCalled();
|
||||
|
||||
act(() => {
|
||||
emitStreamEvent?.({
|
||||
type: "token",
|
||||
sessionId: "chat-stream-1",
|
||||
content: "收到",
|
||||
});
|
||||
});
|
||||
|
||||
await act(async () => {
|
||||
jest.advanceTimersByTime(200);
|
||||
});
|
||||
|
||||
expect(saveActiveChatState).toHaveBeenCalledTimes(1);
|
||||
expect(saveActiveChatState.mock.calls[0][0]).toMatchObject({
|
||||
sessionId: "chat-stream-1",
|
||||
});
|
||||
} finally {
|
||||
jest.useRealTimers();
|
||||
}
|
||||
});
|
||||
|
||||
it("ignores generated session titles after the title was edited manually", async () => {
|
||||
listChatSessions.mockResolvedValue([]);
|
||||
loadActiveChatState.mockResolvedValue({
|
||||
|
||||
Reference in New Issue
Block a user