import { expect, test, type Page } from '@playwright/test'; async function login(page: Page, username = 'mobile-admin', target = '/') { await page.addInitScript(() => localStorage.setItem('pipu.locale', 'ru')); await page.goto(`/login?redirect=${encodeURIComponent(target)}`); await page.getByLabel('Логин').fill(username); await page.getByRole('textbox', { name: 'Пароль' }).fill(`${username}-password`); await page.getByRole('button', { name: 'Войти' }).click(); await expect(page.getByRole('button', { name: 'Меню профиля' })).toBeVisible(); } test('admin selects repository and version, confirms and sees persistent job log', async ({ page }) => { await login(page, 'mobile-admin', '/admin/updates'); await expect(page.getByRole('heading', { name: 'Обновления', exact: true })).toBeVisible(); await expect(page.getByRole('button', { name: 'Последний релиз', exact: true })).toHaveAttribute('aria-pressed', 'true'); await page.getByRole('button', { name: 'Источники обновлений', exact: true }).click(); await page.getByRole('button', { name: 'Добавить источник' }).click(); await page.getByLabel('Название', { exact: true }).last().fill('Тестовая репа'); await page.getByLabel('URL Git-репозитория').last().fill('https://git.example.org/pipu/testing.git'); await page.getByRole('button', { name: 'Сохранить', exact: true }).click(); await expect(page.getByRole('dialog')).not.toBeVisible(); await page.getByLabel('Источник', { exact: true }).selectOption({ label: 'Тестовая репа' }); await page.getByRole('button', { name: 'Выбрать версию', exact: true }).click(); await page.getByLabel('Версия', { exact: true }).selectOption('v1.10.0'); await page.getByRole('button', { name: 'Установить обновление', exact: true }).click(); const dialog = page.getByRole('dialog'); await expect(dialog).toContainText('Тестовая репа'); await expect(dialog).toContainText('v1.10.0'); await dialog.getByRole('button', { name: 'Установить обновление' }).click(); await expect(page.getByRole('status').filter({ hasText: /^Выполняется$/ })).toBeVisible(); await expect(page.getByLabel('Журнал выполнения')).toContainText('build and restart'); await expect(page.getByRole('button', { name: 'Установить обновление', exact: true })).not.toBeVisible(); await page.screenshot({ path: 'test-results/server-updates.png', fullPage: true }); }); test('config keeps secret unchanged, saves local override and applies separately', async ({ page }) => { await login(page, 'mobile-admin', '/admin/config'); await expect(page.getByRole('heading', { name: 'Конфигурация', exact: true })).toBeVisible(); await expect(page.getByLabel('GITEA_TOKEN · config.local', { exact: true })).toHaveValue(''); await expect(page.getByRole('button', { name: 'Сохранить', exact: true })).not.toBeVisible(); await page.getByLabel('LOG_LEVEL · config.local', { exact: true }).fill('debug'); await expect(page.getByText('В .env есть значение:', { exact: false })).toBeVisible(); await page.getByLabel('TRAIN_HOUR · config.local', { exact: true }).fill('6'); await expect(page.getByRole('button', { name: 'Применить и перезапустить', exact: true })).not.toBeVisible(); await page.getByRole('button', { name: 'Сохранить', exact: true }).click(); await expect(page.getByText('Изменения сохранены в config.local', { exact: true })).toBeVisible(); await expect(page.getByRole('button', { name: 'Сохранить', exact: true })).not.toBeVisible(); await expect(page.getByRole('button', { name: 'Отменить правки', exact: true })).not.toBeVisible(); await expect(page.getByLabel('GITEA_TOKEN · config.local', { exact: true })).toHaveAttribute('placeholder', 'Секрет сохранён'); await expect(page.getByText('Выполняется', { exact: true })).not.toBeVisible(); await page.getByRole('main').evaluate((element) => { element.scrollTop = 0; }); await page.screenshot({ path: 'test-results/server-config.png', fullPage: true }); await page.getByRole('button', { name: 'Применить и перезапустить', exact: true }).click(); await page.getByRole('dialog').getByRole('button', { name: 'Применить и перезапустить' }).click(); await expect(page.getByLabel('Журнал выполнения')).toContainText('build and restart'); }); test('update delegate can update but cannot open configuration', async ({ page }) => { await login(page, 'update-manager', '/admin/updates'); await expect(page.getByRole('heading', { name: 'Обновления', exact: true })).toBeVisible(); await page.evaluate(() => { history.pushState({}, '', '/admin/config'); window.dispatchEvent(new PopStateEvent('popstate')); }); await expect(page).toHaveURL('/'); await expect(page.getByRole('heading', { name: 'Конфигурация', exact: true })).not.toBeVisible(); }); test('ordinary user is redirected away from server management', async ({ page }) => { await login(page, 'alice', '/admin/updates'); await expect(page).toHaveURL('/'); }); test('installed main and release show no install action; another version can be installed', async ({ page }) => { await login(page, 'mobile-admin', '/admin/updates'); await expect(page.getByRole('button', { name: 'Установить обновление', exact: true })).toBeVisible(); await page.getByRole('button', { name: 'main', exact: true }).click(); await expect(page.getByText('Эта версия уже установлена — обновлений нет', { exact: true })).toBeVisible(); await expect(page.getByRole('button', { name: 'Установить обновление', exact: true })).not.toBeVisible(); await page.getByRole('button', { name: 'Выбрать версию', exact: true }).click(); await expect(page.getByText('Выберите версию для проверки', { exact: true })).toBeVisible(); await page.getByLabel('Версия', { exact: true }).selectOption('v1.9.0'); await expect(page.getByText('Эта версия уже установлена — обновлений нет', { exact: true })).toBeVisible(); await expect(page.getByRole('button', { name: 'Установить обновление', exact: true })).not.toBeVisible(); await page.getByLabel('Версия', { exact: true }).selectOption('v1.10.0'); await expect(page.getByText('Доступно обновление', { exact: true })).toBeVisible(); await expect(page.getByRole('button', { name: 'Установить обновление', exact: true })).toBeVisible(); }); test('explicit check bypasses cache and hides install action until the check finishes', async ({ page }) => { await login(page, 'mobile-admin', '/admin/updates'); await expect(page.getByRole('button', { name: 'Установить обновление', exact: true })).toBeVisible(); await page.evaluate(() => { const original = window.fetch; window.fetch = async (input, init) => { const request = new Request(input, init); if (request.url.includes('/api/v1/admin/server/updates') && request.url.includes('fresh=true')) { await new Promise((resolve) => setTimeout(resolve, 1000)); const response = await original(input, init); const data = await response.json(); return new Response(JSON.stringify({ ...data, update_state: 'current', target_commit: data.current_commit }), { headers: { 'Content-Type': 'application/json' } }); } return original(input, init); }; }); await page.getByRole('button', { name: 'Проверить обновления', exact: true }).click(); await expect(page.getByText('Проверяем выбранную версию…', { exact: true })).toBeVisible(); await expect(page.getByRole('button', { name: 'Установить обновление', exact: true })).not.toBeVisible(); await expect(page.getByText('Эта версия уже установлена — обновлений нет', { exact: true })).toBeVisible(); }); test('saving panel disappears when all edited values return to their original state', async ({ page }) => { await login(page, 'mobile-admin', '/admin/config'); await expect(page.getByRole('heading', { name: 'Конфигурация', exact: true })).toBeVisible(); const save = page.getByRole('button', { name: 'Сохранить', exact: true }); const apply = page.getByRole('button', { name: 'Применить и перезапустить', exact: true }); await expect(save).not.toBeVisible(); await expect(apply).not.toBeVisible(); const field = page.getByLabel('TRAIN_HOUR · config.local', { exact: true }); await field.fill('6'); await expect(save).toBeVisible(); await field.clear(); await expect(save).not.toBeVisible(); await expect(apply).not.toBeVisible(); await field.fill('7'); await page.getByRole('button', { name: 'Отменить правки', exact: true }).click(); await expect(save).not.toBeVisible(); await expect(field).toHaveValue(''); });