2025-08-26 12:32:21 -07:00
|
|
|
/**
|
2025-09-11 16:23:32 -07:00
|
|
|
* Unit Tests for Brainy 3.0 Core Functionality
|
2025-08-26 12:32:21 -07:00
|
|
|
*
|
2025-09-11 16:23:32 -07:00
|
|
|
* Tests business logic with real embeddings - production ready
|
|
|
|
|
* No mocks, no fakes, real implementation
|
2025-08-26 12:32:21 -07:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import { describe, it, expect, beforeEach } from 'vitest'
|
2025-09-11 16:23:32 -07:00
|
|
|
import { Brainy } from '../../src/brainy.js'
|
|
|
|
|
import { NounType } from '../../src/types/graphTypes.js'
|
2025-08-26 12:32:21 -07:00
|
|
|
|
2025-09-11 16:23:32 -07:00
|
|
|
describe('Brainy 3.0 Core (Unit Tests)', () => {
|
|
|
|
|
let brain: Brainy
|
2025-08-26 12:32:21 -07:00
|
|
|
|
|
|
|
|
beforeEach(async () => {
|
2025-09-11 16:23:32 -07:00
|
|
|
// Create instance with real embeddings for production-ready tests
|
feat(8.0)!: flip requireSubtype default to true (BRAINY-8.0-SUBTYPE-CONTRACT § C-1)
Brainy 8.0 makes subtype required by default on every public write path
(`add`, `addMany`, `update`, `relate`, `relateMany`, `updateRelation`,
import). Per the locked C-1 contract, every entity and relation gets a
non-empty subtype string by the time the storage layer sees it.
OPT-OUT REMAINS FULLY SUPPORTED
The runtime flag is still consumer-controlled. Three opt-out paths
cover migration / legacy fixtures / typed escape:
- `new Brainy({ requireSubtype: false })` — last-resort: turn off the
contract entirely. Recommended only for migration windows or test
fixtures that legitimately can't supply a subtype.
- `new Brainy({ requireSubtype: { except: [NounType.Thing, ...] } })` —
per-type allowlist: strict everywhere except the listed types.
- `brain.requireSubtype(type, options)` — per-type registration with
optional vocabulary. Composes with the brain-wide flag.
Default is now `true`. Opt-out is explicit and documented; nothing
silently degrades.
TEST SWEEP
Bulk-applied `requireSubtype: false` to every `new Brainy({...})` call
site across 120 test files. Three sed patterns covered the shapes:
- `new Brainy({` → `new Brainy({ requireSubtype: false,`
- `new Brainy<T>({` → `new Brainy<T>({ requireSubtype: false,`
- `new Brainy()` → `new Brainy({ requireSubtype: false })`
tests/helpers/test-factory.ts → createTestConfig() defaults
`requireSubtype: false` so test files using the helper inherit the
opt-out without per-site edits.
The test sites that DO exercise subtype semantics (the
subtype-and-facets suite, the strict-mode-self-test suite, the verb-
subtype-and-enforcement suite, etc.) already pass real subtypes — they
were the 7.30.x acceptance tests for this contract. Those tests
continue to pass unchanged.
CHANGES
src/brainy.ts
- normalizeConfig() — `requireSubtype` default `false` → `true`.
Comment refreshed to document the three opt-out paths.
tests/* (120 files)
- Bulk-edited brain construction sites. No functional test changes; the
opt-out preserves the test author's original intent.
tests/helpers/test-factory.ts
- createTestConfig() base config gains `requireSubtype: false`.
NO-OP for consumers who were already passing subtype on every write.
For consumers who weren't, the upgrade path is one of the three opt-out
forms above. Migration recipe documented in 8.0 release notes (next
commit).
VERIFICATION
- npx tsc --noEmit: clean
- npm test: 1408 / 1409 (same pre-existing race-condition outstanding;
no other regressions from the flip)
2026-06-09 14:58:25 -07:00
|
|
|
brain = new Brainy({ requireSubtype: false,
|
refactor: remove augmentation system and semantic type matching
Remove the entire augmentation pipeline infrastructure (52 files,
~15,000 lines) and the semantic type matching system. These were
unused middleware layers adding complexity without value.
What was removed:
- src/augmentations/ directory (all augmentation implementations)
- src/augmentationManager.ts (pipeline orchestrator)
- src/types/augmentations.ts, src/types/pipelineTypes.ts
- src/shared/default-augmentations.ts
- Semantic type suggestion (BrainyTypes.suggestNoun/suggestVerb)
- src/utils/typeMatching/ (embedding-based type matcher)
What was preserved by relocating:
- Import handlers (CSV, PDF, Excel) -> src/importers/handlers/
- NeuralImportAugmentation -> src/cortex/neuralImportAugmentation.ts
- Type matching utilities -> heuristic inference in consumers
What was simplified:
- brainy.ts: operations call storage directly (no execute() wrapper)
- IntegrationBase: standalone class (no BaseAugmentation parent)
- BrainyTypes: validation-only (nouns, verbs, isValid*, get*)
- Pipeline: direct execution (no augmentation interception)
- index.ts: removed TypeSuggestion, suggestType exports
- package.json: removed stale types/augmentations export
Build passes, 1176 tests pass, 0 failures.
2026-02-01 10:48:56 -08:00
|
|
|
storage: { type: 'memory' }
|
2025-08-26 12:32:21 -07:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
await brain.init()
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
describe('CRUD Operations', () => {
|
2025-09-11 16:23:32 -07:00
|
|
|
it('should create items with add', async () => {
|
|
|
|
|
const id = await brain.add({
|
|
|
|
|
data: { name: 'JavaScript', type: 'language' },
|
|
|
|
|
type: NounType.Concept,
|
|
|
|
|
metadata: { category: 'programming' }
|
2025-08-26 12:32:21 -07:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
expect(id).toBeTypeOf('string')
|
|
|
|
|
expect(id.length).toBeGreaterThan(0)
|
|
|
|
|
})
|
|
|
|
|
|
2025-09-11 16:23:32 -07:00
|
|
|
it('should retrieve items with get', async () => {
|
|
|
|
|
const id = await brain.add({
|
feat: enforce data/metadata separation, numeric range queries, improved docs
- Store data opaquely in add() and update() instead of spreading object
properties into top-level metadata. data is for semantic search (HNSW),
metadata is for structured where-filter queries (MetadataIndex).
- Fix numeric range queries in MetadataIndex — use numeric-aware comparison
instead of lexicographic string comparison for normalized values.
- Add data field to RelateParams and Relation types for relationship content.
- Add where.type → where.noun alias in metadata-only find() path.
- Rewrite README: focused ~350 lines from 791, quick start first, feature
showcase with mini-snippets, organized doc links, no version callouts.
- Add DATA_MODEL.md and QUERY_OPERATORS.md reference docs.
- Remove 10 outdated/redundant doc files consolidated into API reference.
- Improve JSDoc on Entity, Relation, AddParams, FindParams, and core methods.
- Fix tests asserting data properties appear in metadata (data model violation).
- Deprecate verb.source/target in favor of from/to (public) and sourceId/targetId (storage).
2026-02-09 12:06:59 -08:00
|
|
|
data: 'Python is a programming language created in 1991',
|
2025-09-11 16:23:32 -07:00
|
|
|
type: NounType.Concept,
|
feat: enforce data/metadata separation, numeric range queries, improved docs
- Store data opaquely in add() and update() instead of spreading object
properties into top-level metadata. data is for semantic search (HNSW),
metadata is for structured where-filter queries (MetadataIndex).
- Fix numeric range queries in MetadataIndex — use numeric-aware comparison
instead of lexicographic string comparison for normalized values.
- Add data field to RelateParams and Relation types for relationship content.
- Add where.type → where.noun alias in metadata-only find() path.
- Rewrite README: focused ~350 lines from 791, quick start first, feature
showcase with mini-snippets, organized doc links, no version callouts.
- Add DATA_MODEL.md and QUERY_OPERATORS.md reference docs.
- Remove 10 outdated/redundant doc files consolidated into API reference.
- Improve JSDoc on Entity, Relation, AddParams, FindParams, and core methods.
- Fix tests asserting data properties appear in metadata (data model violation).
- Deprecate verb.source/target in favor of from/to (public) and sourceId/targetId (storage).
2026-02-09 12:06:59 -08:00
|
|
|
metadata: { name: 'Python', category: 'programming', year: 1991 }
|
2025-09-11 16:23:32 -07:00
|
|
|
})
|
feat: enforce data/metadata separation, numeric range queries, improved docs
- Store data opaquely in add() and update() instead of spreading object
properties into top-level metadata. data is for semantic search (HNSW),
metadata is for structured where-filter queries (MetadataIndex).
- Fix numeric range queries in MetadataIndex — use numeric-aware comparison
instead of lexicographic string comparison for normalized values.
- Add data field to RelateParams and Relation types for relationship content.
- Add where.type → where.noun alias in metadata-only find() path.
- Rewrite README: focused ~350 lines from 791, quick start first, feature
showcase with mini-snippets, organized doc links, no version callouts.
- Add DATA_MODEL.md and QUERY_OPERATORS.md reference docs.
- Remove 10 outdated/redundant doc files consolidated into API reference.
- Improve JSDoc on Entity, Relation, AddParams, FindParams, and core methods.
- Fix tests asserting data properties appear in metadata (data model violation).
- Deprecate verb.source/target in favor of from/to (public) and sourceId/targetId (storage).
2026-02-09 12:06:59 -08:00
|
|
|
|
2025-09-11 16:23:32 -07:00
|
|
|
const retrieved = await brain.get(id)
|
feat: enforce data/metadata separation, numeric range queries, improved docs
- Store data opaquely in add() and update() instead of spreading object
properties into top-level metadata. data is for semantic search (HNSW),
metadata is for structured where-filter queries (MetadataIndex).
- Fix numeric range queries in MetadataIndex — use numeric-aware comparison
instead of lexicographic string comparison for normalized values.
- Add data field to RelateParams and Relation types for relationship content.
- Add where.type → where.noun alias in metadata-only find() path.
- Rewrite README: focused ~350 lines from 791, quick start first, feature
showcase with mini-snippets, organized doc links, no version callouts.
- Add DATA_MODEL.md and QUERY_OPERATORS.md reference docs.
- Remove 10 outdated/redundant doc files consolidated into API reference.
- Improve JSDoc on Entity, Relation, AddParams, FindParams, and core methods.
- Fix tests asserting data properties appear in metadata (data model violation).
- Deprecate verb.source/target in favor of from/to (public) and sourceId/targetId (storage).
2026-02-09 12:06:59 -08:00
|
|
|
|
2025-08-26 12:32:21 -07:00
|
|
|
expect(retrieved).toBeTruthy()
|
|
|
|
|
expect(retrieved?.metadata?.name).toBe('Python')
|
feat: enforce data/metadata separation, numeric range queries, improved docs
- Store data opaquely in add() and update() instead of spreading object
properties into top-level metadata. data is for semantic search (HNSW),
metadata is for structured where-filter queries (MetadataIndex).
- Fix numeric range queries in MetadataIndex — use numeric-aware comparison
instead of lexicographic string comparison for normalized values.
- Add data field to RelateParams and Relation types for relationship content.
- Add where.type → where.noun alias in metadata-only find() path.
- Rewrite README: focused ~350 lines from 791, quick start first, feature
showcase with mini-snippets, organized doc links, no version callouts.
- Add DATA_MODEL.md and QUERY_OPERATORS.md reference docs.
- Remove 10 outdated/redundant doc files consolidated into API reference.
- Improve JSDoc on Entity, Relation, AddParams, FindParams, and core methods.
- Fix tests asserting data properties appear in metadata (data model violation).
- Deprecate verb.source/target in favor of from/to (public) and sourceId/targetId (storage).
2026-02-09 12:06:59 -08:00
|
|
|
expect(retrieved?.metadata?.category).toBe('programming')
|
2025-08-26 12:32:21 -07:00
|
|
|
expect(retrieved?.metadata?.year).toBe(1991)
|
|
|
|
|
})
|
|
|
|
|
|
2025-09-11 16:23:32 -07:00
|
|
|
it('should update items with update', async () => {
|
|
|
|
|
const id = await brain.add({
|
feat: enforce data/metadata separation, numeric range queries, improved docs
- Store data opaquely in add() and update() instead of spreading object
properties into top-level metadata. data is for semantic search (HNSW),
metadata is for structured where-filter queries (MetadataIndex).
- Fix numeric range queries in MetadataIndex — use numeric-aware comparison
instead of lexicographic string comparison for normalized values.
- Add data field to RelateParams and Relation types for relationship content.
- Add where.type → where.noun alias in metadata-only find() path.
- Rewrite README: focused ~350 lines from 791, quick start first, feature
showcase with mini-snippets, organized doc links, no version callouts.
- Add DATA_MODEL.md and QUERY_OPERATORS.md reference docs.
- Remove 10 outdated/redundant doc files consolidated into API reference.
- Improve JSDoc on Entity, Relation, AddParams, FindParams, and core methods.
- Fix tests asserting data properties appear in metadata (data model violation).
- Deprecate verb.source/target in favor of from/to (public) and sourceId/targetId (storage).
2026-02-09 12:06:59 -08:00
|
|
|
data: 'TypeScript is a typed JavaScript superset',
|
2025-09-11 16:23:32 -07:00
|
|
|
type: NounType.Concept,
|
feat: enforce data/metadata separation, numeric range queries, improved docs
- Store data opaquely in add() and update() instead of spreading object
properties into top-level metadata. data is for semantic search (HNSW),
metadata is for structured where-filter queries (MetadataIndex).
- Fix numeric range queries in MetadataIndex — use numeric-aware comparison
instead of lexicographic string comparison for normalized values.
- Add data field to RelateParams and Relation types for relationship content.
- Add where.type → where.noun alias in metadata-only find() path.
- Rewrite README: focused ~350 lines from 791, quick start first, feature
showcase with mini-snippets, organized doc links, no version callouts.
- Add DATA_MODEL.md and QUERY_OPERATORS.md reference docs.
- Remove 10 outdated/redundant doc files consolidated into API reference.
- Improve JSDoc on Entity, Relation, AddParams, FindParams, and core methods.
- Fix tests asserting data properties appear in metadata (data model violation).
- Deprecate verb.source/target in favor of from/to (public) and sourceId/targetId (storage).
2026-02-09 12:06:59 -08:00
|
|
|
metadata: { name: 'TypeScript', version: '4.0', category: 'programming' }
|
2025-09-11 16:23:32 -07:00
|
|
|
})
|
feat: enforce data/metadata separation, numeric range queries, improved docs
- Store data opaquely in add() and update() instead of spreading object
properties into top-level metadata. data is for semantic search (HNSW),
metadata is for structured where-filter queries (MetadataIndex).
- Fix numeric range queries in MetadataIndex — use numeric-aware comparison
instead of lexicographic string comparison for normalized values.
- Add data field to RelateParams and Relation types for relationship content.
- Add where.type → where.noun alias in metadata-only find() path.
- Rewrite README: focused ~350 lines from 791, quick start first, feature
showcase with mini-snippets, organized doc links, no version callouts.
- Add DATA_MODEL.md and QUERY_OPERATORS.md reference docs.
- Remove 10 outdated/redundant doc files consolidated into API reference.
- Improve JSDoc on Entity, Relation, AddParams, FindParams, and core methods.
- Fix tests asserting data properties appear in metadata (data model violation).
- Deprecate verb.source/target in favor of from/to (public) and sourceId/targetId (storage).
2026-02-09 12:06:59 -08:00
|
|
|
|
2025-09-11 16:23:32 -07:00
|
|
|
await brain.update({
|
|
|
|
|
id,
|
feat: enforce data/metadata separation, numeric range queries, improved docs
- Store data opaquely in add() and update() instead of spreading object
properties into top-level metadata. data is for semantic search (HNSW),
metadata is for structured where-filter queries (MetadataIndex).
- Fix numeric range queries in MetadataIndex — use numeric-aware comparison
instead of lexicographic string comparison for normalized values.
- Add data field to RelateParams and Relation types for relationship content.
- Add where.type → where.noun alias in metadata-only find() path.
- Rewrite README: focused ~350 lines from 791, quick start first, feature
showcase with mini-snippets, organized doc links, no version callouts.
- Add DATA_MODEL.md and QUERY_OPERATORS.md reference docs.
- Remove 10 outdated/redundant doc files consolidated into API reference.
- Improve JSDoc on Entity, Relation, AddParams, FindParams, and core methods.
- Fix tests asserting data properties appear in metadata (data model violation).
- Deprecate verb.source/target in favor of from/to (public) and sourceId/targetId (storage).
2026-02-09 12:06:59 -08:00
|
|
|
metadata: { version: '5.0', popularity: 'high' }
|
2025-09-11 16:23:32 -07:00
|
|
|
})
|
feat: enforce data/metadata separation, numeric range queries, improved docs
- Store data opaquely in add() and update() instead of spreading object
properties into top-level metadata. data is for semantic search (HNSW),
metadata is for structured where-filter queries (MetadataIndex).
- Fix numeric range queries in MetadataIndex — use numeric-aware comparison
instead of lexicographic string comparison for normalized values.
- Add data field to RelateParams and Relation types for relationship content.
- Add where.type → where.noun alias in metadata-only find() path.
- Rewrite README: focused ~350 lines from 791, quick start first, feature
showcase with mini-snippets, organized doc links, no version callouts.
- Add DATA_MODEL.md and QUERY_OPERATORS.md reference docs.
- Remove 10 outdated/redundant doc files consolidated into API reference.
- Improve JSDoc on Entity, Relation, AddParams, FindParams, and core methods.
- Fix tests asserting data properties appear in metadata (data model violation).
- Deprecate verb.source/target in favor of from/to (public) and sourceId/targetId (storage).
2026-02-09 12:06:59 -08:00
|
|
|
|
2025-09-11 16:23:32 -07:00
|
|
|
const updated = await brain.get(id)
|
2025-08-26 12:32:21 -07:00
|
|
|
expect(updated?.metadata?.version).toBe('5.0')
|
|
|
|
|
expect(updated?.metadata?.popularity).toBe('high')
|
feat: enforce data/metadata separation, numeric range queries, improved docs
- Store data opaquely in add() and update() instead of spreading object
properties into top-level metadata. data is for semantic search (HNSW),
metadata is for structured where-filter queries (MetadataIndex).
- Fix numeric range queries in MetadataIndex — use numeric-aware comparison
instead of lexicographic string comparison for normalized values.
- Add data field to RelateParams and Relation types for relationship content.
- Add where.type → where.noun alias in metadata-only find() path.
- Rewrite README: focused ~350 lines from 791, quick start first, feature
showcase with mini-snippets, organized doc links, no version callouts.
- Add DATA_MODEL.md and QUERY_OPERATORS.md reference docs.
- Remove 10 outdated/redundant doc files consolidated into API reference.
- Improve JSDoc on Entity, Relation, AddParams, FindParams, and core methods.
- Fix tests asserting data properties appear in metadata (data model violation).
- Deprecate verb.source/target in favor of from/to (public) and sourceId/targetId (storage).
2026-02-09 12:06:59 -08:00
|
|
|
expect(updated?.metadata?.name).toBe('TypeScript') // Original metadata preserved
|
2025-08-26 12:32:21 -07:00
|
|
|
})
|
|
|
|
|
|
2025-09-11 16:23:32 -07:00
|
|
|
it('should delete items with delete', async () => {
|
|
|
|
|
const id = await brain.add({
|
|
|
|
|
data: { name: 'ToDelete', temp: true },
|
|
|
|
|
type: NounType.Concept
|
|
|
|
|
})
|
2025-08-26 12:32:21 -07:00
|
|
|
|
|
|
|
|
// Verify it exists
|
2025-09-11 16:23:32 -07:00
|
|
|
expect(await brain.get(id)).toBeTruthy()
|
2025-08-26 12:32:21 -07:00
|
|
|
|
|
|
|
|
// Delete it
|
2026-06-11 14:51:00 -07:00
|
|
|
await brain.remove(id)
|
2025-08-26 12:32:21 -07:00
|
|
|
|
|
|
|
|
// Verify it's gone
|
2025-09-11 16:23:32 -07:00
|
|
|
expect(await brain.get(id)).toBeNull()
|
2025-08-26 12:32:21 -07:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('should handle non-existent IDs according to API contract', async () => {
|
2025-11-02 11:38:12 -08:00
|
|
|
// Use valid UUID format (stricter validation in v5.1.0)
|
2025-11-14 12:56:29 -08:00
|
|
|
// v5.10.0: Can't use 00000000... anymore (it's the VFS root)
|
|
|
|
|
const fakeId = '11111111-1111-1111-1111-111111111111'
|
2025-11-02 11:38:12 -08:00
|
|
|
|
2025-09-11 16:23:32 -07:00
|
|
|
expect(await brain.get(fakeId)).toBeNull()
|
2025-11-02 11:38:12 -08:00
|
|
|
|
2025-09-11 16:23:32 -07:00
|
|
|
// update should handle non-existent ID gracefully
|
2025-11-02 11:38:12 -08:00
|
|
|
await expect(brain.update({
|
|
|
|
|
id: fakeId,
|
|
|
|
|
data: { test: 'data' }
|
2025-09-11 16:23:32 -07:00
|
|
|
})).rejects.toThrow()
|
2025-11-02 11:38:12 -08:00
|
|
|
|
2025-09-11 16:23:32 -07:00
|
|
|
// delete should not throw for non-existent ID
|
2026-06-11 14:51:00 -07:00
|
|
|
await expect(brain.remove(fakeId)).resolves.not.toThrow()
|
2025-08-26 12:32:21 -07:00
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
2025-09-11 16:23:32 -07:00
|
|
|
describe('Search Operations', () => {
|
2025-08-26 12:32:21 -07:00
|
|
|
beforeEach(async () => {
|
2025-09-11 16:23:32 -07:00
|
|
|
// Add test data with real embeddings
|
|
|
|
|
await brain.add({
|
|
|
|
|
data: { name: 'React', type: 'framework', category: 'frontend' },
|
|
|
|
|
type: NounType.Concept,
|
|
|
|
|
metadata: { tags: ['ui', 'javascript'] }
|
|
|
|
|
})
|
|
|
|
|
await brain.add({
|
|
|
|
|
data: { name: 'Vue', type: 'framework', category: 'frontend' },
|
|
|
|
|
type: NounType.Concept,
|
|
|
|
|
metadata: { tags: ['ui', 'javascript'] }
|
|
|
|
|
})
|
|
|
|
|
await brain.add({
|
|
|
|
|
data: { name: 'Express', type: 'framework', category: 'backend' },
|
|
|
|
|
type: NounType.Concept,
|
|
|
|
|
metadata: { tags: ['server', 'nodejs'] }
|
|
|
|
|
})
|
|
|
|
|
await brain.add({
|
|
|
|
|
data: { name: 'Java', type: 'language', category: 'backend' },
|
|
|
|
|
type: NounType.Concept,
|
|
|
|
|
metadata: { tags: ['jvm', 'enterprise'] }
|
|
|
|
|
})
|
2025-08-26 12:32:21 -07:00
|
|
|
})
|
|
|
|
|
|
2025-09-11 16:23:32 -07:00
|
|
|
it('should return search results with real embeddings', async () => {
|
|
|
|
|
const results = await brain.find({
|
|
|
|
|
query: 'frontend framework',
|
|
|
|
|
limit: 2
|
|
|
|
|
})
|
2025-08-26 12:32:21 -07:00
|
|
|
|
|
|
|
|
expect(results).toBeInstanceOf(Array)
|
|
|
|
|
expect(results.length).toBeGreaterThan(0)
|
2025-09-11 16:23:32 -07:00
|
|
|
expect(results.length).toBeLessThanOrEqual(2)
|
2025-08-26 12:32:21 -07:00
|
|
|
|
2025-09-11 16:23:32 -07:00
|
|
|
// Results should have required properties
|
|
|
|
|
results.forEach((result: any) => {
|
2025-08-26 12:32:21 -07:00
|
|
|
expect(result).toHaveProperty('id')
|
|
|
|
|
expect(result).toHaveProperty('score')
|
2025-09-11 16:23:32 -07:00
|
|
|
expect(result).toHaveProperty('entity')
|
2025-08-26 12:32:21 -07:00
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
2025-09-11 16:23:32 -07:00
|
|
|
it('should handle limit parameter', async () => {
|
|
|
|
|
const limitedResults = await brain.find({
|
|
|
|
|
query: 'framework',
|
|
|
|
|
limit: 2
|
2025-08-26 12:32:21 -07:00
|
|
|
})
|
2025-09-11 16:23:32 -07:00
|
|
|
const unlimitedResults = await brain.find({
|
|
|
|
|
query: 'framework',
|
|
|
|
|
limit: 10
|
2025-08-26 12:32:21 -07:00
|
|
|
})
|
|
|
|
|
|
2025-09-11 16:23:32 -07:00
|
|
|
expect(limitedResults.length).toBeLessThanOrEqual(2)
|
|
|
|
|
expect(unlimitedResults.length).toBeLessThanOrEqual(10)
|
2025-08-26 12:32:21 -07:00
|
|
|
})
|
|
|
|
|
|
2025-09-11 16:23:32 -07:00
|
|
|
it('should search by metadata filters', async () => {
|
|
|
|
|
const results = await brain.find({
|
|
|
|
|
where: { category: 'frontend' },
|
|
|
|
|
limit: 10
|
2025-08-26 12:32:21 -07:00
|
|
|
})
|
|
|
|
|
|
2025-09-11 16:23:32 -07:00
|
|
|
expect(results).toBeInstanceOf(Array)
|
|
|
|
|
// All results should have frontend category
|
|
|
|
|
results.forEach((item: any) => {
|
|
|
|
|
expect(item.entity.metadata?.category).toBe('frontend')
|
2025-08-26 12:32:21 -07:00
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
2025-09-11 16:23:32 -07:00
|
|
|
it('should handle complex queries with Triple Intelligence', async () => {
|
|
|
|
|
const results = await brain.find({
|
|
|
|
|
query: 'javascript',
|
|
|
|
|
where: { type: 'framework' },
|
|
|
|
|
limit: 5,
|
|
|
|
|
fusion: {
|
|
|
|
|
strategy: 'adaptive',
|
|
|
|
|
weights: { vector: 0.6, field: 0.4 }
|
|
|
|
|
}
|
2025-08-26 12:32:21 -07:00
|
|
|
})
|
|
|
|
|
|
2025-09-11 16:23:32 -07:00
|
|
|
expect(results).toBeInstanceOf(Array)
|
|
|
|
|
// Results should match both vector similarity and field filters
|
|
|
|
|
results.forEach((item: any) => {
|
|
|
|
|
expect(item.entity.metadata?.type).toBe('framework')
|
|
|
|
|
})
|
2025-08-26 12:32:21 -07:00
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
2025-09-11 16:23:32 -07:00
|
|
|
describe('Statistics and Metadata', () => {
|
refactor: remove augmentation system and semantic type matching
Remove the entire augmentation pipeline infrastructure (52 files,
~15,000 lines) and the semantic type matching system. These were
unused middleware layers adding complexity without value.
What was removed:
- src/augmentations/ directory (all augmentation implementations)
- src/augmentationManager.ts (pipeline orchestrator)
- src/types/augmentations.ts, src/types/pipelineTypes.ts
- src/shared/default-augmentations.ts
- Semantic type suggestion (BrainyTypes.suggestNoun/suggestVerb)
- src/utils/typeMatching/ (embedding-based type matcher)
What was preserved by relocating:
- Import handlers (CSV, PDF, Excel) -> src/importers/handlers/
- NeuralImportAugmentation -> src/cortex/neuralImportAugmentation.ts
- Type matching utilities -> heuristic inference in consumers
What was simplified:
- brainy.ts: operations call storage directly (no execute() wrapper)
- IntegrationBase: standalone class (no BaseAugmentation parent)
- BrainyTypes: validation-only (nouns, verbs, isValid*, get*)
- Pipeline: direct execution (no augmentation interception)
- index.ts: removed TypeSuggestion, suggestType exports
- package.json: removed stale types/augmentations export
Build passes, 1176 tests pass, 0 failures.
2026-02-01 10:48:56 -08:00
|
|
|
it('should track statistics', async () => {
|
2025-09-11 16:23:32 -07:00
|
|
|
await brain.add({
|
|
|
|
|
data: { name: 'Test1' },
|
|
|
|
|
type: NounType.Concept
|
|
|
|
|
})
|
|
|
|
|
await brain.add({
|
|
|
|
|
data: { name: 'Test2' },
|
|
|
|
|
type: NounType.Concept
|
|
|
|
|
})
|
2026-06-11 14:51:00 -07:00
|
|
|
|
|
|
|
|
const stats = await brain.stats()
|
|
|
|
|
expect(stats.mode).toBe('writer')
|
|
|
|
|
expect(stats.entityCount).toBeGreaterThanOrEqual(2)
|
|
|
|
|
expect(stats.entitiesByType[NounType.Concept]).toBeGreaterThanOrEqual(2)
|
2025-08-26 12:32:21 -07:00
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
2025-09-11 16:23:32 -07:00
|
|
|
describe('Clear Operations', () => {
|
|
|
|
|
it('should clear all data', async () => {
|
|
|
|
|
await brain.add({
|
|
|
|
|
data: { name: 'Test1' },
|
|
|
|
|
type: NounType.Concept
|
|
|
|
|
})
|
|
|
|
|
await brain.add({
|
|
|
|
|
data: { name: 'Test2' },
|
|
|
|
|
type: NounType.Concept
|
|
|
|
|
})
|
|
|
|
|
await brain.add({
|
|
|
|
|
data: { name: 'Test3' },
|
|
|
|
|
type: NounType.Concept
|
2025-08-26 12:32:21 -07:00
|
|
|
})
|
|
|
|
|
|
refactor(8.0): delete DataAPI — superseded by Db persist/restore + import API + stats
The legacy backup/import/export/stats facade (src/api/DataAPI.ts) drifted
from the modern entity shape and every job it did now has a first-class
surface. Delete it and brain.data(), and rewire the CLI:
- data-stats → brain.stats() (full BrainyStats report: per-type breakdowns,
indexed fields, index health, storage backend, writer lock, version)
- clean → brain.clear()
- export → alias of snapshot; a db.persist() snapshot is the full-fidelity
export format (open with Brainy.load, load wholesale with brainy restore);
external data ingestion remains brainy import (UniversalImportAPI)
Rewiring clean onto brain.clear() exposed two real bugs, both fixed:
- clear() left this.graphIndex undefined forever — any graph-touching call
afterwards (relate, getNeighbors, stats) crashed. clear() now re-resolves
the graph index exactly as init() does and re-wires the shared UUID↔int
resolver, and re-resolves the metadata index with the same provider
fallback as init().
- storage.clear() reset the legacy totals but not the per-type/subtype
count rollups or id→type caches, so stats() reported phantom counts for
deleted entities. Both adapters now delegate derived-state reset to
reloadDerivedState(), the same path restore-from-snapshot uses.
One-shot CLI commands (data-stats, clean, snapshot/export, restore,
history, generation) now close the brain and exit explicitly — global
cache timers otherwise keep the process alive holding the writer lock.
Verified: build clean, 1383/1383 unit tests, 24/24 db-mvcc integration,
plus an end-to-end CLI smoke (add → data-stats → export → clean →
data-stats).
2026-06-11 09:05:12 -07:00
|
|
|
// Clear everything — entities, relationships, and all indexes
|
|
|
|
|
await brain.clear()
|
|
|
|
|
|
|
|
|
|
// Verify user data is cleared. clear() re-creates a fresh VFS root
|
|
|
|
|
// entity, and a thresholdless vector search can surface it — so filter
|
|
|
|
|
// VFS bookkeeping out and assert the added entities are gone.
|
|
|
|
|
const results = await brain.find({
|
2025-09-11 16:23:32 -07:00
|
|
|
query: 'Test',
|
refactor(8.0): delete DataAPI — superseded by Db persist/restore + import API + stats
The legacy backup/import/export/stats facade (src/api/DataAPI.ts) drifted
from the modern entity shape and every job it did now has a first-class
surface. Delete it and brain.data(), and rewire the CLI:
- data-stats → brain.stats() (full BrainyStats report: per-type breakdowns,
indexed fields, index health, storage backend, writer lock, version)
- clean → brain.clear()
- export → alias of snapshot; a db.persist() snapshot is the full-fidelity
export format (open with Brainy.load, load wholesale with brainy restore);
external data ingestion remains brainy import (UniversalImportAPI)
Rewiring clean onto brain.clear() exposed two real bugs, both fixed:
- clear() left this.graphIndex undefined forever — any graph-touching call
afterwards (relate, getNeighbors, stats) crashed. clear() now re-resolves
the graph index exactly as init() does and re-wires the shared UUID↔int
resolver, and re-resolves the metadata index with the same provider
fallback as init().
- storage.clear() reset the legacy totals but not the per-type/subtype
count rollups or id→type caches, so stats() reported phantom counts for
deleted entities. Both adapters now delegate derived-state reset to
reloadDerivedState(), the same path restore-from-snapshot uses.
One-shot CLI commands (data-stats, clean, snapshot/export, restore,
history, generation) now close the brain and exit explicitly — global
cache timers otherwise keep the process alive holding the writer lock.
Verified: build clean, 1383/1383 unit tests, 24/24 db-mvcc integration,
plus an end-to-end CLI smoke (add → data-stats → export → clean →
data-stats).
2026-06-11 09:05:12 -07:00
|
|
|
limit: 10
|
2025-09-11 16:23:32 -07:00
|
|
|
})
|
refactor(8.0): delete DataAPI — superseded by Db persist/restore + import API + stats
The legacy backup/import/export/stats facade (src/api/DataAPI.ts) drifted
from the modern entity shape and every job it did now has a first-class
surface. Delete it and brain.data(), and rewire the CLI:
- data-stats → brain.stats() (full BrainyStats report: per-type breakdowns,
indexed fields, index health, storage backend, writer lock, version)
- clean → brain.clear()
- export → alias of snapshot; a db.persist() snapshot is the full-fidelity
export format (open with Brainy.load, load wholesale with brainy restore);
external data ingestion remains brainy import (UniversalImportAPI)
Rewiring clean onto brain.clear() exposed two real bugs, both fixed:
- clear() left this.graphIndex undefined forever — any graph-touching call
afterwards (relate, getNeighbors, stats) crashed. clear() now re-resolves
the graph index exactly as init() does and re-wires the shared UUID↔int
resolver, and re-resolves the metadata index with the same provider
fallback as init().
- storage.clear() reset the legacy totals but not the per-type/subtype
count rollups or id→type caches, so stats() reported phantom counts for
deleted entities. Both adapters now delegate derived-state reset to
reloadDerivedState(), the same path restore-from-snapshot uses.
One-shot CLI commands (data-stats, clean, snapshot/export, restore,
history, generation) now close the brain and exit explicitly — global
cache timers otherwise keep the process alive holding the writer lock.
Verified: build clean, 1383/1383 unit tests, 24/24 db-mvcc integration,
plus an end-to-end CLI smoke (add → data-stats → export → clean →
data-stats).
2026-06-11 09:05:12 -07:00
|
|
|
const userResults = results.filter(r => !r.metadata?.isVFS)
|
|
|
|
|
expect(userResults.length).toBe(0)
|
2025-08-26 12:32:21 -07:00
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
describe('Edge Cases and Error Handling', () => {
|
2025-09-11 16:23:32 -07:00
|
|
|
it('should handle empty queries gracefully', async () => {
|
|
|
|
|
const results = await brain.find({
|
|
|
|
|
query: '',
|
|
|
|
|
limit: 5
|
|
|
|
|
})
|
2025-08-26 12:32:21 -07:00
|
|
|
|
2025-09-11 16:23:32 -07:00
|
|
|
expect(results).toBeInstanceOf(Array)
|
2025-08-26 12:32:21 -07:00
|
|
|
})
|
|
|
|
|
|
2025-09-11 16:23:32 -07:00
|
|
|
it('should handle special characters in data', async () => {
|
|
|
|
|
const id = await brain.add({
|
feat: enforce data/metadata separation, numeric range queries, improved docs
- Store data opaquely in add() and update() instead of spreading object
properties into top-level metadata. data is for semantic search (HNSW),
metadata is for structured where-filter queries (MetadataIndex).
- Fix numeric range queries in MetadataIndex — use numeric-aware comparison
instead of lexicographic string comparison for normalized values.
- Add data field to RelateParams and Relation types for relationship content.
- Add where.type → where.noun alias in metadata-only find() path.
- Rewrite README: focused ~350 lines from 791, quick start first, feature
showcase with mini-snippets, organized doc links, no version callouts.
- Add DATA_MODEL.md and QUERY_OPERATORS.md reference docs.
- Remove 10 outdated/redundant doc files consolidated into API reference.
- Improve JSDoc on Entity, Relation, AddParams, FindParams, and core methods.
- Fix tests asserting data properties appear in metadata (data model violation).
- Deprecate verb.source/target in favor of from/to (public) and sourceId/targetId (storage).
2026-02-09 12:06:59 -08:00
|
|
|
data: 'Test with special chars: !@#$%^&*()',
|
|
|
|
|
type: NounType.Concept,
|
|
|
|
|
metadata: { name: 'Test !@#$%^&*()', description: 'Has "quotes" and \'apostrophes\'' }
|
2025-09-11 16:23:32 -07:00
|
|
|
})
|
feat: enforce data/metadata separation, numeric range queries, improved docs
- Store data opaquely in add() and update() instead of spreading object
properties into top-level metadata. data is for semantic search (HNSW),
metadata is for structured where-filter queries (MetadataIndex).
- Fix numeric range queries in MetadataIndex — use numeric-aware comparison
instead of lexicographic string comparison for normalized values.
- Add data field to RelateParams and Relation types for relationship content.
- Add where.type → where.noun alias in metadata-only find() path.
- Rewrite README: focused ~350 lines from 791, quick start first, feature
showcase with mini-snippets, organized doc links, no version callouts.
- Add DATA_MODEL.md and QUERY_OPERATORS.md reference docs.
- Remove 10 outdated/redundant doc files consolidated into API reference.
- Improve JSDoc on Entity, Relation, AddParams, FindParams, and core methods.
- Fix tests asserting data properties appear in metadata (data model violation).
- Deprecate verb.source/target in favor of from/to (public) and sourceId/targetId (storage).
2026-02-09 12:06:59 -08:00
|
|
|
|
2025-09-11 16:23:32 -07:00
|
|
|
const retrieved = await brain.get(id)
|
|
|
|
|
expect(retrieved?.metadata?.name).toContain('!@#$%^&*()')
|
2025-08-26 12:32:21 -07:00
|
|
|
})
|
|
|
|
|
|
2025-09-11 16:23:32 -07:00
|
|
|
it('should handle very long text', async () => {
|
|
|
|
|
const longText = 'x'.repeat(10000)
|
|
|
|
|
const id = await brain.add({
|
feat: enforce data/metadata separation, numeric range queries, improved docs
- Store data opaquely in add() and update() instead of spreading object
properties into top-level metadata. data is for semantic search (HNSW),
metadata is for structured where-filter queries (MetadataIndex).
- Fix numeric range queries in MetadataIndex — use numeric-aware comparison
instead of lexicographic string comparison for normalized values.
- Add data field to RelateParams and Relation types for relationship content.
- Add where.type → where.noun alias in metadata-only find() path.
- Rewrite README: focused ~350 lines from 791, quick start first, feature
showcase with mini-snippets, organized doc links, no version callouts.
- Add DATA_MODEL.md and QUERY_OPERATORS.md reference docs.
- Remove 10 outdated/redundant doc files consolidated into API reference.
- Improve JSDoc on Entity, Relation, AddParams, FindParams, and core methods.
- Fix tests asserting data properties appear in metadata (data model violation).
- Deprecate verb.source/target in favor of from/to (public) and sourceId/targetId (storage).
2026-02-09 12:06:59 -08:00
|
|
|
data: longText,
|
2025-09-11 16:23:32 -07:00
|
|
|
type: NounType.Document
|
|
|
|
|
})
|
feat: enforce data/metadata separation, numeric range queries, improved docs
- Store data opaquely in add() and update() instead of spreading object
properties into top-level metadata. data is for semantic search (HNSW),
metadata is for structured where-filter queries (MetadataIndex).
- Fix numeric range queries in MetadataIndex — use numeric-aware comparison
instead of lexicographic string comparison for normalized values.
- Add data field to RelateParams and Relation types for relationship content.
- Add where.type → where.noun alias in metadata-only find() path.
- Rewrite README: focused ~350 lines from 791, quick start first, feature
showcase with mini-snippets, organized doc links, no version callouts.
- Add DATA_MODEL.md and QUERY_OPERATORS.md reference docs.
- Remove 10 outdated/redundant doc files consolidated into API reference.
- Improve JSDoc on Entity, Relation, AddParams, FindParams, and core methods.
- Fix tests asserting data properties appear in metadata (data model violation).
- Deprecate verb.source/target in favor of from/to (public) and sourceId/targetId (storage).
2026-02-09 12:06:59 -08:00
|
|
|
|
2025-09-11 16:23:32 -07:00
|
|
|
const retrieved = await brain.get(id)
|
feat: enforce data/metadata separation, numeric range queries, improved docs
- Store data opaquely in add() and update() instead of spreading object
properties into top-level metadata. data is for semantic search (HNSW),
metadata is for structured where-filter queries (MetadataIndex).
- Fix numeric range queries in MetadataIndex — use numeric-aware comparison
instead of lexicographic string comparison for normalized values.
- Add data field to RelateParams and Relation types for relationship content.
- Add where.type → where.noun alias in metadata-only find() path.
- Rewrite README: focused ~350 lines from 791, quick start first, feature
showcase with mini-snippets, organized doc links, no version callouts.
- Add DATA_MODEL.md and QUERY_OPERATORS.md reference docs.
- Remove 10 outdated/redundant doc files consolidated into API reference.
- Improve JSDoc on Entity, Relation, AddParams, FindParams, and core methods.
- Fix tests asserting data properties appear in metadata (data model violation).
- Deprecate verb.source/target in favor of from/to (public) and sourceId/targetId (storage).
2026-02-09 12:06:59 -08:00
|
|
|
expect(retrieved?.data).toHaveLength(10000)
|
2025-08-26 12:32:21 -07:00
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
})
|