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