import '@testing-library/jest-dom/vitest'; import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react'; import { afterEach, describe, expect, it, vi } from 'vitest'; import { api, type RuntimeEnvironment, type RuntimeMaintenanceSite } from './api'; import { RuntimeMaintenancePanel } from './components/RuntimeMaintenancePanel'; afterEach(() => { cleanup(); vi.restoreAllMocks(); }); const php: RuntimeEnvironment = { name: 'PHP', componentTitle: 'PHP', componentStatus: 'installed', componentInstallable: false, installed: ['8.4', '8.5'], available: [], installing: [], patches: [{ branch: '8.4', installed: '8.4.25', latest: '8.4.27', updateAvailable: true }] }; const site: RuntimeMaintenanceSite = { site: { id: 7, projectId: 2, name: 'site', domain: 'app.example.com', runtime: 'Python', version: '3.12.0', status: 'running' }, hostName: 'sv0123456789', installedVersion: '3.12.0', runningVersion: '3.12.0', healthy: true, detail: '' }; describe('运行环境维护', () => { it('submits the advertised PHP patch after explicit confirmation', async () => { vi.spyOn(api, 'runtimeMaintenance').mockResolvedValue({ sites: [], jobs: [] }); const update = vi.spyOn(api, 'updatePHPPatch').mockResolvedValue({ jobId: 1 }); render( undefined} />); await screen.findByText('8.4.27'); fireEvent.click(screen.getByRole('button', { name: '更新 PHP 8.4' })); expect(screen.getByRole('alertdialog')).toHaveTextContent('Sea vHost 之外的网站'); expect(update).not.toHaveBeenCalled(); fireEvent.click(screen.getByRole('button', { name: '确认继续' })); await waitFor(() => expect(update).toHaveBeenCalledWith('8.4', '8.4.27', 7)); }); it('keeps the environment page free of per-site selectors and accepts another queued update',async()=>{ vi.spyOn(api,'runtimeMaintenance').mockResolvedValue({sites:[site],jobs:[{id:9,action:'php_patch',runtime:'PHP',fromVersion:'8.4.25',targetVersion:'8.4.27',status:'queued',phase:'queued',message:'',backupAvailable:false,createdAt:'',updatedAt:''}]}); const check=vi.spyOn(api,'checkRuntimeUpdates').mockResolvedValue({jobId:10}); render(undefined}/>); await screen.findByText('待执行');expect(screen.queryByText('站点运行版本')).not.toBeInTheDocument();expect(screen.getByRole('button',{name:'更新 PHP 8.4'})).toBeDisabled();expect(screen.getByRole('button',{name:'检查更新'})).toBeEnabled(); fireEvent.click(screen.getByRole('button',{name:'检查更新'}));await waitFor(()=>expect(check).toHaveBeenCalled()); }); it('keeps interrupted maintenance visible and blocks new upgrades until resumed', async () => { vi.spyOn(api, 'runtimeMaintenance').mockResolvedValue({ sites: [], jobs: [{ id: 4, action: 'php_patch', runtime: 'PHP', fromVersion: '8.4.25', targetVersion: '8.4.27', status: 'interrupted', phase: 'installing', message: '服务重启', backupAvailable: true, createdAt: '', updatedAt: '' }] }); const resume = vi.spyOn(api, 'resumeRuntimeMaintenance').mockResolvedValue({ ok: true }); render( undefined} />); expect(await screen.findByText('待核验')).toBeInTheDocument(); expect(screen.getByRole('button', { name: '更新 PHP 8.4' })).toBeDisabled(); fireEvent.click(screen.getByRole('button', { name: '恢复核验' })); await waitFor(() => expect(resume).toHaveBeenCalledWith(4, undefined)); }); });