2025-09-30 12:53:39 -07:00
/ * *
* VFS Bug Fix Tests
*
* Tests for issues reported by Brain Studio team :
* - Issue # 1 : Duplicate directory nodes
* - Issue # 2 : File read decompression error
* /
import { describe , it , expect , beforeEach } from 'vitest'
import { Brainy } from '../../src/brainy.js'
import { VirtualFileSystem } from '../../src/vfs/VirtualFileSystem.js'
describe ( 'VFS Bug Fixes' , ( ) = > {
let brain : Brainy
let vfs : VirtualFileSystem
beforeEach ( async ( ) = > {
// Create fresh instance for each test
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 ,
2025-09-30 12:53:39 -07:00
storage : { type : 'memory' } ,
embeddingModel : 'Q8'
} )
await brain . init ( )
2025-11-02 11:38:12 -08:00
vfs = brain . vfs
2025-09-30 12:53:39 -07:00
await vfs . init ( )
} )
describe ( 'Issue #1: Duplicate Directory Nodes' , ( ) = > {
it ( 'should not create duplicate directory entries when writing multiple files to same directory' , async ( ) = > {
// Write multiple files to the same directory (reproduce the bug scenario)
await vfs . writeFile ( '/src/index.ts' , 'export const foo = 1' )
await vfs . writeFile ( '/src/types.ts' , 'export type Foo = string' )
await vfs . writeFile ( '/src/utils.ts' , 'export function bar() {}' )
await vfs . writeFile ( '/README.md' , '# Project' )
// Verify files exist
expect ( await vfs . exists ( '/src/index.ts' ) ) . toBe ( true )
expect ( await vfs . exists ( '/README.md' ) ) . toBe ( true )
// Get direct children of root
const children = await vfs . getDirectChildren ( '/' )
// Debug: print what we got
console . log ( 'Root children:' , children . map ( c = > ( { name : c.metadata.name , type : c . metadata . vfsType } ) ) )
// Count how many times 'src' appears
const srcDirs = children . filter ( child = >
child . metadata . name === 'src' && child . metadata . vfsType === 'directory'
)
// Should only have ONE src directory
expect ( srcDirs . length ) . toBe ( 1 )
// Total children should be at least 2: src directory + README.md
expect ( children . length ) . toBeGreaterThanOrEqual ( 2 )
// Verify children have correct types
const srcDir = children . find ( c = > c . metadata . name === 'src' )
const readme = children . find ( c = > c . metadata . name === 'README.md' )
expect ( srcDir ? . metadata . vfsType ) . toBe ( 'directory' )
expect ( readme ? . metadata . vfsType ) . toBe ( 'file' )
} )
it ( 'should handle concurrent file writes without creating duplicates' , async ( ) = > {
// Write multiple files concurrently (more likely to trigger race conditions)
await Promise . all ( [
vfs . writeFile ( '/data/file1.json' , '{"a": 1}' ) ,
vfs . writeFile ( '/data/file2.json' , '{"b": 2}' ) ,
vfs . writeFile ( '/data/file3.json' , '{"c": 3}' ) ,
vfs . writeFile ( '/data/file4.json' , '{"d": 4}' )
] )
const children = await vfs . getDirectChildren ( '/' )
const dataDirs = children . filter ( c = > c . metadata . name === 'data' )
// Should only have ONE data directory
expect ( dataDirs . length ) . toBe ( 1 )
// Check the data directory has exactly 4 files
const dataChildren = await vfs . getDirectChildren ( '/data' )
expect ( dataChildren . length ) . toBe ( 4 )
} )
} )
describe ( 'Issue #2: File Read Decompression Error' , ( ) = > {
it ( 'should read file content without decompression errors' , async ( ) = > {
const content = 'Hello World! This is test content.'
// Write file
await vfs . writeFile ( '/test.txt' , content )
// Read file - should NOT throw decompression error
const readContent = await vfs . readFile ( '/test.txt' )
// Content should match
expect ( readContent . toString ( 'utf8' ) ) . toBe ( content )
} )
it ( 'should handle reading files with various sizes' , async ( ) = > {
const testFiles = [
{ path : '/small.txt' , content : 'Small' } ,
{ path : '/medium.txt' , content : 'x' . repeat ( 1000 ) } ,
{ path : '/large.txt' , content : 'y' . repeat ( 50000 ) }
]
// Write all files
for ( const file of testFiles ) {
await vfs . writeFile ( file . path , file . content )
}
// Read all files - none should error
for ( const file of testFiles ) {
const readContent = await vfs . readFile ( file . path )
expect ( readContent . toString ( 'utf8' ) ) . toBe ( file . content )
}
} )
2025-11-03 14:08:58 -08:00
it ( 'should correctly handle file storage via BlobStorage (v5.2.0)' , async ( ) = > {
const content = 'Test content for BlobStorage'
2025-09-30 12:53:39 -07:00
// Write file
await vfs . writeFile ( '/test.md' , content )
// Get the entity to inspect storage
const entity = await vfs . getEntity ( '/test.md' )
2025-11-03 14:08:58 -08:00
// v5.2.0: Content is in BlobStorage, not rawData
expect ( entity . metadata . storage ) . toBeDefined ( )
expect ( entity . metadata . storage . type ) . toBe ( 'blob' )
expect ( entity . metadata . size ) . toBe ( content . length )
2025-09-30 12:53:39 -07:00
2025-11-03 14:08:58 -08:00
// Read should work correctly (retrieves from BlobStorage)
2025-09-30 12:53:39 -07:00
const readContent = await vfs . readFile ( '/test.md' )
expect ( readContent . toString ( 'utf8' ) ) . toBe ( content )
} )
it ( 'should handle binary files correctly' , async ( ) = > {
// Create a binary buffer
const binaryContent = Buffer . from ( [ 0x89 , 0x50 , 0x4E , 0x47 , 0x0D , 0x0A , 0x1A , 0x0A ] )
// Write binary file
await vfs . writeFile ( '/image.png' , binaryContent )
// Read binary file
const readContent = await vfs . readFile ( '/image.png' )
// Should match exactly
expect ( Buffer . compare ( readContent , binaryContent ) ) . toBe ( 0 )
} )
it ( 'should handle reading after multiple writes' , async ( ) = > {
// Write file
await vfs . writeFile ( '/counter.txt' , '1' )
// Update file multiple times
await vfs . writeFile ( '/counter.txt' , '2' )
await vfs . writeFile ( '/counter.txt' , '3' )
// Read should work
const content = await vfs . readFile ( '/counter.txt' )
expect ( content . toString ( 'utf8' ) ) . toBe ( '3' )
} )
} )
} )