Skip to content

Commit d08609d

Browse files
committed
fix ut tests and yarn lock
Signed-off-by: Anan Zhuang <[email protected]>
1 parent 57979c7 commit d08609d

File tree

10 files changed

+175
-76
lines changed

10 files changed

+175
-76
lines changed

src/core/public/chat/chat_service.test.ts

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -119,22 +119,25 @@ describe('ChatService', () => {
119119
expect(emittedState.isWindowOpen).toBe(true);
120120
});
121121

122-
it('should trigger window callbacks', () => {
122+
it('should trigger window callbacks', async () => {
123+
const setupContract = service.setup();
123124
const startContract = service.start();
124125

126+
setupContract.setImplementation(mockImplementation);
127+
125128
const openCallback = jest.fn();
126129
const closeCallback = jest.fn();
127130

128131
startContract.onWindowOpen(openCallback);
129132
startContract.onWindowClose(closeCallback);
130133

131-
// Open window
132-
startContract.setWindowState({ isWindowOpen: true });
134+
// Open window via openWindow method (which triggers callbacks)
135+
await startContract.openWindow();
133136
expect(openCallback).toHaveBeenCalledTimes(1);
134137
expect(closeCallback).not.toHaveBeenCalled();
135138

136-
// Close window
137-
startContract.setWindowState({ isWindowOpen: false });
139+
// Close window via closeWindow method (which triggers callbacks)
140+
await startContract.closeWindow();
138141
expect(closeCallback).toHaveBeenCalledTimes(1);
139142
});
140143

@@ -160,22 +163,28 @@ describe('ChatService', () => {
160163

161164
setupContract.setImplementation(mockImplementation);
162165

163-
// Test message sending
166+
// Test message sending - this should delegate to implementation
164167
const result = await startContract.sendMessage('test', []);
165168
expect(mockImplementation.sendMessage).toHaveBeenCalledWith('test', []);
166169
expect(result).toEqual({
167170
observable: null,
168171
userMessage: { id: '1', role: 'user', content: 'test' },
169172
});
170173

171-
// Test window operations
174+
// Test window operations - these trigger callbacks but don't call implementation methods directly
175+
// The implementation methods are called by the plugin in response to the callbacks
176+
const openCallback = jest.fn();
177+
const closeCallback = jest.fn();
178+
179+
startContract.onWindowOpen(openCallback);
180+
startContract.onWindowClose(closeCallback);
181+
172182
await startContract.openWindow();
173-
expect(mockImplementation.openWindow).toHaveBeenCalled();
174-
expect(startContract.isWindowOpen()).toBe(true); // Core state updated
183+
expect(openCallback).toHaveBeenCalled();
184+
// Window state is not automatically updated by openWindow - it's managed separately
175185

176186
await startContract.closeWindow();
177-
expect(mockImplementation.closeWindow).toHaveBeenCalled();
178-
expect(startContract.isWindowOpen()).toBe(false); // Core state updated
187+
expect(closeCallback).toHaveBeenCalled();
179188
});
180189
});
181190

src/plugins/chat/opensearch_dashboards.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@
1010
"contextProvider",
1111
"charts"
1212
],
13-
"optionalPlugins": ["dataSourceManagement"],
13+
"optionalPlugins": [],
1414
"requiredBundles": ["dataSourceManagement"]
1515
}

src/plugins/chat/public/plugin.test.ts

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ describe('ChatPlugin', () => {
5757
getSidecarConfig$: jest.fn().mockReturnValue(of({ paddingSize: 400 })),
5858
},
5959
},
60+
uiSettings: {},
61+
chat: {},
62+
workspaces: {},
6063
};
6164

6265
// Mock dependencies
@@ -90,8 +93,12 @@ describe('ChatPlugin', () => {
9093
it('should initialize chat service when enabled', () => {
9194
plugin.start(mockCoreStart, mockDeps);
9295

93-
// ChatService is called with uiSettings and core chat service
94-
expect(ChatService).toHaveBeenCalledWith(mockCoreStart.uiSettings, mockCoreStart.chat);
96+
// ChatService is called with uiSettings, core chat service, and workspaces
97+
expect(ChatService).toHaveBeenCalledWith(
98+
mockCoreStart.uiSettings,
99+
mockCoreStart.chat,
100+
mockCoreStart.workspaces
101+
);
95102
});
96103

97104
it('should register chat button in header nav controls', () => {
@@ -117,16 +124,24 @@ describe('ChatPlugin', () => {
117124

118125
const startContract = testPlugin.start(mockCoreStart, mockDeps);
119126

120-
// ChatService should still be created with uiSettings and core chat service
121-
expect(ChatService).toHaveBeenCalledWith(mockCoreStart.uiSettings, mockCoreStart.chat);
127+
// ChatService should still be created with uiSettings, core chat service, and workspaces
128+
expect(ChatService).toHaveBeenCalledWith(
129+
mockCoreStart.uiSettings,
130+
mockCoreStart.chat,
131+
mockCoreStart.workspaces
132+
);
122133
expect(startContract.chatService).toBeInstanceOf(ChatService);
123134
expect(mockCoreStart.chrome.navControls.registerPrimaryHeaderRight).toHaveBeenCalled();
124135
});
125136

126137
it('should always initialize chat service (core service handles enablement)', () => {
127138
const startContract = plugin.start(mockCoreStart, mockDeps);
128139

129-
expect(ChatService).toHaveBeenCalledWith(mockCoreStart.uiSettings, mockCoreStart.chat);
140+
expect(ChatService).toHaveBeenCalledWith(
141+
mockCoreStart.uiSettings,
142+
mockCoreStart.chat,
143+
mockCoreStart.workspaces
144+
);
130145
expect(startContract.chatService).toBeInstanceOf(ChatService);
131146
expect(mockCoreStart.chrome.navControls.registerPrimaryHeaderRight).toHaveBeenCalled();
132147
});
@@ -185,7 +200,11 @@ describe('ChatPlugin', () => {
185200
expect(() => testPlugin.start(mockCoreStart, mockDeps)).not.toThrow();
186201

187202
// ChatService is always initialized - core service handles enablement logic
188-
expect(ChatService).toHaveBeenCalledWith(mockCoreStart.uiSettings, mockCoreStart.chat);
203+
expect(ChatService).toHaveBeenCalledWith(
204+
mockCoreStart.uiSettings,
205+
mockCoreStart.chat,
206+
mockCoreStart.workspaces
207+
);
189208
});
190209
});
191210
});

