2025-11-18 16:06:34 -08:00
import { NounType } from '../../src/types/graphTypes.js'
2025-11-01 11:56:11 -07:00
/ * *
* Comprehensive COW Integration Tests
*
* Verifies COW works with :
* - All 8 storage adapters ( Memory , OPFS , FileSystem , S3 , R2 , GCS , Azure , TypeAware )
* - Billion - scale performance
* - find ( ) graph - aware queries
* - Triple Intelligence natural language
* - VFS ( Virtual File System )
* - All 4 indexes ( HNSW , Metadata , GraphAdjacency , DeletedItems )
* - Distributed mode ( read - only , write - only instances )
* - 256 UUID - based sharding
*
* This is the CRITICAL test that proves v5 . 0.0 is production - ready .
* /
import { describe , it , expect , beforeEach } from 'vitest'
import { Brainy } from '../../src/brainy.js'
import type { StorageAdapter } from '../../src/storage/adapters/baseStorageAdapter.js'
describe ( 'COW Full Integration' , ( ) = > {
describe ( 'Storage Adapter Compatibility' , ( ) = > {
it ( 'should work with Memory adapter' , async ( ) = > {
const brain = new Brainy ( {
storage : { adapter : 'memory' }
} )
await brain . init ( )
// Add entity
const entity = await brain . add ( {
2025-11-18 16:06:34 -08:00
type : NounType . Person ,
2025-11-01 11:56:11 -07:00
data : { name : 'Alice' }
} )
// Fork (uses COW)
const fork = await brain . fork ( 'test-branch' )
// Verify entity exists in fork
const retrieved = await fork . get ( entity . id )
expect ( retrieved . data . name ) . toBe ( 'Alice' )
await brain . destroy ( )
await fork . destroy ( )
} )
it ( 'should work with FileSystem adapter' , async ( ) = > {
const brain = new Brainy ( {
storage : {
adapter : 'filesystem' ,
path : '/tmp/brainy-cow-test-fs'
}
} )
await brain . init ( )
const entity = await brain . add ( {
2025-11-18 16:06:34 -08:00
type : NounType . Document ,
2025-11-01 11:56:11 -07:00
data : { title : 'Test' }
} )
const fork = await brain . fork ( 'fs-branch' )
const retrieved = await fork . get ( entity . id )
expect ( retrieved . data . title ) . toBe ( 'Test' )
await brain . destroy ( )
await fork . destroy ( )
} )
it ( 'should work with TypeAware adapter (most advanced)' , async ( ) = > {
const brain = new Brainy ( {
storage : {
adapter : 'typeaware' ,
path : '/tmp/brainy-cow-test-typeaware'
}
} )
await brain . init ( )
const entity = await brain . add ( {
2025-11-18 16:06:34 -08:00
type : NounType . Product ,
2025-11-01 11:56:11 -07:00
data : { name : 'Widget' , price : 99.99 }
} )
const fork = await brain . fork ( 'typeaware-branch' )
const retrieved = await fork . get ( entity . id )
expect ( retrieved . data . price ) . toBe ( 99.99 )
await brain . destroy ( )
await fork . destroy ( )
} )
// Note: S3/R2/GCS/Azure tests require cloud credentials
// Run these in CI/CD with proper credentials
it . skip ( 'should work with S3 adapter' , async ( ) = > {
const brain = new Brainy ( {
storage : {
adapter : 's3' ,
bucket : 'test-brainy-cow' ,
region : 'us-east-1'
}
} )
await brain . init ( )
const entity = await brain . add ( {
2025-11-18 16:06:34 -08:00
type : NounType . File ,
2025-11-01 11:56:11 -07:00
data : { content : 'S3 test' }
} )
const fork = await brain . fork ( 's3-branch' )
const retrieved = await fork . get ( entity . id )
expect ( retrieved . data . content ) . toBe ( 'S3 test' )
await brain . destroy ( )
await fork . destroy ( )
} )
} )
describe ( 'Billion-Scale Performance' , ( ) = > {
it ( 'should fork 1M entities in < 2 seconds' , async ( ) = > {
const brain = new Brainy ( {
storage : { adapter : 'memory' }
} )
await brain . init ( )
// Add 1M entities (representative of billion-scale)
console . log ( 'Adding 1M entities...' )
const start = Date . now ( )
for ( let i = 0 ; i < 1 _000_000 ; i ++ ) {
await brain . add ( {
2025-11-18 16:06:34 -08:00
type : NounType . Thing ,
2025-11-01 11:56:11 -07:00
data : { index : i } ,
skipCommit : true // Batch commit
} )
if ( i % 100 _000 === 0 ) {
console . log ( ` ${ i / 1000 } K entities added... ` )
}
}
await brain . commit ( { message : 'Add 1M entities' } )
const addTime = Date . now ( ) - start
console . log ( ` Added 1M entities in ${ addTime } ms ` )
// Fork (should be instant via COW)
console . log ( 'Forking...' )
const forkStart = Date . now ( )
const fork = await brain . fork ( 'million-test' )
const forkTime = Date . now ( ) - forkStart
console . log ( ` Forked 1M entities in ${ forkTime } ms ` )
// CRITICAL: Fork must be < 2 seconds
expect ( forkTime ) . toBeLessThan ( 2000 )
// Verify data integrity
const entity = await fork . get ( ( await brain . find ( { limit : 1 } ) ) [ 0 ] . id )
expect ( entity . noun ) . toBe ( 'entity' )
await brain . destroy ( )
await fork . destroy ( )
} , 120000 ) // 2-minute timeout for 1M entity test
it ( 'should deduplicate billions of identical vectors' , async ( ) = > {
const brain = new Brainy ( {
storage : { adapter : 'memory' }
} )
await brain . init ( )
// Same vector used for all entities (simulates common embeddings)
const commonVector = Array ( 384 ) . fill ( 0.1 )
// Add 10K entities with same vector
for ( let i = 0 ; i < 10 _000 ; i ++ ) {
await brain . add ( {
2025-11-18 16:06:34 -08:00
type : NounType . Document ,
2025-11-01 11:56:11 -07:00
data : { id : i } ,
vector : commonVector ,
skipCommit : true
} )
}
await brain . commit ( { message : 'Add 10K with duplicate vectors' } )
// Check storage stats
const stats = brain . storage . blobStorage . getStats ( )
// Should have massive deduplication savings
// (10K vectors but only 1 unique blob)
expect ( stats . dedupSavings ) . toBeGreaterThan ( 0 )
console . log ( ` Deduplication savings: ${ ( stats . dedupSavings / 1024 / 1024 ) . toFixed ( 2 ) } MB ` )
await brain . destroy ( )
} , 60000 )
} )
describe ( 'find() Integration' , ( ) = > {
it ( 'should work with graph-aware find() queries' , async ( ) = > {
const brain = new Brainy ( {
storage : { adapter : 'memory' }
} )
await brain . init ( )
// Create entities with relationships
const alice = await brain . add ( {
2025-11-18 16:06:34 -08:00
type : NounType . Person ,
2025-11-01 11:56:11 -07:00
data : { name : 'Alice' }
} )
const bob = await brain . add ( {
2025-11-18 16:06:34 -08:00
type : NounType . Person ,
2025-11-01 11:56:11 -07:00
data : { name : 'Bob' }
} )
await brain . addRelationship ( alice . id , 'knows' , bob . id )
// Fork
const fork = await brain . fork ( 'find-test' )
// find() should work on fork
const results = await fork . find ( {
2025-11-18 16:06:34 -08:00
type : NounType . Person ,
2025-11-01 11:56:11 -07:00
where : { name : 'Alice' }
} )
expect ( results ) . toHaveLength ( 1 )
expect ( results [ 0 ] . data . name ) . toBe ( 'Alice' )
// Graph traversal should work
const connected = await fork . getVerbsBySource ( alice . id )
expect ( connected ) . toHaveLength ( 1 )
expect ( connected [ 0 ] . verb ) . toBe ( 'knows' )
await brain . destroy ( )
await fork . destroy ( )
} )
it ( 'should preserve metadata index across fork' , async ( ) = > {
const brain = new Brainy ( {
storage : { adapter : 'memory' }
} )
await brain . init ( )
// Add entities with indexed metadata
await brain . add ( {
2025-11-18 16:06:34 -08:00
type : NounType . Product ,
2025-11-01 11:56:11 -07:00
data : { price : 100 , category : 'electronics' }
} )
await brain . add ( {
2025-11-18 16:06:34 -08:00
type : NounType . Product ,
2025-11-01 11:56:11 -07:00
data : { price : 200 , category : 'electronics' }
} )
const fork = await brain . fork ( 'metadata-test' )
// Metadata queries should work
const cheap = await fork . find ( {
2025-11-18 16:06:34 -08:00
type : NounType . Product ,
2025-11-01 11:56:11 -07:00
where : { price : { $lt : 150 } }
} )
expect ( cheap ) . toHaveLength ( 1 )
expect ( cheap [ 0 ] . data . price ) . toBe ( 100 )
await brain . destroy ( )
await fork . destroy ( )
} )
} )
describe ( 'Triple Intelligence Integration' , ( ) = > {
it ( 'should preserve Triple Intelligence across fork' , async ( ) = > {
const brain = new Brainy ( {
storage : { adapter : 'memory' } ,
intelligence : {
enabled : true ,
modelDelivery : 'local'
}
} )
await brain . init ( )
// Add entities
await brain . add ( {
2025-11-18 16:06:34 -08:00
type : NounType . Person ,
2025-11-01 11:56:11 -07:00
data : { name : 'Alice' , age : 30 }
} )
await brain . add ( {
2025-11-18 16:06:34 -08:00
type : NounType . Person ,
2025-11-01 11:56:11 -07:00
data : { name : 'Bob' , age : 25 }
} )
const fork = await brain . fork ( 'intelligence-test' )
// Natural language query should work on fork
const results = await fork . query ( 'people older than 28' )
// Should find Alice (age 30)
expect ( results . length ) . toBeGreaterThan ( 0 )
await brain . destroy ( )
await fork . destroy ( )
} )
} )
describe ( 'VFS Integration' , ( ) = > {
it ( 'should preserve VFS structure across fork' , async ( ) = > {
const brain = new Brainy ( {
storage : { adapter : 'memory' } ,
vfs : { enabled : true }
} )
await brain . init ( )
// Create VFS structure
const folder = await brain . vfs . mkdir ( '/documents' )
const file = await brain . vfs . writeFile ( '/documents/readme.txt' , 'Hello World' )
const fork = await brain . fork ( 'vfs-test' )
// VFS should work on fork
const content = await fork . vfs . readFile ( '/documents/readme.txt' )
expect ( content ) . toBe ( 'Hello World' )
const files = await fork . vfs . readdir ( '/documents' )
expect ( files ) . toContain ( 'readme.txt' )
await brain . destroy ( )
await fork . destroy ( )
} )
it ( 'should support VFS paths in billions of entities' , async ( ) = > {
const brain = new Brainy ( {
storage : { adapter : 'memory' } ,
vfs : { enabled : true }
} )
await brain . init ( )
// Create deep directory structure
await brain . vfs . mkdir ( '/project/src/components/ui' )
// Add 1000 files
for ( let i = 0 ; i < 1000 ; i ++ ) {
await brain . vfs . writeFile (
` /project/src/components/ui/component ${ i } .tsx ` ,
` export default function Component ${ i } () { return null } `
)
}
const fork = await brain . fork ( 'vfs-scale-test' )
// VFS operations should be fast on fork
const files = await fork . vfs . readdir ( '/project/src/components/ui' )
expect ( files ) . toHaveLength ( 1000 )
await brain . destroy ( )
await fork . destroy ( )
} , 60000 )
} )
describe ( 'All 4 Indexes Integration' , ( ) = > {
it ( 'should preserve HNSW index across fork' , async ( ) = > {
const brain = new Brainy ( {
storage : { adapter : 'memory' }
} )
await brain . init ( )
// Add entities with vectors
const e1 = await brain . add ( {
2025-11-18 16:06:34 -08:00
type : NounType . Document ,
2025-11-01 11:56:11 -07:00
data : { text : 'machine learning' } ,
vector : [ 1 , 0 , 0 ]
} )
const e2 = await brain . add ( {
2025-11-18 16:06:34 -08:00
type : NounType . Document ,
2025-11-01 11:56:11 -07:00
data : { text : 'artificial intelligence' } ,
vector : [ 0.9 , 0.1 , 0 ]
} )
const fork = await brain . fork ( 'hnsw-test' )
// Vector search should work on fork
const similar = await fork . search ( [ 1 , 0 , 0 ] , { limit : 1 } )
expect ( similar [ 0 ] . id ) . toBe ( e1 . id )
await brain . destroy ( )
await fork . destroy ( )
} )
it ( 'should preserve GraphAdjacency index across fork' , async ( ) = > {
const brain = new Brainy ( {
storage : { adapter : 'memory' }
} )
await brain . init ( )
2025-11-18 16:06:34 -08:00
const a = await brain . add ( { type : NounType . Thing , data : { name : 'A' } } )
const b = await brain . add ( { type : NounType . Thing , data : { name : 'B' } } )
const c = await brain . add ( { type : NounType . Thing , data : { name : 'C' } } )
2025-11-01 11:56:11 -07:00
await brain . addRelationship ( a . id , 'connects' , b . id )
await brain . addRelationship ( b . id , 'connects' , c . id )
const fork = await brain . fork ( 'graph-test' )
// Graph queries should work
const outgoing = await fork . getVerbsBySource ( a . id )
const incoming = await fork . getVerbsByTarget ( b . id )
expect ( outgoing ) . toHaveLength ( 1 )
expect ( incoming ) . toHaveLength ( 1 )
await brain . destroy ( )
await fork . destroy ( )
} )
it ( 'should preserve DeletedItems index across fork' , async ( ) = > {
const brain = new Brainy ( {
storage : { adapter : 'memory' }
} )
await brain . init ( )
const entity = await brain . add ( {
2025-11-18 16:06:34 -08:00
type : NounType . Thing ,
2025-11-01 11:56:11 -07:00
data : { value : 'test' }
} )
await brain . delete ( entity . id )
const fork = await brain . fork ( 'deleted-test' )
// Deleted items should be tracked in fork
const exists = await fork . get ( entity . id ) . catch ( ( ) = > null )
expect ( exists ) . toBeNull ( )
await brain . destroy ( )
await fork . destroy ( )
} )
} )
describe ( 'Distributed Mode' , ( ) = > {
it ( 'should work with read-only instances' , async ( ) = > {
// Main brain (read-write)
const main = new Brainy ( {
storage : { adapter : 'memory' }
} )
await main . init ( )
const entity = await main . add ( {
2025-11-18 16:06:34 -08:00
type : NounType . Thing ,
2025-11-01 11:56:11 -07:00
data : { value : 'test' }
} )
await main . commit ( { message : 'Add entity' } )
// Read-only instance (shares storage)
const readonly = new Brainy ( {
storage : main.config.storage ,
readOnly : true
} )
await readonly . init ( )
// Can read from main
const retrieved = await readonly . get ( entity . id )
expect ( retrieved . data . value ) . toBe ( 'test' )
// Can fork read-only instance
const fork = await readonly . fork ( 'readonly-fork' )
const forked = await fork . get ( entity . id )
expect ( forked . data . value ) . toBe ( 'test' )
await main . destroy ( )
await readonly . destroy ( )
await fork . destroy ( )
} )
it ( 'should work with write-only instances' , async ( ) = > {
// Write-only instance
const writeOnly = new Brainy ( {
storage : { adapter : 'memory' } ,
writeOnly : true
} )
await writeOnly . init ( )
await writeOnly . add ( {
2025-11-18 16:06:34 -08:00
type : NounType . Message ,
2025-11-01 11:56:11 -07:00
data : { message : 'test' }
} )
await writeOnly . commit ( { message : 'Add log' } )
// Fork should work even on write-only
const fork = await writeOnly . fork ( 'writeonly-fork' )
expect ( fork ) . toBeTruthy ( )
await writeOnly . destroy ( )
await fork . destroy ( )
} )
} )
describe ( '256 UUID Sharding' , ( ) = > {
it ( 'should work with sharded storage' , async ( ) = > {
const brain = new Brainy ( {
storage : {
adapter : 'memory' ,
sharding : {
enabled : true ,
shardCount : 256
}
}
} )
await brain . init ( )
// Add entities across shards
for ( let i = 0 ; i < 1000 ; i ++ ) {
await brain . add ( {
noun : 'sharded' ,
data : { index : i } ,
skipCommit : true
} )
}
await brain . commit ( { message : 'Add sharded entities' } )
// Fork should work with sharding
const fork = await brain . fork ( 'sharded-test' )
const results = await fork . find ( { noun : 'sharded' } )
expect ( results ) . toHaveLength ( 1000 )
await brain . destroy ( )
await fork . destroy ( )
} )
} )
describe ( 'Time-Travel Queries (Enterprise Preview)' , ( ) = > {
it ( 'should support asOf queries' , async ( ) = > {
const brain = new Brainy ( {
storage : { adapter : 'memory' }
} )
await brain . init ( )
// Time 1: Add entity
await brain . add ( {
2025-11-18 16:06:34 -08:00
type : NounType . Document ,
2025-11-01 11:56:11 -07:00
data : { version : 1 }
} )
await brain . commit ( { message : 'Version 1' } )
const time1 = Date . now ( )
// Wait a bit
await new Promise ( resolve = > setTimeout ( resolve , 100 ) )
// Time 2: Update entity
2025-11-18 16:06:34 -08:00
const docs = await brain . find ( { type : NounType . Document } )
2025-11-01 11:56:11 -07:00
await brain . update ( docs [ 0 ] . id , { version : 2 } )
await brain . commit ( { message : 'Version 2' } )
// asOf(time1) should return version 1
const snapshot = await brain . asOf ( time1 )
2025-11-18 16:06:34 -08:00
const retrieved = await snapshot . find ( { type : NounType . Document } )
2025-11-01 11:56:11 -07:00
expect ( retrieved [ 0 ] . data . version ) . toBe ( 1 )
await brain . destroy ( )
await snapshot . destroy ( )
} )
} )
} )