// apps/web/e2e/flows/vital-signs-flow.spec.ts
import { test, expect } from '../fixtures/auth.fixture';
import { PatientDetailPage } from '../pages/patient-detail.page';
import { makePatient, makeVitalSigns } from '../fixtures/test-data';

test.describe('@flow 体征数据链路', () => {
  const cleanup: Array<() => Promise<void>> = [];

  test.afterEach(async () => {
    for (const fn of cleanup.reverse()) {
      await fn().catch(() => {});
    }
    cleanup.length = 0;
  });

  test('API录入体征 → 患者详情查看体征数据列表', async ({ api, authenticatedPage: page }) => {
    const patient = await api.createPatient(makePatient());
    cleanup.push(() => api.deletePatient(patient.id, patient.version));

    const vitalSigns = await api.createVitalSigns(patient.id, makeVitalSigns({
      systolic_bp_morning: 130,
      heart_rate: 80,
    }));
    cleanup.push(() => api.deleteVitalSigns(patient.id, vitalSigns.id, vitalSigns.version));

    const detailPage = new PatientDetailPage(page);
    await detailPage.goto(patient.id);

    await detailPage.clickTab('健康数据');
    await page.waitForTimeout(800);
    await detailPage.clickTab('体征数据');

    await page.waitForSelector('.ant-table', { timeout: 10000 });
    const rows = await page.locator('.ant-table-tbody tr').count();
    expect(rows).toBeGreaterThanOrEqual(1);
  });
});
