2026-06-29 11:19:18 -07:00
/ * *
* @module tests / unit / brainy / find - complement - operators
* @description The negation / absence where - operators ( ` ne ` , ` exists:false ` ,
* ` missing:true ` ) are served as a roaring - bitmap difference over the int - id
* universe ( rather than materializing the whole corpus as UUID strings to
* filter ) . These tests pin the exact result set — including the load - bearing
* soft - delete semantic : ` field !== value ` MUST include entities that have no
* such field at all .
* /
2026-09-03 09:06:00 -07:00
import { describe , it , expect , beforeEach , afterEach } from 'vitest'
2026-06-29 11:19:18 -07:00
import { Brainy } from '../../../src/brainy'
import { NounType } from '../../../src/types/graphTypes'
describe ( 'find() complement operators (ne / exists:false / missing:true)' , ( ) = > {
let brain : Brainy < any >
// Three entities WITH a `status` field, two WITHOUT it.
const ids : Record < string , string > = { }
beforeEach ( async ( ) = > {
brain = new Brainy ( { requireSubtype : false , storage : { type : 'memory' } } )
await brain . init ( )
ids . active1 = await brain . add ( { data : 'a1' , type : NounType . Thing , metadata : { status : 'active' } } )
ids . active2 = await brain . add ( { data : 'a2' , type : NounType . Thing , metadata : { status : 'active' } } )
ids . closed = await brain . add ( { data : 'c' , type : NounType . Thing , metadata : { status : 'closed' } } )
ids . noField1 = await brain . add ( { data : 'n1' , type : NounType . Thing , metadata : { other : 1 } } )
ids . noField2 = await brain . add ( { data : 'n2' , type : NounType . Thing , metadata : { other : 2 } } )
} )
2026-09-03 09:06:00 -07:00
afterEach ( async ( ) = > {
await brain . close ( )
} )
2026-06-29 11:19:18 -07:00
it ( 'ne returns everything except the matching value — INCLUDING entities without the field' , async ( ) = > {
const rows = await brain . find ( { where : { status : { ne : 'active' } } , limit : 100 } )
const got = new Set ( rows . map ( ( r ) = > r . id ) )
// closed (status≠active) + the two without a status field at all.
expect ( got ) . toEqual ( new Set ( [ ids . closed , ids . noField1 , ids . noField2 ] ) )
expect ( got . has ( ids . active1 ) ) . toBe ( false )
expect ( got . has ( ids . active2 ) ) . toBe ( false )
} )
it ( 'exists:false returns exactly the entities without the field' , async ( ) = > {
const rows = await brain . find ( { where : { status : { exists : false } } , limit : 100 } )
expect ( new Set ( rows . map ( ( r ) = > r . id ) ) ) . toEqual ( new Set ( [ ids . noField1 , ids . noField2 ] ) )
} )
it ( 'missing:true matches exists:false (entities without the field)' , async ( ) = > {
const rows = await brain . find ( { where : { status : { missing : true } } , limit : 100 } )
expect ( new Set ( rows . map ( ( r ) = > r . id ) ) ) . toEqual ( new Set ( [ ids . noField1 , ids . noField2 ] ) )
} )
it ( 'exists:true returns exactly the entities with the field' , async ( ) = > {
const rows = await brain . find ( { where : { status : { exists : true } } , limit : 100 } )
expect ( new Set ( rows . map ( ( r ) = > r . id ) ) ) . toEqual ( new Set ( [ ids . active1 , ids . active2 , ids . closed ] ) )
} )
it ( 'the complement reflects deletes (a removed entity drops out of ne / exists:false)' , async ( ) = > {
await brain . remove ( ids . noField1 )
const ne = await brain . find ( { where : { status : { ne : 'active' } } , limit : 100 } )
expect ( new Set ( ne . map ( ( r ) = > r . id ) ) ) . toEqual ( new Set ( [ ids . closed , ids . noField2 ] ) )
const absent = await brain . find ( { where : { status : { exists : false } } , limit : 100 } )
expect ( new Set ( absent . map ( ( r ) = > r . id ) ) ) . toEqual ( new Set ( [ ids . noField2 ] ) )
} )
} )