feat: brain.get() metadata-only optimization - Phase 2 (testing)

Fixed convertMetadataToEntity() to properly extract custom metadata fields
using same destructuring pattern as baseStorage.getNoun(). This ensures
metadata is correctly populated in metadata-only entities.

Test Updates:
- Fixed unit tests to add { includeVectors: true } where vectors are checked
- Created comprehensive brain.get() optimization tests (11 tests, all passing)
- Created VFS performance integration tests
- Fixed invalid NounType references (NounType.Place → NounType.Location)
- Adjusted performance expectations for MemoryStorage (10%+ vs 75%+ for FS)

Files Updated:
- src/brainy.ts: Fixed convertMetadataToEntity() destructuring
- tests/unit/brainy-get-optimization.test.ts: New comprehensive tests
- tests/unit/brainy/get.test.ts: Added includeVectors where needed
- tests/unit/brainy/batch-operations.test.ts: Added includeVectors
- tests/unit/brainy/update.test.ts: Added includeVectors
- tests/unit/brainy/add.test.ts: Added includeVectors (3 tests)
- tests/brainy-3.test.ts: Added includeVectors
This commit is contained in:
David Snelling 2025-11-18 15:41:57 -08:00
parent 8dcf299fe7
commit f2f6a6c939
7 changed files with 276 additions and 28 deletions

View file

@ -67,8 +67,8 @@ describe('Brainy.add()', () => {
// Assert
expect(id).toBeDefined()
// Verify entity was stored with correct vector
const entity = await brain.get(id)
// Verify entity was stored with correct vector - v5.11.1: Need includeVectors
const entity = await brain.get(id, { includeVectors: true })
expect(entity).not.toBeNull()
expect(entity!.vector).toEqual(vector)
expect(entity!.metadata.name).toBe('Pre-vectorized item')
@ -201,8 +201,8 @@ describe('Brainy.add()', () => {
// Act
const id = await brain.add(params)
// Assert
const entity = await brain.get(id)
// Assert - v5.11.1: Need includeVectors to check vectors
const entity = await brain.get(id, { includeVectors: true })
expect(entity).not.toBeNull()
expect(entity!.type).toBe('product')
// The JSON should be embedded as a string representation
@ -430,10 +430,11 @@ describe('Brainy.add()', () => {
const id = await brain.add(params)
// Assert - Brainy stores vectors as-is without normalization
const entity = await brain.get(id)
// v5.11.1: Need includeVectors to check vectors
const entity = await brain.get(id, { includeVectors: true })
expect(entity).not.toBeNull()
expect(entity!.vector).toEqual(unnormalizedVector)
// Verify it's not normalized
const magnitude = Math.sqrt(
entity!.vector.reduce((sum: number, val: number) => sum + val * val, 0)

View file

@ -121,9 +121,9 @@ describe('Brainy Batch Operations', () => {
const result = await brain.addMany({ items: entities })
// All should have vectors
// All should have vectors - v5.11.1: Need includeVectors
for (const id of result.successful) {
const entity = await brain.get(id)
const entity = await brain.get(id, { includeVectors: true })
expect(entity?.vector).toBeDefined()
expect(entity?.vector?.length).toBeGreaterThan(0)
}

View file

@ -55,10 +55,10 @@ describe('Brainy.get()', () => {
metadata: { vectorized: true }
})
const id = await brain.add(params)
// Act
const entity = await brain.get(id)
// Act - v5.11.1: Need includeVectors to get vector data
const entity = await brain.get(id, { includeVectors: true })
// Assert
expect(entity).not.toBeNull()
expect(entity!.vector).toEqual(vector)

View file

@ -139,17 +139,18 @@ describe('Brainy.update()', () => {
type: 'thing'
}))
const original = await brain.get(id)
// v5.11.1: Need includeVectors to check vectors
const original = await brain.get(id, { includeVectors: true })
const originalVector = original!.vector
// Act - Update with new data triggers re-embedding
await brain.update({
id,
data: 'Completely different text content'
})
// Assert
const updated = await brain.get(id)
const updated = await brain.get(id, { includeVectors: true })
expect(updated).not.toBeNull()
// Vector should be different after re-embedding
expect(updated!.vector).not.toEqual(originalVector)