Files
genoffice/packages/electron-utils/tests/remote-image.test.ts
T
merrick-2002andGenOffice d1de6ac44b Sync snapshot (2026-08-05) (#34)
- Add a user-selectable update channel (Stable/Beta) with a manual promote-to-stable release flow
- PDF: image signatures and WYSIWYG typed signatures with better placement, plus a status bar, keyboard navigation, and two-page spread reading upgrades
- Docs: Word-parity ribbon font controls and style gallery, an AI Format one-click preset, and restored headers/footers and TOC formatting for Pages-style docx
- Add GenOffice device-code sign-in for AI features
- Refresh UI: AI panel and ribbon styling aligned with the design system, new icons, save toasts, and content-width status bars beside a full-height AI dock
- AI reliability: retry transient image-download failures, detect empty gateway streams without killing long generations, and forward the resolved proxy to spawned CLI children
- Slides: bullet list gallery, type-ahead font filtering, and shorter clarify buttons across locales
- Add a Linux (AppImage) packaging target

Co-authored-by: GenOffice <merrick-2002@users.noreply.github.com>
2026-08-05 21:44:14 +08:00

89 lines
3.2 KiB
TypeScript

import { describe, expect, it, vi } from 'vitest'
import { fetchRemoteImage, remoteImageHeaders } from '../src/remote-image'
const png = () => new Response('img', { status: 200 })
describe('remoteImageHeaders', () => {
it('sends a Referer for genspark hosts', () => {
expect(remoteImageHeaders('https://sspark.genspark.ai/a.png').Referer).toBe(
'https://www.genspark.ai/',
)
expect(remoteImageHeaders('https://genspark.ai/a.png').Referer).toBe('https://www.genspark.ai/')
})
it('sends no Referer for other hosts (including lookalikes)', () => {
expect(remoteImageHeaders('https://example.com/a.png').Referer).toBeUndefined()
expect(remoteImageHeaders('https://evilgenspark.ai/a.png').Referer).toBeUndefined()
})
it('always sends a browser-like User-Agent and image Accept', () => {
const headers = remoteImageHeaders('https://example.com/a.png')
expect(headers['User-Agent']).toBeTruthy()
expect(headers.Accept).toContain('image/')
})
it('does not advertise formats the insert pipelines mislabel (avif/webp)', () => {
const accept = remoteImageHeaders('https://example.com/a.png').Accept!
expect(accept).not.toContain('avif')
expect(accept).not.toContain('webp')
})
})
describe('fetchRemoteImage', () => {
it('returns the response on first success', async () => {
const fetchImpl = vi.fn().mockResolvedValue(png())
const resp = await fetchRemoteImage('https://sspark.genspark.ai/a.png', {
fetchImpl,
retryDelaysMs: [0, 0],
})
expect(resp?.ok).toBe(true)
expect(fetchImpl).toHaveBeenCalledTimes(1)
const headers = fetchImpl.mock.calls[0]![1].headers as Record<string, string>
expect(headers.Referer).toBe('https://www.genspark.ai/')
})
it('retries transient statuses until success', async () => {
const fetchImpl = vi
.fn()
.mockResolvedValueOnce(new Response('nope', { status: 503 }))
.mockResolvedValueOnce(new Response('nope', { status: 403 }))
.mockResolvedValueOnce(png())
const resp = await fetchRemoteImage('https://sspark.genspark.ai/a.png', {
fetchImpl,
retryDelaysMs: [0, 0],
})
expect(resp?.ok).toBe(true)
expect(fetchImpl).toHaveBeenCalledTimes(3)
})
it('retries network errors and returns null when the budget is exhausted', async () => {
const fetchImpl = vi.fn().mockRejectedValue(new Error('ECONNRESET'))
const resp = await fetchRemoteImage('https://sspark.genspark.ai/a.png', {
fetchImpl,
retryDelaysMs: [0],
})
expect(resp).toBeNull()
expect(fetchImpl).toHaveBeenCalledTimes(2)
})
it('does not retry permanent statuses like 404', async () => {
const fetchImpl = vi.fn().mockResolvedValue(new Response('gone', { status: 404 }))
const resp = await fetchRemoteImage('https://sspark.genspark.ai/a.png', {
fetchImpl,
retryDelaysMs: [0, 0],
})
expect(resp?.status).toBe(404)
expect(fetchImpl).toHaveBeenCalledTimes(1)
})
it('does not retry SSRF-blocked URLs', async () => {
const fetchImpl = vi.fn()
const resp = await fetchRemoteImage('https://127.0.0.1/a.png', {
fetchImpl,
retryDelaysMs: [0, 0],
})
expect(resp).toBeNull()
expect(fetchImpl).not.toHaveBeenCalled()
})
})