brainy/tests/integration/all-apis-comprehensive.test.ts
David Snelling 606445cd61 feat(8.0): API simplification — remove neural()/Db.search, one storage path key, integration→0
8.0 RC cleanup toward "one place per thing, zero-config, no deprecation":

- Remove the `brain.neural()` clustering namespace (ImprovedNeuralAPI + the dead
  legacy NeuralAPI + the neural CLI + neural-only types). Similarity is `find({vector})`
  / `similar({to})`; attribute grouping is the aggregation `GROUP BY` engine. The separate
  entity-extraction / smart-import feature (NeuralImport, NeuralEntityExtractor, SmartExtractor,
  NaturalLanguageProcessor, `brain.extract()`/`brain.nlp()`) is kept.
- Remove `Db.search()`; `find()` is the one query verb (accepts a bare string or FindParams).
  Fix the bundled MCP client, which called a non-existent `brain.search(query, limit)` →
  now `find({ query, limit })`.
- Storage config: collapse to one canonical top-level `path` key. The pre-8.0 aliases
  (`rootDirectory`, `options.*`, `fileSystemStorage.*`) are removed and now THROW with the
  exact rename instead of silently defaulting to `./brainy-data` on upgrade. A single resolver
  feeds createStorage, the 7.x→8.0 migration probe, and the plugin-factory handoff, so a native
  storage provider resolves the identical root (no split-brain).
- Fix `similar({ threshold })`: the min-similarity filter was silently dropped; it is now
  applied as a post-filter on `result.score` (the documented way to bound semantic results).
- Fix `vfs.rename()` on a directory: child path updates spread the entity vector into `update()`
  and failed dimension validation; they are metadata-only updates now.
- Fix `vfs.move()`: copy+delete orphaned the content-addressed content blob (the destination
  shared the source hash, then unlink removed it). `move()` now delegates to `rename()` — an
  in-place path change that preserves the blob and the entity id, for files and directories.
- Fix streaming import: the bulk fast path never flushed mid-import nor signalled queryability.
  Entity writes are now chunked by a progressive flush interval (100 → 1000 → 5000); each chunk
  flushes and emits `progress.queryable`, so imported data is queryable during the import.
- Sweep all docs, comments, and JSDoc for the removed/changed APIs.

Integration suite: 49 files / 588 passed / 0 failed. Unit: 80 files / 1456 passed, no type errors.
2026-06-20 13:31:11 -07:00

346 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()
const results = await brain.find({
where: { 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`)
})
})
})