brainy/tests/unit/import
David Snelling 97eb6eec2c fix: binary file corruption in brain.import() with preserveSource
## Bug Fix

**Issue**: When using `brain.import(filePath, { preserveSource: true })`,
binary files (PDFs, images, Excel) were NOT being preserved in VFS, causing
Z_DATA_ERROR when trying to read them back.

**Root Cause**: ImportCoordinator line 444 checked for `type === 'buffer'`,
but normalizeSource() returns `type: 'path'` for file paths (the most common
case). This caused `sourceBuffer = undefined`, silently failing to preserve
the source file.

**Fix**: Changed condition to `Buffer.isBuffer(normalizedSource.data)` to
handle both Buffer objects and file paths correctly.

## Code Changes

**src/import/ImportCoordinator.ts:445**
```typescript
// BEFORE (v5.1.1)
sourceBuffer: normalizedSource.type === 'buffer' ? normalizedSource.data as Buffer : undefined

// AFTER (v5.1.2)
sourceBuffer: Buffer.isBuffer(normalizedSource.data) ? normalizedSource.data as Buffer : undefined
```

## Testing

Added comprehensive tests in `tests/unit/import/preserve-source-fix.test.ts`:
-  File path import with preserveSource: true (main fix)
-  Verify preserveSource: false works correctly
-  Binary file integrity (no corruption)

## Impact

**Before**: Workshop team experienced Z_DATA_ERROR reading imported PDFs
**After**: Binary files correctly preserved and readable from VFS

## Related Issues

Fixes bug reported in:
/media/dpsifr/storage/home/Projects/brain-cloud/apps/workshop/BRAINY_BUG_REPORT.md

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-03 10:00:55 -08:00
..
InstancePool.test.ts feat: implement progressive flush intervals for streaming imports 2025-10-22 17:36:27 -07:00
preserve-source-fix.test.ts fix: binary file corruption in brain.import() with preserveSource 2025-11-03 10:00:55 -08:00