| 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 | 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 | isSessionMetadataLoading: false, |
| 46 | allSessionMetadata: [], |
| 47 | symbols: {}, |
| 48 | codeBlockApplyStates: { |
| 49 | states: [], |
| 50 | curIndex: 0, |
| 51 | }, |
| 52 | newestToolbarPreviewForInput: {}, |
| 53 | compactionLoading: {}, |
| 54 | inlineErrorMessage: undefined, |
| 55 | ...initialState?.session, |
| 56 | }, |
| 57 | ui: { |
| 58 | showDialog: false, |
| 59 | dialogMessage: null, |
| 60 | toolSettings: {}, |
| 61 | ruleSettings: {}, |
| 62 | ...initialState?.ui, |
| 63 | }, |
| 64 | editModeState: { |
| 65 | isInEdit: false, |
| 66 | returnToMode: "chat", |
| 67 | ...initialState?.editModeState, |
| 68 | }, |
| 69 | config: { |
| 70 | config: { |
| 71 | models: [], |
| 72 | tabAutocompleteModel: undefined, |
| 73 | tools: [], |
| 74 | rules: [], |
| 75 | selectedModelByRole: { |
| 76 | chat: null, |
| 77 | apply: null, |
| 78 | edit: null, |
| 79 | summarize: null, |
| 80 | autocomplete: null, |
| 81 | rerank: null, |
| 82 | embed: null, |
| 83 | }, |
| 84 | ...initialState?.config?.config, |
| 85 | }, |
| 86 | lastSelectedModelByRole: {}, |
| 87 | loading: false, |
| 88 | configError: undefined, |
| 89 | ...initialState?.config, |
| 90 | }, |
| 91 | indexing: { |
| 92 | indexing: { |
| 93 | statuses: {}, |
| 94 | hiddenChatPeekTypes: { |
| 95 | docs: false, |
| 96 | }, |
| 97 | }, |
| 98 | ...initialState?.indexing, |
| 99 | }, |
| 100 | tabs: { |
| 101 | tabsItems: [], |
| 102 | ...initialState?.tabs, |
| 103 | }, |
| 104 | profiles: { |
| 105 | organizations: [], |
| 106 | selectedProfileId: null, |
| 107 | selectedOrganizationId: null, |
| 108 | preferencesByProfileId: {}, |
| 109 | ...initialState?.profiles, |
| 110 | }, |
| 111 | } as RootState, |
| 112 | middleware: (getDefaultMiddleware) => |
| 113 | getDefaultMiddleware({ |
| 114 | serializableCheck: { |
| 115 | ignoredPaths: ["session.streamAborter"], |
| 116 | ignoredActions: [ |
| 117 | "chat/streamNormalInput/pending", |
| 118 | "chat/streamNormalInput/fulfilled", |
| 119 | "chat/streamNormalInput/rejected", |
| 120 | ], |
| 121 | }, |
| 122 | thunk: { |
| 123 | extraArgument: { |
| 124 | ideMessenger: mockIdeMessenger, |
| 125 | }, |
| 126 | }, |
| 127 | }), |
| 128 | }); |
| 129 | |
| 130 | // Add getActions method for testing |
| 131 | const actions: any[] = []; |
| 132 | const originalDispatch = store.dispatch; |
| 133 | |
| 134 | // Override dispatch to track actions and inject ideMessenger |
| 135 | store.dispatch = vi.fn((action: any) => { |
| 136 | if (typeof action === "function") { |
| 137 | // For thunks, provide the extra argument with ideMessenger |
| 138 | return action(store.dispatch, store.getState, { |
| 139 | ideMessenger: mockIdeMessenger, |
| 140 | }); |
| 141 | } |
| 142 | actions.push(action); |
| 143 | return originalDispatch(action); |
| 144 | }) as any; |
| 145 | |
| 146 | (store as any).getActions = () => actions; |
| 147 | (store as any).clearActions = () => actions.splice(0, actions.length); |
| 148 | |
| 149 | // Expose mockIdeMessenger so tests can configure it |
| 150 | return { ...store, mockIdeMessenger }; |
| 151 | }; |