cc-haha/desktop/src/lib/appZoom.test.ts
程序员阿江(Relakkes) 95407422c0 Support IDE-style desktop zoom shortcuts
The desktop shell needed predictable zoom controls across macOS,
Windows, and Linux without depending on browser defaults. This adds a
small app zoom controller that persists a bounded zoom factor, maps the
IDE-style primary-modifier shortcuts, and uses native Tauri webview zoom
when available with a browser fallback for H5/dev runs.

Constraint: Issue #407 requested IDE-style zoom shortcuts across macOS, Windows, and Linux
Rejected: Enable Tauri built-in zoom hotkeys only | it would not cover browser/H5 fallback or app-owned persistence consistently
Confidence: high
Scope-risk: narrow
Directive: Keep app zoom bounded and validated before applying persisted localStorage values
Tested: bun run verify; browser smoke on http://127.0.0.1:45679/ with Meta+= and Meta+0
Not-tested: Manual Windows/Linux desktop runtime smoke
2026-05-13 12:43:07 +08:00

80 lines
2.5 KiB
TypeScript

import { beforeEach, describe, expect, it } from 'vitest'
import {
APP_ZOOM_STORAGE_KEY,
applyAppZoomLevel,
getAppZoomKeyboardAction,
initializeAppZoom,
nextAppZoomLevel,
normalizeAppZoomLevel,
} from './appZoom'
describe('appZoom', () => {
beforeEach(() => {
window.localStorage.clear()
document.documentElement.removeAttribute('data-app-zoom-mode')
document.documentElement.removeAttribute('data-app-zoom-percent')
document.documentElement.style.removeProperty('--app-zoom')
document.body.style.removeProperty('zoom')
})
it('normalizes, clamps, and steps app zoom levels', () => {
expect(normalizeAppZoomLevel('1.25')).toBe(1.25)
expect(normalizeAppZoomLevel('bad')).toBe(1)
expect(normalizeAppZoomLevel(4)).toBe(2)
expect(normalizeAppZoomLevel(0.1)).toBe(0.5)
expect(nextAppZoomLevel(1, 'in')).toBe(1.1)
expect(nextAppZoomLevel(1, 'out')).toBe(0.9)
expect(nextAppZoomLevel(1.7, 'reset')).toBe(1)
})
it('applies browser fallback zoom and preserves valid persisted zoom', async () => {
window.localStorage.setItem(APP_ZOOM_STORAGE_KEY, '1.2')
await initializeAppZoom()
expect(document.documentElement.getAttribute('data-app-zoom-mode')).toBe('css')
expect(document.documentElement.getAttribute('data-app-zoom-percent')).toBe('120')
expect(document.documentElement.style.getPropertyValue('--app-zoom')).toBe('1.2')
expect(window.localStorage.getItem(APP_ZOOM_STORAGE_KEY)).toBe('1.2')
})
it('persists app zoom changes', async () => {
await applyAppZoomLevel(1.3)
expect(window.localStorage.getItem(APP_ZOOM_STORAGE_KEY)).toBe('1.3')
expect(document.documentElement.style.getPropertyValue('--app-zoom')).toBe('1.3')
})
it('maps IDE-style zoom shortcuts by platform', () => {
expect(getAppZoomKeyboardAction({
altKey: false,
code: 'Equal',
ctrlKey: false,
key: '=',
metaKey: true,
} as KeyboardEvent, 'MacIntel')).toBe('in')
expect(getAppZoomKeyboardAction({
altKey: false,
code: 'Minus',
ctrlKey: true,
key: '-',
metaKey: false,
} as KeyboardEvent, 'Win32')).toBe('out')
expect(getAppZoomKeyboardAction({
altKey: false,
code: 'Numpad0',
ctrlKey: true,
key: '0',
metaKey: false,
} as KeyboardEvent, 'Linux x86_64')).toBe('reset')
expect(getAppZoomKeyboardAction({
altKey: true,
code: 'Equal',
ctrlKey: true,
key: '=',
metaKey: false,
} as KeyboardEvent, 'Win32')).toBeNull()
})
})