11import { render , screen , waitFor } from "@testing-library/react" ;
2- import { describe , expect , it , Mock , vi } from "vitest" ;
2+ import { describe , expect , it , Mock , vi , beforeEach , afterEach } from "vitest" ;
33import userEvent from "@testing-library/user-event" ;
44import Layout from "../../src/core/components/Layout" ;
5+ import { Slot } from "../../src/core/components/Slot" ;
6+ import { ComponentProps } from "react" ;
7+
58vi . mock ( "../../src/core/components/DebugPanel" , ( ) => ( {
69 default : ( { isOpen } : { isOpen : boolean } ) => (
710 < div > isOpen: { JSON . stringify ( isOpen ) } </ div >
@@ -26,31 +29,39 @@ vi.mock("../../src/core/stores/HistoryStore/selectors", () => {
2629 } ;
2730} ) ;
2831
29- vi . mock (
30- "../../src/plugins/ChatHistoryPlugin/components/ChatHistory.tsx" ,
31- ( ) => ( {
32- default : ( ) => < div > ChatHistory</ div > ,
33- } ) ,
34- ) ;
32+ // Track whether chat history should be enabled
33+ let chatHistoryEnabled = false ;
34+
35+ vi . mock ( "../../src/core/components/Slot.tsx" , ( ) => ( {
36+ Slot : ( { name } : ComponentProps < typeof Slot > ) => {
37+ if ( name === "layout.sidebar" && chatHistoryEnabled ) {
38+ return < div > ChatHistory</ div > ;
39+ }
40+ if ( name === "layout.headerActions" ) {
41+ return < div data-testid = "header-actions-slot" > HeaderActions</ div > ;
42+ }
43+ return null ;
44+ } ,
45+ } ) ) ;
46+
47+ vi . mock ( "../../src/core/utils/slots/useSlotHasFillers.ts" , ( ) => ( {
48+ useSlotHasFillers : ( slot : string ) => {
49+ if ( slot === "layout.sidebar" ) {
50+ return chatHistoryEnabled ;
51+ }
52+ return false ;
53+ } ,
54+ } ) ) ;
3555
3656import { useConfigContext } from "../../src/core/contexts/ConfigContext/useConfigContext" ;
3757import { useThemeContext } from "../../src/core/contexts/ThemeContext/useThemeContext" ;
3858import { Theme } from "../../src/core/contexts/ThemeContext/ThemeContext" ;
3959import { useHistoryActions } from "../../src/core/stores/HistoryStore/selectors" ;
40- import { pluginManager } from "../../src/core/utils/plugins/PluginManager" ;
41- import {
42- ChatHistoryPlugin ,
43- ChatHistoryPluginName ,
44- } from "../../src/plugins/ChatHistoryPlugin" ;
45-
46- function mockConfig (
47- isDebugEnabled : boolean = false ,
48- withClientSideHistory : boolean = false ,
49- ) {
60+
61+ function mockConfig ( isDebugEnabled : boolean = false ) {
5062 ( useConfigContext as Mock ) . mockReturnValue ( {
5163 config : {
5264 debug_mode : isDebugEnabled ,
53- conversationHistory : withClientSideHistory ,
5465 } ,
5566 } ) ;
5667}
@@ -63,18 +74,25 @@ function mockTheme(theme: Theme) {
6374 } ) ;
6475}
6576
66- const clearHistoryMock = vi . fn ( ) ;
77+ const newConversationMock = vi . fn ( ) ;
6778const stopAnsweringMock = vi . fn ( ) ;
6879( useHistoryActions as Mock ) . mockReturnValue ( {
69- clearHistory : clearHistoryMock ,
80+ newConversation : newConversationMock ,
7081 stopAnswering : stopAnsweringMock ,
7182} ) ;
7283
7384describe ( "Layout" , ( ) => {
85+ beforeEach ( ( ) => {
86+ chatHistoryEnabled = false ;
87+ } ) ;
88+
89+ afterEach ( ( ) => {
90+ vi . clearAllMocks ( ) ;
91+ } ) ;
92+
7493 it ( "renders with chat history" , async ( ) => {
75- pluginManager . register ( ChatHistoryPlugin ) ;
76- pluginManager . activate ( ChatHistoryPluginName ) ;
77- mockConfig ( false , true ) ;
94+ chatHistoryEnabled = true ;
95+ mockConfig ( false ) ;
7896 mockTheme ( Theme . LIGHT ) ;
7997 render (
8098 < Layout
@@ -102,9 +120,10 @@ describe("Layout", () => {
102120 ) . toBeInTheDocument ( ) ;
103121 expect ( screen . queryByTestId ( "layout-debug-button" ) ) . toBeNull ( ) ;
104122 } ) ;
123+
105124 it ( "renders without chat history" , async ( ) => {
106- pluginManager . deactivate ( ChatHistoryPluginName ) ;
107- mockConfig ( false , false ) ;
125+ chatHistoryEnabled = false ;
126+ mockConfig ( false ) ;
108127 mockTheme ( Theme . LIGHT ) ;
109128 render (
110129 < Layout
@@ -119,9 +138,8 @@ describe("Layout", () => {
119138 expect ( screen . getByText ( "Custom Title" ) ) . toBeInTheDocument ( ) ;
120139 expect ( screen . getByText ( "Custom Subtitle" ) ) . toBeInTheDocument ( ) ;
121140 expect ( screen . getByText ( "Custom Logo" ) ) . toBeInTheDocument ( ) ;
122- // Chat history
123141
124- // Chat history
142+ // Chat history should not be present
125143 await waitFor ( ( ) => {
126144 expect ( screen . queryByText ( "ChatHistory" ) ) . not . toBeInTheDocument ( ) ;
127145 expect (
@@ -134,6 +152,7 @@ describe("Layout", () => {
134152 ) . toBeInTheDocument ( ) ;
135153 expect ( screen . queryByTestId ( "layout-debug-button" ) ) . toBeNull ( ) ;
136154 } ) ;
155+
137156 it ( "renders without debug panel" , ( ) => {
138157 mockConfig ( ) ;
139158 mockTheme ( Theme . LIGHT ) ;
@@ -197,6 +216,7 @@ describe("Layout", () => {
197216 await user . click ( toggleThemeButton ) ;
198217 expect ( setThemeMock ) . toHaveBeenCalled ( ) ;
199218 } ) ;
219+
200220 it ( 'shows debug panel when "debug button" is clicked' , async ( ) => {
201221 mockConfig ( true ) ;
202222 mockTheme ( Theme . LIGHT ) ;
0 commit comments