|
| 1 | +/* |
| 2 | + * Copyright Red Hat, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import { ConfigApi, FetchApi } from '@backstage/core-plugin-api'; |
| 18 | + |
| 19 | +import { NotebooksApiClient } from '../NotebooksApiClient'; |
| 20 | + |
| 21 | +describe('NotebooksApiClient', () => { |
| 22 | + let mockConfigApi: jest.Mocked<ConfigApi>; |
| 23 | + let mockFetchApi: jest.Mocked<FetchApi>; |
| 24 | + let client: NotebooksApiClient; |
| 25 | + |
| 26 | + beforeEach(() => { |
| 27 | + mockConfigApi = { |
| 28 | + getString: jest.fn().mockReturnValue('http://localhost:7007'), |
| 29 | + } as unknown as jest.Mocked<ConfigApi>; |
| 30 | + |
| 31 | + mockFetchApi = { |
| 32 | + fetch: jest.fn(), |
| 33 | + } as unknown as jest.Mocked<FetchApi>; |
| 34 | + |
| 35 | + client = new NotebooksApiClient({ |
| 36 | + configApi: mockConfigApi, |
| 37 | + fetchApi: mockFetchApi, |
| 38 | + }); |
| 39 | + }); |
| 40 | + |
| 41 | + afterEach(() => { |
| 42 | + jest.clearAllMocks(); |
| 43 | + }); |
| 44 | + |
| 45 | + describe('getBaseUrl', () => { |
| 46 | + it('should return the correct base URL', async () => { |
| 47 | + const baseUrl = await client.getBaseUrl(); |
| 48 | + expect(baseUrl).toBe('http://localhost:7007/api/lightspeed/notebooks'); |
| 49 | + expect(mockConfigApi.getString).toHaveBeenCalledWith('backend.baseUrl'); |
| 50 | + }); |
| 51 | + }); |
| 52 | + |
| 53 | + describe('getSession', () => { |
| 54 | + it('should return session data when API call succeeds', async () => { |
| 55 | + const mockSession = { |
| 56 | + session_id: 'vs_test-123', |
| 57 | + name: 'Test Notebook', |
| 58 | + metadata: { conversation_id: 'conv-456' }, |
| 59 | + }; |
| 60 | + |
| 61 | + mockFetchApi.fetch.mockResolvedValue({ |
| 62 | + ok: true, |
| 63 | + text: jest.fn().mockResolvedValue( |
| 64 | + JSON.stringify({ |
| 65 | + session: mockSession, |
| 66 | + message: 'Session retrieved successfully', |
| 67 | + }), |
| 68 | + ), |
| 69 | + } as unknown as Response); |
| 70 | + |
| 71 | + const result = await client.getSession('vs_test-123'); |
| 72 | + |
| 73 | + expect(result).toEqual(mockSession); |
| 74 | + expect(mockFetchApi.fetch).toHaveBeenCalledWith( |
| 75 | + 'http://localhost:7007/api/lightspeed/notebooks/v1/sessions/vs_test-123', |
| 76 | + expect.objectContaining({ |
| 77 | + headers: { 'Content-Type': 'application/json' }, |
| 78 | + }), |
| 79 | + ); |
| 80 | + }); |
| 81 | + |
| 82 | + it('should encode special characters in session ID', async () => { |
| 83 | + const mockSession = { |
| 84 | + session_id: 'vs_test/special?chars', |
| 85 | + name: 'Test Notebook', |
| 86 | + }; |
| 87 | + |
| 88 | + mockFetchApi.fetch.mockResolvedValue({ |
| 89 | + ok: true, |
| 90 | + text: jest |
| 91 | + .fn() |
| 92 | + .mockResolvedValue(JSON.stringify({ session: mockSession })), |
| 93 | + } as unknown as Response); |
| 94 | + |
| 95 | + await client.getSession('vs_test/special?chars'); |
| 96 | + |
| 97 | + expect(mockFetchApi.fetch).toHaveBeenCalledWith( |
| 98 | + 'http://localhost:7007/api/lightspeed/notebooks/v1/sessions/vs_test%2Fspecial%3Fchars', |
| 99 | + expect.any(Object), |
| 100 | + ); |
| 101 | + }); |
| 102 | + |
| 103 | + it('should throw error when session is not found', async () => { |
| 104 | + mockFetchApi.fetch.mockResolvedValue({ |
| 105 | + ok: false, |
| 106 | + status: 404, |
| 107 | + statusText: 'Not Found', |
| 108 | + text: jest |
| 109 | + .fn() |
| 110 | + .mockResolvedValue(JSON.stringify({ error: 'Session not found' })), |
| 111 | + } as unknown as Response); |
| 112 | + |
| 113 | + await expect(client.getSession('vs_invalid')).rejects.toThrow( |
| 114 | + 'Session not found', |
| 115 | + ); |
| 116 | + }); |
| 117 | + |
| 118 | + it('should throw error when response has no session', async () => { |
| 119 | + mockFetchApi.fetch.mockResolvedValue({ |
| 120 | + ok: true, |
| 121 | + text: jest |
| 122 | + .fn() |
| 123 | + .mockResolvedValue( |
| 124 | + JSON.stringify({ message: 'Success but no session' }), |
| 125 | + ), |
| 126 | + } as unknown as Response); |
| 127 | + |
| 128 | + await expect(client.getSession('vs_test-123')).rejects.toThrow( |
| 129 | + 'Session not found', |
| 130 | + ); |
| 131 | + }); |
| 132 | + |
| 133 | + it('should throw error with custom error message from response', async () => { |
| 134 | + mockFetchApi.fetch.mockResolvedValue({ |
| 135 | + ok: true, |
| 136 | + text: jest |
| 137 | + .fn() |
| 138 | + .mockResolvedValue(JSON.stringify({ error: 'Custom error message' })), |
| 139 | + } as unknown as Response); |
| 140 | + |
| 141 | + await expect(client.getSession('vs_test-123')).rejects.toThrow( |
| 142 | + 'Custom error message', |
| 143 | + ); |
| 144 | + }); |
| 145 | + }); |
| 146 | + |
| 147 | + describe('listSessions', () => { |
| 148 | + it('should return sessions when API call succeeds', async () => { |
| 149 | + const mockSessions = [ |
| 150 | + { session_id: 'vs_1', name: 'Notebook 1' }, |
| 151 | + { session_id: 'vs_2', name: 'Notebook 2' }, |
| 152 | + ]; |
| 153 | + |
| 154 | + mockFetchApi.fetch.mockResolvedValue({ |
| 155 | + ok: true, |
| 156 | + text: jest |
| 157 | + .fn() |
| 158 | + .mockResolvedValue(JSON.stringify({ sessions: mockSessions })), |
| 159 | + } as unknown as Response); |
| 160 | + |
| 161 | + const result = await client.listSessions(); |
| 162 | + |
| 163 | + expect(result).toEqual(mockSessions); |
| 164 | + expect(mockFetchApi.fetch).toHaveBeenCalledWith( |
| 165 | + 'http://localhost:7007/api/lightspeed/notebooks/v1/sessions', |
| 166 | + expect.any(Object), |
| 167 | + ); |
| 168 | + }); |
| 169 | + |
| 170 | + it('should return empty array when sessions is undefined', async () => { |
| 171 | + mockFetchApi.fetch.mockResolvedValue({ |
| 172 | + ok: true, |
| 173 | + text: jest.fn().mockResolvedValue(JSON.stringify({})), |
| 174 | + } as unknown as Response); |
| 175 | + |
| 176 | + const result = await client.listSessions(); |
| 177 | + expect(result).toEqual([]); |
| 178 | + }); |
| 179 | + }); |
| 180 | + |
| 181 | + describe('createSession', () => { |
| 182 | + it('should create and return session', async () => { |
| 183 | + const mockSession = { |
| 184 | + session_id: 'vs_new-123', |
| 185 | + name: 'New Notebook', |
| 186 | + }; |
| 187 | + |
| 188 | + mockFetchApi.fetch.mockResolvedValue({ |
| 189 | + ok: true, |
| 190 | + text: jest |
| 191 | + .fn() |
| 192 | + .mockResolvedValue(JSON.stringify({ session: mockSession })), |
| 193 | + } as unknown as Response); |
| 194 | + |
| 195 | + const result = await client.createSession('New Notebook', 'Description'); |
| 196 | + |
| 197 | + expect(result).toEqual(mockSession); |
| 198 | + expect(mockFetchApi.fetch).toHaveBeenCalledWith( |
| 199 | + 'http://localhost:7007/api/lightspeed/notebooks/v1/sessions', |
| 200 | + expect.objectContaining({ |
| 201 | + method: 'POST', |
| 202 | + body: JSON.stringify({ |
| 203 | + name: 'New Notebook', |
| 204 | + description: 'Description', |
| 205 | + }), |
| 206 | + }), |
| 207 | + ); |
| 208 | + }); |
| 209 | + |
| 210 | + it('should throw error when creation fails', async () => { |
| 211 | + mockFetchApi.fetch.mockResolvedValue({ |
| 212 | + ok: true, |
| 213 | + text: jest |
| 214 | + .fn() |
| 215 | + .mockResolvedValue( |
| 216 | + JSON.stringify({ error: 'Failed to create session' }), |
| 217 | + ), |
| 218 | + } as unknown as Response); |
| 219 | + |
| 220 | + await expect(client.createSession('New Notebook')).rejects.toThrow( |
| 221 | + 'Failed to create session', |
| 222 | + ); |
| 223 | + }); |
| 224 | + }); |
| 225 | + |
| 226 | + describe('renameSession', () => { |
| 227 | + it('should rename session successfully', async () => { |
| 228 | + mockFetchApi.fetch.mockResolvedValue({ |
| 229 | + ok: true, |
| 230 | + text: jest.fn().mockResolvedValue(''), |
| 231 | + } as unknown as Response); |
| 232 | + |
| 233 | + await client.renameSession('vs_test-123', 'New Name'); |
| 234 | + |
| 235 | + expect(mockFetchApi.fetch).toHaveBeenCalledWith( |
| 236 | + 'http://localhost:7007/api/lightspeed/notebooks/v1/sessions/vs_test-123', |
| 237 | + expect.objectContaining({ |
| 238 | + method: 'PUT', |
| 239 | + body: JSON.stringify({ name: 'New Name' }), |
| 240 | + }), |
| 241 | + ); |
| 242 | + }); |
| 243 | + }); |
| 244 | + |
| 245 | + describe('deleteSession', () => { |
| 246 | + it('should delete session successfully', async () => { |
| 247 | + mockFetchApi.fetch.mockResolvedValue({ |
| 248 | + ok: true, |
| 249 | + text: jest.fn().mockResolvedValue(''), |
| 250 | + } as unknown as Response); |
| 251 | + |
| 252 | + await client.deleteSession('vs_test-123'); |
| 253 | + |
| 254 | + expect(mockFetchApi.fetch).toHaveBeenCalledWith( |
| 255 | + 'http://localhost:7007/api/lightspeed/notebooks/v1/sessions/vs_test-123', |
| 256 | + expect.objectContaining({ |
| 257 | + method: 'DELETE', |
| 258 | + }), |
| 259 | + ); |
| 260 | + }); |
| 261 | + }); |
| 262 | +}); |
0 commit comments