Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions common/src/util/__tests__/cache-debug.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { describe, expect, it } from 'bun:test'

import { normalizeProviderRequestBodyForCacheDebug } from '../cache-debug'

describe('cache-debug data URL handling', () => {
it('summarizes valid data URLs', () => {
const result = normalizeProviderRequestBodyForCacheDebug({
provider: 'openai',
body: {
model: 'gpt-4',
messages: [
{
role: 'user',
content: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8/5+hHgAHggJ/PchI7wAAAABJRU5ErkJggg==',
},
],
},
})

const message = (result as { messages: unknown[] }).messages[0] as {
content: { type: string; mediaType: string; payloadLength: number }
}
expect(message.content.type).toBe('data-url')
expect(message.content.mediaType).toBe('image/png')
expect(message.content.payloadLength).toBeGreaterThan(0)
})

it('passes through non-data-URL strings unchanged', () => {
const result = normalizeProviderRequestBodyForCacheDebug({
provider: 'openai',
body: {
model: 'gpt-4',
messages: [
{
role: 'user',
content: 'https://example.com/image.png',
},
],
},
})

const message = (result as { messages: unknown[] }).messages[0] as {
content: string
}
expect(message.content).toBe('https://example.com/image.png')
})

it('handles data URL-like strings without comma gracefully', () => {
// Edge case: string starts with "data:" but has no comma
const result = normalizeProviderRequestBodyForCacheDebug({
provider: 'openai',
body: {
model: 'gpt-4',
messages: [
{
role: 'user',
content: 'data:image/png;base64',
},
],
},
})

const message = (result as { messages: unknown[] }).messages[0] as {
content: { type: string; mediaType: string; payloadLength: number }
}
expect(message.content.type).toBe('data-url')
expect(message.content.mediaType).toBe('image/png')
expect(message.content.payloadLength).toBe(0)
})
})
3 changes: 3 additions & 0 deletions common/src/util/cache-debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ function normalizeForJson(value: unknown): SerializableValue {
}

function summarizeDataUrl(value: string): SerializableValue {
if (!value.startsWith('data:')) {
return value
}
const firstComma = value.indexOf(',')
const header = firstComma >= 0 ? value.slice(0, firstComma) : value
const payload = firstComma >= 0 ? value.slice(firstComma + 1) : ''
Expand Down
Loading