brainy/tests/unit/create-entities-default.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

90 lines
2.9 KiB
TypeScript

/**
* Test for createEntities default value bug fix (v4.3.2)
*
* Bug: If createEntities was undefined, it defaulted to false
* Fix: Now defaults to true when undefined
*/
import { describe, it, expect, beforeEach } from 'vitest'
import { Brainy, NounType } from '../../src/index.js'
import * as fs from 'fs'
import * as path from 'path'
describe('createEntities Default Value (v4.3.2 Bug Fix)', () => {
let brain: Brainy
// Unique testDir per test invocation so parallel vitest shards don't fight
// over the same writer lock on filesystem storage.
const testDir = path.join(
require('os').tmpdir(),
`brainy-create-entities-default-${process.pid}-${Math.random().toString(36).slice(2)}`
)
beforeEach(async () => {
if (fs.existsSync(testDir)) {
fs.rmSync(testDir, { recursive: true })
}
brain = new Brainy({ requireSubtype: false,
storage: {
type: 'filesystem',
path: testDir
}
})
await brain.init()
})
afterEach(async () => {
// Release the writer lock before cleaning up the directory, otherwise
// parallel vitest shards see a stale lockfile on retry.
if (brain) {
try { await brain.close() } catch { /* noop */ }
}
if (fs.existsSync(testDir)) {
fs.rmSync(testDir, { recursive: true })
}
})
it('should create graph entities when createEntities is undefined (default behavior)', async () => {
// Create a minimal CSV to import
const csvContent = `Name,Type
Alice,person
Bob,person
New York,location`
const csvPath = path.join(testDir, 'test.csv')
fs.mkdirSync(testDir, { recursive: true })
fs.writeFileSync(csvPath, csvContent)
// Import WITHOUT specifying createEntities (should default to true)
const result = await brain.import(csvPath, {
vfsPath: '/imports/test',
groupBy: 'flat'
// NOTE: createEntities is NOT specified - should default to true
})
console.log('\n📊 Import Result:')
console.log(` Entities created: ${result.stats.graphNodesCreated}`)
console.log(` VFS files created: ${result.stats.vfsFilesCreated}`)
// Verify graph entities were created
expect(result.stats.graphNodesCreated).toBeGreaterThan(0)
// Verify we can query by type. The exact match-count varies with the
// import path's neural-extraction behavior; the bug this test guards
// against is "createEntities defaulted off and no graph entities were
// produced" — covered by the >0 assertions below.
const people = await brain.find({ type: NounType.Person, limit: 10 })
console.log(`\n🔍 Type Filtering:`)
console.log(` Person filter: ${people.length}`)
expect(people.length).toBeGreaterThan(0)
const locations = await brain.find({ type: NounType.Location, limit: 10 })
console.log(` Location filter: ${locations.length}`)
expect(locations.length).toBeGreaterThan(0)
console.log('\n✅ Graph entities created by default!')
})
})