CaMa development view on the survival tab; governance trains any scenario in the background

This commit is contained in:
rockdrigo
2026-11-21 03:28:13 -06:00
parent cc802cbb57
commit c5cbaaab13
13 changed files with 544 additions and 20 deletions
@@ -0,0 +1,83 @@
// Covers the model versions panel's training: the scenarios the account can train are offered, a training starts in the
// background and its state is shown while it runs, and the versions are read again when it ends. Fixtures: a stub of
// `GovernanceApi` with the scenarios, the versions and the jobs the test sets, and an account that reaches every domain.
import { TestBed } from '@angular/core/testing';
import { of } from 'rxjs';
import { vi } from 'vitest';
import { AuthService } from '../../../auth/auth.service';
import { GovernanceApi } from '../../api/governance.api';
import { TrainingJob } from '../../api/governance.types';
import { ModelVersionsPanel } from './model-versions-panel';
const SCENARIOS = [
{ domain: 'cacu', scenario: 'cin2_logistic', cohort: 'cacu/x', outcome: 'cin2', description: '', kind: 'cohorte', produccion: null },
{ domain: 'cama', scenario: 'survival', cohort: 'cama/cohorte_total', outcome: 'muerte', description: '', kind: 'cohorte', produccion: '2' },
];
const job = (estado: TrainingJob['estado']): TrainingJob => ({
id: 'j1', domain: 'cama', scenario: 'survival', iniciado: '2026-01-01T00:00:00+00:00', estado, codigo: null, salida: 'trained: version 3', error: '',
});
describe('ModelVersionsPanel training', () => {
let api: Record<string, ReturnType<typeof vi.fn>>;
let jobs: TrainingJob[];
beforeEach(() => {
jobs = [];
api = {
listModelVersions: vi.fn(() => of({ model_versions: [] })),
trainableScenarios: vi.fn(() => of({ scenarios: SCENARIOS })),
trainingJobs: vi.fn(() => of({ jobs })),
startTrainingJob: vi.fn(() => of({ job: job('en_curso') })),
};
TestBed.configureTestingModule({
providers: [
{ provide: GovernanceApi, useValue: api },
{ provide: AuthService, useValue: { puedeVer: () => true, dominios: () => ['cacu', 'cama', 'tea'], esAdministrador: () => true, usuario: () => ({ dominios: ['cacu', 'cama', 'tea'] }) } },
],
});
});
// Fails if the panel goes back to offering one hardcoded scenario, or hides a scenario the account can train.
it('offers every trainable scenario of the domains shown', () => {
const f = TestBed.createComponent(ModelVersionsPanel);
f.detectChanges();
const panel = f.componentInstance;
panel.domain.set(null);
expect(panel.offered().map((s) => panel.keyOf(s))).toEqual(['cacu/cin2_logistic', 'cama/survival']);
panel.domain.set('cama');
expect(panel.offered().map((s) => panel.keyOf(s))).toEqual(['cama/survival']);
});
// Fails if the button trains another scenario than the selected one, or waits for the training to end.
it('starts the selected scenario in the background and reads the jobs again', () => {
const f = TestBed.createComponent(ModelVersionsPanel);
f.detectChanges();
const panel = f.componentInstance;
panel.domain.set(null);
panel.selected.set('cama/survival');
panel.train();
expect(api['startTrainingJob']).toHaveBeenCalledExactlyOnceWith('cama', 'survival');
expect(api['trainingJobs']).toHaveBeenCalledTimes(2);
});
// Fails if the running state is lost or the versions are not read again when the running training ends.
it('knows a training is running and reads the versions when it ends', () => {
const f = TestBed.createComponent(ModelVersionsPanel);
jobs = [job('en_curso')];
f.detectChanges();
const panel = f.componentInstance;
expect(panel.running()).toBe(true);
const reads = api['listModelVersions'].mock.calls.length;
jobs = [job('completado')];
panel.refreshJobs();
expect(panel.running()).toBe(false);
expect(api['listModelVersions'].mock.calls.length).toBe(reads + 1);
f.detectChanges();
expect((f.nativeElement as HTMLElement).textContent).toContain('Entrenado y promovido');
});
});