Files
WebApp/src/app/governance/components/tea-eeg-panel/reference-step/reference-step.spec.ts
T

130 lines
5.9 KiB
TypeScript

/**
* Covers the reference step: the cohort table renders sizes as MB/GB and ages as Sí/No, files upload one
* request at a time with progress and a per-file result, the table reloads when the batch ends, a 503
* disables the upload, a bad cohort name blocks it, and a failed file doesn't stop the rest. Fixtures:
* canned `ReferenceCohort[]` answers and multipart uploads through `HttpTestingController`.
*/
import { HttpEventType } from '@angular/common/http';
import { provideHttpClient } from '@angular/common/http';
import { HttpTestingController, provideHttpClientTesting } from '@angular/common/http/testing';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { ReferenceCohort } from '../../../api/tea-eeg.types';
import { ReferenceStep, tamano } from './reference-step';
const LIST_URL = '/governance/tea/reference-cohorts';
const UPLOAD_URL = '/governance/tea/reference-cohorts/referencia_2026/files';
const COHORTS: ReferenceCohort[] = [
{ name: 'referencia_2026', edfs: 120, size_bytes: 2.4e9, has_ages: true, path: '/ref/referencia_2026' },
{ name: 'otra', edfs: 3, size_bytes: 5.2e6, has_ages: false, path: '/ref/otra' },
];
describe('ReferenceStep', () => {
let http: HttpTestingController;
beforeEach(() => {
TestBed.configureTestingModule({ providers: [provideHttpClient(), provideHttpClientTesting()] });
http = TestBed.inject(HttpTestingController);
});
afterEach(() => http.verify());
function create(choice = { mode: 'auto' as const }): ComponentFixture<ReferenceStep> {
const fixture = TestBed.createComponent(ReferenceStep);
fixture.componentRef.setInput('choice', choice);
fixture.detectChanges();
http.expectOne(LIST_URL).flush(COHORTS);
fixture.detectChanges();
return fixture;
}
function subirButton(fixture: ComponentFixture<ReferenceStep>): HTMLButtonElement {
return Array.from<HTMLButtonElement>(fixture.nativeElement.querySelectorAll('button')).find((b) =>
b.textContent?.includes('Subir'),
)!;
}
// Fails if the columns stop rendering, or sizes and ages lose their units/words.
it('lists the cohorts with their size in MB/GB and whether ages are present', () => {
const fixture = create();
const text = fixture.nativeElement.textContent as string;
expect(text).toContain('referencia_2026');
expect(text).toContain('120');
expect(text).toContain('2.4 GB');
expect(text).toContain('5.2 MB');
expect(text).toContain('Sí');
expect(text).toContain('No');
expect(tamano(999e9)).toBe('999.0 GB');
});
// Fails if files stop uploading one at a time, lose their progress/result, or the table doesn't reload.
it('uploads the files sequentially with progress and refreshes the table', () => {
const fixture = create();
const step = fixture.componentInstance;
step.name.setValue('referencia_2026');
step.selectedFiles.set([new File(['a'], 'a.edf'), new File(['b'], 'b.edf')]);
step.upload();
fixture.detectChanges();
const first = http.expectOne(UPLOAD_URL);
http.expectNone(() => true);
first.event({ type: HttpEventType.UploadProgress, loaded: 50, total: 100 });
expect(step.uploads()[0]).toMatchObject({ fileName: 'a.edf', progress: 50, status: 'uploading', detail: '50%' });
first.flush({ name: 'referencia_2026', file: 'a.edf', size_bytes: 4e6 });
expect(step.uploads()[0].status).toBe('done');
const second = http.expectOne(UPLOAD_URL);
second.flush({ name: 'referencia_2026', file: 'b.edf', size_bytes: 8e6 });
const reload = http.expectOne(LIST_URL);
reload.flush([...COHORTS, { name: 'nueva', edfs: 1, size_bytes: 1e6, has_ages: false, path: '/ref/nueva' }]);
fixture.detectChanges();
expect(step.uploads().map((u) => u.status)).toEqual(['done', 'done']);
expect(step.uploads()[1].detail).toContain('b.edf guardado');
expect(step.cohorts().map((c) => c.name)).toEqual(['referencia_2026', 'otra', 'nueva']);
expect(step.selectedFiles()).toEqual([]);
});
// Fails if a 503 stops disabling the upload, or the server's message is replaced by a generic one.
it('shows the server message and disables the upload when the volume answers 503', () => {
const fixture = TestBed.createComponent(ReferenceStep);
fixture.detectChanges();
http.expectOne(LIST_URL).flush({ error: 'TEA_REFERENCE_ROOT no está configurado' }, { status: 503, statusText: 'Service Unavailable' });
fixture.detectChanges();
expect(fixture.nativeElement.textContent).toContain('TEA_REFERENCE_ROOT no está configurado');
expect(fixture.componentInstance.rootMissing()).toBe(true);
expect(subirButton(fixture).disabled).toBe(true);
});
// Fails if a bad cohort name still uploads, or the pattern loosens beyond the route's.
it('blocks the upload until the cohort name matches the route pattern', () => {
const fixture = create();
const step = fixture.componentInstance;
step.name.setValue('nombre inválido');
step.selectedFiles.set([new File(['a'], 'a.edf')]);
step.upload();
http.expectNone(() => true);
step.name.setValue('referencia_2026');
expect(step.name.valid).toBe(true);
});
// Fails if a failed file stops the batch, hides its error, or skips the final reload.
it('keeps uploading after a failed file and still refreshes the table', () => {
const fixture = create();
const step = fixture.componentInstance;
step.name.setValue('referencia_2026');
step.selectedFiles.set([new File(['a'], 'a.edf'), new File(['b'], 'b.edf')]);
step.upload();
http.expectOne(UPLOAD_URL).flush({ name: 'referencia_2026', file: 'a.edf', size_bytes: 4e6 });
http.expectOne(UPLOAD_URL).flush({ error: 'EDF inválido' }, { status: 400, statusText: 'Bad Request' });
http.expectOne(LIST_URL).flush(COHORTS);
fixture.detectChanges();
expect(step.uploads().map((u) => u.status)).toEqual(['done', 'error']);
expect(step.uploads()[1].detail).toBe('EDF inválido');
expect(fixture.nativeElement.textContent).toContain('EDF inválido');
});
});