|
| 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 { http, HttpResponse, type HttpHandler } from 'msw'; |
| 18 | + |
| 19 | +// Use port 7007 for tests (matches notebooksRouter.test.ts config) |
| 20 | +const TEST_LIGHTSPEED_SERVICE_PORT = 7007; |
| 21 | +export const LIGHTSPEED_CORE_ADDR = `http://0.0.0.0:${TEST_LIGHTSPEED_SERVICE_PORT}`; |
| 22 | + |
| 23 | +// Mock session data |
| 24 | +export const mockSession1 = { |
| 25 | + session_id: 'session-1', |
| 26 | + name: 'Test Session 1', |
| 27 | + user_id: 'user:default/guest', |
| 28 | + description: 'Test description', |
| 29 | + created_at: '2024-01-01T00:00:00.000Z', |
| 30 | + updated_at: '2024-01-01T00:00:00.000Z', |
| 31 | + metadata: { |
| 32 | + embedding_model: 'sentence-transformers/nomic-ai/nomic-embed-text-v1.5', |
| 33 | + embedding_dimension: 768, |
| 34 | + provider_id: 'notebooks', |
| 35 | + conversation_id: null, |
| 36 | + }, |
| 37 | +}; |
| 38 | + |
| 39 | +export const mockSession2 = { |
| 40 | + session_id: 'session-2', |
| 41 | + name: 'Test Session 2', |
| 42 | + user_id: 'user:default/guest', |
| 43 | + description: 'Another test', |
| 44 | + created_at: '2024-01-02T00:00:00.000Z', |
| 45 | + updated_at: '2024-01-02T00:00:00.000Z', |
| 46 | + metadata: { |
| 47 | + embedding_model: 'sentence-transformers/nomic-ai/nomic-embed-text-v1.5', |
| 48 | + embedding_dimension: 768, |
| 49 | + provider_id: 'notebooks', |
| 50 | + conversation_id: 'conv-1', |
| 51 | + }, |
| 52 | +}; |
| 53 | + |
| 54 | +export const mockFile1 = { |
| 55 | + id: 'file-1', |
| 56 | + created_at: 1704067200, |
| 57 | + status: 'completed' as const, |
| 58 | + attributes: { |
| 59 | + document_id: 'test-document', |
| 60 | + user_id: 'user:default/guest', |
| 61 | + title: 'Test Document', |
| 62 | + session_id: 'session-1', |
| 63 | + source_type: 'text', |
| 64 | + created_at: '2024-01-01T00:00:00.000Z', |
| 65 | + }, |
| 66 | +}; |
| 67 | + |
| 68 | +// In-memory storage for tests |
| 69 | +const vectorStores = new Map<string, any>(); |
| 70 | +const files = new Map<string, any>(); |
| 71 | +const vectorStoreFiles = new Map<string, any[]>(); |
| 72 | + |
| 73 | +export function resetMockStorage() { |
| 74 | + vectorStores.clear(); |
| 75 | + files.clear(); |
| 76 | + vectorStoreFiles.clear(); |
| 77 | +} |
| 78 | + |
| 79 | +/** |
| 80 | + * MSW handlers for lightspeed-core vector store endpoints |
| 81 | + * These mock the endpoints created in lightspeed-core that proxy to llama stack |
| 82 | + */ |
| 83 | +export const lightspeedCoreHandlers: HttpHandler[] = [ |
| 84 | + // Create vector store |
| 85 | + http.post(`${LIGHTSPEED_CORE_ADDR}/v1/vector-stores`, async ({ request }) => { |
| 86 | + const body = (await request.json()) as any; |
| 87 | + const id = `vs-${Date.now()}`; |
| 88 | + const vectorStore = { |
| 89 | + id, |
| 90 | + name: body.name, |
| 91 | + embedding_model: body.embedding_model, |
| 92 | + embedding_dimension: body.embedding_dimension, |
| 93 | + provider_id: body.provider_id, |
| 94 | + metadata: body.metadata || {}, |
| 95 | + }; |
| 96 | + vectorStores.set(id, vectorStore); |
| 97 | + vectorStoreFiles.set(id, []); |
| 98 | + return HttpResponse.json(vectorStore); |
| 99 | + }), |
| 100 | + |
| 101 | + // Get vector store |
| 102 | + http.get(`${LIGHTSPEED_CORE_ADDR}/v1/vector-stores/:id`, ({ params }) => { |
| 103 | + const { id } = params; |
| 104 | + const vectorStore = vectorStores.get(id as string); |
| 105 | + if (!vectorStore) { |
| 106 | + return HttpResponse.json( |
| 107 | + { detail: 'Vector store not found' }, |
| 108 | + { status: 404 }, |
| 109 | + ); |
| 110 | + } |
| 111 | + return HttpResponse.json(vectorStore); |
| 112 | + }), |
| 113 | + |
| 114 | + // Update vector store |
| 115 | + http.put( |
| 116 | + `${LIGHTSPEED_CORE_ADDR}/v1/vector-stores/:id`, |
| 117 | + async ({ params, request }) => { |
| 118 | + const { id } = params; |
| 119 | + const vectorStore = vectorStores.get(id as string); |
| 120 | + if (!vectorStore) { |
| 121 | + return HttpResponse.json( |
| 122 | + { detail: 'Vector store not found' }, |
| 123 | + { status: 404 }, |
| 124 | + ); |
| 125 | + } |
| 126 | + const body = (await request.json()) as any; |
| 127 | + const updated = { |
| 128 | + ...vectorStore, |
| 129 | + metadata: body.metadata || vectorStore.metadata, |
| 130 | + }; |
| 131 | + vectorStores.set(id as string, updated); |
| 132 | + return HttpResponse.json(updated); |
| 133 | + }, |
| 134 | + ), |
| 135 | + |
| 136 | + // Delete vector store |
| 137 | + http.delete(`${LIGHTSPEED_CORE_ADDR}/v1/vector-stores/:id`, ({ params }) => { |
| 138 | + const { id } = params; |
| 139 | + if (!vectorStores.has(id as string)) { |
| 140 | + return HttpResponse.json( |
| 141 | + { detail: 'Vector store not found' }, |
| 142 | + { status: 404 }, |
| 143 | + ); |
| 144 | + } |
| 145 | + vectorStores.delete(id as string); |
| 146 | + vectorStoreFiles.delete(id as string); |
| 147 | + return HttpResponse.json({ deleted: true }); |
| 148 | + }), |
| 149 | + |
| 150 | + // List vector stores |
| 151 | + http.get(`${LIGHTSPEED_CORE_ADDR}/v1/vector-stores`, () => { |
| 152 | + const data = Array.from(vectorStores.values()); |
| 153 | + return HttpResponse.json({ data }); |
| 154 | + }), |
| 155 | + |
| 156 | + // Upload file |
| 157 | + http.post(`${LIGHTSPEED_CORE_ADDR}/v1/files`, async () => { |
| 158 | + const fileId = `file-${Date.now()}`; |
| 159 | + const file = { |
| 160 | + id: fileId, |
| 161 | + created_at: Date.now(), |
| 162 | + purpose: 'assistants', |
| 163 | + }; |
| 164 | + files.set(fileId, file); |
| 165 | + return HttpResponse.json(file); |
| 166 | + }), |
| 167 | + |
| 168 | + // Add file to vector store |
| 169 | + http.post( |
| 170 | + `${LIGHTSPEED_CORE_ADDR}/v1/vector-stores/:id/files`, |
| 171 | + async ({ params, request }) => { |
| 172 | + const { id } = params; |
| 173 | + const vectorStore = vectorStores.get(id as string); |
| 174 | + if (!vectorStore) { |
| 175 | + return HttpResponse.json( |
| 176 | + { detail: 'Vector store not found' }, |
| 177 | + { status: 404 }, |
| 178 | + ); |
| 179 | + } |
| 180 | + |
| 181 | + const body = (await request.json()) as any; |
| 182 | + const vectorStoreFile = { |
| 183 | + id: body.file_id, |
| 184 | + status: 'completed' as const, |
| 185 | + created_at: Date.now(), |
| 186 | + chunks_count: 1, |
| 187 | + attributes: body.attributes || {}, |
| 188 | + }; |
| 189 | + |
| 190 | + const storeFiles = vectorStoreFiles.get(id as string) || []; |
| 191 | + storeFiles.push(vectorStoreFile); |
| 192 | + vectorStoreFiles.set(id as string, storeFiles); |
| 193 | + |
| 194 | + return HttpResponse.json(vectorStoreFile); |
| 195 | + }, |
| 196 | + ), |
| 197 | + |
| 198 | + // List files in vector store |
| 199 | + http.get( |
| 200 | + `${LIGHTSPEED_CORE_ADDR}/v1/vector-stores/:id/files`, |
| 201 | + ({ params }) => { |
| 202 | + const { id } = params; |
| 203 | + const storeFiles = vectorStoreFiles.get(id as string) || []; |
| 204 | + return HttpResponse.json({ data: storeFiles }); |
| 205 | + }, |
| 206 | + ), |
| 207 | + |
| 208 | + // Get file from vector store |
| 209 | + http.get( |
| 210 | + `${LIGHTSPEED_CORE_ADDR}/v1/vector-stores/:id/files/:fileId`, |
| 211 | + ({ params }) => { |
| 212 | + const { id, fileId } = params; |
| 213 | + const storeFiles = vectorStoreFiles.get(id as string) || []; |
| 214 | + const file = storeFiles.find(f => f.id === fileId); |
| 215 | + if (!file) { |
| 216 | + return HttpResponse.json({ detail: 'File not found' }, { status: 404 }); |
| 217 | + } |
| 218 | + return HttpResponse.json(file); |
| 219 | + }, |
| 220 | + ), |
| 221 | + |
| 222 | + // Delete file from vector store |
| 223 | + http.delete( |
| 224 | + `${LIGHTSPEED_CORE_ADDR}/v1/vector-stores/:id/files/:fileId`, |
| 225 | + ({ params }) => { |
| 226 | + const { id, fileId } = params; |
| 227 | + const storeFiles = vectorStoreFiles.get(id as string) || []; |
| 228 | + const fileIndex = storeFiles.findIndex(f => f.id === fileId); |
| 229 | + if (fileIndex === -1) { |
| 230 | + return HttpResponse.json({ detail: 'File not found' }, { status: 404 }); |
| 231 | + } |
| 232 | + storeFiles.splice(fileIndex, 1); |
| 233 | + vectorStoreFiles.set(id as string, storeFiles); |
| 234 | + return HttpResponse.json({ deleted: true }); |
| 235 | + }, |
| 236 | + ), |
| 237 | +]; |
0 commit comments