import { expect, test as base, type APIRequestContext } from '@playwright/test'; const apiUrl = 'http://localhost:5302/api'; const password = 'E2ePassword123!'; async function login(api: APIRequestContext, email: string) { expect((await api.post(`${apiUrl}/auth/login`, { data: { email, password } })).status()).toBe(200); const me = await (await api.get(`${apiUrl}/auth/me`)).json(); const csrf = (await api.storageState()).cookies.find(cookie => cookie.name === 'XSRF-TOKEN')!.value; return { 'X-CSRF-TOKEN': csrf, 'X-Session-Context': me.sessionContext as string }; } const test = base.extend<{ headers: Record }>({ headers: async ({ playwright, page, context }, provide) => { const admin = await playwright.request.newContext(); try { const adminHeaders = await login(admin, 'e2e@example.test'); const email = `rules-repair-${Date.now()}-${Math.random().toString(36).slice(2)}@example.test`; expect((await admin.post(`${apiUrl}/users`, { headers: adminHeaders, data: { email, password, roles: [] } })).status()).toBe(200); await context.addInitScript(() => localStorage.setItem('uiLanguage', 'en')); await context.route('**/*', route => { const url = new URL(route.request().url()); return ['localhost', '127.0.0.1'].includes(url.hostname) || ['data:', 'blob:', 'about:'].includes(url.protocol) ? route.continue() : route.abort(); }); await provide(await login(page.request, email)); } finally { await admin.dispose(); } }, }); test('[P5-15-01R-02 PRES-S03 W11] unsaved Rules draft survives an internal tab round trip without saving', async ({ page, headers }, info) => { expect((await page.request.put(`${apiUrl}/rules`, { headers, data: { id: 1, appliedFollowUpDays: 14, appliedGhostDays: 30, offerFollowUpDays: 7, offerGhostDays: 14, feedbackFollowUpDays: 7, feedbackGhostDays: 14, } })).status()).toBe(204); let writes = 0; page.on('request', request => { if (new URL(request.url()).pathname === '/api/rules' && request.method() === 'PUT') writes++; }); await page.goto('/settings'); await page.getByRole('tab', { name: 'Follow-ups', exact: true }).click(); const field = page.getByRole('spinbutton', { name: 'Applied: follow-up days', exact: true }); await expect(field).toHaveValue('14'); await field.fill('5'); await expect(field).toHaveValue('5'); await page.getByRole('tab', { name: 'Notifications', exact: true }).click(); await expect(page.getByLabel('Email reminders for follow-ups', { exact: true })).toBeVisible(); await page.getByRole('tab', { name: 'Follow-ups', exact: true }).click(); await expect(field).toBeVisible(); const persisted = await (await page.request.get(`${apiUrl}/rules`)).json(); await info.attach('rules-draft-tab-round-trip', { body: JSON.stringify({ initial: 14, entered: 5, afterReturn: await field.inputValue(), persisted: persisted.appliedFollowUpDays, writes, }), contentType: 'application/json' }); expect(writes).toBe(0); expect(persisted.appliedFollowUpDays).toBe(14); await expect(field).toHaveValue('5'); });