Skip to content

The all-in-one local AI studio for your desktop: chat, image and video generation and a coding agent in one free, open source app. Windows and Linux. No Docker, no terminal, no cloud required.

test(cloud): unit-cover dataUrlToBlob decoding (base64, percent, mime default)

purple098c8654ae14
diff --git a/src/hooks/__tests__/dataUrlToBlob.test.ts b/src/hooks/__tests__/dataUrlToBlob.test.ts
new file mode 100644
index 00000000..fc622bf4
--- /dev/null
+++ b/src/hooks/__tests__/dataUrlToBlob.test.ts
@@ -0,0 +1,34 @@
+import { describe, it, expect } from 'vitest'
+import { dataUrlToBlob } from '../useCloudCreate'
+
+const PNG_HEADER = [0x89, 0x50, 0x4e, 0x47]
+
+function toDataUrl(bytes: number[], mime: string): string {
+ return `data:${mime};base64,${btoa(String.fromCharCode(...bytes))}`
+}
+
+describe('dataUrlToBlob', () => {
+ it('decodes a base64 data URL preserving bytes and mime', async () => {
+ const bytes = [...PNG_HEADER, 0x00, 0xff, 0x7f]
+ const blob = dataUrlToBlob(toDataUrl(bytes, 'image/png'))
+ expect(blob.type).toBe('image/png')
+ expect([...new Uint8Array(await blob.arrayBuffer())]).toEqual(bytes)
+ })
+
+ it('decodes a percent-encoded (non-base64) data URL', async () => {
+ const blob = dataUrlToBlob('data:text/plain,hello%20world')
+ expect(blob.type).toBe('text/plain')
+ expect(await blob.text()).toBe('hello world')
+ })
+
+ it('handles a mime with charset parameter before base64 marker', async () => {
+ const blob = dataUrlToBlob(`data:image/jpeg;base64,${btoa('\xff\xd8\xff')}`)
+ expect(blob.type).toBe('image/jpeg')
+ expect((await blob.arrayBuffer()).byteLength).toBe(3)
+ })
+
+ it('defaults the mime when the data URL omits it', () => {
+ const blob = dataUrlToBlob(`data:;base64,${btoa('x')}`)
+ expect(blob.type).toBe('application/octet-stream')
+ })
+})
diff --git a/src/hooks/useCloudCreate.ts b/src/hooks/useCloudCreate.ts
index 864f9ab1..17b81785 100644
--- a/src/hooks/useCloudCreate.ts
+++ b/src/hooks/useCloudCreate.ts
@@ -33,7 +33,7 @@ import { checkPromptSafety, SAFETY_BLOCK_MESSAGE } from '../lib/render/safety'
// Decoded by hand instead of fetch(dataUrl): the webview CSP's connect-src
// (rightly) has no data: entry, so fetching a data URL throws "Load failed"
// and killed every source-needing op before the upload even started.
-function dataUrlToBlob(dataUrl: string): Blob {
+export function dataUrlToBlob(dataUrl: string): Blob {
const comma = dataUrl.indexOf(',')
const meta = dataUrl.slice(5, comma)
const data = dataUrl.slice(comma + 1)