/** * E2E test: Settings export/import flow. * * Validates: * 1. Set theme to "Export Test" and save * 2. Export settings (triggers download) * 3. Change theme to "Changed" and save * 4. Import the previously exported file * 5. Assert theme shows "Export Test" again */ import { test, expect } from '@playwright/test'; import { loginAsUser } from '../helpers/auth'; import * as fs from 'node:fs'; import * as path from 'node:path'; test.describe('Settings export/import', () => { test('should export and re-import settings correctly', async ({ page }) => { // Step 1: Login as regular user via cookie injection await loginAsUser(page); // Step 2: Navigate to settings await page.goto('/settings', { waitUntil: 'domcontentloaded' }); // Wait for the settings page to load await expect( page.locator('h1', { hasText: 'Parametres de generation' }), ).toBeVisible({ timeout: 10_000 }); // Step 3: Set theme to "Export Test" const themeInput = page.locator('#theme'); await themeInput.fill('Export Test'); // Save await page .locator('button', { hasText: 'Enregistrer les parametres' }) .click(); // Wait for save confirmation await expect( page.getByText('Parametres enregistres avec succes'), ).toBeVisible({ timeout: 5_000 }); // Step 4: Click the export button and capture the download const downloadPromise = page.waitForEvent('download'); await page.locator('button', { hasText: 'Exporter' }).first().click(); const download = await downloadPromise; // Save the downloaded file to a temp location const downloadPath = path.join( '/tmp', `e2e-settings-export-${Date.now()}.json`, ); await download.saveAs(downloadPath); // Verify the downloaded file contains our theme const fileContent = fs.readFileSync(downloadPath, 'utf-8'); const parsed = JSON.parse(fileContent); expect(parsed.theme).toBe('Export Test'); // Step 5: Change theme to "Changed" and save await themeInput.fill('Changed'); await page .locator('button', { hasText: 'Enregistrer les parametres' }) .click(); await expect( page.getByText('Parametres enregistres avec succes'), ).toBeVisible({ timeout: 5_000 }); // Verify it changed await expect(themeInput).toHaveValue('Changed'); // Step 6: Import the previously downloaded file // The import button triggers a hidden file input. We need to set the file // on the hidden input element directly. const fileInput = page.locator('input[type="file"][accept=".json"]'); await fileInput.setInputFiles(downloadPath); // Wait for import success message await expect( page.getByText("Configuration importee avec succes"), ).toBeVisible({ timeout: 5_000 }); // Step 7: Assert the theme input shows "Export Test" again await expect(themeInput).toHaveValue('Export Test'); // Clean up temp file try { fs.unlinkSync(downloadPath); } catch { // Ignore cleanup errors } }); });