Skip to content

Commit

Permalink
feature: add e2e tests for new supported image formats
Browse files Browse the repository at this point in the history
  • Loading branch information
lifeart committed Dec 6, 2024
1 parent 8bc93f7 commit d30b56f
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import type { CreateRequest, ProgressResponse } from './interfaces.js'

const sharp = _sharp.default

const IMAGE_EXTENSIONS_TO_CONVERT = ['webp', 'avif', 'gif', 'svg', 'tiff']
const IMAGE_EXTENSIONS_TO_CONVERT = ['webp', 'avif', 'gif', 'svg', 'tiff', 'tif']
export class Ollama extends OllamaBrowser {
async encodeImage(image: Uint8Array | Buffer | string): Promise<string> {
if (typeof image !== 'string') {
Expand Down
50 changes: 50 additions & 0 deletions test/e2e-image-formats.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { describe, it, expect } from 'vitest'
import { Ollama } from '../src/index'
import path from 'path'
import { fileURLToPath } from 'url'
import { dirname } from 'path'


async function describeImage(imageName: string) {
const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)
const instance = new Ollama()
const imagePath = path.resolve(__dirname, `./mocks/images/${imageName}`)
const response = await instance.chat({
model: 'llama3.2-vision',
messages: [{ role: 'user', content: 'what is this?', images: [imagePath] }],
})
return response.message.content;
}

const testConfig = {
timeout: 5 * 60 * 1000, // 5 minutes
retry: 3,
}

describe('Ollama | Nodejs | Vision image formats', () => {
it('support ".webp" image recognition', testConfig, async () => {
const result = await describeImage('WebP-Gradient.webp')
expect(result.toLowerCase()).toContain('gradient')
})

it('support ".gif" image recognition', testConfig, async () => {
const result = await describeImage('200w.gif')
expect(result.toLowerCase()).toContain('cat')
})

it('support ".avif" image recognition', testConfig, async () => {
const result = await describeImage('fox.profile0.8bpc.yuv420.avif')
expect(result.toLowerCase()).toContain('fox')
})

it('support ".tiff/.tif" image recognition', testConfig, async () => {
const result = await describeImage('julia.tif')
expect(result.toLowerCase()).toContain('julia')
})

it('support ".svg" image recognition', testConfig, async () => {
const result = await describeImage('house.svg')
expect(result.toLowerCase()).toContain('house')
})
})
Binary file added test/mocks/images/200w.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/mocks/images/fox.profile0.8bpc.yuv420.avif
Binary file not shown.
37 changes: 37 additions & 0 deletions test/mocks/images/house.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/mocks/images/julia.tif
Binary file not shown.

0 comments on commit d30b56f

Please sign in to comment.