import { statSync } from 'node:fs'; import { expect, Locator, Page, test } from '@playwright/test'; function watch(page: Page): string[] { const problems: string[] = []; page.on('console', (m) => m.type() === 'error' && problems.push(`console: ${m.text()}`)); page.on('response', (r) => r.status() >= 400 && problems.push(`${r.status()} ${r.url()}`)); page.on('pageerror', (e) => problems.push(`page: ${e.message}`)); return problems; } test.beforeEach(async ({ page }) => { const signed = await page.request.post('/api/auth/login', { form: { username: 'admin@isssteleon.mx', password: 'solo-para-pruebas' }, }); expect(signed.status()).toBe(204); }); async function downloaded(page: Page, button: Locator, kind: 'pdf' | 'xlsx'): Promise { const [file] = await Promise.all([page.waitForEvent('download'), button.click()]); const name = file.suggestedFilename(); expect(name).toMatch(new RegExp(`^tea-[a-z]+-\\d{4}-\\d{2}-\\d{2}\\.${kind}$`)); expect(name).not.toMatch(/N-?\d{4}/); expect(statSync(await file.path()).size).toBeGreaterThan(0); return name; } test('tea indicators: every tab loads, and the follow-up list exports to Excel', async ({ page, }) => { const problems = watch(page); const listed = page.waitForResponse( (r) => r.url().includes('/tea/fhir/Measure') && !r.url().includes('MeasureReport'), ); await page.goto('/indicadores/tea'); const measures = (await (await listed).json()).entry.length; await expect(page.getByRole('tab', { name: 'Resumen' })).toBeVisible(); await expect(page.locator('app-tab-indicadores [data-measure]').first()).toBeVisible(); await page.getByRole('tab', { name: 'Trazabilidad' }).click(); await expect(page.getByText('Progresión del proceso diagnóstico')).toBeVisible(); await expect(page.locator('app-tab-trazabilidad app-plot').first()).toBeVisible(); await page.getByRole('tab', { name: 'Seguimiento' }).click(); const rows = page.locator('app-tab-seguimiento-tea table tbody tr'); await expect(rows.first().locator('td').first()).toHaveText(/^N-?\d{4}$/); const name = await downloaded( page, page.getByRole('button', { name: 'Exportar a Excel' }), 'xlsx', ); expect(name).toMatch(/^tea-seguimiento-/); await page.getByRole('tab', { name: 'Análisis de riesgo' }).click(); const risk = page.locator('app-tab-riesgos-tea'); await expect( risk.getByRole('heading', { name: 'EEG de los pacientes del periodo' }), ).toBeVisible(); await expect(risk.locator('table.worklist tbody tr').first().locator('td').first()).toHaveText( /^N-?\d{4}$/, ); await risk.getByRole('combobox', { name: 'Modelo de asociación' }).click(); await page.getByRole('option', { name: 'Con resultados' }).click(); await expect(risk.getByText('No hay registros para los filtros aplicados.')).toBeVisible(); await page.getByRole('tab', { name: 'Datos y definiciones' }).click(); await expect(page.locator('app-tab-definiciones details')).toHaveCount(measures); expect(problems).toEqual([]); }); test('tea patient: the record and the risk page load and export to PDF', async ({ page }) => { const problems = watch(page); await page.goto('/indicadores/tea'); await page.getByRole('tab', { name: 'Seguimiento' }).click(); const first = page.locator('app-tab-seguimiento-tea table tbody tr').first(); await expect(first.locator('td').first()).toHaveText(/^N-?\d{4}$/); await first.getByRole('link').click(); await expect(page).toHaveURL(/\/indicadores\/tea\/pacientes\/[^/]+$/); await expect(page.getByRole('heading', { level: 1 })).toBeVisible(); await expect(page.locator('app-tea-section').first()).toBeVisible(); const record = await downloaded(page, page.getByRole('button', { name: 'Exportar PDF' }), 'pdf'); expect(record).toMatch(/^tea-seguimiento-/); await page.getByRole('link', { name: 'Regresar a Seguimiento' }).click(); const focused = page.locator('app-tab-seguimiento-tea'); await expect(focused.getByRole('button', { name: 'Ver todos los pacientes' })).toBeVisible(); const focusedRows = focused.locator('table tbody tr'); await expect(focusedRows).toHaveCount(1); await expect(focusedRows.first().locator('td').nth(4)).toHaveText( /M-CHAT|Psicología|Neuropediatría|EEG/, ); await expect(focusedRows.first().locator('td').nth(5)).toHaveText( /Diagnóstico confirmado|En proceso|Sin TEA/, ); await focusedRows.first().getByRole('link').click(); await page.getByRole('link', { name: 'Análisis de riesgo' }).click(); await expect(page.getByRole('heading', { name: 'Análisis de riesgo TEA' })).toBeVisible(); await expect(page.locator('app-tea-section').first()).toBeVisible(); expect(await downloaded(page, page.getByRole('button', { name: 'Exportar PDF' }), 'pdf')).toMatch( /^tea-riesgos-/, ); expect(problems).toEqual([]); }); test('tea gobernanza: every section in sync mode, and the preview saved as PDF', async ({ page, }) => { const problems = watch(page); await page.goto('/gobernanza/tea'); await page.getByLabel('Modo').selectOption('sync'); await expect(page.getByRole('status').filter({ hasText: 'Disponible' })).toBeVisible(); await page.getByRole('button', { name: 'Entrenar modelos' }).click(); await expect(page.getByRole('heading', { name: 'Entrenar modelos', level: 3 })).toBeVisible(); await expect(page.locator('app-governance-training formly-form')).toBeVisible(); const monitor = page.locator('app-gobernanza > section > section app-run-monitor'); await page.getByRole('button', { name: 'Analizar EEG' }).click(); await expect(page.getByRole('heading', { name: 'Análisis de EEG' })).toBeVisible(); await expect(monitor.getByRole('status')).toHaveText('SUCCESS', { timeout: 10 * 60_000 }); await page.getByRole('button', { name: 'Calcular riesgos' }).click(); await expect(page.getByRole('heading', { name: 'Cálculo de riesgos' })).toBeVisible(); await expect(monitor.getByRole('status')).toHaveText('SUCCESS', { timeout: 10 * 60_000 }); await page.getByRole('button', { name: 'Promover candidatas' }).click(); await expect(page.getByText('No hay candidatas que se puedan promover.')).toBeVisible(); const studies = page.locator('app-eeg-studies'); await expect(studies.locator('table.eegs tbody tr').first()).toContainText(/Válido|No válido/); await studies.locator('table.eegs tbody button:enabled').first().click(); await expect(studies.getByRole('heading', { name: /^EEG .* meses$/ })).toBeVisible(); await studies.getByRole('button', { name: 'Analizar', exact: true }).click(); await expect(studies.locator('app-run-monitor').first().getByRole('status')).toHaveText( 'SUCCESS', { timeout: 10 * 60_000 }, ); const preview = page.locator('app-eeg-preview'); await preview.getByLabel('EDF').setInputFiles(process.env['TEA_EDF']!); await preview.getByLabel('ADOS-2 (PDF)').setInputFiles(process.env['TEA_ADOS2']!); await preview.getByLabel('Fecha de nacimiento').fill(process.env['TEA_BIRTH']!); await preview.getByRole('button', { name: 'Vista previa' }).click(); const dialog = page.getByRole('dialog', { name: 'Vista previa del estudio' }); await expect(dialog.getByText('Resultado de la vista previa')).toBeVisible({ timeout: 10 * 60_000, }); await expect(dialog.locator('dt').first()).toHaveText(/^[AB]-\d+ \S/); expect( await downloaded(page, dialog.getByRole('button', { name: 'Guardar como PDF' }), 'pdf'), ).toMatch(/^tea-gobernanza-/); await dialog.getByRole('button', { name: 'Cerrar' }).click(); const bundles = page.locator('app-governance-bundles'); await bundles.getByLabel('Versiones').selectOption('all'); await bundles.getByRole('button', { name: 'Exportar bundle' }).click(); await expect(bundles.getByRole('alert')).toContainText('nothing to export'); expect(problems.filter((p) => !p.includes('409'))).toEqual([]); });