feat: add ImageHandler with EXIF extraction and comprehensive MIME detection (v5.2.0)
Implements Phase 1.5 (Comprehensive MIME Type Detection) and adds built-in image processing support to IntelligentImportAugmentation. **New Features:** - ImageHandler: Extracts image metadata (dimensions, format, color space) using sharp - EXIF extraction: Camera data, GPS, timestamps using exifr library - Support for JPEG, PNG, WebP, GIF, TIFF, BMP, SVG, HEIC, AVIF formats - MimeTypeDetector: Unified MIME type detection with magic byte support - FormatDetector: Enhanced with image format detection via MIME + magic bytes **Architecture Fixes:** - Fixed brain.import() augmentation pipeline integration (src/brainy.ts:3140-3154) - Added parameter spreading for ImportSource objects to enable augmentation access - Fixed metadata propagation through ImportCoordinator to final results - Added augmentation data check in ImportCoordinator.extract() **Integration:** - ImageHandler registered as built-in handler alongside CSV, Excel, PDF - Images import as 'media' entities with 'image' subtype - Full metadata preserved in knowledge graph entities - Configuration options: enableImage, extractEXIF, imageDefaults **Test Coverage:** - 15 integration tests (image-import.test.ts) - 100% passing - 27 unit tests (image-handler.test.ts) - 100% passing - Format detection tests for all supported image types - Error handling and resilience tests **Breaking Changes:** None - backward compatible Generated with Claude Code Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
parent
6345f87eb2
commit
1874b77896
26 changed files with 5079 additions and 434 deletions
187
tests/unit/vfs/blob-storage-integration.test.ts
Normal file
187
tests/unit/vfs/blob-storage-integration.test.ts
Normal file
|
|
@ -0,0 +1,187 @@
|
|||
import { describe, it, expect, beforeEach, afterEach } from 'vitest'
|
||||
import { Brainy } from '../../../src/brainy.js'
|
||||
import { VirtualFileSystem } from '../../../src/vfs/VirtualFileSystem.js'
|
||||
import * as fs from 'fs/promises'
|
||||
import * as path from 'path'
|
||||
|
||||
/**
|
||||
* v5.2.0: Test unified BlobStorage integration with VFS
|
||||
*
|
||||
* This test verifies that:
|
||||
* 1. All files (small, medium, large) use BlobStorage
|
||||
* 2. No size-based branching occurs
|
||||
* 3. Content is stored and retrieved correctly
|
||||
* 4. Deduplication works automatically
|
||||
*
|
||||
* Note: Uses FileSystemStorage because BlobStorage is only available
|
||||
* in COW-enabled storage adapters (not MemoryStorage)
|
||||
*/
|
||||
describe('VFS Unified BlobStorage (v5.2.0)', () => {
|
||||
let brain: Brainy
|
||||
let vfs: VirtualFileSystem
|
||||
let testDir: string
|
||||
|
||||
beforeEach(async () => {
|
||||
// Create temporary directory for test storage
|
||||
testDir = path.join('/tmp', `brainy-test-blob-${Date.now()}-${Math.random().toString(36).slice(2)}`)
|
||||
await fs.mkdir(testDir, { recursive: true })
|
||||
|
||||
brain = new Brainy({
|
||||
storage: {
|
||||
type: 'filesystem',
|
||||
options: { path: testDir }
|
||||
},
|
||||
silent: true
|
||||
})
|
||||
await brain.init()
|
||||
vfs = brain.vfs
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
await brain.close()
|
||||
|
||||
// Clean up temporary directory
|
||||
try {
|
||||
await fs.rm(testDir, { recursive: true, force: true })
|
||||
} catch (error) {
|
||||
// Ignore cleanup errors
|
||||
}
|
||||
})
|
||||
|
||||
describe('Unified Storage Path', () => {
|
||||
it('should store small files (<100KB) in BlobStorage', async () => {
|
||||
const content = 'Small file content'
|
||||
await vfs.writeFile('/small.txt', content)
|
||||
|
||||
// Get entity directly using VFS API
|
||||
const entity = await vfs.getEntity('/small.txt')
|
||||
|
||||
expect(entity.metadata.vfsType).toBe('file')
|
||||
expect(entity.metadata.storage?.type).toBe('blob')
|
||||
expect(entity.metadata.storage?.hash).toBeDefined()
|
||||
|
||||
const readContent = await vfs.readFile('/small.txt')
|
||||
expect(readContent.toString()).toBe(content)
|
||||
})
|
||||
|
||||
it('should store medium files (100KB-10MB) in BlobStorage', async () => {
|
||||
const content = Buffer.alloc(200_000, 'M') // 200KB
|
||||
await vfs.writeFile('/medium.bin', content)
|
||||
|
||||
const entity = await vfs.getEntity('/medium.bin')
|
||||
|
||||
expect(entity.metadata.vfsType).toBe('file')
|
||||
expect(entity.metadata.storage?.type).toBe('blob')
|
||||
expect(entity.metadata.storage?.hash).toBeDefined()
|
||||
|
||||
const readContent = await vfs.readFile('/medium.bin')
|
||||
expect(Buffer.compare(readContent, content)).toBe(0)
|
||||
})
|
||||
|
||||
it('should store large files (>10MB) in BlobStorage', async () => {
|
||||
const content = Buffer.alloc(11_000_000, 'L') // 11MB
|
||||
await vfs.writeFile('/large.bin', content)
|
||||
|
||||
const entity = await vfs.getEntity('/large.bin')
|
||||
|
||||
expect(entity.metadata.vfsType).toBe('file')
|
||||
expect(entity.metadata.storage?.type).toBe('blob')
|
||||
expect(entity.metadata.storage?.hash).toBeDefined()
|
||||
|
||||
const readContent = await vfs.readFile('/large.bin')
|
||||
expect(Buffer.compare(readContent, content)).toBe(0)
|
||||
})
|
||||
})
|
||||
|
||||
describe('Deduplication', () => {
|
||||
it('should deduplicate identical files', async () => {
|
||||
const content = 'Duplicate content test'
|
||||
|
||||
// Write same content to two different paths
|
||||
await vfs.writeFile('/file1.txt', content)
|
||||
await vfs.writeFile('/file2.txt', content)
|
||||
|
||||
// Get entities directly
|
||||
const entity1 = await vfs.getEntity('/file1.txt')
|
||||
const entity2 = await vfs.getEntity('/file2.txt')
|
||||
|
||||
// Both should have blob storage
|
||||
expect(entity1.metadata.storage?.type).toBe('blob')
|
||||
expect(entity2.metadata.storage?.type).toBe('blob')
|
||||
|
||||
// But same blob hash (deduplicated)
|
||||
const hash1 = entity1.metadata.storage?.hash
|
||||
const hash2 = entity2.metadata.storage?.hash
|
||||
|
||||
expect(hash1).toBeDefined()
|
||||
expect(hash2).toBeDefined()
|
||||
expect(hash1).toBe(hash2) // Same content = same hash
|
||||
})
|
||||
})
|
||||
|
||||
describe('File Operations', () => {
|
||||
it('should update files correctly', async () => {
|
||||
await vfs.writeFile('/update.txt', 'Original content')
|
||||
await vfs.writeFile('/update.txt', 'Updated content')
|
||||
|
||||
const content = await vfs.readFile('/update.txt')
|
||||
expect(content.toString()).toBe('Updated content')
|
||||
})
|
||||
|
||||
it('should delete files and decrement blob refs', async () => {
|
||||
await vfs.writeFile('/delete.txt', 'Delete me')
|
||||
|
||||
await vfs.unlink('/delete.txt')
|
||||
|
||||
await expect(vfs.readFile('/delete.txt')).rejects.toThrow()
|
||||
})
|
||||
|
||||
it('should append to files', async () => {
|
||||
await vfs.writeFile('/append.txt', 'First part')
|
||||
await vfs.appendFile('/append.txt', ' Second part')
|
||||
|
||||
const content = await vfs.readFile('/append.txt')
|
||||
expect(content.toString()).toBe('First part Second part')
|
||||
})
|
||||
})
|
||||
|
||||
describe('Binary Files', () => {
|
||||
it('should handle binary files correctly', async () => {
|
||||
const binary = Buffer.from([0x00, 0xFF, 0xAB, 0xCD, 0xEF])
|
||||
await vfs.writeFile('/binary.dat', binary)
|
||||
|
||||
const read = await vfs.readFile('/binary.dat')
|
||||
expect(Buffer.compare(read, binary)).toBe(0)
|
||||
})
|
||||
|
||||
it('should preserve binary file integrity', async () => {
|
||||
// Create a buffer with various byte patterns
|
||||
const buffer = Buffer.alloc(1000)
|
||||
for (let i = 0; i < 1000; i++) {
|
||||
buffer[i] = i % 256
|
||||
}
|
||||
|
||||
await vfs.writeFile('/integrity.bin', buffer)
|
||||
const read = await vfs.readFile('/integrity.bin')
|
||||
|
||||
expect(Buffer.compare(read, buffer)).toBe(0)
|
||||
expect(read.length).toBe(buffer.length)
|
||||
})
|
||||
})
|
||||
|
||||
describe('Metadata', () => {
|
||||
it('should store correct metadata', async () => {
|
||||
const content = 'Test file'
|
||||
await vfs.writeFile('/meta.txt', content)
|
||||
|
||||
const entity = await vfs.getEntity('/meta.txt')
|
||||
|
||||
expect(entity.metadata.size).toBe(content.length)
|
||||
expect(entity.metadata.vfsType).toBe('file')
|
||||
expect(entity.metadata.storage?.type).toBe('blob')
|
||||
expect(entity.metadata.storage?.size).toBe(content.length)
|
||||
expect(entity.metadata.mimeType).toBeDefined()
|
||||
})
|
||||
})
|
||||
|
||||
})
|
||||
230
tests/unit/vfs/mime-type-detection.test.ts
Normal file
230
tests/unit/vfs/mime-type-detection.test.ts
Normal file
|
|
@ -0,0 +1,230 @@
|
|||
/**
|
||||
* Comprehensive MIME Type Detection Tests (v5.2.0)
|
||||
*
|
||||
* Verifies that MimeTypeDetector correctly identifies:
|
||||
* - Standard types (via mime library)
|
||||
* - Custom developer types (shell, configs, modern languages)
|
||||
* - Office formats (Microsoft, OpenDocument)
|
||||
* - Special files (Dockerfile, Makefile, dotfiles)
|
||||
*/
|
||||
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { mimeDetector } from '../../../src/vfs/MimeTypeDetector.js'
|
||||
|
||||
describe('MimeTypeDetector (v5.2.0)', () => {
|
||||
describe('Code Files - Programming Languages', () => {
|
||||
it('should detect TypeScript files', () => {
|
||||
expect(mimeDetector.detectMimeType('file.ts')).toBe('text/typescript')
|
||||
expect(mimeDetector.detectMimeType('component.tsx')).toBe('text/typescript')
|
||||
})
|
||||
|
||||
it('should detect JavaScript files', () => {
|
||||
// mime library returns text/javascript for .js (IANA standard)
|
||||
expect(mimeDetector.detectMimeType('file.js')).toBe('text/javascript')
|
||||
expect(mimeDetector.detectMimeType('component.jsx')).toBe('text/javascript')
|
||||
expect(mimeDetector.detectMimeType('module.mjs')).toBe('text/javascript')
|
||||
})
|
||||
|
||||
it('should detect modern languages', () => {
|
||||
expect(mimeDetector.detectMimeType('file.kt')).toBe('text/x-kotlin')
|
||||
expect(mimeDetector.detectMimeType('file.swift')).toBe('text/x-swift')
|
||||
expect(mimeDetector.detectMimeType('file.dart')).toBe('text/x-dart')
|
||||
expect(mimeDetector.detectMimeType('script.lua')).toBe('text/x-lua')
|
||||
expect(mimeDetector.detectMimeType('app.scala')).toBe('text/x-scala')
|
||||
})
|
||||
|
||||
it('should detect traditional languages', () => {
|
||||
expect(mimeDetector.detectMimeType('script.py')).toBe('text/x-python')
|
||||
expect(mimeDetector.detectMimeType('main.go')).toBe('text/x-go')
|
||||
expect(mimeDetector.detectMimeType('lib.rs')).toBe('text/x-rust')
|
||||
expect(mimeDetector.detectMimeType('App.java')).toBe('text/x-java')
|
||||
expect(mimeDetector.detectMimeType('main.c')).toBe('text/x-c')
|
||||
expect(mimeDetector.detectMimeType('main.cpp')).toBe('text/x-c++')
|
||||
})
|
||||
|
||||
it('should detect functional languages', () => {
|
||||
expect(mimeDetector.detectMimeType('main.hs')).toBe('text/x-haskell')
|
||||
expect(mimeDetector.detectMimeType('core.clj')).toBe('text/x-clojure')
|
||||
expect(mimeDetector.detectMimeType('server.erl')).toBe('text/x-erlang')
|
||||
expect(mimeDetector.detectMimeType('lib.ex')).toBe('text/x-elixir')
|
||||
})
|
||||
})
|
||||
|
||||
describe('Shell Scripts', () => {
|
||||
it('should detect various shell script types', () => {
|
||||
expect(mimeDetector.detectMimeType('script.sh')).toBe('application/x-sh')
|
||||
expect(mimeDetector.detectMimeType('setup.bash')).toBe('text/x-shellscript')
|
||||
expect(mimeDetector.detectMimeType('config.zsh')).toBe('text/x-shellscript')
|
||||
expect(mimeDetector.detectMimeType('functions.fish')).toBe('text/x-shellscript')
|
||||
})
|
||||
})
|
||||
|
||||
describe('Configuration Files', () => {
|
||||
it('should detect config file formats', () => {
|
||||
expect(mimeDetector.detectMimeType('.env')).toBe('text/x-env')
|
||||
expect(mimeDetector.detectMimeType('config.ini')).toBe('text/x-ini')
|
||||
expect(mimeDetector.detectMimeType('app.properties')).toBe('text/x-java-properties')
|
||||
expect(mimeDetector.detectMimeType('settings.conf')).toBe('text/plain')
|
||||
})
|
||||
|
||||
it('should detect dotfiles', () => {
|
||||
expect(mimeDetector.detectMimeType('.gitignore')).toBe('text/plain')
|
||||
expect(mimeDetector.detectMimeType('.dockerignore')).toBe('text/plain')
|
||||
expect(mimeDetector.detectMimeType('.npmignore')).toBe('text/plain')
|
||||
expect(mimeDetector.detectMimeType('.editorconfig')).toBe('text/plain')
|
||||
})
|
||||
})
|
||||
|
||||
describe('Build & Project Files', () => {
|
||||
it('should detect special build files', () => {
|
||||
expect(mimeDetector.detectMimeType('Dockerfile')).toBe('text/x-dockerfile')
|
||||
expect(mimeDetector.detectMimeType('Makefile')).toBe('text/x-makefile')
|
||||
expect(mimeDetector.detectMimeType('build.gradle')).toBe('text/x-gradle')
|
||||
expect(mimeDetector.detectMimeType('CMakeLists.txt.cmake')).toBe('text/x-cmake')
|
||||
})
|
||||
})
|
||||
|
||||
describe('Web Framework Components', () => {
|
||||
it('should detect modern web frameworks', () => {
|
||||
expect(mimeDetector.detectMimeType('Component.vue')).toBe('text/x-vue')
|
||||
expect(mimeDetector.detectMimeType('Page.svelte')).toBe('text/x-svelte')
|
||||
expect(mimeDetector.detectMimeType('layout.astro')).toBe('text/x-astro')
|
||||
})
|
||||
|
||||
it('should detect style preprocessors', () => {
|
||||
expect(mimeDetector.detectMimeType('styles.scss')).toBe('text/x-scss')
|
||||
expect(mimeDetector.detectMimeType('theme.sass')).toBe('text/x-sass')
|
||||
expect(mimeDetector.detectMimeType('main.less')).toBe('text/x-less')
|
||||
})
|
||||
})
|
||||
|
||||
describe('Data Formats', () => {
|
||||
it('should detect standard data formats', () => {
|
||||
expect(mimeDetector.detectMimeType('data.json')).toBe('application/json')
|
||||
expect(mimeDetector.detectMimeType('config.yaml')).toBe('text/yaml')
|
||||
expect(mimeDetector.detectMimeType('config.yml')).toBe('text/yaml')
|
||||
expect(mimeDetector.detectMimeType('data.xml')).toBe('text/xml')
|
||||
expect(mimeDetector.detectMimeType('data.csv')).toBe('text/csv')
|
||||
})
|
||||
|
||||
it('should detect big data formats', () => {
|
||||
expect(mimeDetector.detectMimeType('data.parquet')).toBe('application/vnd.apache.parquet')
|
||||
expect(mimeDetector.detectMimeType('schema.avro')).toBe('application/avro')
|
||||
expect(mimeDetector.detectMimeType('api.proto')).toBe('text/x-protobuf')
|
||||
})
|
||||
})
|
||||
|
||||
describe('Office Formats - Microsoft Office', () => {
|
||||
it('should detect Word documents', () => {
|
||||
expect(mimeDetector.detectMimeType('document.docx'))
|
||||
.toBe('application/vnd.openxmlformats-officedocument.wordprocessingml.document')
|
||||
expect(mimeDetector.detectMimeType('template.dotx'))
|
||||
.toBe('application/vnd.openxmlformats-officedocument.wordprocessingml.template')
|
||||
})
|
||||
|
||||
it('should detect Excel spreadsheets', () => {
|
||||
expect(mimeDetector.detectMimeType('spreadsheet.xlsx'))
|
||||
.toBe('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
|
||||
expect(mimeDetector.detectMimeType('template.xltx'))
|
||||
.toBe('application/vnd.openxmlformats-officedocument.spreadsheetml.template')
|
||||
})
|
||||
|
||||
it('should detect PowerPoint presentations', () => {
|
||||
expect(mimeDetector.detectMimeType('presentation.pptx'))
|
||||
.toBe('application/vnd.openxmlformats-officedocument.presentationml.presentation')
|
||||
})
|
||||
})
|
||||
|
||||
describe('Office Formats - OpenDocument', () => {
|
||||
it('should detect OpenDocument formats', () => {
|
||||
expect(mimeDetector.detectMimeType('document.odt')).toBe('application/vnd.oasis.opendocument.text')
|
||||
expect(mimeDetector.detectMimeType('spreadsheet.ods')).toBe('application/vnd.oasis.opendocument.spreadsheet')
|
||||
expect(mimeDetector.detectMimeType('presentation.odp')).toBe('application/vnd.oasis.opendocument.presentation')
|
||||
})
|
||||
})
|
||||
|
||||
describe('Media Files', () => {
|
||||
it('should detect image formats', () => {
|
||||
expect(mimeDetector.detectMimeType('photo.jpg')).toBe('image/jpeg')
|
||||
expect(mimeDetector.detectMimeType('logo.png')).toBe('image/png')
|
||||
expect(mimeDetector.detectMimeType('icon.svg')).toBe('image/svg+xml')
|
||||
expect(mimeDetector.detectMimeType('graphic.webp')).toBe('image/webp')
|
||||
})
|
||||
|
||||
it('should detect video formats', () => {
|
||||
expect(mimeDetector.detectMimeType('video.mp4')).toBe('video/mp4')
|
||||
expect(mimeDetector.detectMimeType('movie.mov')).toBe('video/quicktime')
|
||||
expect(mimeDetector.detectMimeType('clip.webm')).toBe('video/webm')
|
||||
})
|
||||
|
||||
it('should detect audio formats', () => {
|
||||
expect(mimeDetector.detectMimeType('song.mp3')).toBe('audio/mpeg')
|
||||
expect(mimeDetector.detectMimeType('sound.wav')).toBe('audio/wav')
|
||||
expect(mimeDetector.detectMimeType('podcast.ogg')).toBe('audio/ogg')
|
||||
})
|
||||
})
|
||||
|
||||
describe('Text File Detection', () => {
|
||||
it('should identify text files correctly', () => {
|
||||
// Code files
|
||||
expect(mimeDetector.isTextFile('text/typescript')).toBe(true)
|
||||
expect(mimeDetector.isTextFile('text/x-python')).toBe(true)
|
||||
expect(mimeDetector.isTextFile('application/javascript')).toBe(true)
|
||||
|
||||
// Data formats
|
||||
expect(mimeDetector.isTextFile('application/json')).toBe(true)
|
||||
expect(mimeDetector.isTextFile('text/yaml')).toBe(true)
|
||||
expect(mimeDetector.isTextFile('text/xml')).toBe(true)
|
||||
|
||||
// Binary files
|
||||
expect(mimeDetector.isTextFile('image/png')).toBe(false)
|
||||
expect(mimeDetector.isTextFile('video/mp4')).toBe(false)
|
||||
expect(mimeDetector.isTextFile('application/pdf')).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
describe('Edge Cases', () => {
|
||||
it('should handle files without extensions', () => {
|
||||
expect(mimeDetector.detectMimeType('Dockerfile')).toBe('text/x-dockerfile')
|
||||
expect(mimeDetector.detectMimeType('Makefile')).toBe('text/x-makefile')
|
||||
})
|
||||
|
||||
it('should handle unknown extensions', () => {
|
||||
expect(mimeDetector.detectMimeType('file.unknown')).toBe('application/octet-stream')
|
||||
expect(mimeDetector.detectMimeType('random.foobar123')).toBe('application/octet-stream')
|
||||
})
|
||||
|
||||
it('should handle case variations', () => {
|
||||
expect(mimeDetector.detectMimeType('FILE.TS')).toBe('text/typescript')
|
||||
expect(mimeDetector.detectMimeType('DoCtUmEnT.JSON')).toBe('application/json')
|
||||
})
|
||||
})
|
||||
|
||||
describe('Coverage Verification', () => {
|
||||
it('should handle 40+ file types from mime library', () => {
|
||||
// Standard web types (from mime library)
|
||||
expect(mimeDetector.detectMimeType('page.html')).toBe('text/html')
|
||||
expect(mimeDetector.detectMimeType('style.css')).toBe('text/css')
|
||||
expect(mimeDetector.detectMimeType('doc.pdf')).toBe('application/pdf')
|
||||
expect(mimeDetector.detectMimeType('archive.zip')).toBe('application/zip')
|
||||
expect(mimeDetector.detectMimeType('data.tar')).toBe('application/x-tar')
|
||||
})
|
||||
|
||||
it('should handle 50+ custom types', () => {
|
||||
// All custom types should be defined
|
||||
const customTypes = [
|
||||
['.bash', 'text/x-shellscript'],
|
||||
['.kt', 'text/x-kotlin'],
|
||||
['.swift', 'text/x-swift'],
|
||||
['.dart', 'text/x-dart'],
|
||||
['.env', 'text/x-env'],
|
||||
['.vue', 'text/x-vue'],
|
||||
['.parquet', 'application/vnd.apache.parquet']
|
||||
]
|
||||
|
||||
customTypes.forEach(([ext, expected]) => {
|
||||
expect(mimeDetector.detectMimeType(`file${ext}`)).toBe(expected)
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
Loading…
Add table
Add a link
Reference in a new issue