- **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>
195 lines
7.1 KiB
TypeScript
195 lines
7.1 KiB
TypeScript
import { afterEach, describe, expect, it, vi } from 'vitest'
|
|
import { chatForProvider } from '../src/chat'
|
|
import { errorResponse, jsonResponse } from './test-utils'
|
|
|
|
afterEach(() => {
|
|
vi.unstubAllGlobals()
|
|
})
|
|
|
|
describe('chatForProvider', () => {
|
|
it('anthropic: extracts joined text content blocks', async () => {
|
|
vi.stubGlobal(
|
|
'fetch',
|
|
vi.fn().mockResolvedValue(
|
|
jsonResponse({
|
|
content: [
|
|
{ type: 'text', text: 'hello ' },
|
|
{ type: 'text', text: 'world' },
|
|
],
|
|
}),
|
|
),
|
|
)
|
|
const result = await chatForProvider(
|
|
'anthropic',
|
|
{ apiKey: 'k', model: 'claude-sonnet-5' },
|
|
'sys',
|
|
'hi',
|
|
)
|
|
expect(result).toEqual({ ok: true, content: 'hello world' })
|
|
})
|
|
|
|
it('anthropic: surfaces HTTP errors', async () => {
|
|
vi.stubGlobal('fetch', vi.fn().mockResolvedValue(errorResponse(401, 'bad key')))
|
|
const result = await chatForProvider('anthropic', { apiKey: 'k', model: 'm' }, 'sys', 'hi')
|
|
expect(result.ok).toBe(false)
|
|
expect(result.error).toMatch(/Claude HTTP 401/)
|
|
})
|
|
|
|
it('anthropic: replaces an HTML error body with a readable note', async () => {
|
|
const html =
|
|
'<!doctype html>\n<html>\n<head><title>Genspark</title></head><body>app shell</body></html>'
|
|
vi.stubGlobal('fetch', vi.fn().mockResolvedValue(errorResponse(403, html)))
|
|
const result = await chatForProvider('anthropic', { apiKey: 'k', model: 'm' }, 'sys', 'hi')
|
|
expect(result.ok).toBe(false)
|
|
expect(result.error).toMatch(/Claude HTTP 403/)
|
|
expect(result.error).toMatch(/web page.*instead of an API response/)
|
|
expect(result.error).not.toContain('<!doctype')
|
|
})
|
|
|
|
it('gemini: extracts joined parts text', async () => {
|
|
vi.stubGlobal(
|
|
'fetch',
|
|
vi
|
|
.fn()
|
|
.mockResolvedValue(
|
|
jsonResponse({ candidates: [{ content: { parts: [{ text: 'hi there' }] } }] }),
|
|
),
|
|
)
|
|
const result = await chatForProvider(
|
|
'gemini',
|
|
{ apiKey: 'k', model: 'gemini-2.5-flash' },
|
|
'sys',
|
|
'hi',
|
|
)
|
|
expect(result).toEqual({ ok: true, content: 'hi there' })
|
|
})
|
|
|
|
it('deepseek and openai hit their fixed base URLs', async () => {
|
|
const fetchMock = vi
|
|
.fn()
|
|
.mockResolvedValue(jsonResponse({ choices: [{ message: { content: 'ok' } }] }))
|
|
vi.stubGlobal('fetch', fetchMock)
|
|
await chatForProvider('deepseek', { apiKey: 'k', model: 'deepseek-v4-pro' }, 'sys', 'hi')
|
|
expect(fetchMock).toHaveBeenCalledWith(
|
|
'https://api.deepseek.com/v1/chat/completions',
|
|
expect.anything(),
|
|
)
|
|
})
|
|
|
|
it('custom: uses the configured base URL', async () => {
|
|
const fetchMock = vi
|
|
.fn()
|
|
.mockResolvedValue(jsonResponse({ choices: [{ message: { content: 'ok' } }] }))
|
|
vi.stubGlobal('fetch', fetchMock)
|
|
await chatForProvider(
|
|
'custom',
|
|
{ apiKey: 'k', model: 'm', baseUrl: 'https://my-endpoint.example.com/v1' },
|
|
'sys',
|
|
'hi',
|
|
)
|
|
expect(fetchMock).toHaveBeenCalledWith(
|
|
'https://my-endpoint.example.com/v1/chat/completions',
|
|
expect.anything(),
|
|
)
|
|
})
|
|
|
|
it('custom: rejects without a base URL, without calling fetch', async () => {
|
|
const fetchMock = vi.fn()
|
|
vi.stubGlobal('fetch', fetchMock)
|
|
const result = await chatForProvider('custom', { apiKey: 'k', model: 'm' }, 'sys', 'hi')
|
|
expect(result).toEqual({ ok: false, error: 'A custom provider requires a Base URL' })
|
|
expect(fetchMock).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('custom: omits Authorization when the key is empty for local servers', async () => {
|
|
const fetchMock = vi
|
|
.fn()
|
|
.mockResolvedValue(jsonResponse({ choices: [{ message: { content: 'ok' } }] }))
|
|
vi.stubGlobal('fetch', fetchMock)
|
|
await chatForProvider(
|
|
'custom',
|
|
{ apiKey: '', model: 'llama3', baseUrl: 'http://localhost:11434/v1' },
|
|
'sys',
|
|
'hi',
|
|
)
|
|
const headers = fetchMock.mock.calls[0]![1].headers as Record<string, string>
|
|
expect(headers.Authorization).toBeUndefined()
|
|
fetchMock.mockClear()
|
|
fetchMock.mockResolvedValue(jsonResponse({ choices: [{ message: { content: 'ok' } }] }))
|
|
await chatForProvider(
|
|
'custom',
|
|
{ apiKey: 'k', model: 'm', baseUrl: 'https://my-endpoint.example.com/v1' },
|
|
'sys',
|
|
'hi',
|
|
)
|
|
expect((fetchMock.mock.calls[0]![1].headers as Record<string, string>).Authorization).toBe(
|
|
'Bearer k',
|
|
)
|
|
})
|
|
|
|
it('genspark: routes by model prefix to the proxy endpoints', async () => {
|
|
const fetchMock = vi
|
|
.fn()
|
|
.mockResolvedValue(jsonResponse({ content: [{ type: 'text', text: 'ok' }] }))
|
|
vi.stubGlobal('fetch', fetchMock)
|
|
await chatForProvider('genspark', { apiKey: 'gsk-k', model: 'claude-opus-4-7' }, 'sys', 'hi')
|
|
expect(fetchMock).toHaveBeenCalledWith(
|
|
'https://www.genspark.ai/api/anthropic/v1/messages',
|
|
expect.anything(),
|
|
)
|
|
fetchMock.mockResolvedValue(jsonResponse({ choices: [{ message: { content: 'ok' } }] }))
|
|
await chatForProvider('genspark', { apiKey: 'gsk-k', model: 'gpt-5.2' }, 'sys', 'hi')
|
|
expect(fetchMock).toHaveBeenLastCalledWith(
|
|
'https://www.genspark.ai/api/llm_proxy/v1/chat/completions',
|
|
expect.anything(),
|
|
)
|
|
})
|
|
|
|
it('genspark: stamps X-Agent-Type; direct vendors do not get it', async () => {
|
|
const fetchMock = vi
|
|
.fn()
|
|
.mockImplementation(async () => jsonResponse({ content: [{ type: 'text', text: 'ok' }] }))
|
|
vi.stubGlobal('fetch', fetchMock)
|
|
await chatForProvider('genspark', { apiKey: 'gsk-k', model: 'claude-opus-4-7' }, 'sys', 'hi')
|
|
expect((fetchMock.mock.calls[0]![1].headers as Record<string, string>)['X-Agent-Type']).toBe(
|
|
'genoffice',
|
|
)
|
|
fetchMock.mockClear()
|
|
await chatForProvider('anthropic', { apiKey: 'k', model: 'claude-opus-4-7' }, 'sys', 'hi')
|
|
expect(
|
|
(fetchMock.mock.calls[0]![1].headers as Record<string, string>)['X-Agent-Type'],
|
|
).toBeUndefined()
|
|
})
|
|
|
|
it('treats an empty response body as an error', async () => {
|
|
vi.stubGlobal('fetch', vi.fn().mockResolvedValue(jsonResponse({ choices: [{ message: {} }] })))
|
|
const result = await chatForProvider(
|
|
'openai',
|
|
{ apiKey: 'k', model: 'gpt-4.1-mini' },
|
|
'sys',
|
|
'hi',
|
|
)
|
|
expect(result).toEqual({ ok: false, error: 'AI returned an empty response' })
|
|
})
|
|
|
|
it('anthropic: a 200 with an HTML body is an error, not a thrown SyntaxError', async () => {
|
|
const html = '<!doctype html><html><body>gateway</body></html>'
|
|
vi.stubGlobal('fetch', vi.fn().mockResolvedValue(new Response(html, { status: 200 })))
|
|
const result = await chatForProvider('anthropic', { apiKey: 'k', model: 'm' }, 'sys', 'hi')
|
|
expect(result.ok).toBe(false)
|
|
expect(result.error).toMatch(/non-JSON response/)
|
|
expect(result.error).toMatch(/web page.*instead of an API response/)
|
|
})
|
|
|
|
it('openai: a 200 with an empty body is an error, not a thrown SyntaxError', async () => {
|
|
vi.stubGlobal('fetch', vi.fn().mockResolvedValue(new Response('', { status: 200 })))
|
|
const result = await chatForProvider(
|
|
'openai',
|
|
{ apiKey: 'k', model: 'gpt-4.1-mini' },
|
|
'sys',
|
|
'hi',
|
|
)
|
|
expect(result).toEqual({ ok: false, error: 'AI returned a non-JSON response: ' })
|
|
})
|
|
})
|