Repository: apache/airflow
File: airflow-core/src/airflow/ui/tests/e2e/specs/backfill.spec.ts (TypeScript)

Code before:

  let backfillPage: BackfillPage;

  test.beforeEach(async ({ page }) => {
    backfillPage = new BackfillPage(page);

    // Cancel any existing backfill before creating a new one
    await backfillPage.navigateToDagDetail(testDagId);
    try {
      await backfillPage.clickCancelButton();
    } catch {
      // No active backfill to cancel
    }

    await backfillPage.createBackfill(testDagId, {
      fromDate: controlFromDate,
      reprocessBehavior: "All Runs",
      toDate: controlToDate,
    });

    await backfillPage.navigateToBackfillsTab(testDagId);
  });

  test.afterEach(async () => {
    try {
      await backfillPage.clickCancelButton();
    } catch {
    }
  });

  test("verify pause and resume backfill", async () => {
    await backfillPage.clickPauseButton();
    expect(await backfillPage.isBackfillPaused()).toBe(true);

    await backfillPage.clickPauseButton();
    expect(await backfillPage.isBackfillPaused()).toBe(false);
  });

  test("verify cancel backfill", async () => {
    await backfillPage.clickCancelButton();
    await expect(backfillPage.pauseButton).not.toBeVisible();
    await expect(backfillPage.cancelButton).not.toBeVisible();
  });
});


Review comment (suggestion):
```suggestion
test.afterEach(async () => {
  // Cleanup: cancel backfill if still active
  if (await backfillPage.cancelButton.isVisible()) {
    await backfillPage.clickCancelButton();
  }
});
```

Code after:
    backfillPage = new BackfillPage(page);
    await backfillPage.navigateToDagDetail(testDagId);

    if (await backfillPage.cancelButton.isVisible({ timeout: 5000 }).catch(() => false)) {
      await backfillPage.clickCancelButton();
    }

    await backfillPage.createBackfill(testDagId, {
      fromDate: controlFromDate,
      reprocessBehavior: "All Runs",
      toDate: controlToDate,
    });

    await expect(async () => {
      await backfillPage.page.reload();
      await expect(backfillPage.triggerButton).toBeVisible({ timeout: 10_000 });
      await expect(backfillPage.pauseButton).toBeVisible({ timeout: 5000 });
    }).toPass({ timeout: 60_000 });
  });

  test.afterEach(async () => {
    await backfillPage.navigateToDagDetail(testDagId);
    if (await backfillPage.cancelButton.isVisible({ timeout: 5000 }).catch(() => false)) {
      await backfillPage.clickCancelButton();
    }
  });

  test("verify pause and resume backfill", async () => {
    await backfillPage.clickPauseButton();
    expect(await backfillPage.isBackfillPaused()).toBe(true);

    await backfillPage.clickPauseButton();
    expect(await backfillPage.isBackfillPaused()).toBe(false);
  });

  test("verify cancel backfill", async () => {
    await backfillPage.clickCancelButton();
    await expect(backfillPage.pauseButton).not.toBeVisible({ timeout: 10_000 });
    await expect(backfillPage.cancelButton).not.toBeVisible({ timeout: 10_000 });
  });

  test("verify cancelled backfill cannot be resumed", async () => {
    await backfillPage.clickCancelButton();
    await expect(backfillPage.pauseButton).not.toBeVisible({ timeout: 10_000 });

    await backfillPage.page.reload();
    await expect(backfillPage.triggerButton).toBeVisible({ timeout: 30_000 });
    await expect(backfillPage.pauseButton).not.toBeVisible({ timeout: 10_000 });

    await backfillPage.navigateToBackfillsTab(testDagId);