Files
WebApp/src/app/connect/state/connect-workspace.store.spec.ts
T

123 lines
5.2 KiB
TypeScript

import { TestBed } from '@angular/core/testing';
import { Observable, firstValueFrom, of } from 'rxjs';
import { BatchResult, ConflictStrategy, ImportSourcePayload, TargetMeta } from '../api/api.types';
import { ImportsApi } from '../api/imports.api';
import { SourceGateway } from '../api/source-gateway';
import { TargetsApi } from '../api/targets.api';
import { FakeConnection, FakeTable, NOTA, PACIENTE } from '../testing/fakes';
import { ConnectWorkspaceStore } from './connect-workspace.store';
class FakeImportsApi {
committed: { sources: ImportSourcePayload[]; onConflict: ConflictStrategy } | null = null;
validate = () => of({ total_rows: 2, valid_count: 2, error_count: 0, errors: [] });
commit(sources: ImportSourcePayload[], onConflict: ConflictStrategy): Observable<BatchResult> {
this.committed = { sources, onConflict };
return of({
ok: true,
results: sources.map((s) => ({ target: s.target, label: s.label ?? null, total: 2, created: 2, updated: 0, rejected: 0, errors: [] })),
});
}
}
describe('ConnectWorkspaceStore', () => {
let store: ConnectWorkspaceStore;
let imports: FakeImportsApi;
const tama: TargetMeta = { ...PACIENTE, key: 'tea.Pacientes', domain: 'tea', table: 'Pacientes', conflict_fields: null };
beforeEach(() => {
imports = new FakeImportsApi();
TestBed.configureTestingModule({
providers: [
ConnectWorkspaceStore,
{ provide: TargetsApi, useValue: { list: () => of([PACIENTE, NOTA, tama]) } },
{ provide: ImportsApi, useValue: imports },
{ provide: SourceGateway, useValue: {} },
],
});
store = TestBed.inject(ConnectWorkspaceStore);
});
function flowFrom(label: string, target: TargetMeta) {
const connection = new FakeConnection(label);
store.addConnection(connection);
const flow = store.createFlow(new FakeTable(connection), target);
flow.map('Clave', 'pac_id_hospital');
return { connection, flow };
}
it('filters targets by the domain, and locks the domain once a flow exists', () => {
expect(store.targets().map((t) => t.key)).toEqual(['cacu.Paciente', 'cacu.NotaMedica']);
flowFrom('a.csv', PACIENTE);
store.setDomain('tea');
expect(store.domain()).toBe('cacu');
});
it('removing a connection removes the flows built on its tables', () => {
const { connection } = flowFrom('a.csv', PACIENTE);
flowFrom('b.csv', PACIENTE);
store.removeConnection(connection);
expect(store.flows().map((f) => f.label)).toEqual(['b.csv']);
expect(store.selectedFlow()?.label).toBe('b.csv');
});
it('counts how many flows feed each destination table', () => {
flowFrom('a.csv', PACIENTE);
flowFrom('b.csv', PACIENTE);
expect(store.flowsByTarget().get('cacu.Paciente')).toBe(2);
});
it('commits every submittable flow in ONE request and hands each flow its result', async () => {
const { flow: first } = flowFrom('a.csv', PACIENTE);
const { flow: second } = flowFrom('b.csv', PACIENTE);
const incomplete = store.createFlow(new FakeTable(new FakeConnection('c.csv')), PACIENTE);
await firstValueFrom(store.commit());
expect(imports.committed?.sources.map((s) => s.label)).toEqual(['a.csv', 'b.csv']);
expect(incomplete.status()).toBe('incomplete');
expect(first.status()).toBe('loaded');
expect(second.result()?.created).toBe(2);
// Loaded flows are not re-sent (keyless tables would get duplicates)…
expect(store.submittableFlows()).toEqual([]);
// …until they're edited.
first.setDefault('pac_parentesco', 'Afiliado');
expect(store.submittableFlows()).toEqual([first]);
});
it('only sends upsert when every target has a natural key', async () => {
flowFrom('a.csv', PACIENTE);
store.setOnConflict('upsert');
await firstValueFrom(store.commit());
expect(imports.committed?.onConflict).toBe('upsert');
const { flow: again } = flowFrom('a2.csv', PACIENTE);
expect(store.submittableFlows()).toEqual([again]);
const nota = store.createFlow(new FakeTable(new FakeConnection('n.csv')), NOTA);
nota.map('Clave', 'pac_id_hospital');
nota.map('Sexo', 'nm_id_hospital');
expect(store.upsertAvailable()).toBe(false);
await firstValueFrom(store.commit());
expect(imports.committed?.onConflict).toBe('reject');
});
it('with mergeDuplicates on, a flow missing a required field joins the batch when a sibling shares its key', async () => {
const { flow: complete } = flowFrom('padron.csv', PACIENTE);
const incomplete = store.createFlow(new FakeTable(new FakeConnection('ficha.pdf')), PACIENTE);
// Off by default: the incomplete one stays out, same as before this feature.
expect(store.submittableFlows()).toEqual([complete]);
store.setMergeDuplicates(true);
expect(store.submittableFlows()).toEqual([complete, incomplete]);
await firstValueFrom(store.commit());
expect(imports.committed?.sources.map((s) => s.label)).toEqual(['padron.csv', 'ficha.pdf']);
// Both flows are now 'loaded'; a fresh incomplete flow with no live sibling on
// the same keyed target still never submits, even with merging on.
store.createFlow(new FakeTable(new FakeConnection('solo.pdf')), PACIENTE);
expect(store.submittableFlows()).toEqual([]);
});
});