brainy/tests/integration/remaining-apis.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

372 lines
12 KiB
TypeScript

/**
* Remaining APIs Comprehensive Test (v4.4.0)
*
* Tests APIs that weren't covered in all-apis-comprehensive.test.ts:
* - brain.updateMany()
* - brain.import() (CSV/Excel/PDF with VFS)
* - brain.clear()
* - vfs file operations (unlink, rmdir, rename, copy, move)
*/
import { describe, it, expect, beforeAll, afterAll } from 'vitest'
import { Brainy } from '../../src/brainy.js'
import { NounType } from '../../src/types/graphTypes.js'
import * as fs from 'fs'
import * as path from 'path'
describe('Remaining APIs Comprehensive Test', () => {
const testDir = path.join(process.cwd(), 'test-remaining-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 background flush / writer-lock heartbeat stop before
// the dir is removed (prevents teardown timeouts and cross-test bleed).
await brain.close()
if (fs.existsSync(testDir)) {
fs.rmSync(testDir, { recursive: true, force: true })
}
})
describe('brain.updateMany()', () => {
it('should batch update multiple entities', async () => {
console.log('\n📋 Test: brain.updateMany()')
// Create test entities
const ids = await brain.addMany({
items: [
{ data: 'Entity 1', type: NounType.Document, metadata: { status: 'draft' } },
{ data: 'Entity 2', type: NounType.Document, metadata: { status: 'draft' } },
{ data: 'Entity 3', type: NounType.Document, metadata: { status: 'draft' } }
]
})
console.log(` Created ${ids.successful.length} entities`)
// Batch update all to published
await brain.updateMany({
items: ids.successful.map(id => ({
id,
metadata: { status: 'published', updatedAt: Date.now() }
}))
})
// Verify all updated (status changed from 'draft' to 'published')
for (const id of ids.successful) {
const entity = await brain.get(id)
expect(entity?.metadata?.status).toBe('published')
}
console.log(` ✅ brain.updateMany() updated ${ids.successful.length} entities`)
})
it('should handle partial failures gracefully', async () => {
console.log('\n📋 Test: brain.updateMany() error handling')
const validId = await brain.add({
data: 'Valid entity',
type: NounType.Document
})
// Mix valid and invalid IDs
await brain.updateMany({
items: [
{ id: validId, metadata: { updated: true } },
{ id: 'invalid-id-123', metadata: { updated: true } }
]
})
// Valid entity should be updated
const entity = await brain.get(validId)
expect(entity?.metadata?.updated).toBe(true)
console.log(` ✅ brain.updateMany() handled partial failures`)
})
})
describe('brain.import() with VFS', () => {
it('should import CSV and create VFS entities when vfsPath specified', async () => {
console.log('\n📋 Test: brain.import() with VFS')
// Create test CSV file
const csvPath = path.join(testDir, 'test-data.csv')
const csvContent = `name,age,role
Alice,30,Engineer
Bob,25,Designer
Carol,35,Manager`
fs.writeFileSync(csvPath, csvContent)
// Import with VFS path
const result = await brain.import(csvPath, {
vfsPath: '/imports/test-data.csv',
createEntities: true
})
console.log(` Imported ${result.stats.graphNodesCreated} entities`)
console.log(` VFS files: ${result.stats.vfsFilesCreated}`)
expect(result.stats.graphNodesCreated).toBeGreaterThan(0)
expect(result.stats.vfsFilesCreated).toBeGreaterThan(0)
// Verify VFS file was created
const vfs = brain.vfs
await vfs.init()
const exists = await vfs.exists('/imports/test-data.csv')
expect(exists).toBe(true)
// Verify entities were created (should exclude VFS by default)
// Note: Import creates entities based on CSV content
const allEntities = await brain.find({
limit: 100
})
// Filter out VFS entities
const knowledgeEntities = allEntities.filter(e => e.metadata?.isVFS !== true)
console.log(` Total entities: ${allEntities.length}, Knowledge: ${knowledgeEntities.length}`)
// Should have created at least one knowledge entity from CSV
expect(knowledgeEntities.length).toBeGreaterThan(0)
console.log(` ✅ brain.import() created VFS file and entities`)
})
it('should import without VFS when vfsPath not specified', async () => {
console.log('\n📋 Test: brain.import() without VFS')
// Create test CSV
const csvPath = path.join(testDir, 'no-vfs.csv')
const csvContent = `product,price
Widget,10
Gadget,20`
fs.writeFileSync(csvPath, csvContent)
// Import without VFS path
const result = await brain.import(csvPath, {
createEntities: true
})
console.log(` Imported ${result.stats.graphNodesCreated} entities`)
console.log(` VFS files: ${result.stats.vfsFilesCreated || 0}`)
expect(result.stats.graphNodesCreated).toBeGreaterThan(0)
// Note: Import always creates VFS files (uses default path if not specified)
expect(result.stats.vfsFilesCreated).toBeGreaterThan(0)
console.log(` ✅ brain.import() worked without VFS`)
})
})
describe('VFS File Operations', () => {
let vfs: any
beforeAll(async () => {
vfs = brain.vfs
await vfs.init()
})
it('vfs.unlink() - should delete files', async () => {
console.log('\n📋 Test: vfs.unlink()')
await vfs.writeFile('/test-unlink.txt', 'Delete me')
expect(await vfs.exists('/test-unlink.txt')).toBe(true)
await vfs.unlink('/test-unlink.txt')
expect(await vfs.exists('/test-unlink.txt')).toBe(false)
console.log(` ✅ vfs.unlink() deleted file`)
})
it('vfs.rmdir() - should remove directories', async () => {
console.log('\n📋 Test: vfs.rmdir()')
await vfs.mkdir('/test-dir-delete', { recursive: true })
expect(await vfs.exists('/test-dir-delete')).toBe(true)
await vfs.rmdir('/test-dir-delete')
expect(await vfs.exists('/test-dir-delete')).toBe(false)
console.log(` ✅ vfs.rmdir() removed directory`)
})
it('vfs.rmdir() - should remove directories recursively', async () => {
console.log('\n📋 Test: vfs.rmdir() recursive')
await vfs.mkdir('/nested/deep/dir', { recursive: true })
await vfs.writeFile('/nested/deep/dir/file.txt', 'content')
await vfs.rmdir('/nested', { recursive: true })
expect(await vfs.exists('/nested')).toBe(false)
console.log(` ✅ vfs.rmdir() removed directory recursively`)
})
it('vfs.rename() - should rename files', async () => {
console.log('\n📋 Test: vfs.rename()')
await vfs.writeFile('/old-name.txt', 'Content')
await vfs.rename('/old-name.txt', '/new-name.txt')
expect(await vfs.exists('/old-name.txt')).toBe(false)
expect(await vfs.exists('/new-name.txt')).toBe(true)
const content = await vfs.readFile('/new-name.txt')
expect(content.toString()).toBe('Content')
console.log(` ✅ vfs.rename() renamed file`)
})
it('vfs.rename() - should rename directories', async () => {
console.log('\n📋 Test: vfs.rename() directory')
await vfs.mkdir('/old-dir', { recursive: true })
await vfs.writeFile('/old-dir/file.txt', 'test')
await vfs.rename('/old-dir', '/new-dir')
expect(await vfs.exists('/old-dir')).toBe(false)
expect(await vfs.exists('/new-dir')).toBe(true)
expect(await vfs.exists('/new-dir/file.txt')).toBe(true)
console.log(` ✅ vfs.rename() renamed directory`)
})
it('vfs.copy() - should copy files', async () => {
console.log('\n📋 Test: vfs.copy()')
await vfs.writeFile('/source.txt', 'Original content')
await vfs.copy('/source.txt', '/destination.txt')
expect(await vfs.exists('/source.txt')).toBe(true)
expect(await vfs.exists('/destination.txt')).toBe(true)
const content = await vfs.readFile('/destination.txt')
expect(content.toString()).toBe('Original content')
console.log(` ✅ vfs.copy() copied file`)
})
it('vfs.copy() - should copy directories recursively', async () => {
console.log('\n📋 Test: vfs.copy() directory')
await vfs.mkdir('/copy-source', { recursive: true })
await vfs.writeFile('/copy-source/file1.txt', 'content1')
await vfs.writeFile('/copy-source/file2.txt', 'content2')
await vfs.copy('/copy-source', '/copy-dest', { recursive: true })
expect(await vfs.exists('/copy-source')).toBe(true)
expect(await vfs.exists('/copy-dest')).toBe(true)
expect(await vfs.exists('/copy-dest/file1.txt')).toBe(true)
expect(await vfs.exists('/copy-dest/file2.txt')).toBe(true)
console.log(` ✅ vfs.copy() copied directory recursively`)
})
it('vfs.move() - should move files', async () => {
console.log('\n📋 Test: vfs.move()')
await vfs.writeFile('/move-source.txt', 'Move me')
await vfs.move('/move-source.txt', '/move-dest.txt')
expect(await vfs.exists('/move-source.txt')).toBe(false)
expect(await vfs.exists('/move-dest.txt')).toBe(true)
const content = await vfs.readFile('/move-dest.txt')
expect(content.toString()).toBe('Move me')
console.log(` ✅ vfs.move() moved file`)
})
it('VFS operations preserve isVFS flag', async () => {
console.log('\n📋 Test: VFS operations preserve metadata')
await vfs.writeFile('/vfs-test.txt', 'test')
// Verify entity has isVFS flag
const entities = await brain.find({
where: { path: '/vfs-test.txt' },
includeVFS: true,
limit: 1
})
expect(entities.length).toBe(1)
expect(entities[0].metadata?.isVFS).toBe(true)
expect(entities[0].metadata?.vfsType).toBe('file')
console.log(` ✅ VFS operations preserve isVFS metadata`)
})
})
describe('Production Quality Verification', () => {
it('should handle large batch updates efficiently', async () => {
console.log('\n📋 Test: Large batch updateMany()')
const start = Date.now()
// Create 50 entities
const ids = await brain.addMany({
items: Array(50).fill(null).map((_, i) => ({
data: `Batch update entity ${i}`,
type: NounType.Document,
metadata: { version: 1 }
}))
})
// Batch update all
await brain.updateMany({
items: ids.successful.map(id => ({
id,
metadata: { version: 2, updated: true }
}))
})
const time = Date.now() - start
// Verify all updated
const sample = await brain.get(ids.successful[0])
expect(sample?.metadata?.version).toBe(2)
expect(sample?.metadata?.updated).toBe(true)
expect(time).toBeLessThan(10000) // < 10 seconds for 50 updates
console.log(` ✅ Updated 50 entities in ${time}ms`)
})
it('should handle VFS file operations at scale', async () => {
console.log('\n📋 Test: VFS operations at scale')
const vfs = brain.vfs
const start = Date.now()
// Create 20 files
for (let i = 0; i < 20; i++) {
await vfs.writeFile(`/scale-test/file${i}.txt`, `Content ${i}`)
}
// Copy all
await vfs.copy('/scale-test', '/scale-test-copy', { recursive: true })
// Verify
const entries = await vfs.readdir('/scale-test-copy')
const time = Date.now() - start
expect(entries.length).toBe(20)
expect(time).toBeLessThan(5000) // < 5 seconds
console.log(` ✅ Created and copied 20 files in ${time}ms`)
})
})
})