The write side of the law, ruled 2026-08-03: data is either in main space where developers can use anything, or it is in system.*. - The reserved-name write door DIES: add/update/relate/updateRelation metadata bags accept EVERY name (confidence, type, id, data, level, content, ...) as ordinary user fields — indexed, filterable, sortable, aggregatable, identical to any other field. The remap/enforce/warn machinery, the reservedFieldPolicy config (now a typed init refusal), and the compile-time metadata key bans are all removed. The one write refusal left: keys spelled 'system.*' (namespace forgery), now enforced on all four write doors. - STORED RECORDS GO NESTED (v2): engine fields top-level, the user bag nested verbatim under 'metadata', sealed by a format stamp — by-name storage discrimination is unsound once colliders are admitted. Legacy flat records stay readable forever through the shape-aware splitters (sound for them: the old door refused colliders). Time travel rides the same split (generation store snapshots whole records). - Name-based index exclusions DIE: user frame indexes every name; the excludeFields/indexedFields knobs and their silent-[] holes are gone; bulk-payload protection is value-shape only, uniform across names. - Consumer-sweep findings fixed in the same wave: per-type counts read the frozen 'system.type' column (addToIndex sort, affinity tracking, cold-count rehydration, VFS type bitmaps — legacy 'noun' fallback for pre-rebuild reads); resolveHiddenIds addresses 'system.visibility' (bare 'visibility' was a silent no-op under the law — VFS/system entities leaked into default reads). - Fidelity fallout fixed in the owning layers: readEntityFieldAddress reads the bag first (colliders were absent-shadowed by its own guard) and never serves system addresses from the bag; blob history refs read the bag shape-aware; migration transforms now receive ONE normalized view (engine fields + nested bag) regardless of stored era, and stray flat-habit keys refuse with the fix in the message. - THE REOPEN-COLLIDER CONFORMANCE CASE (required before any RC counts as gates-green): all ten collider names + plumbing names written as user fields, verified verbatim + queryable across live reads, flush+reopen, a forced epoch rebuild, and asOf time travel; relation mirror; forgery refusals; legacy flat-record compat. 8/8 green. Gates: unit 1901/1901 (exit 0) · integration 758 (exit 0) · conformance 27/27 (exit 0) · consumer test sweep migrated (10 files).
348 lines
11 KiB
TypeScript
348 lines
11 KiB
TypeScript
/**
|
|
* Comprehensive All-APIs Test (v4.4.0)
|
|
*
|
|
* Systematically tests EVERY public API to verify:
|
|
* 1. Code is actually wired up
|
|
* 2. VFS filtering works correctly
|
|
* 3. Production quality (no errors, proper returns)
|
|
*
|
|
* APIs tested:
|
|
* - brain.add(), brain.get(), brain.update(), brain.remove()
|
|
* - brain.find(), brain.similar()
|
|
* - brain.relate(), brain.related(), brain.unrelate()
|
|
* - brain.addMany(), brain.updateMany(), brain.removeMany(), brain.relateMany()
|
|
* - vfs.* (init, mkdir, writeFile, readdir, readFile, stat, exists)
|
|
* - neural.* (similar, neighbors, outliers)
|
|
* - import.* (would test if CSV/Excel/PDF available)
|
|
*/
|
|
|
|
import { describe, it, expect, beforeAll, afterAll } from 'vitest'
|
|
import { Brainy } from '../../src/brainy.js'
|
|
import { NounType, VerbType } from '../../src/types/graphTypes.js'
|
|
import * as fs from 'fs'
|
|
import * as path from 'path'
|
|
|
|
describe('Comprehensive All-APIs Test', () => {
|
|
const testDir = path.join(process.cwd(), 'test-all-apis')
|
|
let brain: Brainy
|
|
|
|
beforeAll(async () => {
|
|
if (fs.existsSync(testDir)) {
|
|
fs.rmSync(testDir, { recursive: true, force: true })
|
|
}
|
|
fs.mkdirSync(testDir, { recursive: true })
|
|
|
|
brain = new Brainy({ requireSubtype: false,
|
|
storage: {
|
|
type: 'filesystem',
|
|
path: testDir
|
|
}
|
|
})
|
|
await brain.init()
|
|
})
|
|
|
|
afterAll(async () => {
|
|
// Close the brain so the writer lock is released and the background flush /
|
|
// heartbeat does not bleed into other suites (also avoids ~8s teardown hangs).
|
|
await brain.close()
|
|
if (fs.existsSync(testDir)) {
|
|
fs.rmSync(testDir, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
describe('Core Entity APIs', () => {
|
|
let entityId: string
|
|
|
|
it('brain.add() - should create entity', async () => {
|
|
entityId = await brain.add({
|
|
data: 'Test entity content',
|
|
type: NounType.Document,
|
|
metadata: { title: 'Test Doc', category: 'test' }
|
|
})
|
|
|
|
expect(typeof entityId).toBe('string')
|
|
expect(entityId.length).toBeGreaterThan(0)
|
|
console.log(`✅ brain.add() created: ${entityId}`)
|
|
})
|
|
|
|
it('brain.get() - should retrieve entity', async () => {
|
|
const entity = await brain.get(entityId)
|
|
|
|
expect(entity).not.toBeNull()
|
|
expect(entity?.id).toBe(entityId)
|
|
expect(entity?.type).toBe(NounType.Document)
|
|
expect(entity?.metadata?.title).toBe('Test Doc')
|
|
console.log(`✅ brain.get() retrieved entity`)
|
|
})
|
|
|
|
it('brain.update() - should update entity', async () => {
|
|
await brain.update({
|
|
id: entityId,
|
|
metadata: { updated: true }
|
|
})
|
|
|
|
const updated = await brain.get(entityId)
|
|
expect(updated?.metadata?.updated).toBe(true)
|
|
console.log(`✅ brain.update() updated metadata`)
|
|
})
|
|
|
|
it('brain.find() - should find entities (excludes VFS)', async () => {
|
|
const results = await brain.find({
|
|
where: { category: 'test' },
|
|
limit: 10
|
|
})
|
|
|
|
expect(results.length).toBeGreaterThan(0)
|
|
expect(results.some(r => r.id === entityId)).toBe(true)
|
|
expect(results.every(r => r.metadata?.isVFS !== true)).toBe(true) // No VFS
|
|
console.log(`✅ brain.find() found ${results.length} entities (no VFS)`)
|
|
})
|
|
|
|
it('brain.similar() - should find similar entities (excludes VFS)', async () => {
|
|
// Create another similar entity
|
|
await brain.add({
|
|
data: 'Another test document',
|
|
type: NounType.Document,
|
|
metadata: { category: 'test' }
|
|
})
|
|
|
|
const similar = await brain.similar({
|
|
to: entityId,
|
|
limit: 10
|
|
})
|
|
|
|
expect(Array.isArray(similar)).toBe(true)
|
|
expect(similar.every(r => r.metadata?.isVFS !== true)).toBe(true) // No VFS
|
|
console.log(`✅ brain.similar() found ${similar.length} similar entities (no VFS)`)
|
|
})
|
|
|
|
it('brain.remove() - should delete entity', async () => {
|
|
await brain.remove(entityId)
|
|
|
|
const deleted = await brain.get(entityId)
|
|
expect(deleted).toBeNull()
|
|
console.log(`✅ brain.remove() deleted entity`)
|
|
})
|
|
})
|
|
|
|
describe('Relationship APIs', () => {
|
|
let entity1Id: string
|
|
let entity2Id: string
|
|
let relationId: string
|
|
|
|
it('brain.relate() - should create relationship', async () => {
|
|
entity1Id = await brain.add({
|
|
data: 'Entity 1',
|
|
type: NounType.Concept
|
|
})
|
|
|
|
entity2Id = await brain.add({
|
|
data: 'Entity 2',
|
|
type: NounType.Concept
|
|
})
|
|
|
|
relationId = await brain.relate({
|
|
from: entity1Id,
|
|
to: entity2Id,
|
|
type: VerbType.RelatedTo
|
|
})
|
|
|
|
expect(typeof relationId).toBe('string')
|
|
console.log(`✅ brain.relate() created relation: ${relationId}`)
|
|
})
|
|
|
|
it('brain.related() - should retrieve relationships', async () => {
|
|
const relations = await brain.related({
|
|
from: entity1Id
|
|
})
|
|
|
|
expect(relations.length).toBeGreaterThan(0)
|
|
expect(relations.some(r => r.id === relationId)).toBe(true)
|
|
console.log(`✅ brain.related() found ${relations.length} relations`)
|
|
})
|
|
|
|
it('brain.unrelate() - should delete relationship', async () => {
|
|
await brain.unrelate(relationId)
|
|
|
|
const relations = await brain.related({
|
|
from: entity1Id
|
|
})
|
|
|
|
expect(relations.every(r => r.id !== relationId)).toBe(true)
|
|
console.log(`✅ brain.unrelate() deleted relation`)
|
|
})
|
|
})
|
|
|
|
describe('Batch APIs', () => {
|
|
it('brain.addMany() - should create multiple entities', async () => {
|
|
const result = await brain.addMany({
|
|
items: [
|
|
{ data: 'Batch 1', type: NounType.Document },
|
|
{ data: 'Batch 2', type: NounType.Document },
|
|
{ data: 'Batch 3', type: NounType.Document }
|
|
]
|
|
})
|
|
|
|
expect(result.successful.length).toBe(3)
|
|
expect(result.failed.length).toBe(0)
|
|
console.log(`✅ brain.addMany() created ${result.successful.length} entities`)
|
|
})
|
|
|
|
it('brain.relateMany() - should create multiple relationships', async () => {
|
|
const ids = await brain.addMany({
|
|
items: [
|
|
{ data: 'A', type: NounType.Concept },
|
|
{ data: 'B', type: NounType.Concept }
|
|
]
|
|
})
|
|
|
|
const relationIds = await brain.relateMany({
|
|
items: [
|
|
{ from: ids.successful[0], to: ids.successful[1], type: VerbType.References }
|
|
]
|
|
})
|
|
|
|
expect(relationIds.length).toBe(1)
|
|
console.log(`✅ brain.relateMany() created ${relationIds.length} relations`)
|
|
})
|
|
|
|
it('brain.removeMany() - should delete multiple entities', async () => {
|
|
const ids = await brain.addMany({
|
|
items: [
|
|
{ data: 'Delete 1', type: NounType.Document },
|
|
{ data: 'Delete 2', type: NounType.Document }
|
|
]
|
|
})
|
|
|
|
const result = await brain.removeMany({
|
|
ids: ids.successful
|
|
})
|
|
|
|
expect(result.successful.length).toBe(2)
|
|
console.log(`✅ brain.removeMany() deleted ${result.successful.length} entities`)
|
|
})
|
|
})
|
|
|
|
describe('VFS APIs', () => {
|
|
let vfs: any
|
|
|
|
it('vfs.init() - should initialize VFS', async () => {
|
|
vfs = brain.vfs
|
|
await vfs.init()
|
|
|
|
expect(vfs).toBeDefined()
|
|
console.log(`✅ vfs.init() initialized`)
|
|
})
|
|
|
|
it('vfs.mkdir() - should create directory', async () => {
|
|
await vfs.mkdir('/test-dir', { recursive: true })
|
|
|
|
const exists = await vfs.exists('/test-dir')
|
|
expect(exists).toBe(true)
|
|
console.log(`✅ vfs.mkdir() created directory`)
|
|
})
|
|
|
|
it('vfs.writeFile() - should create file', async () => {
|
|
await vfs.writeFile('/test-dir/file.txt', 'Hello World')
|
|
|
|
const exists = await vfs.exists('/test-dir/file.txt')
|
|
expect(exists).toBe(true)
|
|
console.log(`✅ vfs.writeFile() created file`)
|
|
})
|
|
|
|
it('vfs.readFile() - should read file content', async () => {
|
|
const content = await vfs.readFile('/test-dir/file.txt')
|
|
|
|
expect(content.toString()).toBe('Hello World')
|
|
console.log(`✅ vfs.readFile() read content`)
|
|
})
|
|
|
|
it('vfs.readdir() - should list directory', async () => {
|
|
// 8.0: readdir() returns string[] by default; pass { withFileTypes: true }
|
|
// to get VFSDirent objects (each with .name / .type / .path / .entityId).
|
|
const entries = await vfs.readdir('/test-dir', { withFileTypes: true })
|
|
|
|
expect(entries.length).toBeGreaterThan(0)
|
|
expect(entries.some((e: any) => e.name === 'file.txt')).toBe(true)
|
|
console.log(`✅ vfs.readdir() listed ${entries.length} entries`)
|
|
})
|
|
|
|
it('vfs.stat() - should get file stats', async () => {
|
|
const stats = await vfs.stat('/test-dir/file.txt')
|
|
|
|
// 8.0: VFSStats mirrors Node's fs.Stats — type is exposed via isFile() /
|
|
// isDirectory() / isSymbolicLink() predicates, not a `type` string field.
|
|
expect(stats.size).toBeGreaterThan(0)
|
|
expect(stats.isFile()).toBe(true)
|
|
expect(stats.isDirectory()).toBe(false)
|
|
console.log(`✅ vfs.stat() got stats: ${stats.size} bytes`)
|
|
})
|
|
|
|
it('VFS entities have isVFS flag', async () => {
|
|
const vfsEntities = await brain.find({
|
|
where: { path: '/test-dir/file.txt' },
|
|
includeVFS: true,
|
|
limit: 1
|
|
})
|
|
|
|
expect(vfsEntities.length).toBe(1)
|
|
expect(vfsEntities[0].metadata?.isVFS).toBe(true)
|
|
expect(vfsEntities[0].metadata?.path).toBe('/test-dir/file.txt')
|
|
console.log(`✅ VFS entities properly flagged with isVFS`)
|
|
})
|
|
|
|
it('VFS entities excluded from knowledge graph via excludeVFS', async () => {
|
|
// 8.0: find() INCLUDES VFS entities by default (one unified store). Pass
|
|
// excludeVFS:true to get a clean knowledge-graph view — this asserts that
|
|
// opt-in exclusion actually strips every VFS-flagged entity.
|
|
const knowledge = await brain.find({
|
|
type: NounType.Document,
|
|
excludeVFS: true,
|
|
limit: 100
|
|
})
|
|
|
|
const vfsCount = knowledge.filter(
|
|
r => r.metadata?.isVFS === true || r.metadata?.isVFSEntity === true
|
|
).length
|
|
expect(vfsCount).toBe(0)
|
|
console.log(`✅ Knowledge graph clean: 0 VFS entities leaked`)
|
|
})
|
|
})
|
|
|
|
describe('Production Quality Checks', () => {
|
|
it('should handle large batch operations', async () => {
|
|
const start = Date.now()
|
|
|
|
const result = await brain.addMany({
|
|
items: Array(100).fill(null).map((_, i) => ({
|
|
data: `Batch entity ${i}`,
|
|
type: NounType.Document
|
|
}))
|
|
})
|
|
|
|
const time = Date.now() - start
|
|
|
|
expect(result.successful.length).toBe(100)
|
|
expect(result.failed.length).toBe(0)
|
|
expect(time).toBeLessThan(30000) // < 30 seconds for 100 entities
|
|
console.log(`✅ Created 100 entities in ${time}ms`)
|
|
})
|
|
|
|
it('should handle metadata queries efficiently', async () => {
|
|
const start = Date.now()
|
|
|
|
// system.type — the legacy where.type→noun alias is dead; bare 'type'
|
|
// in where now addresses the user's own metadata field.
|
|
const results = await brain.find({
|
|
where: { 'system.type': NounType.Document },
|
|
limit: 100
|
|
})
|
|
|
|
const time = Date.now() - start
|
|
|
|
expect(results.length).toBeGreaterThan(0)
|
|
expect(time).toBeLessThan(1000) // < 1 second
|
|
console.log(`✅ Metadata query in ${time}ms`)
|
|
})
|
|
})
|
|
})
|