import { provideHttpClient } from '@angular/common/http'; import { HttpTestingController, provideHttpClientTesting } from '@angular/common/http/testing'; import { TestBed } from '@angular/core/testing'; import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog'; import { NotebookRunDialog } from './notebook-run-dialog'; /** * The dialog that uploads a notebook and runs it on a cohort build: it starts * from a version's defaults, offers the domain's exported builds, and sends * the build the person chose. The API is `HttpTestingController`; the dialog * reference is a stub that records how it closed. */ describe('NotebookRunDialog', () => { let http: HttpTestingController; const close = vi.fn(); const build = { url: 'cacu/cohorte_vigilancia', source: 'dbt', build: 4, datos_as_of: '2025-12-31', n_members: '10' }; function open(data: object) { TestBed.configureTestingModule({ imports: [NotebookRunDialog], providers: [ provideHttpClient(), provideHttpClientTesting(), { provide: MAT_DIALOG_DATA, useValue: data }, { provide: MatDialogRef, useValue: { close } }, ], }); http = TestBed.inject(HttpTestingController); const fixture = TestBed.createComponent(NotebookRunDialog); fixture.detectChanges(); return fixture; } afterEach(() => { http.verify(); close.mockReset(); }); it('sends the chosen build and version with the notebook, and closes with the run', () => { // Fails if the form stops carrying the build's url, source and number, or drops the version it was opened with. const fixture = open({ domain: 'cacu', scenario: 'cin2_logistic', version: '3' }); http.expectOne((r) => r.url === '/governance/cohort-files' && r.params.get('domain') === 'cacu').flush({ builds: [build] }); http.expectOne((r) => r.url === '/governance/model-versions').flush({ model_versions: [] }); const dialog = fixture.componentInstance; expect(dialog.ready()).toBe(false); dialog.file.set(new File(['{}'], 'analisis.ipynb')); expect(dialog.ready()).toBe(true); dialog.run(); const request = http.expectOne('/governance/experiments'); const form = request.request.body as FormData; expect([form.get('url'), form.get('source'), form.get('build'), form.get('version')]).toEqual([ 'cacu/cohorte_vigilancia', 'dbt', '4', '3', ]); request.flush({ run_id: 'r1', status: 'en_curso' }); expect(close).toHaveBeenCalledWith(expect.objectContaining({ run_id: 'r1' })); }); it('refuses a scenario that the API would reject', () => { // Fails if `ready` stops checking the scenario's characters before uploading. const fixture = open({}); http.expectOne((r) => r.url === '/governance/cohort-files').flush({ builds: [build] }); const dialog = fixture.componentInstance; dialog.file.set(new File(['{}'], 'a.ipynb')); dialog.setScenario("x' OR 1=1"); expect(dialog.ready()).toBe(false); dialog.setScenario('exploracion'); http.expectOne((r) => r.url === '/governance/model-versions').flush({ model_versions: [] }); expect(dialog.ready()).toBe(true); }); });