149 lines
7.0 KiB
TypeScript
149 lines
7.0 KiB
TypeScript
/**
|
|
* Covers the training step: the form builds the train body (cohorte or cluster folder, line Hz, promote,
|
|
* picked cluster), the TEA jobs are polled every 3 s while one runs, the ended job lists the eeg-*
|
|
* versions it produced, and a 409 surfaces the message plus the running job. Fixtures: a real
|
|
* `HttpClient` answered by `HttpTestingController` for both the TEA and the governance routes.
|
|
*/
|
|
import { provideHttpClient } from '@angular/common/http';
|
|
import { HttpTestingController, provideHttpClientTesting } from '@angular/common/http/testing';
|
|
import { ComponentFixture, TestBed } from '@angular/core/testing';
|
|
import { ModelVersion, TrainingJob } from '../../../api/governance.types';
|
|
import { TrainingStep } from './training-step';
|
|
|
|
const COHORTS_URL = '/governance/tea/reference-cohorts';
|
|
const JOBS_URL = '/governance/training-jobs';
|
|
const TRAIN_URL = '/governance/model-versions/tea/eeg/train-jobs';
|
|
const PROGRESS_URL = '/governance/tea/jobs/train/t1/progress';
|
|
const VERSIONS_URL = '/governance/model-versions';
|
|
|
|
const job = (estado: TrainingJob['estado']): TrainingJob => ({
|
|
id: 't1', domain: 'tea', scenario: 'eeg', iniciado: '2026-11-20T10:00:00+00:00',
|
|
estado, codigo: estado === 'fallido' ? 1 : 0, salida: 'trained: version 3', error: '',
|
|
});
|
|
|
|
// Seven eeg-* versions plus one TEA version of another scenario that must stay out of the list.
|
|
const VERSIONS: ModelVersion[] = [
|
|
...['eeg-alpha', 'eeg-beta', 'eeg-coherence', 'eeg-fractal', 'eeg-nsr', 'eeg-ratios', 'eeg-theta'].map(
|
|
(scenario, i): ModelVersion => ({
|
|
name: `tea-${scenario}`, version: String(i + 1), run_id: 'r1',
|
|
tags: { domain: 'tea', scenario, environment: 'prod' }, metrics: {},
|
|
created_at: `2026-11-2${i}T00:00:00+00:00`, status: 'produccion',
|
|
}),
|
|
),
|
|
{ name: 'tea-riesgo', version: '9', run_id: 'r9', tags: { domain: 'tea', scenario: 'riesgo_tea' }, metrics: {}, created_at: '2026-11-29T00:00:00+00:00', status: 'produccion' },
|
|
];
|
|
|
|
describe('TrainingStep', () => {
|
|
let http: HttpTestingController;
|
|
|
|
beforeEach(() => {
|
|
TestBed.configureTestingModule({ providers: [provideHttpClient(), provideHttpClientTesting()] });
|
|
http = TestBed.inject(HttpTestingController);
|
|
});
|
|
afterEach(() => http.verify());
|
|
|
|
function create(choice = { mode: 'remote' as const, scheduler: 'tcp://equipo:8786' }): ComponentFixture<TrainingStep> {
|
|
const fixture = TestBed.createComponent(TrainingStep);
|
|
fixture.componentRef.setInput('choice', choice);
|
|
fixture.detectChanges();
|
|
http.expectOne(COHORTS_URL).flush([{ name: 'referencia_2026', edfs: 120, size_bytes: 1e9, has_ages: true, path: '/ref' }]);
|
|
http.expectOne(JOBS_URL).flush({ jobs: [] });
|
|
fixture.detectChanges();
|
|
return fixture;
|
|
}
|
|
|
|
function expectVersions() {
|
|
const req = http.expectOne((r) => r.url === VERSIONS_URL);
|
|
expect(req.request.params.get('domain')).toBe('tea');
|
|
return req;
|
|
}
|
|
|
|
// Fails if the form stops mapping to the train body, or the cluster choice doesn't reach it.
|
|
it('starts the training with the form values and the picked cluster', () => {
|
|
const fixture = create();
|
|
fixture.componentInstance.form.setValue({ reference_name: 'referencia_2026', reference_dir: '', reference_ages: '', line_hz: 50, promote: false });
|
|
fixture.componentInstance.train();
|
|
|
|
const req = http.expectOne(TRAIN_URL);
|
|
expect(req.request.method).toBe('POST');
|
|
expect(req.request.body).toEqual({
|
|
reference_name: 'referencia_2026', line_hz: 50, promote: false,
|
|
cluster: { mode: 'remote', scheduler: 'tcp://equipo:8786' },
|
|
});
|
|
req.flush({ job: job('en_curso') });
|
|
http.expectOne(JOBS_URL).flush({ jobs: [job('en_curso')] });
|
|
});
|
|
|
|
// Fails if the 3 s polling is dropped, the state label/output disappear, or the ended job stops
|
|
// listing the eeg-* versions.
|
|
it('polls while the job runs and lists the eeg-* versions when it ends', () => {
|
|
vi.useFakeTimers();
|
|
try {
|
|
const fixture = create();
|
|
const step = fixture.componentInstance;
|
|
step.form.setValue({ reference_name: '', reference_dir: '/datos/tea/referencia', reference_ages: '', line_hz: 60, promote: true });
|
|
step.train();
|
|
http.expectOne(TRAIN_URL).flush({ job: job('en_curso') });
|
|
http.expectOne(JOBS_URL).flush({ jobs: [job('en_curso')] });
|
|
fixture.detectChanges();
|
|
http.expectOne(PROGRESS_URL).flush(null, { status: 404, statusText: 'Not Found' });
|
|
expect(fixture.nativeElement.textContent).toContain('En curso');
|
|
expect(fixture.nativeElement.textContent).toContain('trained: version 3');
|
|
|
|
vi.advanceTimersByTime(2000);
|
|
http.expectOne(PROGRESS_URL).flush(null, { status: 404, statusText: 'Not Found' });
|
|
vi.advanceTimersByTime(1000);
|
|
http.expectOne(JOBS_URL).flush({ jobs: [job('completado')] });
|
|
expectVersions().flush({ model_versions: VERSIONS });
|
|
fixture.detectChanges();
|
|
|
|
http.expectOne(PROGRESS_URL).flush(null, { status: 404, statusText: 'Not Found' });
|
|
expect(step.running()).toBe(false);
|
|
expect(step.versions()).toHaveLength(7);
|
|
const text = fixture.nativeElement.textContent as string;
|
|
expect(text).toContain('Entrenamiento terminado');
|
|
expect(text).not.toContain('promovido');
|
|
expect(text).toContain('tea-eeg-nsr:5');
|
|
expect(text).not.toContain('tea-riesgo');
|
|
fixture.destroy();
|
|
} finally {
|
|
vi.useRealTimers();
|
|
}
|
|
});
|
|
|
|
// Fails if a 409 stops refreshing the jobs, or the server's message is not shown.
|
|
it('answers a 409 with the message and the running job', () => {
|
|
const fixture = create();
|
|
fixture.componentInstance.train();
|
|
http.expectOne(TRAIN_URL).flush({ error: 'Ya hay un entrenamiento en curso' }, { status: 409, statusText: 'Conflict' });
|
|
http.expectOne(JOBS_URL).flush({ jobs: [job('en_curso')] });
|
|
fixture.detectChanges();
|
|
|
|
http.expectOne(PROGRESS_URL).flush(null, { status: 404, statusText: 'Not Found' });
|
|
const text = fixture.nativeElement.textContent as string;
|
|
expect(text).toContain('Ya hay un entrenamiento en curso');
|
|
expect(text).toContain('En curso');
|
|
});
|
|
|
|
// Fails if a cluster-side ages table typed in the form stops reaching the training request, which leaves the
|
|
// workers reading ages from EDF headers that the reference files may not carry.
|
|
it('sends the cluster-side ages table with the folder', () => {
|
|
const fixture = create();
|
|
const step = fixture.componentInstance;
|
|
step.form.setValue({
|
|
reference_name: '', reference_dir: ' /referencia/helsinki ', reference_ages: ' /referencia/helsinki/edades.csv ',
|
|
line_hz: 50, promote: false,
|
|
});
|
|
step.train();
|
|
const request = http.expectOne(TRAIN_URL);
|
|
expect(request.request.body).toMatchObject({
|
|
reference_dir: '/referencia/helsinki', reference_ages: '/referencia/helsinki/edades.csv', line_hz: 50,
|
|
});
|
|
request.flush({ job: job('en_curso') });
|
|
http.expectOne(JOBS_URL).flush({ jobs: [job('en_curso')] });
|
|
fixture.detectChanges();
|
|
http.expectOne(PROGRESS_URL).flush(null, { status: 404, statusText: 'Not Found' });
|
|
fixture.destroy();
|
|
});
|
|
});
|