brainy/tests/unit/brainy-core.unit.test.ts
David Snelling 1aa1f22d22 🧠 Brainy 2.0.0 - Zero-Configuration AI Database with Triple Intelligence™
MAJOR RELEASE: Complete evolution of Brainy with groundbreaking features and performance.

🎯 KEY FEATURES:
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 Triple Intelligence™ Engine
  - Unified Vector + Metadata + Graph search
  - O(log n) performance on all operations
  - 3ms average search latency at any scale

 API Consolidation
  - 15+ search methods → 2 clean APIs
  - search() for vector similarity
  - find() for natural language queries

 Natural Language Processing
  - 220+ pre-computed NLP patterns
  - Instant context understanding
  - "Show me recent React components with tests"

 Zero Configuration
  - Works instantly, no setup required
  - Built-in embedding models (no API keys)
  - Smart defaults for everything
  - Automatic optimization

 Enterprise Features (Free for Everyone)
  - Scales to 10M+ items
  - Write-Ahead Logging (WAL) for durability
  - Distributed architecture with sharding
  - Read/write separation
  - Connection pooling & request deduplication
  - Built-in monitoring & health checks

 Universal Compatibility
  - Node.js, Browser, Edge Workers
  - 4 Storage Adapters (Memory, FileSystem, OPFS, S3)
  - TypeScript with full type safety
  - Worker-based embeddings

📦 WHAT'S INCLUDED:
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
• Core AI Database with HNSW indexing
• 19 Production-ready augmentations
• Universal Memory Manager
• Complete CLI with all commands
• Brain Cloud integration (soulcraft.com)
• Comprehensive documentation
• 52 test files with 400+ tests
• Migration guide from 1.x

📊 PERFORMANCE:
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
• Initialize: 450ms (24MB memory)
• Search: 3ms average (up to 10M items)
• Metadata Filter: 0.8ms (O(log n))
• Bulk Import: 2.3s per 1000 items
• Production Scale: 5.8ms at 10M items

🔧 TECHNICAL IMPROVEMENTS:
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
• TypeScript compilation: 153 errors → 0
• Memory usage: 200MB → 24MB baseline
• Circular dependencies resolved
• Worker thread communication fixed
• Storage adapter consistency
• Request coalescing for 3x performance

🛠️ CLI FEATURES:
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
• brainy add - Smart data ingestion
• brainy find - Natural language search
• brainy search - Vector similarity
• brainy chat - AI conversation mode
• brainy cloud - Brain Cloud integration
• brainy augment - Manage extensions
• 100% API compatibility

📚 DOCUMENTATION:
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
• Professional README with examples
• Quick Start guide (5 minutes)
• Enterprise Features guide
• Migration guide from 1.x
• API reference
• Architecture documentation

🌟 USE CASES:
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
• AI memory layer for chatbots
• Semantic document search
• Code intelligence platforms
• Knowledge management systems
• Real-time recommendation engines
• Customer support automation

MIT License - Enterprise features included free for everyone.
No premium tiers, no paywalls, no limits.

Built with ❤️ by the Brainy community.
Visit https://soulcraft.com for Brain Cloud integration.
2025-08-26 12:32:21 -07:00

290 lines
No EOL
9.6 KiB
TypeScript