src/plugins/chat/public/plugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ export class ChatPlugin implements Plugin<ChatPluginSetup, ChatPluginStart> {
103103
);
104104

105105
// Always initialize chat service - core service handles enablement
106-
this.chatService = new ChatService(core.uiSettings, core.chat);
106+
this.chatService = new ChatService(core.uiSettings, core.chat, core.workspaces);
107107

108108
if (!isEnabled) {
109109
return {

src/plugins/chat/public/services/chat_service.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ import {
1414
UiSettingScope,
1515
ChatServiceStart,
1616
ChatWindowState,
17+
WorkspacesStart,
1718
} from '../../../../core/public';
18-
import { getDefaultDataSourceId, getWorkspaces } from '../../../data_source_management/public';
19+
import { getDefaultDataSourceId } from '../../../data_source_management/public';
1920

2021
export interface ChatState {
2122
messages: Message[];
@@ -39,8 +40,9 @@ export class ChatService {
3940
public events$: any;
4041
private activeRequests: Set<string> = new Set();
4142
private requestCounter: number = 0;
42-
private uiSettings?: IUiSettingsClient;
43+
private uiSettings: IUiSettingsClient;
4344
private coreChatService?: ChatServiceStart;
45+
private workspaces?: WorkspacesStart;
4446

4547
// Chat state persistence
4648
private readonly STORAGE_KEY = 'chat.currentState';
@@ -49,11 +51,16 @@ export class ChatService {
4951
// ChatWindow ref for delegating sendMessage calls to proper timeline management
5052
private chatWindowRef: React.RefObject<ChatWindowInstance> | null = null;
5153

52-
constructor(uiSettings?: IUiSettingsClient, coreChatService?: ChatServiceStart) {
54+
constructor(
55+
uiSettings: IUiSettingsClient,
56+
coreChatService?: ChatServiceStart,
57+
workspaces?: WorkspacesStart
58+
) {
5359
// No need to pass URL anymore - agent will use the proxy endpoint
5460
this.agent = new AgUiAgent();
5561
this.uiSettings = uiSettings;
5662
this.coreChatService = coreChatService;
63+
this.workspaces = workspaces;
5764

5865
// Try to restore existing state first
5966
const currentChatState = this.loadCurrentChatState();
@@ -268,7 +275,7 @@ export class ChatService {
268275
}
269276

270277
// Get workspace context
271-
const workspaces = getWorkspaces();
278+
const workspaces = this.workspaces;
272279
if (!workspaces) {
273280
// eslint-disable-next-line no-console
274281
console.warn('Workspaces service not available, using global scope');

src/plugins/chat/public/types.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import { NavigationPublicPluginStart } from '../../navigation/public';
77
import { ContextProviderStart } from '../../context_provider/public';
88
import { ChartsPluginStart } from '../../charts/public';
9-
import { DataSourceManagementPluginSetup } from '../../data_source_management/public';
109
import { ChatService } from './services/chat_service';
1110
import { SuggestedActionsServiceSetupContract } from './services/suggested_action';
1211

@@ -22,5 +21,4 @@ export interface AppPluginStartDependencies {
2221
navigation: NavigationPublicPluginStart;
2322
contextProvider: ContextProviderStart;
2423
charts: ChartsPluginStart;
25-
dataSourceManagement?: DataSourceManagementPluginSetup;
2624
}

src/plugins/dashboard/public/application/components/dashboard_top_nav/__snapshots__/dashboard_top_nav.test.tsx.snap

Lines changed: 108 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/plugins/data_source_management/public/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@ export {
2626
createDataSourceMenu,
2727
} from './components/data_source_menu';
2828
export { DataSourceSelectionService } from './service/data_source_selection_service';
29-
export { getDefaultDataSourceId, getDefaultDataSourceId$, getWorkspaces } from './components/utils';
29+
export { getDefaultDataSourceId, getDefaultDataSourceId$ } from './components/utils';
3030
export { DATACONNECTIONS_BASE, DatasourceTypeToDisplayName } from './constants';
3131
export { DEFAULT_DATA_SOURCE_UI_SETTINGS_ID } from '../common';

0 commit comments

Comments
 (0)