**feat(core, migration, docs): introduce dimension mismatch resolution tools and migration guide**
- **Core**: - Added `check-database.js` to verify database status and validate search functionality. - Created `fix-dimension-mismatch.js` to handle re-embedding of existing data to resolve dimension mismatch from 3 to 512. - Improved test cases by updating vector operations to support 512 dimensions, replacing previously hardcoded dimensions. - **Migration**: - Developed `DIMENSION_MISMATCH_SUMMARY.md`, detailing the root cause, solution, and preventive strategies for dimension mismatch issues. - Added `production-migration-guide.md` for structured production migration with detailed steps on re-embedding strategies, batching, and error handling. - **Tests**: - Enhanced test coverage with 512-dimensional vector validation. - Introduced helper functions for consistent vector testing behavior and streamlined search test cases. - **Documentation**: - Updated project documentation to highlight the resolution process for dimension mismatches, emphasizing preventive mechanisms such as auto-migration and version tracking. **Purpose**: Address critical dimension mismatch issues caused by embedding changes, restore functionality, and provide a roadmap for robust prevention strategies and migration processes.
This commit is contained in:
parent
b2b50b71c0
commit
a010d3a92c
11 changed files with 814 additions and 141 deletions
|
|
@ -5,6 +5,17 @@
|
|||
|
||||
import { describe, it, expect, beforeAll } from 'vitest'
|
||||
|
||||
/**
|
||||
* 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('Brainy Core Functionality', () => {
|
||||
let brainy: any
|
||||
|
||||
|
|
@ -42,17 +53,14 @@ describe('Brainy Core Functionality', () => {
|
|||
|
||||
describe('BrainyData Configuration', () => {
|
||||
it('should create instance with minimal configuration', () => {
|
||||
const data = new brainy.BrainyData({
|
||||
dimensions: 3
|
||||
})
|
||||
const data = new brainy.BrainyData({})
|
||||
|
||||
expect(data).toBeDefined()
|
||||
expect(data.dimensions).toBe(3)
|
||||
expect(data.dimensions).toBe(512)
|
||||
})
|
||||
|
||||
it('should create instance with full configuration', () => {
|
||||
const data = new brainy.BrainyData({
|
||||
dimensions: 128,
|
||||
metric: 'cosine',
|
||||
maxConnections: 32,
|
||||
efConstruction: 200,
|
||||
|
|
@ -60,29 +68,28 @@ describe('Brainy Core Functionality', () => {
|
|||
})
|
||||
|
||||
expect(data).toBeDefined()
|
||||
expect(data.dimensions).toBe(128)
|
||||
expect(data.dimensions).toBe(512)
|
||||
})
|
||||
|
||||
it('should validate configuration parameters', () => {
|
||||
it('should not throw with valid configuration parameters', () => {
|
||||
// Dimensions are now fixed at 512 and not configurable
|
||||
expect(() => {
|
||||
new brainy.BrainyData({
|
||||
dimensions: 0 // Invalid dimensions
|
||||
metric: 'cosine'
|
||||
})
|
||||
}).toThrow()
|
||||
}).not.toThrow()
|
||||
|
||||
expect(() => {
|
||||
new brainy.BrainyData({
|
||||
dimensions: -1 // Invalid dimensions
|
||||
metric: 'euclidean'
|
||||
})
|
||||
}).toThrow()
|
||||
}).not.toThrow()
|
||||
})
|
||||
|
||||
it('should use default values for optional parameters', () => {
|
||||
const data = new brainy.BrainyData({
|
||||
dimensions: 10
|
||||
})
|
||||
const data = new brainy.BrainyData({})
|
||||
|
||||
expect(data.dimensions).toBe(10)
|
||||
expect(data.dimensions).toBe(512)
|
||||
// Should have reasonable defaults for other parameters
|
||||
expect(data.maxConnections).toBeGreaterThan(0)
|
||||
expect(data.efConstruction).toBeGreaterThan(0)
|
||||
|
|
@ -92,20 +99,19 @@ describe('Brainy Core Functionality', () => {
|
|||
describe('Vector Operations', () => {
|
||||
it('should handle vector addition and search', async () => {
|
||||
const data = new brainy.BrainyData({
|
||||
dimensions: 3,
|
||||
metric: 'euclidean'
|
||||
})
|
||||
|
||||
await data.init()
|
||||
await data.clear() // Clear any existing data
|
||||
|
||||
// Add vectors
|
||||
await data.add([1, 0, 0], { id: 'v1', label: 'x-axis' })
|
||||
await data.add([0, 1, 0], { id: 'v2', label: 'y-axis' })
|
||||
await data.add([0, 0, 1], { id: 'v3', label: 'z-axis' })
|
||||
// Add vectors using helper function
|
||||
await data.add(createTestVector(0), { id: 'v1', label: 'x-axis' })
|
||||
await data.add(createTestVector(1), { id: 'v2', label: 'y-axis' })
|
||||
await data.add(createTestVector(2), { id: 'v3', label: 'z-axis' })
|
||||
|
||||
// Search for similar vector
|
||||
const results = await data.search([1, 0, 0], 1)
|
||||
const results = await data.search(createTestVector(0), 1)
|
||||
|
||||
expect(results).toBeDefined()
|
||||
expect(results.length).toBe(1)
|
||||
|
|
@ -114,7 +120,6 @@ describe('Brainy Core Functionality', () => {
|
|||
|
||||
it('should handle batch vector operations', async () => {
|
||||
const data = new brainy.BrainyData({
|
||||
dimensions: 2,
|
||||
metric: 'euclidean'
|
||||
})
|
||||
|
||||
|
|
@ -123,9 +128,9 @@ describe('Brainy Core Functionality', () => {
|
|||
|
||||
// Add multiple vectors
|
||||
const vectors = [
|
||||
{ vector: [1, 1], metadata: { id: 'batch1' } },
|
||||
{ vector: [2, 2], metadata: { id: 'batch2' } },
|
||||
{ vector: [3, 3], metadata: { id: 'batch3' } }
|
||||
{ vector: createTestVector(10), metadata: { id: 'batch1' } },
|
||||
{ vector: createTestVector(20), metadata: { id: 'batch2' } },
|
||||
{ vector: createTestVector(30), metadata: { id: 'batch3' } }
|
||||
]
|
||||
|
||||
for (const { vector, metadata } of vectors) {
|
||||
|
|
@ -133,18 +138,16 @@ describe('Brainy Core Functionality', () => {
|
|||
}
|
||||
|
||||
// Search should return results
|
||||
const results = await data.search([1.5, 1.5], 3)
|
||||
const results = await data.search(createTestVector(15), 3)
|
||||
expect(results.length).toBe(3)
|
||||
})
|
||||
|
||||
it('should handle different distance metrics', async () => {
|
||||
const euclideanData = new brainy.BrainyData({
|
||||
dimensions: 2,
|
||||
metric: 'euclidean'
|
||||
})
|
||||
|
||||
const cosineData = new brainy.BrainyData({
|
||||
dimensions: 2,
|
||||
metric: 'cosine'
|
||||
})
|
||||
|
||||
|
|
@ -155,7 +158,7 @@ describe('Brainy Core Functionality', () => {
|
|||
await euclideanData.clear()
|
||||
await cosineData.clear()
|
||||
|
||||
const vector = [1, 1]
|
||||
const vector = createTestVector(5)
|
||||
const metadata = { id: 'test' }
|
||||
|
||||
await euclideanData.add(vector, metadata)
|
||||
|
|
@ -237,7 +240,6 @@ describe('Brainy Core Functionality', () => {
|
|||
describe('Error Handling', () => {
|
||||
it('should handle invalid vector dimensions', async () => {
|
||||
const data = new brainy.BrainyData({
|
||||
dimensions: 3,
|
||||
metric: 'euclidean'
|
||||
})
|
||||
|
||||
|
|
@ -245,22 +247,20 @@ describe('Brainy Core Functionality', () => {
|
|||
|
||||
// Try to add vector with wrong dimensions
|
||||
await expect(data.add([1, 2], { id: 'wrong' })).rejects.toThrow()
|
||||
await expect(data.add([1, 2, 3, 4], { id: 'wrong' })).rejects.toThrow()
|
||||
await expect(data.add(new Array(100).fill(0), { id: 'wrong' })).rejects.toThrow()
|
||||
})
|
||||
|
||||
it('should handle search before initialization', async () => {
|
||||
const data = new brainy.BrainyData({
|
||||
dimensions: 2,
|
||||
metric: 'euclidean'
|
||||
})
|
||||
|
||||
// Try to search without initialization
|
||||
await expect(data.search([1, 2], 1)).rejects.toThrow()
|
||||
await expect(data.search(createTestVector(0), 1)).rejects.toThrow()
|
||||
})
|
||||
|
||||
it('should handle empty search results gracefully', async () => {
|
||||
const data = new brainy.BrainyData({
|
||||
dimensions: 2,
|
||||
metric: 'euclidean'
|
||||
})
|
||||
|
||||
|
|
@ -268,7 +268,7 @@ describe('Brainy Core Functionality', () => {
|
|||
await data.clear() // Clear any existing data
|
||||
|
||||
// Search in empty database
|
||||
const results = await data.search([1, 2], 1)
|
||||
const results = await data.search(createTestVector(0), 1)
|
||||
expect(results).toBeDefined()
|
||||
expect(Array.isArray(results)).toBe(true)
|
||||
expect(results.length).toBe(0)
|
||||
|
|
@ -278,7 +278,6 @@ describe('Brainy Core Functionality', () => {
|
|||
describe('Performance and Scalability', () => {
|
||||
it('should handle moderate number of vectors efficiently', async () => {
|
||||
const data = new brainy.BrainyData({
|
||||
dimensions: 10,
|
||||
metric: 'euclidean'
|
||||
})
|
||||
|
||||
|
|
@ -288,21 +287,14 @@ describe('Brainy Core Functionality', () => {
|
|||
|
||||
// Add 100 test vectors
|
||||
for (let i = 0; i < 100; i++) {
|
||||
const vector =
|
||||
globalThis.testUtils?.createTestVector(10) ||
|
||||
Array.from({ length: 10 }, (_, i) => (i + 1) / 10)
|
||||
await data.add(vector, { id: `item_${i}`, index: i })
|
||||
await data.add(createTestVector(i), { id: `item_${i}`, index: i })
|
||||
}
|
||||
|
||||
const addTime = Date.now() - startTime
|
||||
|
||||
// Search should be fast
|
||||
const searchStart = Date.now()
|
||||
const results = await data.search(
|
||||
globalThis.testUtils?.createTestVector(10) ||
|
||||
Array.from({ length: 10 }, (_, i) => (i + 1) / 10),
|
||||
10
|
||||
)
|
||||
const results = await data.search(createTestVector(50), 10)
|
||||
const searchTime = Date.now() - searchStart
|
||||
|
||||
expect(results.length).toBeLessThanOrEqual(10)
|
||||
|
|
@ -346,7 +338,6 @@ describe('Brainy Core Functionality', () => {
|
|||
describe('Database Statistics', () => {
|
||||
it('should provide accurate statistics about the database', async () => {
|
||||
const data = new brainy.BrainyData({
|
||||
dimensions: 3,
|
||||
metric: 'euclidean'
|
||||
})
|
||||
|
||||
|
|
@ -354,9 +345,9 @@ describe('Brainy Core Functionality', () => {
|
|||
await data.clear() // Clear any existing data
|
||||
|
||||
// Add some vectors (nouns)
|
||||
await data.add([1, 0, 0], { id: 'v1', label: 'x-axis' })
|
||||
await data.add([0, 1, 0], { id: 'v2', label: 'y-axis' })
|
||||
await data.add([0, 0, 1], { id: 'v3', label: 'z-axis' })
|
||||
await data.add(createTestVector(0), { id: 'v1', label: 'x-axis' })
|
||||
await data.add(createTestVector(1), { id: 'v2', label: 'y-axis' })
|
||||
await data.add(createTestVector(2), { id: 'v3', label: 'z-axis' })
|
||||
|
||||
// Add some connections (verbs)
|
||||
await data.connect('v1', 'v2', 'related_to')
|
||||
|
|
|
|||
|
|
@ -6,6 +6,17 @@
|
|||
|
||||
import { describe, it, expect, beforeAll, vi } from 'vitest'
|
||||
|
||||
/**
|
||||
* 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('Brainy in Browser Environment', () => {
|
||||
let brainy: any
|
||||
|
||||
|
|
@ -53,7 +64,6 @@ describe('Brainy in Browser Environment', () => {
|
|||
describe('Core Functionality - Add Data and Search', () => {
|
||||
it('should create database and add vector data', async () => {
|
||||
const db = new brainy.BrainyData({
|
||||
dimensions: 3,
|
||||
metric: 'euclidean',
|
||||
storage: {
|
||||
forceMemoryStorage: true
|
||||
|
|
@ -63,12 +73,12 @@ describe('Brainy in Browser Environment', () => {
|
|||
await db.init()
|
||||
|
||||
// Add some test vectors
|
||||
await db.add([1, 0, 0], { id: 'item1', label: 'x-axis' })
|
||||
await db.add([0, 1, 0], { id: 'item2', label: 'y-axis' })
|
||||
await db.add([0, 0, 1], { id: 'item3', label: 'z-axis' })
|
||||
await db.add(createTestVector(0), { id: 'item1', label: 'x-axis' })
|
||||
await db.add(createTestVector(1), { id: 'item2', label: 'y-axis' })
|
||||
await db.add(createTestVector(2), { id: 'item3', label: 'z-axis' })
|
||||
|
||||
// Search should work
|
||||
const results = await db.search([1, 0, 0], 1)
|
||||
const results = await db.search(createTestVector(0), 1)
|
||||
expect(results).toBeDefined()
|
||||
expect(results.length).toBe(1)
|
||||
expect(results[0].metadata.id).toBe('item1')
|
||||
|
|
@ -102,7 +112,6 @@ describe('Brainy in Browser Environment', () => {
|
|||
|
||||
it('should handle multiple data types', async () => {
|
||||
const db = new brainy.BrainyData({
|
||||
dimensions: 2,
|
||||
metric: 'euclidean',
|
||||
storage: {
|
||||
forceMemoryStorage: true
|
||||
|
|
@ -113,9 +122,9 @@ describe('Brainy in Browser Environment', () => {
|
|||
|
||||
// Add different types of data
|
||||
const testData = [
|
||||
{ vector: [1, 1], metadata: { type: 'point', name: 'A' } },
|
||||
{ vector: [2, 2], metadata: { type: 'point', name: 'B' } },
|
||||
{ vector: [3, 3], metadata: { type: 'point', name: 'C' } }
|
||||
{ vector: createTestVector(10), metadata: { type: 'point', name: 'A' } },
|
||||
{ vector: createTestVector(20), metadata: { type: 'point', name: 'B' } },
|
||||
{ vector: createTestVector(30), metadata: { type: 'point', name: 'C' } }
|
||||
]
|
||||
|
||||
for (const item of testData) {
|
||||
|
|
@ -123,7 +132,7 @@ describe('Brainy in Browser Environment', () => {
|
|||
}
|
||||
|
||||
// Search should return relevant results
|
||||
const results = await db.search([1.5, 1.5], 2)
|
||||
const results = await db.search(createTestVector(15), 2)
|
||||
expect(results.length).toBe(2)
|
||||
expect(
|
||||
results.every(
|
||||
|
|
@ -134,15 +143,14 @@ describe('Brainy in Browser Environment', () => {
|
|||
})
|
||||
|
||||
describe('Error Handling', () => {
|
||||
it('should handle invalid configurations gracefully', () => {
|
||||
it('should not throw with valid configuration', () => {
|
||||
expect(() => {
|
||||
new brainy.BrainyData({ dimensions: 0 })
|
||||
}).toThrow()
|
||||
new brainy.BrainyData({ metric: 'euclidean' })
|
||||
}).not.toThrow()
|
||||
})
|
||||
|
||||
it('should handle search on empty database', async () => {
|
||||
const db = new brainy.BrainyData({
|
||||
dimensions: 2,
|
||||
metric: 'euclidean',
|
||||
storage: {
|
||||
forceMemoryStorage: true
|
||||
|
|
@ -151,7 +159,7 @@ describe('Brainy in Browser Environment', () => {
|
|||
|
||||
await db.init()
|
||||
|
||||
const results = await db.search([1, 2], 5)
|
||||
const results = await db.search(createTestVector(0), 5)
|
||||
expect(results).toBeDefined()
|
||||
expect(Array.isArray(results)).toBe(true)
|
||||
expect(results.length).toBe(0)
|
||||
|
|
|
|||
|
|
@ -5,6 +5,17 @@
|
|||
|
||||
import { describe, it, expect, beforeAll } from 'vitest'
|
||||
|
||||
/**
|
||||
* 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('Brainy in Node.js Environment', () => {
|
||||
let brainy: any
|
||||
|
||||
|
|
@ -53,7 +64,6 @@ describe('Brainy in Node.js Environment', () => {
|
|||
return
|
||||
}
|
||||
const db = new brainy.BrainyData({
|
||||
dimensions: 3,
|
||||
metric: 'euclidean',
|
||||
storage: {
|
||||
forceMemoryStorage: true
|
||||
|
|
@ -64,12 +74,12 @@ describe('Brainy in Node.js Environment', () => {
|
|||
await db.clear() // Clear any existing data
|
||||
|
||||
// Add some test vectors
|
||||
await db.add([1, 0, 0], { id: 'item1', label: 'x-axis' })
|
||||
await db.add([0, 1, 0], { id: 'item2', label: 'y-axis' })
|
||||
await db.add([0, 0, 1], { id: 'item3', label: 'z-axis' })
|
||||
await db.add(createTestVector(0), { id: 'item1', label: 'x-axis' })
|
||||
await db.add(createTestVector(1), { id: 'item2', label: 'y-axis' })
|
||||
await db.add(createTestVector(2), { id: 'item3', label: 'z-axis' })
|
||||
|
||||
// Search should work
|
||||
const results = await db.search([1, 0, 0], 1)
|
||||
const results = await db.search(createTestVector(0), 1)
|
||||
expect(results).toBeDefined()
|
||||
expect(results.length).toBe(1)
|
||||
expect(results[0].metadata.id).toBe('item1')
|
||||
|
|
@ -114,7 +124,6 @@ describe('Brainy in Node.js Environment', () => {
|
|||
return
|
||||
}
|
||||
const db = new brainy.BrainyData({
|
||||
dimensions: 2,
|
||||
metric: 'euclidean',
|
||||
storage: {
|
||||
forceMemoryStorage: true
|
||||
|
|
@ -126,9 +135,9 @@ describe('Brainy in Node.js Environment', () => {
|
|||
|
||||
// Add different types of data
|
||||
const testData = [
|
||||
{ vector: [1, 1], metadata: { type: 'point', name: 'A' } },
|
||||
{ vector: [2, 2], metadata: { type: 'point', name: 'B' } },
|
||||
{ vector: [3, 3], metadata: { type: 'point', name: 'C' } }
|
||||
{ vector: createTestVector(10), metadata: { type: 'point', name: 'A' } },
|
||||
{ vector: createTestVector(20), metadata: { type: 'point', name: 'B' } },
|
||||
{ vector: createTestVector(30), metadata: { type: 'point', name: 'C' } }
|
||||
]
|
||||
|
||||
for (const item of testData) {
|
||||
|
|
@ -136,7 +145,7 @@ describe('Brainy in Node.js Environment', () => {
|
|||
}
|
||||
|
||||
// Search should return relevant results
|
||||
const results = await db.search([1.5, 1.5], 2)
|
||||
const results = await db.search(createTestVector(15), 2)
|
||||
expect(results.length).toBe(2)
|
||||
expect(
|
||||
results.every(
|
||||
|
|
@ -147,14 +156,14 @@ describe('Brainy in Node.js Environment', () => {
|
|||
})
|
||||
|
||||
describe('Error Handling', () => {
|
||||
it('should handle invalid configurations gracefully', () => {
|
||||
it('should not throw with valid configuration', () => {
|
||||
if (brainy === null) {
|
||||
console.warn('Skipping test due to TensorFlow.js initialization issue')
|
||||
return
|
||||
}
|
||||
expect(() => {
|
||||
new brainy.BrainyData({ dimensions: 0 })
|
||||
}).toThrow()
|
||||
new brainy.BrainyData({ metric: 'euclidean' })
|
||||
}).not.toThrow()
|
||||
})
|
||||
|
||||
it('should handle search on empty database', async () => {
|
||||
|
|
@ -163,7 +172,6 @@ describe('Brainy in Node.js Environment', () => {
|
|||
return
|
||||
}
|
||||
const db = new brainy.BrainyData({
|
||||
dimensions: 2,
|
||||
metric: 'euclidean',
|
||||
storage: {
|
||||
forceMemoryStorage: true
|
||||
|
|
@ -173,7 +181,7 @@ describe('Brainy in Node.js Environment', () => {
|
|||
await db.init()
|
||||
await db.clear() // Clear any existing data
|
||||
|
||||
const results = await db.search([1, 2], 5)
|
||||
const results = await db.search(createTestVector(0), 5)
|
||||
expect(results).toBeDefined()
|
||||
expect(Array.isArray(results)).toBe(true)
|
||||
expect(results.length).toBe(0)
|
||||
|
|
|
|||
|
|
@ -5,6 +5,17 @@
|
|||
|
||||
import { describe, it, expect, beforeAll } from 'vitest'
|
||||
|
||||
/**
|
||||
* 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('Brainy Statistics Functionality', () => {
|
||||
let brainy: any
|
||||
|
||||
|
|
@ -24,7 +35,6 @@ describe('Brainy Statistics Functionality', () => {
|
|||
it('should retrieve statistics from a BrainyData instance', async () => {
|
||||
// Create a BrainyData instance
|
||||
const data = new brainy.BrainyData({
|
||||
dimensions: 3,
|
||||
metric: 'euclidean'
|
||||
})
|
||||
|
||||
|
|
@ -32,12 +42,12 @@ describe('Brainy Statistics Functionality', () => {
|
|||
await data.clear() // Clear any existing data
|
||||
|
||||
// Add some test data
|
||||
await data.add([1, 0, 0], { id: 'v1', label: 'x-axis' })
|
||||
await data.add([0, 1, 0], { id: 'v2', label: 'y-axis' })
|
||||
await data.add([0, 0, 1], { id: 'v3', label: 'z-axis' })
|
||||
await data.add(createTestVector(0), { id: 'v1', label: 'x-axis' })
|
||||
await data.add(createTestVector(1), { id: 'v2', label: 'y-axis' })
|
||||
await data.add(createTestVector(2), { id: 'v3', label: 'z-axis' })
|
||||
|
||||
// Add a verb
|
||||
await data.addVerb('v1', 'v2', [0.5, 0.5, 0], { type: 'connected_to' })
|
||||
await data.addVerb('v1', 'v2', createTestVector(3), { type: 'connected_to' })
|
||||
|
||||
// Get statistics using the standalone function
|
||||
const stats = await brainy.getStatistics(data)
|
||||
|
|
@ -56,14 +66,12 @@ describe('Brainy Statistics Functionality', () => {
|
|||
|
||||
it('should match the instance method results', async () => {
|
||||
// Create a BrainyData instance
|
||||
const data = new brainy.BrainyData({
|
||||
dimensions: 3
|
||||
})
|
||||
const data = new brainy.BrainyData({})
|
||||
|
||||
await data.init()
|
||||
|
||||
// Add some test data
|
||||
await data.add([1, 1, 1], { id: 'test1' })
|
||||
await data.add(createTestVector(5), { id: 'test1' })
|
||||
|
||||
// Get statistics using both methods
|
||||
const instanceStats = await data.getStatistics()
|
||||
|
|
@ -76,7 +84,6 @@ describe('Brainy Statistics Functionality', () => {
|
|||
it('should track statistics by service', async () => {
|
||||
// Create a BrainyData instance
|
||||
const data = new brainy.BrainyData({
|
||||
dimensions: 3,
|
||||
metric: 'euclidean'
|
||||
})
|
||||
|
||||
|
|
@ -84,9 +91,9 @@ describe('Brainy Statistics Functionality', () => {
|
|||
await data.clear() // Clear any existing data
|
||||
|
||||
// Add data from different services
|
||||
await data.add([1, 0, 0], { id: 'v1', label: 'service1-item' }, { service: 'service1' })
|
||||
await data.add([0, 1, 0], { id: 'v2', label: 'service1-item' }, { service: 'service1' })
|
||||
await data.add([0, 0, 1], { id: 'v3', label: 'service2-item' }, { service: 'service2' })
|
||||
await data.add(createTestVector(10), { id: 'v1', label: 'service1-item' }, { service: 'service1' })
|
||||
await data.add(createTestVector(20), { id: 'v2', label: 'service1-item' }, { service: 'service1' })
|
||||
await data.add(createTestVector(30), { id: 'v3', label: 'service2-item' }, { service: 'service2' })
|
||||
|
||||
// Add verbs from different services
|
||||
await data.addVerb('v1', 'v2', undefined, { type: 'related_to', service: 'service1' })
|
||||
|
|
|
|||
|
|
@ -1,6 +1,17 @@
|
|||
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')
|
||||
|
|
@ -14,23 +25,21 @@ describe('Vector Operations', () => {
|
|||
const brainy = await import('../dist/unified.js')
|
||||
|
||||
const db = new brainy.BrainyData({
|
||||
dimensions: 3,
|
||||
distanceFunction: euclideanDistance
|
||||
})
|
||||
|
||||
expect(db).toBeDefined()
|
||||
expect(db.dimensions).toBe(3)
|
||||
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 2D vector operations', async () => {
|
||||
it('should handle simple vector operations', async () => {
|
||||
const brainy = await import('../dist/unified.js')
|
||||
|
||||
const db = new brainy.BrainyData({
|
||||
dimensions: 2,
|
||||
distanceFunction: euclideanDistance
|
||||
})
|
||||
|
||||
|
|
@ -38,10 +47,11 @@ describe('Vector Operations', () => {
|
|||
await db.clear() // Clear any existing data
|
||||
|
||||
// Add a simple vector
|
||||
await db.add([1, 2], { id: 'test' })
|
||||
const testVector = createTestVector(1)
|
||||
await db.add(testVector, { id: 'test' })
|
||||
|
||||
// Search for the same vector
|
||||
const results = await db.search([1, 2], 1)
|
||||
const results = await db.search(testVector, 1)
|
||||
|
||||
expect(results).toBeDefined()
|
||||
expect(results.length).toBeGreaterThan(0)
|
||||
|
|
@ -52,7 +62,6 @@ describe('Vector Operations', () => {
|
|||
const brainy = await import('../dist/unified.js')
|
||||
|
||||
const db = new brainy.BrainyData({
|
||||
dimensions: 3,
|
||||
distanceFunction: euclideanDistance
|
||||
})
|
||||
|
||||
|
|
@ -60,13 +69,17 @@ describe('Vector Operations', () => {
|
|||
await db.clear() // Clear any existing data
|
||||
|
||||
// Add multiple vectors
|
||||
await db.add([1, 0, 0], { id: 'vec1', type: 'unit' })
|
||||
await db.add([0, 1, 0], { id: 'vec2', type: 'unit' })
|
||||
await db.add([0, 0, 1], { id: 'vec3', type: 'unit' })
|
||||
await db.add([0.5, 0.5, 0], { id: 'vec4', type: 'mixed' })
|
||||
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([1, 0, 0], 3)
|
||||
const results = await db.search(createTestVector(0), 3)
|
||||
|
||||
expect(results).toBeDefined()
|
||||
expect(results.length).toBeGreaterThanOrEqual(1)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue