|
| 1 | +import axios from 'axios'; |
| 2 | +import DependenciesEmbeddingHandler from '../dependencies-embedding-handler'; |
| 3 | +import { Logger } from '@nestjs/common'; |
| 4 | + |
| 5 | +// Initialize a global logger instance |
| 6 | +const logger = new Logger('dependencies embed tester'); |
| 7 | + |
| 8 | +// Mock axios to control HTTP requests during tests |
| 9 | +jest.mock('axios'); |
| 10 | +const mockedAxios = axios as jest.Mocked<typeof axios>; |
| 11 | + |
| 12 | +// Mock fastembed to control embedding behavior during tests |
| 13 | +jest.mock('fastembed', () => ({ |
| 14 | + EmbeddingModel: { |
| 15 | + BGEBaseEN: 'BGEBaseEN', |
| 16 | + }, |
| 17 | + FlagEmbedding: { |
| 18 | + init: jest.fn().mockResolvedValue({ |
| 19 | + passageEmbed: jest.fn(async function* ( |
| 20 | + types: string[], |
| 21 | + batchSize: number, |
| 22 | + ) { |
| 23 | + for (const type of types) { |
| 24 | + // Yield simulated embedding data as Float32Array |
| 25 | + yield [new Float32Array([1, 2, 3])]; |
| 26 | + } |
| 27 | + }), |
| 28 | + queryEmbed: jest.fn(async (query: string) => [1, 2, 3]), |
| 29 | + }), |
| 30 | + }, |
| 31 | +})); |
| 32 | + |
| 33 | +describe('DependenciesEmbeddingHandler', () => { |
| 34 | + let handler: DependenciesEmbeddingHandler; |
| 35 | + |
| 36 | + beforeEach(() => { |
| 37 | + // Initialize a new instance of DependenciesEmbeddingHandler before each test |
| 38 | + handler = new DependenciesEmbeddingHandler(); |
| 39 | + // Clear all mock calls and instances before each test |
| 40 | + jest.clearAllMocks(); |
| 41 | + }); |
| 42 | + |
| 43 | + /** |
| 44 | + * Test Case: Successfully add a package with built-in type definitions |
| 45 | + * |
| 46 | + * Purpose: |
| 47 | + * - To verify that DependenciesEmbeddingHandler can correctly add a package that includes built-in type definitions. |
| 48 | + * - To ensure that the handler retrieves the package's package.json, extracts the type definitions, and generates embeddings. |
| 49 | + * |
| 50 | + * Steps: |
| 51 | + * 1. Mock axios.get to return a package.json containing the 'types' field. |
| 52 | + * 2. Mock axios.get to return the content of the type definitions file. |
| 53 | + * 3. Call the addPackage method to add the package. |
| 54 | + * 4. Verify that the package information is correctly stored, including the generated embedding. |
| 55 | + */ |
| 56 | + test('should successfully add a package with built-in types', async () => { |
| 57 | + // Mock the response for fetching package.json, including the 'types' field |
| 58 | + mockedAxios.get.mockImplementationOnce(() => |
| 59 | + Promise.resolve({ |
| 60 | + data: { |
| 61 | + name: 'test-package', |
| 62 | + version: '1.0.0', |
| 63 | + types: 'dist/index.d.ts', |
| 64 | + }, |
| 65 | + }), |
| 66 | + ); |
| 67 | + |
| 68 | + // Mock the response for fetching the type definitions file |
| 69 | + mockedAxios.get.mockImplementationOnce(() => |
| 70 | + Promise.resolve({ |
| 71 | + data: ` |
| 72 | + interface TestInterface { |
| 73 | + prop1: string; |
| 74 | + prop2: number; |
| 75 | + } |
| 76 | + |
| 77 | + type TestType = { |
| 78 | + field1: string; |
| 79 | + field2: boolean; |
| 80 | + }; |
| 81 | + `, |
| 82 | + }), |
| 83 | + ); |
| 84 | + |
| 85 | + // Add the package using the handler |
| 86 | + await handler.addPackage('test-package', '1.0.0'); |
| 87 | + |
| 88 | + // Retrieve the added package information |
| 89 | + const packageInfo = handler.getPackageInfo('test-package'); |
| 90 | + |
| 91 | + // Assertions to ensure the package was added correctly |
| 92 | + expect(packageInfo).toBeDefined(); |
| 93 | + expect(packageInfo?.name).toBe('test-package'); |
| 94 | + expect(packageInfo?.version).toBe('1.0.0'); |
| 95 | + expect(packageInfo?.embedding).toBeDefined(); |
| 96 | + }); |
| 97 | + |
| 98 | + /** |
| 99 | + * Test Case: Successfully search for relevant type definitions |
| 100 | + * |
| 101 | + * Purpose: |
| 102 | + * - To verify that DependenciesEmbeddingHandler can generate query embeddings from a search string and return the most relevant packages. |
| 103 | + * - To ensure that similarity calculations are accurate and results are correctly sorted based on similarity. |
| 104 | + * |
| 105 | + * Why the Search Returns Relevant Results: |
| 106 | + * - The `FlagEmbedding` mock is set up to return identical embeddings for both package types and the query. |
| 107 | + * - This setup ensures that the cosine similarity between the query embedding and each package's embedding is maximized for relevant packages. |
| 108 | + * - As a result, the search function can accurately identify and return the most relevant packages based on the query. |
| 109 | + * |
| 110 | + * Steps: |
| 111 | + * 1. Mock axios.get to return package.json and type definitions for two different packages. |
| 112 | + * 2. Call addPackage method to add both packages. |
| 113 | + * 3. Use a search query to call searchContext method. |
| 114 | + * 4. Verify that the search results contain the relevant package and are sorted by similarity. |
| 115 | + */ |
| 116 | + test('should successfully search for relevant type definitions', async () => { |
| 117 | + // Mock responses for the first package's package.json and type definitions |
| 118 | + mockedAxios.get |
| 119 | + .mockImplementationOnce(() => |
| 120 | + Promise.resolve({ |
| 121 | + data: { |
| 122 | + types: 'index.d.ts', |
| 123 | + }, |
| 124 | + }), |
| 125 | + ) |
| 126 | + .mockImplementationOnce(() => |
| 127 | + Promise.resolve({ |
| 128 | + data: ` |
| 129 | + interface UserInterface { |
| 130 | + id: string; |
| 131 | + name: string; |
| 132 | + email: string; |
| 133 | + } |
| 134 | + `, |
| 135 | + }), |
| 136 | + ) |
| 137 | + // Mock responses for the second package's package.json and type definitions |
| 138 | + .mockImplementationOnce(() => |
| 139 | + Promise.resolve({ |
| 140 | + data: { |
| 141 | + types: 'index.d.ts', |
| 142 | + }, |
| 143 | + }), |
| 144 | + ) |
| 145 | + .mockImplementationOnce(() => |
| 146 | + Promise.resolve({ |
| 147 | + data: ` |
| 148 | + interface ProductInterface { |
| 149 | + id: string; |
| 150 | + price: number; |
| 151 | + description: string; |
| 152 | + } |
| 153 | + `, |
| 154 | + }), |
| 155 | + ); |
| 156 | + |
| 157 | + // Add the first package 'user-package' |
| 158 | + await handler.addPackage('user-package', '1.0.0'); |
| 159 | + // Add the second package 'product-package' |
| 160 | + await handler.addPackage('product-package', '1.0.0'); |
| 161 | + |
| 162 | + const searchQuery = 'user interface with email'; |
| 163 | + |
| 164 | + // Log the search query |
| 165 | + logger.log('Search Query:', searchQuery); |
| 166 | + |
| 167 | + // Perform the search using the handler |
| 168 | + const results = await handler.searchContext(searchQuery); |
| 169 | + |
| 170 | + // Log the search results |
| 171 | + logger.log('Search Results:', results); |
| 172 | + |
| 173 | + // Assertions to ensure that search results are as expected |
| 174 | + expect(results.length).toBeGreaterThan(0); |
| 175 | + expect(results[0].types?.content[0]).toContain('UserInterface'); |
| 176 | + }, 100000); |
| 177 | +}); |
0 commit comments