Skip to content

AI-native design editor. Open-source Figma alternative.

test(core): cover utility-backed API boundaries

Danila Poyarkovbfe9a5a4ba4b
diff --git a/packages/core/src/icons/api.ts b/packages/core/src/icons/api.ts
index a508d0221..3511ad627 100644
--- a/packages/core/src/icons/api.ts
+++ b/packages/core/src/icons/api.ts
@@ -1,46 +1,65 @@
-import { ofetch } from 'ofetch'
+import { createFetch } from 'ofetch'
import type { IconifyResponse, IconSearchResult } from './types'
const ICONIFY_API = 'https://api.iconify.design'
const FETCH_TIMEOUT_MS = 10_000
-const iconifyApi = ofetch.create({
- baseURL: ICONIFY_API,
- retry: 0,
- timeout: FETCH_TIMEOUT_MS
-})
+export function createIconifyAPIClient(
+ fetcher: typeof globalThis.fetch = globalThis.fetch,
+ baseURL = ICONIFY_API
+) {
+ const iconifyApi = createFetch({ fetch: fetcher }).create({
+ baseURL,
+ retry: 0,
+ timeout: FETCH_TIMEOUT_MS
+ })
+
+ return {
+ async fetchCollection(prefix: string, iconNames: string[]): Promise<IconifyResponse> {
+ const response = await iconifyApi.raw<IconifyResponse>(`/${prefix}.json`, {
+ ignoreResponseError: true,
+ query: { icons: iconNames.join(',') }
+ })
+ if (!response.ok) {
+ throw new Error(`Iconify API error: ${response.status} for prefix "${prefix}"`)
+ }
+ return response._data as IconifyResponse
+ },
+
+ async search(
+ query: string,
+ options?: { limit?: number; prefix?: string }
+ ): Promise<IconSearchResult> {
+ const response = await iconifyApi.raw<IconSearchResult>('/search', {
+ ignoreResponseError: true,
+ query: { query, limit: options?.limit, prefix: options?.prefix }
+ })
+ if (!response.ok) throw new Error(`Iconify search error: ${response.status}`)
+
+ const data = response._data
+ const limit = options?.limit ?? 5
+ return {
+ icons: data?.icons.slice(0, limit) ?? [],
+ total: data?.total ?? 0,
+ collections: data?.collections ?? {}
+ }
+ }
+ }
+}
-export async function fetchIconifyCollection(
+const iconifyAPIClient = createIconifyAPIClient()
+
+export function fetchIconifyCollection(
prefix: string,
iconNames: string[]
): Promise<IconifyResponse> {
- const response = await iconifyApi.raw<IconifyResponse>(`/${prefix}.json`, {
- ignoreResponseError: true,
- query: { icons: iconNames.join(',') }
- })
- if (!response.ok) throw new Error(`Iconify API error: ${response.status} for prefix "${prefix}"`)
- return response._data as IconifyResponse
+ return iconifyAPIClient.fetchCollection(prefix, iconNames)
}
-export async function searchIconify(
+export function searchIconify(
query: string,
- options?: {
- limit?: number
- prefix?: string
- }
+ options?: { limit?: number; prefix?: string }
): Promise<IconSearchResult> {
- const response = await iconifyApi.raw<IconSearchResult>('/search', {
- ignoreResponseError: true,
- query: { query, limit: options?.limit, prefix: options?.prefix }
- })
- if (!response.ok) throw new Error(`Iconify search error: ${response.status}`)
-
- const data = response._data
- const limit = options?.limit ?? 5
- return {
- icons: data?.icons.slice(0, limit) ?? [],
- total: data?.total ?? 0,
- collections: data?.collections ?? {}
- }
+ return iconifyAPIClient.search(query, options)
}
diff --git a/tests/engine/icons/api.test.ts b/tests/engine/icons/api.test.ts
new file mode 100644
index 000000000..794f98233
--- /dev/null
+++ b/tests/engine/icons/api.test.ts
@@ -0,0 +1,64 @@
+import { describe, expect, mock, test } from 'bun:test'
+
+import { createIconifyAPIClient } from '#core/icons/api'
+
+function createMockFetch(responder: (request: Request) => Response) {
+ return mock(async (input: RequestInfo | URL, init?: RequestInit) =>
+ responder(new Request(input, init))
+ ) as typeof fetch
+}
+
+describe('Iconify API client', () => {
+ test('encodes collection query parameters through ofetch', async () => {
+ const fetcher = createMockFetch((request) => {
+ expect(request.method).toBe('GET')
+ const url = new URL(request.url)
+ expect(url.origin).toBe('https://icons.example')
+ expect(url.pathname).toBe('/lucide.json')
+ expect(url.searchParams.get('icons')).toBe('home,search')
+ return Response.json({
+ prefix: 'lucide',
+ icons: {
+ home: { body: '<path />' },
+ search: { body: '<path />' }
+ }
+ })
+ })
+ const client = createIconifyAPIClient(fetcher, 'https://icons.example')
+
+ const result = await client.fetchCollection('lucide', ['home', 'search'])
+
+ expect(result.prefix).toBe('lucide')
+ expect(fetcher).toHaveBeenCalledTimes(1)
+ })
+
+ test('passes search options and applies the requested result limit', async () => {
+ const fetcher = createMockFetch((request) => {
+ const url = new URL(request.url)
+ expect(url.pathname).toBe('/search')
+ expect(url.searchParams.get('query')).toBe('arrow left')
+ expect(url.searchParams.get('limit')).toBe('2')
+ expect(url.searchParams.get('prefix')).toBe('lucide')
+ return Response.json({
+ icons: ['lucide:arrow-left', 'lucide:arrow-right', 'lucide:arrow-up'],
+ total: 3,
+ collections: {}
+ })
+ })
+ const client = createIconifyAPIClient(fetcher, 'https://icons.example')
+
+ const result = await client.search('arrow left', { limit: 2, prefix: 'lucide' })
+
+ expect(result.icons).toEqual(['lucide:arrow-left', 'lucide:arrow-right'])
+ })
+
+ test('preserves contextual HTTP error messages without retrying', async () => {
+ const fetcher = createMockFetch(() => new Response('Unavailable', { status: 503 }))
+ const client = createIconifyAPIClient(fetcher, 'https://icons.example')
+
+ await expect(client.fetchCollection('lucide', ['home'])).rejects.toThrow(
+ 'Iconify API error: 503 for prefix "lucide"'
+ )
+ expect(fetcher).toHaveBeenCalledTimes(1)
+ })
+})
diff --git a/tests/engine/tools/stock-photo/requests.test.ts b/tests/engine/tools/stock-photo/requests.test.ts
new file mode 100644
index 000000000..e11be0f92
--- /dev/null
+++ b/tests/engine/tools/stock-photo/requests.test.ts
@@ -0,0 +1,27 @@
+import { describe, expect, test } from 'bun:test'
+
+import { parsePhotoRequests } from '#core/tools/stock-photo/requests'
+
+describe('parsePhotoRequests', () => {
+ test('parses a single request and request arrays', () => {
+ expect(parsePhotoRequests('{"id":"1:2","query":"forest"}')).toEqual([
+ { id: '1:2', query: 'forest' }
+ ])
+ expect(parsePhotoRequests('[{"id":"1:2","query":"forest"}]')).toEqual([
+ { id: '1:2', query: 'forest' }
+ ])
+ })
+
+ test('rejects malformed JSON', () => {
+ expect(parsePhotoRequests('[{"id":')).toEqual({ error: 'Invalid JSON in requests' })
+ })
+
+ test('removes prototype-polluting properties', () => {
+ const result = parsePhotoRequests(
+ '[{"id":"1:2","query":"forest","__proto__":{"polluted":true}}]'
+ )
+
+ expect(result).toEqual({ error: 'Invalid JSON in requests' })
+ expect(Object.hasOwn(Object.prototype, 'polluted')).toBe(false)
+ })
+})