// Covers the layer that reads every row of a follow-up query: the page size it asks for, the cut-off and filters it // keeps, the limit that refuses instead of truncating, the checks that the pages form one list, and cancellation. // Fixture: fixtures/cama_seguimiento.json (27 synthetic rows). `servidor` pages it like the API does and counts // the requests it receives. import { Observable, of, Subject, toArray, lastValueFrom } from 'rxjs'; import fixtureData from '../fixtures/cama_seguimiento.json'; import { SeguimientoTablaQuery, SeguimientoTablaResponse, SeguimientoTablaService, seguimientoTablaResponse } from '../seguimiento-tabla.service'; import { ExportInconsistent, ExportLimitExceeded, TrackingExportEvent } from './tracking-export.models'; import { fetchAllTracking, queryOf } from './tracking-export-source'; const base = () => structuredClone(fixtureData) as unknown as Record; const TOTAL = (base()['items'] as unknown[]).length; // A server that pages the fixture. `tweak` can alter the answer of a page to simulate a list that changes, and // `size` makes it serve smaller pages than asked for, so that 27 rows span several pages. function servidor(tweak: (page: number, answer: Record) => Record = (_, a) => a, size?: number) { const requests: SeguimientoTablaQuery[] = []; const service = { consultar(_dominio: string, q: SeguimientoTablaQuery): Observable { requests.push(q); const d = base(), all = d['items'] as unknown[], tam = size ?? q.page_size; d['items'] = all.slice((q.page - 1) * tam, q.page * tam); d['paginacion'] = { page: q.page, page_size: tam, total_items: all.length, total_pages: Math.max(1, Math.ceil(all.length / tam)) }; d['corte'] = q.corte ?? d['corte']; d['ventana'] = { ...d['ventana'], meses: q.meses }; d['filtros'] = { ...d['filtros'], ...q.filtros }; return of(seguimientoTablaResponse(tweak(q.page, d))); }, }; return { service: service as unknown as SeguimientoTablaService, requests }; } const consulta: SeguimientoTablaQuery = { corte: '2026-10-20', meses: 3, page: 1, page_size: 5, filtros: { estatus: 'BENIGNO' } }; const eventos = (obs: Observable) => lastValueFrom(obs.pipe(toArray())); describe('Exportación: lectura completa del seguimiento', () => { // Fails if the layer asks for the page size the user was looking at instead of the largest one, or loses a row. it('reads every row in pages of 200 and returns them in the server order, whatever page the user was on', async () => { const { service, requests } = servidor(); const out = await eventos(fetchAllTracking(service, 'cama', { ...consulta, page: 3 }, 5000)); const done = out.at(-1)!; expect(done.kind).toBe('done'); if (done.kind !== 'done') return; expect(done.snapshot.items).toHaveLength(TOTAL); expect(done.snapshot.items.map(i => i['id_seudonimo'])).toEqual((base()['items'] as Record[]).map(i => i['id_seudonimo'])); expect(requests.map(r => r.page_size)).toEqual([200]); }); // Fails if later pages are asked without the first answer's cut-off and filters, which would mix two lists. it('keeps the cut-off, window and filters of the shown query in every request', async () => { const { service, requests } = servidor(undefined, 10); await eventos(fetchAllTracking(service, 'cama', consulta, 5000)); expect(requests).toHaveLength(3); for (const r of requests) expect(r).toMatchObject({ corte: '2026-10-20', meses: 3, filtros: { estatus: 'BENIGNO' } }); }); // Fails if progress stops being reported or does not end at the announced total. it('reports progress up to the announced total and then the finished snapshot', async () => { const { service } = servidor(); const out = await eventos(fetchAllTracking(service, 'cama', consulta, 5000)); expect(out[0]).toEqual({ kind: 'progress', fetched: TOTAL, total: TOTAL }); expect(out.at(-1)!.kind).toBe('done'); }); // Fails if a query above the limit starts downloading pages, or if it returns a subset without saying so. it('refuses a query above the limit before asking for more pages and never returns a subset', async () => { const { service, requests } = servidor(); await expect(eventos(fetchAllTracking(service, 'cama', consulta, TOTAL - 1))).rejects.toBeInstanceOf(ExportLimitExceeded); expect(requests).toHaveLength(1); }); // Fails if the limit becomes exclusive: a list of exactly the limit must still be exported. it('accepts a query that is exactly at the limit', async () => { const { service } = servidor(); const out = await eventos(fetchAllTracking(service, 'cama', consulta, TOTAL)); expect(out.at(-1)!.kind).toBe('done'); }); // Fails if an answer that did not apply the asked filter is accepted: the file would list other patients. it('fails when the server does not report the filters that were asked for', async () => { const { service } = servidor((_page, a) => ({ ...a, filtros: { ...a['filtros'], estatus: null } })); await expect(eventos(fetchAllTracking(service, 'cama', consulta, 5000))).rejects.toBeInstanceOf(ExportInconsistent); }); // Fails if a total that changes between pages is accepted: the file would mix two states of the list. it('fails when the total changes between pages', async () => { const { service } = servidor((page, a) => page === 1 ? a : { ...a, paginacion: { ...a['paginacion'], total_items: TOTAL + 1 } }, 10); await expect(eventos(fetchAllTracking(service, 'cama', consulta, 5000))).rejects.toBeInstanceOf(ExportInconsistent); }); // Fails if a repeated row (the same id on two pages) is accepted as if it were two patients. it('fails when a row repeats across pages', async () => { const { service } = servidor((page, a) => page === 1 ? a : { ...a, items: [(base()['items'] as unknown[])[0]] }, 10); await expect(eventos(fetchAllTracking(service, 'cama', consulta, 5000))).rejects.toBeInstanceOf(ExportInconsistent); }); // Fails if cancelling (unsubscribing) leaves requests pending or still emits events afterwards. it('stops asking when the subscriber cancels', () => { const first = new Subject(); const requests: SeguimientoTablaQuery[] = []; const service = { consultar: (_d: string, q: SeguimientoTablaQuery) => { requests.push(q); return first; } } as unknown as SeguimientoTablaService; const seen: TrackingExportEvent[] = []; const subscription = fetchAllTracking(service, 'cama', consulta, 5000).subscribe(e => seen.push(e)); subscription.unsubscribe(); first.next(seguimientoTablaResponse(base())); expect(requests).toHaveLength(1); expect(seen).toEqual([]); }); // Fails if an invalid query reaches the network instead of failing inside the stream. it('turns an invalid query into an error of the stream', async () => { const real = { consultar: (_d: string, _q: SeguimientoTablaQuery) => { throw new Error('invalid'); } } as unknown as SeguimientoTablaService; await expect(eventos(fetchAllTracking(real, 'cama', consulta, 5000))).rejects.toThrow('invalid'); }); }); describe('Exportación: consulta de una respuesta', () => { // Fails if the export is built from the form being edited instead of what the shown answer says it applied. it('rebuilds the query from the answer: cut-off, window months and only the filters that were applied', () => { const answer = seguimientoTablaResponse({ ...base(), filtros: { estatus: 'BENIGNO', birads: null, grupo_edad: null, prioridad: null } }); expect(queryOf(answer)).toEqual({ corte: answer.corte, meses: answer.ventana.meses, page: 1, page_size: 200, filtros: { estatus: 'BENIGNO' } }); }); });