/** * 住院临床分析页(/analysis/clinical)验收测试。 * 与 user-flows.spec.ts 一致的鉴权策略:addInitScript 注入 cbpoa_token, * 用 page.route 拦截 /api/**,对 inpatient-clinical 返回合法小样本,其余返回 {}。 */ import { test, expect, Page } from '@playwright/test'; import { TESTIDS } from '../src/utils/testids'; const CLINICAL_FIXTURE = { kpis: { total_admissions: 5822, median_los_days: 4, cure_rate: 0.991, emergency_admit_ratio: 0.47, }, los_histogram: [ { bin_label: '1-2', count: 1200 }, { bin_label: '3-4', count: 2100 }, { bin_label: '5-7', count: 1500 }, ], los_by_disease: [ { diagnosis: '肺炎', p25: 3, median: 5, p75: 7, n: 800 }, { diagnosis: '支气管炎', p25: 2, median: 4, p75: 6, n: 600 }, ], outcome_counts: [ { outcome: '治愈', count: 3474 }, { outcome: '好转', count: 2298 }, { outcome: '其他', count: 35 }, { outcome: '未愈', count: 12 }, { outcome: '死亡', count: 3 }, ], admission_route_counts: [ { route: '急诊', count: 2700 }, { route: '门诊', count: 3122 }, ], bmi_by_age_band: [ { age_band: '0-2', p25: 14, median: 16, p75: 18, n: 400 }, { age_band: '3-6', p25: 15, median: 17, p75: 19, n: 500 }, ], }; async function seedAuthAndMockApi(page: Page) { await page.addInitScript(() => { localStorage.setItem('cbpoa_token', 'e2e-test-token'); }); await page.route('/api/**', (route) => { const url = route.request().url(); if (url.includes('/stats/inpatient-clinical')) { route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify(CLINICAL_FIXTURE), }); return; } // 其余接口返回空对象,本页不依赖。 route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify({}), }); }); } test.describe('住院临床分析页', () => { test.beforeEach(async ({ page }) => { await seedAuthAndMockApi(page); }); test('deep-link /analysis/clinical mounts page-clinical + clinical-kpis', async ({ page }) => { await page.goto('/analysis/clinical'); await expect(page.locator(`[data-testid="${TESTIDS.pageClinical}"]`)).toBeVisible(); await expect(page.locator(`[data-testid="${TESTIDS.clinicalKpis}"]`)).toBeVisible(); }); test('no horizontal scroll at 375px', async ({ page }) => { await page.setViewportSize({ width: 375, height: 812 }); await page.goto('/analysis/clinical'); await expect(page.locator(`[data-testid="${TESTIDS.pageClinical}"]`)).toBeVisible(); await expect(page.locator(`[data-testid="${TESTIDS.clinicalKpis}"]`)).toBeVisible(); const noHorizontalScroll = await page.evaluate( () => document.documentElement.scrollWidth <= document.documentElement.clientWidth ); expect(noHorizontalScroll).toBe(true); }); });