import { provideHttpClient } from '@angular/common/http'; import { HttpTestingController, provideHttpClientTesting } from '@angular/common/http/testing'; import { TestBed } from '@angular/core/testing'; import { ReportsPage } from './reportes-page'; describe('ReportsPage', () => { let http: HttpTestingController; beforeEach(() => { TestBed.configureTestingModule({ imports: [ReportsPage], providers: [provideHttpClient(), provideHttpClientTesting()], }); http = TestBed.inject(HttpTestingController); }); afterEach(() => http.verify()); it('shows the indicator status and the aggregate attempts', () => { const fixture = TestBed.createComponent(ReportsPage); fixture.detectChanges(); http.expectOne('/reportes/indicadores').flush({ cacu: { estado: 'ok', pendiente: false, fin: '2026-10-29T10:00:00+00:00', duracion_s: 12 }, cama: { estado: 'error', pendiente: false, error: { tipo: 'OperationalError', mensaje: 'x' } }, tea: { estado: 'sin_correr', pendiente: true }, }); http.expectOne((r) => r.url === '/reportes/importaciones').flush({ resumen: { intentos: {}, ultimo: null }, intentos: [{ id: 'a', fecha: '2026-10-29T10:00:00+00:00', operacion: 'carga', dominio: 'cacu', resultado: 'completada', duracion_s: 1, totales: { leidas: 10, validas: 8, insertadas: 8, rechazadas_en_validacion: 2 }, fuentes: [{ destino: 'cacu.Paciente', origen: { tipo: 'csv' }, mapeo: { requeridos_sin_mapear: [], campos_sin_mapear: [], columnas_origen: [] }, filas: {}, campos: { pac_sexo: { con_valor: 10, vacios: 0, errores: { out_of_catalog: 2 } } }, }], }], }); fixture.detectChanges(); const text: string = fixture.nativeElement.textContent; expect(text).toContain('Actualizado'); expect(text).toContain('Error en el cálculo'); expect(text).toContain('En espera de cálculo'); expect(text).toContain('cacu.Paciente'); (fixture.nativeElement.querySelector('.intentos button') as HTMLButtonElement).click(); fixture.detectChanges(); expect(fixture.nativeElement.querySelector('.campos').textContent).toContain('out_of_catalog: 2'); }); it('keeps polling after a failed request', () => { vi.useFakeTimers(); try { const fixture = TestBed.createComponent(ReportsPage); fixture.detectChanges(); http.expectOne('/reportes/indicadores').flush('caída', { status: 502, statusText: 'Bad Gateway' }); http.expectOne((r) => r.url === '/reportes/importaciones'); expect(fixture.componentInstance.error()).toBeTruthy(); vi.advanceTimersByTime(15000); http.expectOne('/reportes/indicadores').flush({ cacu: { estado: 'ok', pendiente: false } }); http.expectOne((r) => r.url === '/reportes/importaciones').flush({ resumen: { intentos: {}, ultimo: null }, intentos: [] }); expect(fixture.componentInstance.error()).toBeNull(); expect(fixture.componentInstance.statuses()?.cacu.estado).toBe('ok'); fixture.destroy(); } finally { vi.useRealTimers(); } }); it('asks for a recomputation', () => { const fixture = TestBed.createComponent(ReportsPage); fixture.componentInstance.recompute('tea'); http.expectOne('/reportes/indicadores/tea/recalcular').flush({ pendiente: true }); }); });