72 lines
2.6 KiB
TypeScript
72 lines
2.6 KiB
TypeScript
import { fireEvent, render, screen } from '@testing-library/react'
|
|
import userEvent from '@testing-library/user-event'
|
|
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
|
import Sidebar from '../../src/components/layout/Sidebar'
|
|
import { useSessionStore, type Session } from '../../src/stores/sessionStore'
|
|
import { useTabStore } from '../../src/stores/tabStore'
|
|
import { mockDesktopApi } from '../setup'
|
|
|
|
const sessions: Session[] = [
|
|
{ id: 'prod-a', name: 'Prod A', host: '10.0.0.1', port: 22, username: 'root', authType: 'key' },
|
|
{ id: 'prod-b', name: 'Prod B', host: '10.0.0.2', port: 22, username: 'root', authType: 'key' },
|
|
]
|
|
|
|
describe('batch session operations', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
window.opsxApi = mockDesktopApi
|
|
vi.mocked(mockDesktopApi.sessionGetAll).mockResolvedValueOnce({ success: true, sessions })
|
|
useSessionStore.setState({ sessions: [] })
|
|
useTabStore.setState({
|
|
tabs: [{ id: 'welcome', title: '欢迎', type: 'welcome', isActive: true }],
|
|
activeTabId: 'welcome',
|
|
})
|
|
})
|
|
|
|
it('selects multiple saved sessions and opens connected terminal tabs', async () => {
|
|
const user = userEvent.setup()
|
|
render(
|
|
<Sidebar
|
|
onNewSession={vi.fn()}
|
|
onQuickConnect={vi.fn()}
|
|
onEditSession={vi.fn()}
|
|
/>
|
|
)
|
|
|
|
await screen.findByText('Prod A')
|
|
await user.click(screen.getByRole('button', { name: '批量操作' }))
|
|
await user.click(screen.getByText('Prod A'))
|
|
await user.click(screen.getByText('Prod B'))
|
|
|
|
expect(screen.getByText('已选 2 个会话')).toBeVisible()
|
|
await user.click(screen.getByRole('button', { name: /打开终端/ }))
|
|
|
|
expect(useTabStore.getState().tabs).toEqual(expect.arrayContaining([
|
|
expect.objectContaining({ sessionId: 'prod-a', type: 'terminal', connectOnMount: true }),
|
|
expect.objectContaining({ sessionId: 'prod-b', type: 'terminal', connectOnMount: true }),
|
|
]))
|
|
})
|
|
|
|
it('opens SFTP only from the saved session context menu', async () => {
|
|
const user = userEvent.setup()
|
|
render(
|
|
<Sidebar
|
|
onNewSession={vi.fn()}
|
|
onQuickConnect={vi.fn()}
|
|
onEditSession={vi.fn()}
|
|
/>
|
|
)
|
|
|
|
const sessionName = await screen.findByText('Prod A')
|
|
expect(screen.queryByTitle('SFTP')).not.toBeInTheDocument()
|
|
expect(screen.queryByText('SFTP 文件')).not.toBeInTheDocument()
|
|
|
|
fireEvent.contextMenu(sessionName)
|
|
await user.click(screen.getByRole('button', { name: 'SFTP 文件' }))
|
|
|
|
expect(useTabStore.getState().tabs).toEqual(expect.arrayContaining([
|
|
expect.objectContaining({ sessionId: 'prod-a', type: 'sftp' }),
|
|
]))
|
|
})
|
|
})
|