// apps/web/e2e/flows/follow-up-flow.spec.ts
import { test, expect } from '../fixtures/auth.fixture';
import { makePatient, makeFollowUpTemplate, makeFollowUpTask } 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('创建模板 → 创建任务 → 查看任务列表', async ({ api, authenticatedPage: page }) => {
    const patient = await api.createPatient(makePatient());
    cleanup.push(() => api.deletePatient(patient.id, patient.version));

    const template = await api.createFollowUpTemplate(makeFollowUpTemplate());
    cleanup.push(() => api.deleteFollowUpTemplate(template.id, template.version));

    await page.goto('/#/health/follow-up-tasks');
    await page.waitForSelector('.ant-table', { timeout: 10000 });

    const task = await api.createFollowUpTask(
      makeFollowUpTask(patient.id, template.id),
    );
    cleanup.push(() => api.deleteFollowUpTask(task.id, task.version));

    await page.reload();
    await page.waitForSelector('.ant-table');

    const rowCount = await page.locator('.ant-table-tbody tr').count();
    expect(rowCount).toBeGreaterThanOrEqual(1);
  });
});
