- **New app: GenOffice HTML.** Open, edit and generate .html pages: a source pane with a sandboxed live preview, click-to-select elements with a floating toolbar and style panel, device-size preview, image tools and a present mode. AI generates whole pages from a brief with style directions and offers page-wide Restyle, Theme and Summarize. Export to Word runs locally through the built-in html2docx converter (native, editable OOXML; no print-to-PDF detour). - **AI:** separate BYOK providers for web search, image generation and image/video analysis; new Codex App Server provider; Ask AI quotes the selected text and keeps it highlighted while it runs; custom AI panel text size; blocked requests retry through the Chromium network stack. - **App:** global AutoSave setting shared by every editor; AI panel text size and chat spellcheck toggles (genoffice#249); Czech (cs) UI locale (genoffice#259); Save As defaults to the document's folder; PDF export no longer aborts on CJK file paths on Windows. - **Docs:** Export as HTML; comments and replies edit in place from the comments pane; the caret can leave a trailing table (genoffice#266); spellcheck re-marks text as soon as it is re-enabled; AI edits are character-precise on partial selections; zoom keeps the scroll position; pasting a cell from Sheets takes the destination font. - **Sheets:** Find no longer freezes on wide or streamed grids; Replace searches as you type; cached values of external-workbook formulas are kept instead of #ERROR! (genoffice#235); "number stored as text" is a warning, not an error (genoffice#236); paste repeats into the target range like Excel; filter criteria survive save/reopen; saves retry when Windows locks the file; many Excel-fidelity fixes for column widths, row heights, charts, slicers, number formats and CJK fonts. - **Slides:** PDF export works for large presentations (genoffice#264); EMF/WMF picture fonts and East Asian theme fonts resolve correctly; decks with missing layouts or embedded styles still open; outline-only pie slices render. - **PDF:** highlight and underline geometry aligns with the text; Shift+arrows extend the selection instead of paging. - **Markdown:** Find and replace (Ctrl+F / Ctrl+H) (genoffice#253); mermaid code blocks render as diagrams (genoffice#263); better print quality. - Thanks to community contributors @GoodScholar, @aniruddhaadak80 and @Sun8854, and to @hanyscz, @qgzln, @zyp997, @bisslot, @krakotay and @Ben03151 for the reports. Co-authored-by: GenOffice <merrick-2002@users.noreply.github.com>
121 lines
5.0 KiB
TypeScript
121 lines
5.0 KiB
TypeScript
import { afterEach, describe, expect, it, vi } from 'vitest'
|
|
import { AI_DEFAULT_USER_AGENT, aiFetch, setAiUserAgent, setRescueFetch } from '../src/fetch'
|
|
|
|
afterEach(() => {
|
|
vi.unstubAllGlobals()
|
|
setRescueFetch(null)
|
|
setAiUserAgent(AI_DEFAULT_USER_AGENT)
|
|
})
|
|
|
|
function sentHeaders(fetchMock: ReturnType<typeof vi.fn>): Headers {
|
|
const init = fetchMock.mock.calls[0]![1] as RequestInit
|
|
return new Headers(init.headers)
|
|
}
|
|
|
|
describe('aiFetch', () => {
|
|
it('identifies the client to gateways that flag anonymous traffic', async () => {
|
|
const fetchMock = vi.fn().mockResolvedValue(new Response('ok'))
|
|
vi.stubGlobal('fetch', fetchMock)
|
|
await aiFetch('https://x/', { headers: { Authorization: 'Bearer k' } })
|
|
const headers = sentHeaders(fetchMock)
|
|
expect(headers.get('user-agent')).toBe('GenOffice')
|
|
expect(headers.get('authorization')).toBe('Bearer k')
|
|
})
|
|
|
|
it('lets the host refine the user agent and never overrides an explicit one', async () => {
|
|
const fetchMock = vi.fn().mockResolvedValue(new Response('ok'))
|
|
vi.stubGlobal('fetch', fetchMock)
|
|
setAiUserAgent('GenOffice/1.2.3')
|
|
await aiFetch('https://x/', {})
|
|
expect(sentHeaders(fetchMock).get('user-agent')).toBe('GenOffice/1.2.3')
|
|
|
|
fetchMock.mockClear()
|
|
await aiFetch('https://x/', { headers: { 'User-Agent': 'custom/9' } })
|
|
expect(sentHeaders(fetchMock).get('user-agent')).toBe('custom/9')
|
|
})
|
|
|
|
it('passes the same identified request to the rescue fetch', async () => {
|
|
vi.stubGlobal('fetch', vi.fn().mockRejectedValue(new Error('fetch failed')))
|
|
const rescue = vi.fn().mockResolvedValue(new Response('rescued'))
|
|
setRescueFetch(rescue)
|
|
await aiFetch('https://x/', {})
|
|
expect(sentHeaders(rescue).get('user-agent')).toBe('GenOffice')
|
|
})
|
|
|
|
it('returns the primary response without touching the rescue path', async () => {
|
|
const ok = new Response('ok')
|
|
vi.stubGlobal('fetch', vi.fn().mockResolvedValue(ok))
|
|
const rescue = vi.fn()
|
|
setRescueFetch(rescue)
|
|
expect(await aiFetch('https://x/', {})).toBe(ok)
|
|
expect(rescue).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('retries over the rescue fetch when the primary fails', async () => {
|
|
vi.stubGlobal('fetch', vi.fn().mockRejectedValue(new Error('fetch failed')))
|
|
const ok = new Response('rescued')
|
|
const rescue = vi.fn().mockResolvedValue(ok)
|
|
setRescueFetch(rescue)
|
|
expect(await aiFetch('https://x/', {})).toBe(ok)
|
|
expect(rescue).toHaveBeenCalledOnce()
|
|
})
|
|
|
|
it('throws the primary error when no rescue fetch is set', async () => {
|
|
vi.stubGlobal('fetch', vi.fn().mockRejectedValue(new Error('ECONNRESET')))
|
|
await expect(aiFetch('https://x/', {})).rejects.toThrow('ECONNRESET')
|
|
})
|
|
|
|
it('throws the primary error when the rescue fetch also fails', async () => {
|
|
vi.stubGlobal('fetch', vi.fn().mockRejectedValue(new Error('primary down')))
|
|
setRescueFetch(vi.fn().mockRejectedValue(new Error('rescue down')))
|
|
await expect(aiFetch('https://x/', {})).rejects.toThrow('primary down')
|
|
})
|
|
|
|
it('retries a 403 HTML block page over the rescue fetch', async () => {
|
|
const blocked = () =>
|
|
new Response('<!doctype html><title>Just a moment...</title>', {
|
|
status: 403,
|
|
headers: { 'content-type': 'text/html; charset=utf-8' },
|
|
})
|
|
vi.stubGlobal('fetch', vi.fn().mockResolvedValue(blocked()))
|
|
const ok = new Response('{}', { headers: { 'content-type': 'application/json' } })
|
|
const rescue = vi.fn().mockResolvedValue(ok)
|
|
setRescueFetch(rescue)
|
|
expect(await aiFetch('https://www.genspark.ai/api/x', { method: 'POST', body: '{}' })).toBe(ok)
|
|
expect(rescue).toHaveBeenCalledOnce()
|
|
|
|
// still blocked on the rescue path: the primary answer stands
|
|
const primary = blocked()
|
|
vi.stubGlobal('fetch', vi.fn().mockResolvedValue(primary))
|
|
setRescueFetch(vi.fn().mockResolvedValue(blocked()))
|
|
expect(await aiFetch('https://www.genspark.ai/api/x', { body: '{}' })).toBe(primary)
|
|
})
|
|
|
|
it('does not treat an API 403 or a stream body as a block page', async () => {
|
|
const denied = new Response('{"error":"forbidden"}', {
|
|
status: 403,
|
|
headers: { 'content-type': 'application/json' },
|
|
})
|
|
vi.stubGlobal('fetch', vi.fn().mockResolvedValue(denied))
|
|
const rescue = vi.fn()
|
|
setRescueFetch(rescue)
|
|
expect(await aiFetch('https://x/', { body: '{}' })).toBe(denied)
|
|
expect(rescue).not.toHaveBeenCalled()
|
|
|
|
const html = new Response('<html>', { status: 403, headers: { 'content-type': 'text/html' } })
|
|
vi.stubGlobal('fetch', vi.fn().mockResolvedValue(html))
|
|
expect(await aiFetch('https://x/', { body: new ReadableStream() })).toBe(html)
|
|
expect(rescue).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('does not retry an aborted request', async () => {
|
|
vi.stubGlobal('fetch', vi.fn().mockRejectedValue(new Error('aborted')))
|
|
const rescue = vi.fn()
|
|
setRescueFetch(rescue)
|
|
const controller = new AbortController()
|
|
controller.abort()
|
|
await expect(aiFetch('https://x/', { signal: controller.signal })).rejects.toThrow('aborted')
|
|
expect(rescue).not.toHaveBeenCalled()
|
|
})
|
|
})
|