import { test, expect } from '../../web/node_modules/@playwright/test/index.js'; import { createServer, type ViteDevServer } from '../../web/node_modules/vite/dist/node/index.js'; import { resolve } from 'node:path'; let server: ViteDevServer; let origin: string; test.beforeAll(async () => { server = await createServer({ root: resolve(__dirname, '../../web'), configFile: false, server: { host: '127.0.0.1', port: 0 }, logLevel: 'silent' }); await server.listen(); const address = server.httpServer!.address(); if (!address || typeof address === 'string') throw new Error('Missing local fixture address'); origin = `http://127.0.0.1:${address.port}`; }); test.afterAll(async () => { await server?.close(); }); test('shows, edits and clears device purpose without giving readers an editor', async ({ page }) => { const customer = { tenant_id: '11111111-1111-4111-8111-111111111111', name: 'Fixture' }; const path = `/api/v1/tenants/${customer.tenant_id}/devices/device-1`; const listing = (items: unknown[]) => ({ items, next_cursor: null, truncated: false, completeness: 'complete', as_of: '2026-11-19T12:00:00Z' }); let purpose: string | null = null; let writer = true; let reject = true; const writes: string[] = []; const device = () => ({ tenant_id: customer.tenant_id, customer_name: customer.name, asset_id: 'device-1', agent_id: 'agent-1', lifecycle: 'current', status: 'online', hostname: 'DATEN-SRV', os: 'Linux', purpose }); await page.route(`${origin}/auth/config`, route => route.fulfill({ json: { local_login_enabled: true, oidc_configured: false, oidc_login_url: null, dev_login_enabled: false } })); await page.route(`${origin}/auth/session`, route => route.fulfill({ json: { principal: writer ? 'device-operator' : 'inventory-reader', testsimulation: false, tenant_id: customer.tenant_id, csrf_token: 'purpose-csrf', permissions: writer ? ['inventory:read', 'devices:revoke'] : ['inventory:read'] } })); await page.route(`${origin}/api/v1/**`, route => { const request = route.request(); const pathname = new URL(request.url()).pathname; if (request.method() === 'GET') { if (pathname === '/api/v1/tenants') return route.fulfill({ json: listing([customer]) }); if (pathname === '/api/v1/devices') return route.fulfill({ json: listing([device()]) }); if (pathname === path) return route.fulfill({ json: device() }); } if (request.method() === 'PATCH' && pathname === `${path}/purpose`) { expect(writer).toBe(true); expect(request.headers()['x-csrf-token']).toBe('purpose-csrf'); expect(request.headers()['x-rmm-tenant']).toBe(customer.tenant_id); const text = request.postDataJSON().purpose; writes.push(text); if (reject) return route.fulfill({ status: 403, json: { code: 'FORBIDDEN', message: 'Speichern abgelehnt' } }); purpose = text.trim() || null; return route.fulfill({ json: { asset_id: 'device-1', purpose } }); } return route.fulfill({ status: 404, json: { code: 'UNEXPECTED_FIXTURE_REQUEST', message: pathname } }); }); await page.goto(`${origin}/#geraete`); await expect(page.getByRole('columnheader', { name: 'Zweck' })).toBeVisible(); await expect(page.getByRole('cell', { name: 'Nicht hinterlegt' })).toBeVisible(); await page.getByRole('button', { name: 'DATEN-SRV' }).click(); await page.getByRole('button', { name: 'Zweck bearbeiten' }).click(); const editor = page.getByRole('textbox', { name: 'Zweck des Geräts' }); const save = page.getByRole('button', { name: 'Zweck speichern' }); await editor.fill('ü'.repeat(501)); await expect(save).toBeDisabled(); expect(writes).toEqual([]); const text = 'Dateien für Nürnberg\n'; await editor.fill(text); await save.click(); await expect(page.getByRole('alert')).toContainText('Speichern abgelehnt'); await expect(editor).toHaveValue(text); reject = false; await save.click(); await expect(page.getByText('Zweck gespeichert.')).toBeVisible(); await page.getByRole('button', { name: 'Zur Geräteliste' }).click(); await expect(page.locator('td[data-label="Zweck"]')).toHaveText(text.replace('\n', ' ')); await page.setViewportSize({ width: 390, height: 844 }); await expect(page.locator('td[data-label="Zweck"]')).toBeVisible(); await page.getByRole('button', { name: 'DATEN-SRV' }).click(); await page.getByRole('button', { name: 'Zweck bearbeiten' }).click(); await expect(editor).toHaveValue(text); await editor.fill(''); await save.click(); await expect(page.getByText('Nicht hinterlegt', { exact: true })).toBeVisible(); expect(writes).toEqual([text, text, '']); writer = false; purpose = text; await page.reload(); await expect(page.locator('.device-purpose .purpose-text')).toHaveText(text.replace('\n', ' ')); await expect(page.getByRole('button', { name: 'Zweck bearbeiten' })).toHaveCount(0); });