Files
genoffice/packages/electron-utils/tests/app-menu.test.ts
T
merrick-2002andGenOffice d24c964a3e Sync snapshot (2026-09-09) (#271)
- **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>
2026-09-09 18:19:13 +08:00

167 lines
4.6 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from 'vitest'
const electronMock = vi.hoisted(() => ({
getFocusedWebContents: vi.fn(),
}))
vi.mock('electron', () => ({
webContents: {
getFocusedWebContents: electronMock.getFocusedWebContents,
},
}))
import {
appMenuLabels,
editMenuTemplate,
viewMenuTemplate,
windowMenuTemplate,
type AppMenuLabels,
} from '../src/index'
const LANGS = [
'zh',
'en',
'ja',
'ko',
'fr',
'de',
'es',
'th',
'id',
'ru',
'ar',
'pt',
'it',
'pl',
'cs',
'nl',
'ms',
'he',
'hi',
'zh-TW',
] as const
const en = appMenuLabels('en')
type Item = { role?: string; type?: string; label?: string; accelerator?: string }
const submenuOf = (tpl: { submenu?: unknown }): Item[] => tpl.submenu as Item[]
beforeEach(() => {
vi.clearAllMocks()
})
describe('appMenuLabels', () => {
it('covers all 20 languages with every key non-empty', () => {
const keys = Object.keys(en) as (keyof AppMenuLabels)[]
for (const lang of LANGS) {
const labels = appMenuLabels(lang)
for (const key of keys) expect(labels[key], `${lang}.${key}`).toBeTruthy()
}
})
it('falls back to English for unknown languages', () => {
expect(appMenuLabels('xx')).toEqual(en)
})
it('merges the context-menu clipboard labels', () => {
const zh = appMenuLabels('zh')
expect(zh.copy).toBe('复制')
expect(zh.selectAll).toBe('全选')
})
})
describe('windowMenuTemplate', () => {
it('keeps the native role on macOS', () => {
expect(windowMenuTemplate('darwin', en)).toEqual({ role: 'windowMenu', label: 'Window' })
})
it.each(['win32', 'linux'] as const)('builds Minimize/Close only on %s', (platform) => {
const tpl = windowMenuTemplate(platform, en)
expect(tpl.role).toBeUndefined()
expect(tpl.label).toBe('Window')
const items = submenuOf(tpl)
expect(items.map((i) => i.label ?? i.type)).toEqual(['Minimize', 'separator', 'Close Window'])
// no mac-only entries, no roles, no Ctrl+M
for (const item of items) {
expect(item.role).toBeUndefined()
expect(item.accelerator).toBeUndefined()
expect(item.label ?? '').not.toMatch(/zoom|front/i)
}
})
it('localizes the Windows submenu', () => {
const items = submenuOf(windowMenuTemplate('win32', appMenuLabels('zh')))
expect(items[0]!.label).toBe('最小化')
expect(items[2]!.label).toBe('关闭窗口')
})
})
describe('editMenuTemplate', () => {
it('keeps the native role on macOS', () => {
expect(editMenuTemplate('darwin', en)).toEqual({ role: 'editMenu', label: 'Edit' })
})
it('mirrors the non-mac role expansion with labeled roles on Windows', () => {
const tpl = editMenuTemplate('win32', en)
expect(tpl.label).toBe('Edit')
expect(submenuOf(tpl).map((i) => i.role ?? i.type)).toEqual([
'undo',
'redo',
'separator',
'cut',
'copy',
'paste',
'delete',
'separator',
'selectAll',
])
for (const item of submenuOf(tpl)) if (item.role) expect(item.label).toBeTruthy()
})
})
describe('viewMenuTemplate', () => {
it('mirrors the viewMenu role expansion with labeled roles', () => {
const tpl = viewMenuTemplate(en)
expect(tpl.label).toBe('View')
expect(submenuOf(tpl).map((i) => i.role ?? i.type ?? i.label)).toEqual([
'reload',
'forceReload',
'Developer Tools',
'separator',
'resetZoom',
'zoomIn',
'zoomOut',
'separator',
'togglefullscreen',
])
for (const item of submenuOf(tpl)) if (item.role) expect(item.label).toBeTruthy()
})
it('opens DevTools via an explicit click item, not the docking role', () => {
const item = submenuOf(viewMenuTemplate(en))[2] as Item & { click?: unknown }
expect(item.role).toBeUndefined()
expect(typeof item.click).toBe('function')
expect(item.accelerator).toBe(process.platform === 'darwin' ? 'Alt+Command+I' : 'Ctrl+Shift+I')
})
it('closes detached DevTools when they have focus', async () => {
const target = {
devToolsWebContents: {},
isDestroyed: vi.fn(() => false),
isDevToolsOpened: vi.fn().mockReturnValueOnce(false).mockReturnValueOnce(true),
openDevTools: vi.fn(),
closeDevTools: vi.fn(),
}
electronMock.getFocusedWebContents.mockReturnValueOnce(target).mockReturnValueOnce(null)
const item = submenuOf(viewMenuTemplate(en))[2] as Item & {
click: () => Promise<void>
}
await item.click()
await item.click()
expect(target.openDevTools).toHaveBeenCalledWith({ mode: 'detach' })
expect(target.closeDevTools).toHaveBeenCalledOnce()
})
})