2025-09-26 14:27:46 -07:00
|
|
|
/**
|
|
|
|
|
* VFS Initialization Tests
|
|
|
|
|
*
|
2025-11-02 11:38:12 -08:00
|
|
|
* Tests v5.1.0+ auto-initialization behavior
|
2025-09-26 14:27:46 -07:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import { describe, it, expect } from 'vitest'
|
|
|
|
|
import { VirtualFileSystem } from '../../src/vfs/index.js'
|
|
|
|
|
import { Brainy } from '../../src/brainy.js'
|
|
|
|
|
|
|
|
|
|
describe('VFS Initialization', () => {
|
|
|
|
|
|
2025-11-02 11:38:12 -08:00
|
|
|
describe('Auto-Initialization (v5.1.0+)', () => {
|
|
|
|
|
it('should auto-initialize VFS during brain.init()', async () => {
|
2025-09-26 14:27:46 -07:00
|
|
|
const brain = new Brainy({
|
|
|
|
|
storage: { type: 'memory' },
|
|
|
|
|
silent: true
|
|
|
|
|
})
|
|
|
|
|
await brain.init()
|
|
|
|
|
|
2025-11-02 11:38:12 -08:00
|
|
|
const vfs = brain.vfs
|
2025-09-26 14:27:46 -07:00
|
|
|
|
2025-11-02 11:38:12 -08:00
|
|
|
// VFS is ready to use immediately after brain.init()
|
2025-09-26 14:27:46 -07:00
|
|
|
await vfs.writeFile('/test.txt', 'Hello World')
|
2025-11-02 11:38:12 -08:00
|
|
|
const content = await vfs.readFile('/test.txt')
|
|
|
|
|
expect(content.toString()).toBe('Hello World')
|
2025-09-26 14:27:46 -07:00
|
|
|
|
|
|
|
|
await brain.close()
|
|
|
|
|
})
|
|
|
|
|
|
2025-11-02 11:38:12 -08:00
|
|
|
it('should automatically create root directory on brain.init()', async () => {
|
2025-09-26 14:27:46 -07:00
|
|
|
const brain = new Brainy({
|
|
|
|
|
storage: { type: 'memory' },
|
|
|
|
|
silent: true
|
|
|
|
|
})
|
|
|
|
|
await brain.init()
|
|
|
|
|
|
2025-11-02 11:38:12 -08:00
|
|
|
const vfs = brain.vfs
|
2025-09-26 14:27:46 -07:00
|
|
|
|
2025-11-02 11:38:12 -08:00
|
|
|
// Root directory exists after brain.init()
|
2025-09-26 14:27:46 -07:00
|
|
|
expect(await vfs.exists('/')).toBe(true)
|
|
|
|
|
|
|
|
|
|
// Root is a directory
|
|
|
|
|
const stats = await vfs.stat('/')
|
|
|
|
|
expect(stats.isDirectory()).toBe(true)
|
|
|
|
|
|
2025-11-02 11:38:12 -08:00
|
|
|
// Can list root directory
|
2025-09-26 14:27:46 -07:00
|
|
|
const entries = await vfs.readdir('/')
|
|
|
|
|
expect(Array.isArray(entries)).toBe(true)
|
|
|
|
|
|
|
|
|
|
await brain.close()
|
|
|
|
|
})
|
|
|
|
|
|
2025-11-02 11:38:12 -08:00
|
|
|
it('should handle nested directories after brain.init()', async () => {
|
2025-09-26 14:27:46 -07:00
|
|
|
const brain = new Brainy({
|
|
|
|
|
storage: { type: 'memory' },
|
|
|
|
|
silent: true
|
|
|
|
|
})
|
|
|
|
|
await brain.init()
|
|
|
|
|
|
2025-11-02 11:38:12 -08:00
|
|
|
const vfs = brain.vfs
|
2025-09-26 14:27:46 -07:00
|
|
|
|
|
|
|
|
// Create nested structure
|
|
|
|
|
await vfs.mkdir('/documents')
|
|
|
|
|
await vfs.writeFile('/documents/readme.txt', 'Important info')
|
|
|
|
|
await vfs.mkdir('/documents/reports')
|
|
|
|
|
await vfs.writeFile('/documents/reports/q1.txt', 'Q1 Report')
|
|
|
|
|
|
|
|
|
|
// Verify structure
|
|
|
|
|
const rootEntries = await vfs.readdir('/')
|
|
|
|
|
expect(rootEntries).toContain('documents')
|
|
|
|
|
|
|
|
|
|
const docEntries = await vfs.readdir('/documents')
|
|
|
|
|
expect(docEntries).toContain('readme.txt')
|
|
|
|
|
expect(docEntries).toContain('reports')
|
|
|
|
|
|
|
|
|
|
const reportEntries = await vfs.readdir('/documents/reports')
|
|
|
|
|
expect(reportEntries).toContain('q1.txt')
|
|
|
|
|
|
2025-11-02 11:38:12 -08:00
|
|
|
await brain.close()
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('should work with VFS operations immediately', async () => {
|
|
|
|
|
const brain = new Brainy({
|
|
|
|
|
storage: { type: 'memory' },
|
|
|
|
|
silent: true
|
|
|
|
|
})
|
|
|
|
|
await brain.init()
|
|
|
|
|
|
|
|
|
|
const vfs = brain.vfs
|
|
|
|
|
|
|
|
|
|
// All VFS operations work immediately
|
|
|
|
|
await vfs.writeFile('/test.txt', 'Hello')
|
|
|
|
|
await vfs.writeFile('/data.json', '{"key": "value"}')
|
|
|
|
|
|
|
|
|
|
const entries = await vfs.readdir('/')
|
|
|
|
|
expect(entries).toContain('test.txt')
|
|
|
|
|
expect(entries).toContain('data.json')
|
|
|
|
|
|
|
|
|
|
const testContent = await vfs.readFile('/test.txt')
|
|
|
|
|
expect(testContent.toString()).toBe('Hello')
|
|
|
|
|
|
2025-09-26 14:27:46 -07:00
|
|
|
await brain.close()
|
|
|
|
|
})
|
|
|
|
|
})
|
2025-11-02 11:38:12 -08:00
|
|
|
})
|