hermes-agent/apps/desktop/src/lib/embedded-images.test.ts
Brooklyn Nicholson a6b670d4a2 fix(desktop): avoid stack overflow on embedded image replay
Replace the giant embedded-image regex with a bounded scanner so opening sessions with multi-megabyte data URLs does not crash the renderer.
2026-06-22 18:19:36 -05:00

44 lines
1.7 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import { extractEmbeddedImages } from './embedded-images'
const SAMPLE_PNG_DATA_URL = 'data:image/png;base64,' + 'A'.repeat(120)
describe('extractEmbeddedImages', () => {
it('returns text untouched when no data URL is present', () => {
expect(extractEmbeddedImages('describe this')).toEqual({ cleanedText: 'describe this', images: [] })
})
it('lifts a bare data:image URL out of prose', () => {
const result = extractEmbeddedImages(`describe this ${SAMPLE_PNG_DATA_URL}`)
expect(result.cleanedText).toBe('describe this')
expect(result.images).toEqual([SAMPLE_PNG_DATA_URL])
})
it('lifts a JSON-wrapped image_url envelope out of prose', () => {
const result = extractEmbeddedImages(
`describe this{"type":"image_url","image_url":{"url":"${SAMPLE_PNG_DATA_URL}"}}`
)
expect(result.cleanedText).toBe('describe this')
expect(result.images).toEqual([SAMPLE_PNG_DATA_URL])
})
it('extracts multiple embedded images', () => {
const second = 'data:image/jpeg;base64,' + 'B'.repeat(96)
const result = extractEmbeddedImages(`first ${SAMPLE_PNG_DATA_URL} mid ${second} tail`)
expect(result.cleanedText).toBe('first mid tail')
expect(result.images).toEqual([SAMPLE_PNG_DATA_URL, second])
})
it('handles multi-megabyte data URLs without overflowing the JS stack', () => {
const hugeDataUrl = 'data:image/png;base64,' + 'A'.repeat(8_000_000)
const result = extractEmbeddedImages(`describe this ${hugeDataUrl} thanks`)
expect(result.cleanedText).toBe('describe this thanks')
expect(result.images).toHaveLength(1)
expect(result.images[0]).toHaveLength(hugeDataUrl.length)
})
})