feat(desktop): add previewBridge wrapper over tauri preview commands

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
程序员阿江(Relakkes) 2026-05-29 11:46:04 +08:00
parent 633ab63f36
commit 25ea37bb3d
2 changed files with 47 additions and 0 deletions

View File

@ -0,0 +1,31 @@
import { afterEach, describe, expect, it, vi } from 'vitest'
import type { WebviewBounds } from '../components/browser/computeWebviewBounds'
const invoke = vi.fn()
vi.mock('@tauri-apps/api/core', () => ({ invoke: (...a: unknown[]) => invoke(...a) }))
vi.mock('./desktopRuntime', () => ({ isTauriRuntime: () => true }))
afterEach(() => { invoke.mockReset() })
describe('previewBridge', () => {
it('openPreview forwards url + bounds to preview_open', async () => {
const { previewBridge } = await import('./previewBridge')
const bounds: WebviewBounds = { x: 1, y: 2, width: 3, height: 4 }
await previewBridge.open('http://localhost/a', bounds)
expect(invoke).toHaveBeenCalledWith('preview_open', { url: 'http://localhost/a', bounds })
})
it('setBounds forwards to preview_set_bounds', async () => {
const { previewBridge } = await import('./previewBridge')
await previewBridge.setBounds({ x: 0, y: 0, width: 10, height: 10 })
expect(invoke).toHaveBeenCalledWith('preview_set_bounds', { bounds: { x: 0, y: 0, width: 10, height: 10 } })
})
it('is a no-op outside the Tauri runtime', async () => {
vi.resetModules()
vi.doMock('./desktopRuntime', () => ({ isTauriRuntime: () => false }))
const { previewBridge } = await import('./previewBridge')
await previewBridge.open('http://localhost/a', { x: 0, y: 0, width: 1, height: 1 })
expect(invoke).not.toHaveBeenCalled()
})
})

View File

@ -0,0 +1,16 @@
import { invoke } from '@tauri-apps/api/core'
import { isTauriRuntime } from './desktopRuntime'
import type { WebviewBounds } from '../components/browser/computeWebviewBounds'
async function call(cmd: string, args?: Record<string, unknown>): Promise<void> {
if (!isTauriRuntime()) return
await invoke(cmd, args)
}
export const previewBridge = {
open: (url: string, bounds: WebviewBounds) => call('preview_open', { url, bounds }),
navigate: (url: string) => call('preview_navigate', { url }),
setBounds: (bounds: WebviewBounds) => call('preview_set_bounds', { bounds }),
setVisible: (visible: boolean) => call('preview_set_visible', { visible }),
close: () => call('preview_close'),
}