import { describe, it, expect } from 'vitest' import { euclideanDistance } from '../src/utils/distance.js' /** * Helper function to create a 512-dimensional vector for testing * @param primaryIndex The index to set to 1.0, all other indices will be 0.0 * @returns A 512-dimensional vector with a single 1.0 value at the specified index */ function createTestVector(primaryIndex: number = 0): number[] { const vector = new Array(512).fill(0) vector[primaryIndex % 512] = 1.0 return vector } describe('Vector Operations', () => { it('should load brainy library successfully', async () => { const brainy = await import('../dist/unified.js') expect(brainy).toBeDefined() expect(typeof brainy.BrainyData).toBe('function') expect(brainy.environment).toBeDefined() }) it('should create and initialize BrainyData instance', async () => { const brainy = await import('../dist/unified.js') const db = new brainy.BrainyData({ distanceFunction: euclideanDistance }) expect(db).toBeDefined() expect(db.dimensions).toBe(512) await db.init() // If we get here without throwing, initialization was successful expect(true).toBe(true) }) it('should handle simple vector operations', async () => { const brainy = await import('../dist/unified.js') const db = new brainy.BrainyData({ distanceFunction: euclideanDistance }) await db.init() await db.clear() // Clear any existing data // Add a simple vector const testVector = createTestVector(1) await db.add(testVector, { id: 'test' }) // Search for the same vector const results = await db.search(testVector, 1) expect(results).toBeDefined() expect(results.length).toBeGreaterThan(0) expect(results[0].metadata.id).toBe('test') }) it('should handle multiple vector searches correctly', async () => { const brainy = await import('../dist/unified.js') const db = new brainy.BrainyData({ distanceFunction: euclideanDistance }) await db.init() await db.clear() // Clear any existing data // Add multiple vectors await db.add(createTestVector(0), { id: 'vec1', type: 'unit' }) await db.add(createTestVector(1), { id: 'vec2', type: 'unit' }) await db.add(createTestVector(2), { id: 'vec3', type: 'unit' }) // Create a mixed vector with two non-zero elements const mixedVector = createTestVector(3) mixedVector[4] = 0.5 await db.add(mixedVector, { id: 'vec4', type: 'mixed' }) // Search for multiple results const results = await db.search(createTestVector(0), 3) expect(results).toBeDefined() expect(results.length).toBeGreaterThanOrEqual(1) expect(results.length).toBeLessThanOrEqual(3) // The closest should be the exact match expect(results[0].metadata.id).toBe('vec1') }) })