const { test, expect } = require('@playwright/test'); const username = process.env.E2E_USERNAME; const password = process.env.E2E_PASSWORD; const requiresCredentials = username && password; test.describe('ComfyUI Portal browser flow', () => { test.beforeEach(async ({ page }) => { test.skip(!requiresCredentials, 'E2E_USERNAME and E2E_PASSWORD are required for deployed portal tests'); await page.goto('/login'); }); async function signIn(page, user = username, pass = password) { await page.getByLabel('Username').fill(user); await page.getByLabel('Password').fill(pass); await page.getByRole('button', { name: 'Sign in' }).click(); } test('valid credentials open the protected job monitor', async ({ page }) => { await signIn(page); await expect(page).toHaveURL(/\/jobs/); await expect(page.getByRole('heading', { name: 'Job monitor' })).toBeVisible(); await expect(page.getByText('ComfyUI status')).toBeVisible(); }); test('invalid credentials remain on sign in with a clear error', async ({ page }) => { await signIn(page, username, String(password) + '-invalid'); await expect(page).toHaveURL(/\/login/); await expect(page.getByText('Invalid username or password.')).toBeVisible(); }); test('protected jobs route redirects an unauthenticated visitor', async ({ page }) => { await page.context().clearCookies(); await page.goto('/jobs'); await expect(page).toHaveURL(/\/login/); await expect(page.getByRole('heading', { name: 'Sign in' })).toBeVisible(); }); test('the job monitor exposes stable loading or empty/error states', async ({ page }) => { await signIn(page); await expect(page.getByRole('heading', { name: 'Job monitor' })).toBeVisible(); await expect(page.locator('.server-card')).toBeVisible(); await expect(page.locator('.loading, .empty, .notice, .grid .job, .server-error').first()).toBeVisible({ timeout: 15000 }); }); test('an authenticated user can sign out', async ({ page }) => { await signIn(page); await expect(page.getByRole('button', { name: 'Sign out' })).toBeVisible(); await page.getByRole('button', { name: 'Sign out' }).click(); await expect(page).toHaveURL(/\/login/); await expect(page.getByRole('heading', { name: 'Sign in' })).toBeVisible(); }); test('the portal remains usable at mobile width without horizontal overflow', async ({ page }) => { await page.setViewportSize({ width: 390, height: 844 }); await signIn(page); await expect(page.getByRole('heading', { name: 'Job monitor' })).toBeVisible(); const overflow = await page.evaluate(() => document.documentElement.scrollWidth > window.innerWidth + 1); expect(overflow, 'mobile layout should not create horizontal scrolling').toBe(false); }); }); const adminUser = { id: 'user_admin', username: 'admin', displayName: 'Admin', role: 'admin' }; const regularUser = { id: 'user_regular', username: 'user', displayName: 'User', role: 'user' }; const connectedServer = { reachable: true, version: '0.3.0', os: 'linux', pythonVersion: '3.12.8', devices: [{ name: 'GPU 0', type: 'cuda', vramTotal: 8 * 1073741824, vramFree: 6 * 1073741824 }], queue: { running: 0, pending: 0, total: 0 }, }; const managerInfo = { managerVersion: '3.0.0', currentVersion: '0.3.0', versions: ['0.3.0', '0.2.9'] }; async function mockReadOnlyApi(page, { user = adminUser, jobs = [], running = null, server = connectedServer, manager = managerInfo } = {}) { test.skip(!requiresCredentials, 'deployed read-only UI tests require the configured E2E account'); if (user) { await page.context().addCookies([{ name: 'cp_auth', value: username, url: process.env.E2E_BASE_URL || 'http://127.0.0.1:7002' }]); } await page.route('**/api/**', async (route) => { const request = route.request(); const url = new URL(request.url()); if (url.pathname === '/api/me') return route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify({ user }) }); if (url.pathname === '/api/jobs') { if (jobs === 'unavailable') return route.fulfill({ status: 503, contentType: 'application/json', body: JSON.stringify({ error: 'Jobs unavailable' }) }); return route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify({ jobs, running }) }); } if (url.pathname === '/api/server-status') { if (server === 'unavailable') return route.fulfill({ status: 502, contentType: 'application/json', body: JSON.stringify({ error: 'ComfyUI is unavailable right now.' }) }); return route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify({ status: server }) }); } if (url.pathname === '/api/comfy/manager') { if (manager === 'unavailable') return route.fulfill({ status: 502, contentType: 'application/json', body: JSON.stringify({ error: 'Manager unavailable' }) }); return route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify(manager) }); } if (url.pathname === '/api/assets/meta') return route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify({ width: 1280, height: 720, sizeBytes: 2097152 }) }); if (url.pathname === '/api/assets') return route.fulfill({ status: 200, contentType: 'application/octet-stream', body: 'test asset' }); return route.continue(); }); } test.describe('ComfyUI Portal read-only UI states', () => { test('root and unknown routes redirect an unauthenticated visitor to sign in', async ({ page }) => { await mockReadOnlyApi(page, { user: null }); await page.goto('/'); await expect(page).toHaveURL(/\/login/); await expect(page.getByRole('heading', { name: 'Sign in' })).toBeVisible(); await page.goto('/does-not-exist'); await expect(page).toHaveURL(/\/login/); }); test('refresh reloads the job list and returns to a non-loading state', async ({ page }) => { let jobRequestCount = 0; await mockReadOnlyApi(page, { jobs: [] }); await page.unroute('**/api/**'); await page.route('**/api/**', async (route) => { const pathname = new URL(route.request().url()).pathname; if (pathname === '/api/me') return route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify({ user: adminUser }) }); if (pathname === '/api/server-status') return route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify({ status: connectedServer }) }); if (pathname === '/api/comfy/manager') return route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify(managerInfo) }); if (pathname === '/api/jobs') { jobRequestCount += 1; const jobs = jobRequestCount > 1 ? [{ promptId: 'job-refresh', title: 'Refreshed job', status: 'completed', queuedAt: 1000, durationMs: 2500, outputCount: 0, assets: [], models: [], progress: null }] : []; return route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify({ jobs, running: null }) }); } return route.continue(); }); await page.goto('/jobs'); await expect(page.getByText('No remote jobs were returned.')).toBeVisible(); await page.getByRole('button', { name: 'Refresh' }).click(); await expect(page.getByText('Refreshed job')).toBeVisible(); await expect(page.getByText('No remote jobs were returned.')).toHaveCount(0); }); test('renders running progress, queue metadata, models, and asset metadata', async ({ page }) => { const running = { promptId: 'running-1', title: 'Generating scene', status: 'running', queuedAt: Date.now() - 65000, durationMs: null, outputCount: 0, assets: [], models: [{ nodeId: '1', type: 'CheckpointLoader', name: 'model.safetensors' }], progress: { value: 3, max: 10, percent: 30 } }; const jobs = [{ promptId: 'done-1', title: 'Finished scene', status: 'completed', queuedAt: 1000, durationMs: 65000, outputCount: 1, assets: [{ filename: 'scene.png', url: '/api/assets?filename=scene.png&subfolder=&type=output', type: 'image' }], models: [], progress: null }]; await mockReadOnlyApi(page, { jobs, running }); await page.goto('/jobs'); await expect(page.getByRole('heading', { name: 'Generating scene' })).toBeVisible(); await expect(page.getByText('30% complete')).toBeVisible(); await expect(page.getByText('model.safetensors')).toBeVisible(); await expect(page.getByRole('heading', { name: 'Finished scene' })).toBeVisible(); await expect(page.getByText('scene.png')).toBeVisible(); await expect(page.getByText('1280 × 720')).toBeVisible(); await expect(page.getByText('2.0 MB')).toBeVisible(); }); test('renders failed jobs and jobs without output assets', async ({ page }) => { const jobs = [ { promptId: 'failed-1', title: 'Failed scene', status: 'failed', queuedAt: 1000, durationMs: null, outputCount: 0, assets: [], models: [], progress: null }, { promptId: 'queued-1', title: 'Queued scene', status: 'queued', queuedAt: null, durationMs: null, outputCount: 0, assets: [], models: [], progress: null }, ]; await mockReadOnlyApi(page, { jobs }); await page.goto('/jobs'); await expect(page.getByRole('heading', { name: 'Failed scene' })).toBeVisible(); await expect(page.getByText('failed', { exact: true })).toBeVisible(); await expect(page.getByRole('heading', { name: 'Queued scene' })).toBeVisible(); await expect(page.getByText('queued', { exact: true })).toBeVisible(); await expect(page.getByText('No output assets')).toHaveCount(2); }); test('shows a clear unavailable state when ComfyUI and Manager status cannot be read', async ({ page }) => { await mockReadOnlyApi(page, { server: 'unavailable', manager: 'unavailable' }); await page.goto('/jobs'); await expect(page.getByText('Unavailable', { exact: true })).toBeVisible(); await expect(page.getByText('Unable to read server status')).toBeVisible(); await expect(page.getByText('No remote jobs were returned.')).toBeVisible(); }); test('shows Manager controls only to administrators', async ({ page }) => { await mockReadOnlyApi(page, { user: regularUser }); await page.goto('/jobs'); await expect(page.getByText('Runtime controls')).toHaveCount(0); await page.unroute('**/api/**'); await mockReadOnlyApi(page, { user: adminUser }); await page.reload(); await expect(page.getByText('Runtime controls')).toBeVisible(); await expect(page.getByRole('button', { name: 'Restart ComfyUI' })).toBeVisible(); await expect(page.getByRole('button', { name: 'Update all' })).toBeVisible(); }); test('renders connected server details, queue counts, and device capacity', async ({ page }) => { await mockReadOnlyApi(page); await page.goto('/jobs'); await expect(page.getByText('Connected', { exact: true })).toBeVisible(); await expect(page.getByText('0', { exact: true }).first()).toBeVisible(); await expect(page.getByText('linux', { exact: true })).toBeVisible(); await expect(page.getByText('3.12.8', { exact: true })).toBeVisible(); await expect(page.getByText('GPU 0', { exact: true })).toBeVisible(); await expect(page.getByText('6.0 GB free of 8.0 GB')).toBeVisible(); }); test('renders video assets with metadata and safe output links', async ({ page }) => { const jobs = [{ promptId: 'video-1', title: 'Finished video', status: 'completed', queuedAt: 1000, durationMs: 12000, outputCount: 1, assets: [{ filename: 'clip.mp4', url: '/api/assets?filename=clip.mp4&subfolder=&type=output', type: 'video' }], models: [], progress: null, }]; await mockReadOnlyApi(page, { jobs }); await page.goto('/jobs'); await expect(page.locator('video')).toHaveAttribute('src', /filename=clip\.mp4/); await expect(page.getByText('clip.mp4')).toBeVisible(); await expect(page.getByText('2.0 MB')).toBeVisible(); await expect(page.locator('figure a')).toHaveAttribute('target', '_blank'); await expect(page.locator('figure a')).toHaveAttribute('rel', 'noopener'); }); test('shows a clear jobs error while retaining server status', async ({ page }) => { await mockReadOnlyApi(page, { jobs: 'unavailable' }); await page.goto('/jobs'); await expect(page.getByText('Unable to load jobs.')).toBeVisible(); await expect(page.getByText('Connected', { exact: true })).toBeVisible(); await expect(page.getByText('No remote jobs were returned.')).toHaveCount(0); }); test('does not invoke a runtime control when an administrator cancels confirmation', async ({ page }) => { const requests = []; await mockReadOnlyApi(page); await page.route('**/api/comfy/restart', async (route) => { requests.push(route.request().method()); await route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify({ message: 'accepted' }) }); }); await page.goto('/jobs'); page.once('dialog', dialog => dialog.dismiss()); await page.getByRole('button', { name: 'Restart ComfyUI' }).click(); await expect.poll(() => requests).toEqual([]); await expect(page.getByRole('button', { name: 'Restart ComfyUI' })).toBeEnabled(); }); });