| 
									
										
										
										
											2023-10-25 23:38:55 +08:00
										 |  |  | import { llms } from '@grafana/experimental'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-11 21:56:42 +08:00
										 |  |  | import { createDashboardModelFixture, createPanelSaveModel } from '../../state/__fixtures__/dashboardFixtures'; | 
					
						
							| 
									
										
										
										
											2023-09-21 20:41:49 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-06 20:11:15 +08:00
										 |  |  | import { getDashboardChanges, isLLMPluginEnabled, sanitizeReply } from './utils'; | 
					
						
							| 
									
										
										
										
											2023-09-19 21:40:33 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | // Mock the llms.openai module
 | 
					
						
							| 
									
										
										
										
											2023-10-25 23:38:55 +08:00
										 |  |  | jest.mock('@grafana/experimental', () => ({ | 
					
						
							|  |  |  |   llms: { | 
					
						
							|  |  |  |     openai: { | 
					
						
							|  |  |  |       streamChatCompletions: jest.fn(), | 
					
						
							|  |  |  |       accumulateContent: jest.fn(), | 
					
						
							|  |  |  |       enabled: jest.fn(), | 
					
						
							|  |  |  |     }, | 
					
						
							| 
									
										
										
										
											2023-09-19 21:40:33 +08:00
										 |  |  |   }, | 
					
						
							|  |  |  | })); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-21 20:41:49 +08:00
										 |  |  | describe('getDashboardChanges', () => { | 
					
						
							|  |  |  |   it('should correctly split user changes and migration changes', () => { | 
					
						
							|  |  |  |     // Mock data for testing
 | 
					
						
							|  |  |  |     const deprecatedOptions = { | 
					
						
							|  |  |  |       legend: { displayMode: 'hidden', showLegend: false }, | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     const deprecatedVersion = 37; | 
					
						
							|  |  |  |     const dashboard = createDashboardModelFixture({ | 
					
						
							|  |  |  |       schemaVersion: deprecatedVersion, | 
					
						
							| 
									
										
										
										
											2023-10-11 21:56:42 +08:00
										 |  |  |       panels: [createPanelSaveModel({ title: 'Panel 1', options: deprecatedOptions })], | 
					
						
							| 
									
										
										
										
											2023-09-21 20:41:49 +08:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Update title for the first panel
 | 
					
						
							|  |  |  |     dashboard.updatePanels([ | 
					
						
							|  |  |  |       { | 
					
						
							|  |  |  |         ...dashboard.panels[0], | 
					
						
							|  |  |  |         title: 'New title', | 
					
						
							|  |  |  |       }, | 
					
						
							|  |  |  |       ...dashboard.panels.slice(1), | 
					
						
							|  |  |  |     ]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Call the function to test
 | 
					
						
							|  |  |  |     const result = getDashboardChanges(dashboard); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Assertions
 | 
					
						
							| 
									
										
										
										
											2023-10-14 08:02:45 +08:00
										 |  |  |     expect(result.migrationChanges).toEqual( | 
					
						
							|  |  |  |       '===================================================================\n' + | 
					
						
							|  |  |  |         '--- Before migration changes\t\n' + | 
					
						
							|  |  |  |         '+++ After migration changes\t\n' + | 
					
						
							|  |  |  |         '@@ -1,9 +1,9 @@\n' + | 
					
						
							|  |  |  |         ' {\n' + | 
					
						
							|  |  |  |         '   "editable": true,\n' + | 
					
						
							|  |  |  |         '   "graphTooltip": 0,\n' + | 
					
						
							|  |  |  |         '-  "schemaVersion": 37,\n' + | 
					
						
							|  |  |  |         '+  "schemaVersion": 38,\n' + | 
					
						
							|  |  |  |         '   "timezone": "",\n' + | 
					
						
							|  |  |  |         '   "panels": [\n' + | 
					
						
							|  |  |  |         '     {\n' + | 
					
						
							|  |  |  |         '       "type": "timeseries",\n' + | 
					
						
							|  |  |  |         '       "title": "Panel 1",\n' | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  |     expect(result.userChanges).toEqual( | 
					
						
							|  |  |  |       '===================================================================\n' + | 
					
						
							|  |  |  |         '--- Before user changes\t\n' + | 
					
						
							|  |  |  |         '+++ After user changes\t\n' + | 
					
						
							|  |  |  |         '@@ -3,16 +3,17 @@\n' + | 
					
						
							|  |  |  |         '   "graphTooltip": 0,\n' + | 
					
						
							|  |  |  |         '   "schemaVersion": 38,\n' + | 
					
						
							|  |  |  |         '   "timezone": "",\n' + | 
					
						
							|  |  |  |         '   "panels": [\n' + | 
					
						
							|  |  |  |         '     {\n' + | 
					
						
							|  |  |  |         '-      "type": "timeseries",\n' + | 
					
						
							|  |  |  |         '-      "title": "Panel 1",\n' + | 
					
						
							|  |  |  |         '+      "id": 1,\n' + | 
					
						
							|  |  |  |         '       "options": {\n' + | 
					
						
							|  |  |  |         '         "legend": {\n' + | 
					
						
							|  |  |  |         '           "displayMode": "hidden",\n' + | 
					
						
							|  |  |  |         '           "showLegend": false\n' + | 
					
						
							|  |  |  |         '         }\n' + | 
					
						
							|  |  |  |         '-      }\n' + | 
					
						
							|  |  |  |         '+      },\n' + | 
					
						
							|  |  |  |         '+      "title": "New title",\n' + | 
					
						
							|  |  |  |         '+      "type": "timeseries"\n' + | 
					
						
							|  |  |  |         '     }\n' + | 
					
						
							|  |  |  |         '   ]\n' + | 
					
						
							|  |  |  |         ' }\n' + | 
					
						
							|  |  |  |         '\\ No newline at end of file\n' | 
					
						
							|  |  |  |     ); | 
					
						
							| 
									
										
										
										
											2023-09-21 20:41:49 +08:00
										 |  |  |     expect(result.migrationChanges).toBeDefined(); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2023-09-27 22:47:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | describe('isLLMPluginEnabled', () => { | 
					
						
							|  |  |  |   it('should return true if LLM plugin is enabled', async () => { | 
					
						
							|  |  |  |     // Mock llms.openai.enabled to return true
 | 
					
						
							| 
									
										
										
										
											2023-10-25 23:38:55 +08:00
										 |  |  |     jest.mocked(llms.openai.enabled).mockResolvedValue({ ok: true, configured: false }); | 
					
						
							| 
									
										
										
										
											2023-09-27 22:47:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     const enabled = await isLLMPluginEnabled(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     expect(enabled).toBe(true); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('should return false if LLM plugin is not enabled', async () => { | 
					
						
							|  |  |  |     // Mock llms.openai.enabled to return false
 | 
					
						
							| 
									
										
										
										
											2023-10-25 23:38:55 +08:00
										 |  |  |     jest.mocked(llms.openai.enabled).mockResolvedValue({ ok: false, configured: false }); | 
					
						
							| 
									
										
										
										
											2023-09-27 22:47:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     const enabled = await isLLMPluginEnabled(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     expect(enabled).toBe(false); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2023-10-06 20:11:15 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | describe('sanitizeReply', () => { | 
					
						
							|  |  |  |   it('should remove quotes from the beginning and end of a string', () => { | 
					
						
							|  |  |  |     expect(sanitizeReply('"Hello, world!"')).toBe('Hello, world!'); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('should not remove quotes from the middle of a string', () => { | 
					
						
							|  |  |  |     expect(sanitizeReply('Hello, "world"!')).toBe('Hello, "world"!'); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('should only remove quotes if they are at the beginning or end of a string, and not in the middle', () => { | 
					
						
							|  |  |  |     expect(sanitizeReply('"Hello", world!')).toBe('Hello", world!'); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('should return an empty string if given an empty string', () => { | 
					
						
							|  |  |  |     expect(sanitizeReply('')).toBe(''); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | }); |