Elffuss BraidHub

kikocis/continue public

86e71fea71da · 3.9 KiBRawPermalink
1import { configureStore } from "@reduxjs/toolkit";
2import { vi } from "vitest";
3import configReducer from "../../redux/slices/configSlice";
4import editModeStateReducer from "../../redux/slices/editState";
5import indexingReducer from "../../redux/slices/indexingSlice";
6import { profilesReducer } from "../../redux/slices/profilesSlice";
7import sessionReducer from "../../redux/slices/sessionSlice";
8import tabsReducer from "../../redux/slices/tabsSlice";
9import uiReducer from "../../redux/slices/uiSlice";
10import { RootState } from "../../redux/store";
11
12export const createMockStore = (initialState?: Partial<RootState>) => {
13 const mockIdeMessenger = {
14 request: vi.fn(),
15 post: vi.fn(),
16 respond: vi.fn(),
17 streamRequest: vi.fn(),
18 llmStreamChat: vi.fn(),
19 ide: {} as any,
20 };
21
22 const store = configureStore({
23 reducer: {
24 session: sessionReducer,
25 ui: uiReducer,
26 editModeState: editModeStateReducer,
27 config: configReducer,
28 indexing: indexingReducer,
29 tabs: tabsReducer,
30 profiles: profilesReducer,
31 },
32 preloadedState: {
33 session: {
34 history: [],
35 title: "",
36 id: "",
37 lastSessionId: undefined,
38 mode: "chat",
39 isStreaming: false,
40 streamAborter: new AbortController(),
41 hasReasoningEnabled: false,
42 contextPercentage: 0,
43 isPruned: false,
44 isInEdit: false,
45 ...initialState?.session,
46 },
47 ui: {
48 showDialog: false,
49 dialogMessage: null,
50 toolSettings: {},
51 ruleSettings: {},
52 ...initialState?.ui,
53 },
54 editModeState: {
55 isInEdit: false,
56 returnToMode: "chat",
57 ...initialState?.editModeState,
58 },
59 config: {
60 config: {
61 models: [],
62 tabAutocompleteModel: undefined,
63 tools: [],
64 rules: [],
65 selectedModelByRole: {
66 chat: null,
67 apply: null,
68 edit: null,
69 summarize: null,
70 autocomplete: null,
71 rerank: null,
72 embed: null,
73 },
74 ...initialState?.config?.config,
75 },
76 lastSelectedModelByRole: {},
77 loading: false,
78 configError: undefined,
79 ...initialState?.config,
80 },
81 indexing: {
82 indexing: {
83 statuses: {},
84 hiddenChatPeekTypes: {
85 docs: false,
86 },
87 },
88 ...initialState?.indexing,
89 },
90 tabs: {
91 tabsItems: [],
92 ...initialState?.tabs,
93 },
94 profiles: {
95 organizations: [],
96 selectedProfileId: null,
97 selectedOrganizationId: null,
98 preferencesByProfileId: {},
99 ...initialState?.profiles,
100 },
101 } as RootState,
102 middleware: (getDefaultMiddleware) =>
103 getDefaultMiddleware({
104 serializableCheck: {
105 ignoredPaths: ["session.streamAborter"],
106 ignoredActions: [
107 "chat/streamNormalInput/pending",
108 "chat/streamNormalInput/fulfilled",
109 "chat/streamNormalInput/rejected",
110 ],
111 },
112 thunk: {
113 extraArgument: {
114 ideMessenger: mockIdeMessenger,
115 },
116 },
117 }),
118 });
119
120 // Add getActions method for testing
121 const actions: any[] = [];
122 const originalDispatch = store.dispatch;
123
124 // Override dispatch to track actions and inject ideMessenger
125 store.dispatch = vi.fn((action: any) => {
126 if (typeof action === "function") {
127 // For thunks, provide the extra argument with ideMessenger
128 return action(store.dispatch, store.getState, {
129 ideMessenger: mockIdeMessenger,
130 });
131 }
132 actions.push(action);
133 return originalDispatch(action);
134 }) as any;
135
136 (store as any).getActions = () => actions;
137 (store as any).clearActions = () => actions.splice(0, actions.length);
138
139 // Expose mockIdeMessenger so tests can configure it
140 return { ...store, mockIdeMessenger };
141};