import type { Page } from '@playwright/test'; /** SIMULATED answers. The pages read the API's aggregated MeasureReports; these helpers pass them * through the browser and rewrite the screening-round strata, so the cards can be tried with the * groups the contract of `rebuild` allows (stratum `round`, crossed `round-category`) without * loading data. They prove how the page draws such answers, not what the backend computes. * * - `as-is`: the answers untouched, but the old candidate's `screening_round` is named `round`. * - `no-initial`: nobody is in the initial group (the group is absent and the totals drop). * - `unclassified`: the initial group folds into "Sin dato" (what 5740's answers look like). * - `unpublished`: the round strata are not in the answers. */ export type Simulation = 'as-is' | 'no-initial' | 'unclassified' | 'unpublished'; type Stratum = { value?: { text?: string }; component?: { value?: { text?: string } }[]; population?: { code?: { coding?: { code?: string }[] }; count: number }[]; }; type Stratifier = { code?: { coding?: { code?: string }[] }[]; stratum?: Stratum[] }; const named = (s: Stratifier) => (s.code ?? []).map((c) => c.coding?.[0]?.code).join('-'); const label = (x: Stratum) => x.component?.[0]?.value?.text ?? x.value?.text; const pop = (o: { population?: Stratum['population'] }, code: string) => o.population?.find((p) => p.code?.coding?.[0]?.code === code); function fold(into: Stratum, from: Stratum) { for (const p of from.population ?? []) { const q = pop(into, p.code!.coding![0].code!); if (q) q.count += p.count; else (into.population ??= []).push(structuredClone(p)); } } export function simulate(report: any, how: Simulation) { const group = report?.group?.[0]; if (!group) return report; for (const s of group.stratifier ?? ([] as Stratifier[])) for (const c of s.code ?? []) { const k = c.coding?.[0]; if (k?.code === 'screening_round') k.code = 'round'; } const rounds: Stratifier[] = (group.stratifier ?? []).filter((s: Stratifier) => ['round', 'round-category'].includes(named(s)), ); if (how === 'unpublished') group.stratifier = (group.stratifier ?? []).filter((s: Stratifier) => !rounds.includes(s)); if (how === 'no-initial' || how === 'unclassified') for (const s of rounds) { const initial = (s.stratum ?? []).filter((x) => label(x) === 'initial'); const rest = (s.stratum ?? []).filter((x) => label(x) !== 'initial'); for (const x of initial) { if (how === 'unclassified') { const other = x.component?.[1]?.value?.text; const target = rest.find( (y) => label(y) === 'no_data' && (!other || y.component?.[1]?.value?.text === other), ); if (target) fold(target, x); else { (x.component ? x.component[0].value! : x.value!).text = 'no_data'; rest.push(x); } } else if (named(s) === 'round') { // the group vanishes and the period's total drops by its patients: a real zero for (const p of x.population ?? []) { const total = pop(group, p.code!.coding![0].code!); if (total && p.code!.coding![0].code !== 'numerator') total.count = Math.max(0, total.count - p.count); } } } s.stratum = rest; } return report; } /** Answers every CaCu MeasureReport search through `simulate`. */ export async function simulateRounds(page: Page, how: Simulation) { await page.route('**/api/cacu/fhir/MeasureReport**', async (route) => { const response = await route.fetch(); const body = await response.json(); for (const entry of body.entry ?? []) simulate(entry.resource, how); await route.fulfill({ response, body: JSON.stringify(body), contentType: 'application/fhir+json', }); }); }