import { fireEvent, render, screen, waitFor } from '@testing-library/react' import { afterEach, describe, expect, it, vi } from 'vitest' import App from '../../src/app/App' import { useSessionStore } from '../../src/stores/sessionStore' 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() expect(await screen.findByRole('heading', { name: 'OpsXShell' })).toBeVisible() expect(mockDesktopApi.onMenuOpenSettings).toHaveBeenCalled() const title = screen.getByText('OpsXShell', { selector: 'span' }) expect(title.closest('header')).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() 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() 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() const title = await screen.findByText('OpsXShell', { selector: 'span' }) const titleBar = title.closest('header') expect(titleBar).toHaveAttribute('data-tauri-drag-region') expect(titleBar).not.toHaveClass('titlebar-drag') fireEvent.doubleClick(titleBar!) expect(mockDesktopApi.toggleMaximize).not.toHaveBeenCalled() }) it('writes exported sessions to the selected JSON file', async () => { 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-sessions.json', }) render() await waitFor(() => expect(mockDesktopApi.onMenuExportSessions).toHaveBeenCalled()) const callback = vi.mocked(mockDesktopApi.onMenuExportSessions).mock.calls[0][0] await callback() expect(mockDesktopApi.writeFile).toHaveBeenCalledWith( '/tmp/opsxshell-sessions.json', expect.stringContaining('"prod.example.com"') ) }) })