125 lines
4.5 KiB
TypeScript
125 lines
4.5 KiB
TypeScript
import { fireEvent, render, screen, waitFor } from '@testing-library/react'
|
|
import userEvent from '@testing-library/user-event'
|
|
import { afterEach, describe, expect, it, vi } from 'vitest'
|
|
import App from '../../src/app/App'
|
|
import { useSessionStore } from '../../src/stores/sessionStore'
|
|
import { decodeBase64ToText } from '../../src/utils/textBase64'
|
|
import { decryptSessionBackup } from '../../src/utils/sessionBackupCrypto'
|
|
import { mockDesktopApi } from '../setup'
|
|
import { unlockProForTests } from '../helpers/entitlement'
|
|
|
|
describe('App foundation acceptance', () => {
|
|
afterEach(() => {
|
|
window.opsxApi = mockDesktopApi
|
|
})
|
|
|
|
it('renders the application shell with the native desktop contract', async () => {
|
|
render(<App />)
|
|
|
|
expect(await screen.findByRole('heading', { name: 'OpsXShell' })).toBeVisible()
|
|
expect(mockDesktopApi.onMenuOpenSettings).toHaveBeenCalled()
|
|
|
|
const titleBar = document.querySelector('header[data-tauri-drag-region]')
|
|
expect(titleBar).toBeTruthy()
|
|
expect(titleBar).toHaveAttribute('data-tauri-drag-region')
|
|
})
|
|
|
|
it('renders the shell while the native bridge is unavailable', async () => {
|
|
Object.defineProperty(window, 'opsxApi', {
|
|
configurable: true,
|
|
value: undefined,
|
|
writable: true,
|
|
})
|
|
|
|
render(<App />)
|
|
|
|
expect(await screen.findByRole('heading', { name: 'OpsXShell' })).toBeVisible()
|
|
})
|
|
|
|
it('opens quick connect with a queued ssh:// target', async () => {
|
|
vi.clearAllMocks()
|
|
window.opsxApi = mockDesktopApi
|
|
vi.mocked(mockDesktopApi.takePendingSshLinks).mockResolvedValueOnce([
|
|
{ host: 'opsbase.cn', port: 55555, username: 'root' },
|
|
])
|
|
|
|
render(<App />)
|
|
|
|
expect(await screen.findByDisplayValue('root@opsbase.cn:55555')).toBeVisible()
|
|
expect(mockDesktopApi.onSshLinkAvailable).toHaveBeenCalled()
|
|
})
|
|
|
|
it('delegates title-bar double click to a single native drag-region handler', async () => {
|
|
vi.clearAllMocks()
|
|
window.opsxApi = mockDesktopApi
|
|
render(<App />)
|
|
|
|
await screen.findByRole('heading', { name: 'OpsXShell' })
|
|
const titleBar = document.querySelector('header[data-tauri-drag-region]')
|
|
expect(titleBar).toHaveAttribute('data-tauri-drag-region')
|
|
expect(titleBar).not.toHaveClass('titlebar-drag')
|
|
|
|
fireEvent.doubleClick(titleBar!)
|
|
expect(mockDesktopApi.toggleMaximize).not.toHaveBeenCalled()
|
|
expect(document.querySelector('[data-app-overlay]')).toBeNull()
|
|
})
|
|
|
|
it('writes encrypted exported sessions to the selected backup file', async () => {
|
|
const user = userEvent.setup()
|
|
vi.clearAllMocks()
|
|
unlockProForTests()
|
|
vi.mocked(mockDesktopApi.licenseGet).mockResolvedValue({
|
|
plan: 'pro',
|
|
deviceId: 'test-device',
|
|
activated: true,
|
|
features: ['*'],
|
|
})
|
|
window.opsxApi = mockDesktopApi
|
|
useSessionStore.setState({
|
|
sessions: [
|
|
{
|
|
id: 'session-1',
|
|
name: 'Production',
|
|
host: 'prod.example.com',
|
|
port: 22,
|
|
username: 'root',
|
|
authType: 'agent',
|
|
},
|
|
],
|
|
})
|
|
vi.mocked(mockDesktopApi.showSaveDialog).mockResolvedValueOnce({
|
|
canceled: false,
|
|
filePath: '/tmp/opsxshell-hosts.oxs',
|
|
})
|
|
render(<App />)
|
|
|
|
await waitFor(() => expect(mockDesktopApi.onMenuExportSessions).toHaveBeenCalled())
|
|
const callback = vi.mocked(mockDesktopApi.onMenuExportSessions).mock.calls[0][0]
|
|
const exportPromise = callback()
|
|
|
|
const passwordInput = await screen.findByPlaceholderText('输入备份密码')
|
|
await user.type(passwordInput, 'backup-secret')
|
|
await user.click(screen.getByRole('button', { name: '确认' }))
|
|
|
|
const confirmInput = await screen.findByPlaceholderText('再次输入备份密码')
|
|
await user.type(confirmInput, 'backup-secret')
|
|
await user.click(screen.getByRole('button', { name: '确认' }))
|
|
|
|
await exportPromise
|
|
|
|
expect(mockDesktopApi.showSaveDialog).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
defaultPath: 'opsxshell-hosts.oxs',
|
|
})
|
|
)
|
|
expect(mockDesktopApi.writeFile).toHaveBeenCalledWith('/tmp/opsxshell-hosts.oxs', expect.any(String))
|
|
const [, encoded] = vi.mocked(mockDesktopApi.writeFile).mock.calls[0]
|
|
const decoded = decodeBase64ToText(String(encoded))
|
|
expect(decoded).toContain('"format": "opsxshell-backup"')
|
|
expect(decoded).not.toContain('prod.example.com')
|
|
const envelope = JSON.parse(decoded)
|
|
const plaintext = await decryptSessionBackup(envelope, 'backup-secret')
|
|
expect(plaintext).toContain('"prod.example.com"')
|
|
})
|
|
})
|