File size: 2,828 Bytes
4e7644b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import { beforeEach, describe, expect, it, vi } from "vitest";
import { screen, waitFor } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { useNavigate } from "react-router-dom";
import { renderWithProviders } from "@/test/common_setup";
import { useSessionListStore } from "@/stores/sessionListStore";
import ChatSessionInitializer from "./index";

const {
  mockCreateSession,
  mockSessionState,
  mockSetCurrentSessionId,
  mockSetSessions,
} = vi.hoisted(() => ({
  mockCreateSession: vi.fn(),
  mockSessionState: {
    sessions: [] as Array<{ id: string; realId?: string }>,
    currentSessionId: undefined as string | undefined,
  },
  mockSetCurrentSessionId: vi.fn(),
  mockSetSessions: vi.fn(),
}));

vi.mock("@agentscope-ai/chat", () => ({
  useChatAnywhereSessions: () => ({ createSession: mockCreateSession }),
  useChatAnywhereSessionsState: () => ({
    sessions: mockSessionState.sessions,
    currentSessionId: mockSessionState.currentSessionId,
    setCurrentSessionId: mockSetCurrentSessionId,
    setSessions: mockSetSessions,
  }),
}));

vi.mock("../../sessionApi", () => ({
  default: {
    finishSessionSwitch: vi.fn(),
    getEffectiveSessionId: vi.fn((sessionId: string) => sessionId),
    isSessionSwitching: false,
    lastNavigatedChatId: null,
    preferredChatId: null,
    preloadSession: vi.fn(),
    trackNavigatedSession: vi.fn(),
  },
}));

const HISTORY_SESSION_ID = "history-session";
const NEW_SESSION_ID = "1787000000000-abcdefg";

function NavigationHarness() {
  const navigate = useNavigate();

  return (
    <>
      <button onClick={() => navigate("/chat")}>New chat</button>
      <button onClick={() => navigate(`/chat/${HISTORY_SESSION_ID}`)}>
        History session
      </button>
      <ChatSessionInitializer />
    </>
  );
}

describe("ChatSessionInitializer", () => {
  beforeEach(() => {
    vi.clearAllMocks();
    mockSessionState.sessions = [{ id: HISTORY_SESSION_ID }];
    mockSessionState.currentSessionId = HISTORY_SESSION_ID;
    useSessionListStore.setState({
      sessions: [],
      lastUpdated: 0,
      _setLibrarySessions: null,
    });
  });

  it("reopens the only history session after starting a blank chat", async () => {
    const user = userEvent.setup();
    renderWithProviders(<NavigationHarness />, {
      initialEntries: [`/chat/${HISTORY_SESSION_ID}`],
    });

    mockSessionState.sessions = [
      { id: NEW_SESSION_ID },
      { id: HISTORY_SESSION_ID },
    ];
    mockSessionState.currentSessionId = NEW_SESSION_ID;
    await user.click(screen.getByRole("button", { name: "New chat" }));
    await user.click(screen.getByRole("button", { name: "History session" }));

    await waitFor(() => {
      expect(mockSetCurrentSessionId).toHaveBeenCalledWith(HISTORY_SESSION_ID);
    });
  });
});