/**
* Unit Tests for Brainy Core Functionality
*
* Tests business logic with mocked AI - fast and reliable
* Based on industry practices from HuggingFace, etc.
*/
import { describe, it, expect, beforeEach } from 'vitest'
import { BrainyData } from '../../dist/index.js'
import { mockEmbedding } from '../setup-unit.js'
describe('Brainy Core (Unit Tests)', () => {
let brain: BrainyData
beforeEach(async () => {
// Create instance with mocked embedding for fast, reliable tests
brain = new BrainyData({
storage: { forceMemoryStorage: true },
verbose: false,
embeddingFunction: mockEmbedding
})
await brain.init()
await brain.clearAll({ force: true })
})
describe('CRUD Operations', () => {
it('should create items with addNoun', async () => {
const id = await brain.addNoun({
name: 'JavaScript',
type: 'language'
})
expect(id).toBeTypeOf('string')
expect(id.length).toBeGreaterThan(0)
})
it('should retrieve items with getNoun', async () => {
const testData = { name: 'Python', type: 'language', year: 1991 }
const id = await brain.addNoun(testData)
const retrieved = await brain.getNoun(id)
expect(retrieved).toBeTruthy()
expect(retrieved?.metadata?.name).toBe('Python')
expect(retrieved?.metadata?.type).toBe('language')
expect(retrieved?.metadata?.year).toBe(1991)
})
it('should update items with updateNoun', async () => {
const id = await brain.addNoun({ name: 'TypeScript', version: '4.0' })
await brain.updateNoun(id, { version: '5.0', popularity: 'high' })
const updated = await brain.getNoun(id)
expect(updated?.metadata?.version).toBe('5.0')
expect(updated?.metadata?.popularity).toBe('high')
expect(updated?.metadata?.name).toBe('TypeScript') // Original data preserved
})
it('should delete items with deleteNoun', async () => {
const id = await brain.addNoun({ name: 'ToDelete', temp: true })
// Verify it exists
expect(await brain.getNoun(id)).toBeTruthy()
// Delete it
await brain.deleteNoun(id)
// Verify it's gone
expect(await brain.getNoun(id)).toBeNull()
})
it('should handle non-existent IDs according to API contract', async () => {
const fakeId = 'non-existent-id'
expect(await brain.getNoun(fakeId)).toBeNull()
// updateNoun should throw for non-existent ID (matches existing error handling tests)
await expect(brain.updateNoun(fakeId, { test: 'data' })).rejects.toThrow()
// deleteNoun should return false for non-existent ID (soft failure)
expect(await brain.deleteNoun(fakeId)).toBe(false)
})
})
describe('Search Operations (Mocked AI)', () => {
beforeEach(async () => {
// Add test data
await brain.addNoun({ name: 'React', type: 'framework', category: 'frontend' })
await brain.addNoun({ name: 'Vue', type: 'framework', category: 'frontend' })
await brain.addNoun({ name: 'Express', type: 'framework', category: 'backend' })
await brain.addNoun({ name: 'Java', type: 'language', category: 'backend' })
})
it('should return search results with mocked embeddings', async () => {
const results = await brain.search('frontend framework', { limit: 5 })
expect(results).toBeInstanceOf(Array)
expect(results.length).toBeGreaterThan(0)
expect(results.length).toBeLessThanOrEqual(5)
// Each result should have required structure
results.forEach(result => {
expect(result).toHaveProperty('id')
expect(result).toHaveProperty('metadata')
expect(result).toHaveProperty('score')
})
})
it('should respect search limits', async () => {
const results1 = await brain.search('framework', { limit: 1 })
const results2 = await brain.search('framework', { limit: 2 })
const results3 = await brain.search('framework', { limit: 10 })
expect(results1).toHaveLength(1)
expect(results2).toHaveLength(2)
expect(results3.length).toBeLessThanOrEqual(4) // We only have 4 items total
})
})
describe('Brain Patterns (Metadata Filtering)', () => {
beforeEach(async () => {
// Add test data with various metadata
await brain.addNoun({ name: 'Django', type: 'framework', year: 2005, language: 'Python' })
await brain.addNoun({ name: 'FastAPI', type: 'framework', year: 2018, language: 'Python' })
await brain.addNoun({ name: 'Rails', type: 'framework', year: 2004, language: 'Ruby' })
await brain.addNoun({ name: 'Spring', type: 'framework', year: 2002, language: 'Java' })
})
it('should filter by exact metadata match', async () => {
const pythonFrameworks = await brain.search('*', { limit: 10,
metadata: {
type: 'framework',
language: 'Python'
}
})
expect(pythonFrameworks).toHaveLength(2)
pythonFrameworks.forEach(item => {
expect(item.metadata?.language).toBe('Python')
expect(item.metadata?.type).toBe('framework')
})
})
it('should handle range queries with Brain Patterns', async () => {
const modernFrameworks = await brain.search('*', { limit: 10,
metadata: {
type: 'framework',
year: { greaterThan: 2010 }
}
})
expect(modernFrameworks).toHaveLength(1) // Only FastAPI (2018)
expect(modernFrameworks[0].metadata?.name).toBe('FastAPI')
})
it('should handle multiple range conditions', async () => {
const earlyFrameworks = await brain.search('*', { limit: 10,
metadata: {
year: {
greaterThan: 2000,
lessThan: 2010
}
}
})
expect(earlyFrameworks).toHaveLength(2) // Django (2005) and Rails (2004)
earlyFrameworks.forEach(item => {
expect(item.metadata?.year).toBeGreaterThan(2000)
expect(item.metadata?.year).toBeLessThan(2010)
})
})
it('should return empty results for non-matching filters', async () => {
const results = await brain.search('*', { limit: 10,
metadata: { language: 'NonExistent' }
})
expect(results).toHaveLength(0)
})
})
describe('Statistics and Monitoring', () => {
it('should provide basic statistics', async () => {
await brain.addNoun({ name: 'Item1' })
await brain.addNoun({ name: 'Item2' })
const stats = await brain.getStatistics()
expect(stats).toHaveProperty('nounCount')
expect(stats).toHaveProperty('verbCount')
expect(stats).toHaveProperty('hnswIndexSize')
expect(stats.nounCount).toBeGreaterThanOrEqual(2)
expect(stats.verbCount).toBe(0)
expect(typeof stats.hnswIndexSize).toBe('number')
})
it('should handle statistics for empty database', async () => {
const stats = await brain.getStatistics()
expect(stats.nounCount).toBe(0)
expect(stats.verbCount).toBe(0)
})
})
describe('Bulk Operations', () => {
it('should search all items with wildcard', async () => {
await brain.addNoun({ name: 'Item1', category: 'test' })
await brain.addNoun({ name: 'Item2', category: 'test' })
await brain.addNoun({ name: 'Item3', category: 'test' })
const allItems = await brain.search('*', { limit: 100 })
expect(allItems).toHaveLength(3)
allItems.forEach(item => {
expect(item).toHaveProperty('id')
expect(item).toHaveProperty('metadata')
expect(item.metadata?.category).toBe('test')
})
})
it('should clear database with clearAll', async () => {
await brain.addNoun({ name: 'Item1' })
await brain.addNoun({ name: 'Item2' })
// Verify items exist
expect((await brain.search('*', { limit: 100 }))).toHaveLength(2)
// Clear database
await brain.clearAll({ force: true })
// Verify empty
expect((await brain.search('*', { limit: 100 }))).toHaveLength(0)
expect((await brain.getStatistics()).nounCount).toBe(0)
})
it('should require force flag for clearAll', async () => {
await brain.addNoun({ name: 'Item1' })
await expect(brain.clearAll()).rejects.toThrow(/force.*true/)
// Data should still be there
expect((await brain.search('*', { limit: 100 }))).toHaveLength(1)
})
})
describe('Edge Cases and Error Handling', () => {
it('should handle empty string input', async () => {
const id = await brain.addNoun('')
expect(id).toBeTypeOf('string')
const retrieved = await brain.getNoun(id)
expect(retrieved).toBeTruthy()
})
it('should handle null/undefined metadata gracefully', async () => {
const id1 = await brain.addNoun(null as any)
const id2 = await brain.addNoun(undefined as any)
expect(id1).toBeTypeOf('string')
expect(id2).toBeTypeOf('string')
})
it('should handle complex nested metadata', async () => {
const complexData = {
name: 'Complex Item',
nested: {
level1: {
level2: {
deep: 'value'
}
}
},
array: [1, 2, 3, { nested: true }],
boolean: true,
number: 42
}
const id = await brain.addNoun(complexData)
const retrieved = await brain.getNoun(id)
expect(retrieved?.metadata?.nested?.level1?.level2?.deep).toBe('value')
expect(retrieved?.metadata?.array).toEqual([1, 2, 3, { nested: true }])
expect(retrieved?.metadata?.boolean).toBe(true)
expect(retrieved?.metadata?.number).toBe(42)
})
})
})